2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include "procheader.h"
34 #include "prefs_common.h"
39 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
44 HeaderEntry *hp = NULL;
47 /* skip non-required headers */
50 if (fgets(buf, len, fp) == NULL)
52 if (buf[0] == '\r' || buf[0] == '\n')
54 } while (buf[0] == ' ' || buf[0] == '\t');
56 for (hp = hentry, hnum = 0; hp->name != NULL;
58 if (!strncasecmp(hp->name, buf,
62 } while (hp->name == NULL);
64 if (fgets(buf, len, fp) == NULL) return -1;
65 if (buf[0] == '\r' || buf[0] == '\n') return -1;
68 /* unfold the specified folded line */
69 if (hp && hp->unfold) {
70 gboolean folded = FALSE;
71 gchar *bufp = buf + strlen(buf);
74 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
82 if (nexthead == ' ' || nexthead == '\t')
84 else if (nexthead == EOF)
86 else if (folded == TRUE) {
87 if (nexthead == '\r' || nexthead == '\n') {
92 if ((len - (bufp - buf)) <= 2) break;
94 /* replace return code on the tail end
99 /* concatenate next line */
100 if (fgets(bufp, len - (bufp - buf), fp)
102 bufp += strlen(bufp);
105 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
111 ungetc(nexthead, fp);
120 nexthead = fgetc(fp);
121 if (nexthead == ' ' || nexthead == '\t') {
122 size_t buflen = strlen(buf);
124 /* concatenate next line */
125 if ((len - buflen) > 2) {
126 gchar *p = buf + buflen;
131 if (fgets(p, len - buflen, fp) == NULL)
137 ungetc(nexthead, fp);
142 /* remove trailing return code */
148 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
150 gboolean folded = FALSE;
154 if (fgets(buf, len, fp) == NULL) return NULL;
155 if (buf[0] == '\r' || buf[0] == '\n') return NULL;
156 bufp = buf + strlen(buf);
159 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
164 nexthead = fgetc(fp);
167 if (nexthead == ' ' || nexthead == '\t')
169 else if (nexthead == EOF)
171 else if (folded == TRUE) {
172 if (nexthead == '\r' || nexthead == '\n') {
177 if ((len - (bufp - buf)) <= 2) break;
179 /* replace return code on the tail end
185 /* concatenate next line */
186 if (fgets(bufp, len - (bufp - buf), fp)
188 bufp += strlen(bufp);
191 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
197 ungetc(nexthead, fp);
202 /* remove trailing return code */
208 GSList *procheader_get_header_list_from_file(const gchar *file)
213 if ((fp = fopen(file, "rb")) == NULL) {
214 FILE_OP_ERROR(file, "fopen");
218 hlist = procheader_get_header_list(fp);
224 GSList *procheader_get_header_list(FILE *fp)
227 GSList *hlist = NULL;
230 g_return_val_if_fail(fp != NULL, NULL);
232 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
233 if ((header = procheader_parse_header(buf)) != NULL)
234 hlist = g_slist_append(hlist, header);
236 if (*buf == ':') continue;
237 for (p = buf; *p && *p != ' '; p++) {
239 header = g_new(Header, 1);
240 header->name = g_strndup(buf, p - buf);
242 while (*p == ' ' || *p == '\t') p++;
243 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
244 header->body = g_strdup(tmp);
246 hlist = g_slist_append(hlist, header);
256 GPtrArray *procheader_get_header_array(FILE *fp)
262 g_return_val_if_fail(fp != NULL, NULL);
264 headers = g_ptr_array_new();
266 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
267 if ((header = procheader_parse_header(buf)) != NULL)
268 g_ptr_array_add(headers, header);
270 if (*buf == ':') continue;
271 for (p = buf; *p && *p != ' '; p++) {
273 header = g_new(Header, 1);
274 header->name = g_strndup(buf, p - buf);
276 while (*p == ' ' || *p == '\t') p++;
277 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
278 header->body = g_strdup(tmp);
280 g_ptr_array_add(headers, header);
290 GPtrArray *procheader_get_header_array_asis(FILE *fp)
296 g_return_val_if_fail(fp != NULL, NULL);
298 headers = g_ptr_array_new();
300 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
301 if ((header = procheader_parse_header(buf)) != NULL)
302 g_ptr_array_add(headers, header);
304 if (*buf == ':') continue;
305 for (p = buf; *p && *p != ' '; p++) {
307 header = g_new(Header, 1);
308 header->name = g_strndup(buf, p - buf);
310 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
311 header->body = g_strdup(tmp);
313 g_ptr_array_add(headers, header);
323 void procheader_header_list_destroy(GSList *hlist)
327 while (hlist != NULL) {
328 header = hlist->data;
329 procheader_header_free(header);
330 hlist = g_slist_remove(hlist, header);
334 void procheader_header_array_destroy(GPtrArray *harray)
339 for (i = 0; i < harray->len; i++) {
340 header = g_ptr_array_index(harray, i);
341 procheader_header_free(header);
344 g_ptr_array_free(harray, TRUE);
347 void procheader_header_free(Header *header)
351 g_free(header->name);
352 g_free(header->body);
357 tests whether two headers' names are equal
358 remove the trailing ':' or ' ' before comparing
361 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
368 if (hdr1[len1 - 1] == ':')
370 if (hdr2[len2 - 1] == ':')
375 return (g_strncasecmp(hdr1, hdr2, len1) == 0);
379 parse headers, for example :
380 From: dinh@enseirb.fr becomes :
381 header->name = "From:"
382 header->body = "dinh@enseirb.fr"
385 Header * procheader_parse_header(gchar * buf)
391 if ((*buf == ':') || (*buf == ' '))
394 for (p = buf; *p ; p++) {
395 if ((*p == ':') || (*p == ' ')) {
396 header = g_new(Header, 1);
397 header->name = g_strndup(buf, p - buf + 1);
399 while (*p == ' ' || *p == '\t') p++;
400 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
402 header->body = g_strdup(p);
404 header->body = g_strdup(tmp);
411 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
418 if (hentry == NULL) return;
420 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
424 p = buf + strlen(hp->name);
425 while (*p == ' ' || *p == '\t') p++;
427 if (hp->body == NULL)
428 hp->body = g_strdup(p);
429 else if (procheader_headername_equal(hp->name, "To") ||
430 procheader_headername_equal(hp->name, "Cc")) {
431 gchar *tp = hp->body;
432 hp->body = g_strconcat(tp, ", ", p, NULL);
438 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
439 gboolean full, gboolean decrypted)
444 if ((fp = fopen(file, "rb")) == NULL) {
445 FILE_OP_ERROR(file, "fopen");
449 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
454 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
460 if ((fp = str_open_as_stream(str)) == NULL)
463 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
485 H_DISPOSITION_NOTIFICATION_TO = 15,
486 H_RETURN_RECEIPT_TO = 16
489 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
490 {"From:", NULL, TRUE},
493 {"Newsgroups:", NULL, TRUE},
494 {"Subject:", NULL, TRUE},
495 {"Message-Id:", NULL, FALSE},
496 {"References:", NULL, FALSE},
497 {"In-Reply-To:", NULL, FALSE},
498 {"Content-Type:", NULL, FALSE},
499 {"Seen:", NULL, FALSE},
500 {"Status:", NULL, FALSE},
501 {"X-Status:", NULL, FALSE},
502 {"From ", NULL, FALSE},
503 {"X-Face:", NULL, FALSE},
504 {"Disposition-Notification-To:", NULL, FALSE},
505 {"Return-Receipt-To:", NULL, FALSE},
506 {NULL, NULL, FALSE}};
508 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
509 {"From:", NULL, TRUE},
512 {"Newsgroups:", NULL, TRUE},
513 {"Subject:", NULL, TRUE},
514 {"Message-Id:", NULL, FALSE},
515 {"References:", NULL, FALSE},
516 {"In-Reply-To:", NULL, FALSE},
517 {"Content-Type:", NULL, FALSE},
518 {"Seen:", NULL, FALSE},
519 {"Status:", NULL, FALSE},
520 {"X-Status:", NULL, FALSE},
521 {"From ", NULL, FALSE},
522 {NULL, NULL, FALSE}};
524 HeaderEntry* procheader_get_headernames(gboolean full)
526 return full ? hentry_full : hentry_short;
529 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
533 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
534 gchar *reference = NULL;
540 hentry = procheader_get_headernames(full);
542 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
543 while (fgets(buf, sizeof(buf), fp) != NULL)
544 if (buf[0] == '\r' || buf[0] == '\n') break;
547 msginfo = procmsg_msginfo_new();
549 if (flags.tmp_flags || flags.perm_flags)
550 msginfo->flags = flags;
552 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
555 MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MIME);
557 msginfo->inreplyto = NULL;
559 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
561 hp = buf + strlen(hentry[hnum].name);
562 while (*hp == ' ' || *hp == '\t') hp++;
566 if (msginfo->date) break;
568 procheader_date_parse(NULL, hp, 0);
569 msginfo->date = g_strdup(hp);
572 if (msginfo->from) break;
573 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
574 msginfo->from = g_strdup(tmp);
575 msginfo->fromname = procheader_get_fromname(tmp);
578 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
582 g_strconcat(p, ", ", tmp, NULL);
585 msginfo->to = g_strdup(tmp);
588 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
592 g_strconcat(p, ", ", tmp, NULL);
595 msginfo->cc = g_strdup(tmp);
598 if (msginfo->newsgroups) {
599 p = msginfo->newsgroups;
600 msginfo->newsgroups =
601 g_strconcat(p, ",", hp, NULL);
604 msginfo->newsgroups = g_strdup(hp);
607 if (msginfo->subject) break;
608 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
609 msginfo->subject = g_strdup(tmp);
612 if (msginfo->msgid) break;
614 extract_parenthesis(hp, '<', '>');
616 msginfo->msgid = g_strdup(hp);
621 msginfo->references = g_strdup(hp);
622 eliminate_parenthesis(hp, '(', ')');
623 if ((p = strrchr(hp, '<')) != NULL &&
624 strchr(p + 1, '>') != NULL) {
625 extract_parenthesis(p, '<', '>');
628 reference = g_strdup(p);
634 if (!strncasecmp(hp, "multipart", 9)) {
635 if (strncasecmp(hp, "multipart/signed", 16)) {
636 MSG_SET_TMP_FLAGS(msginfo->flags,
639 MSG_SET_TMP_FLAGS(msginfo->flags,
644 else if (!strncasecmp(hp, "multipart/encrypted", 19)) {
645 MSG_SET_TMP_FLAGS(msginfo->flags,
648 else if (!strncasecmp(hp, "multipart", 9) &&
649 !strncasecmp(hp, "multipart/signed", 16)) {
650 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_SIGNED);
652 else if (!strncasecmp(hp, "multipart", 9))
653 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
655 #ifdef ALLOW_HEADER_HINT
657 /* mnews Seen header */
658 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
662 if (msginfo->xface) break;
663 msginfo->xface = g_strdup(hp);
665 case H_DISPOSITION_NOTIFICATION_TO:
666 if (msginfo->dispositionnotificationto) break;
667 msginfo->dispositionnotificationto = g_strdup(hp);
668 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_RETRCPT_PENDING);
670 case H_RETURN_RECEIPT_TO:
671 if (msginfo->returnreceiptto) break;
672 msginfo->returnreceiptto = g_strdup(hp);
673 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_RETRCPT_PENDING);
675 #ifdef ALLOW_HEADER_HINT
677 if (strchr(hp, 'R') != NULL)
678 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
679 if (strchr(hp, 'O') != NULL)
680 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
681 if (strchr(hp, 'U') != NULL)
682 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
685 if (strchr(hp, 'D') != NULL)
686 MSG_SET_PERM_FLAGS(msginfo->flags,
688 if (strchr(hp, 'F') != NULL)
689 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
690 if (strchr(hp, 'd') != NULL)
691 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
692 if (strchr(hp, 'r') != NULL)
693 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
694 if (strchr(hp, 'f') != NULL)
695 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
699 if (msginfo->fromspace) break;
700 msginfo->fromspace = g_strdup(hp);
706 msginfo->inreplyto = reference;
711 gchar *procheader_get_fromname(const gchar *str)
715 Xstrdup_a(tmp, str, return NULL);
718 extract_quote(tmp, '\"');
720 } else if (strchr(tmp, '<')) {
721 eliminate_parenthesis(tmp, '<', '>');
725 extract_parenthesis(tmp, '<', '>');
728 } else if (strchr(tmp, '(')) {
729 extract_parenthesis(tmp, '(', ')');
734 name = g_strdup(str);
736 name = g_strdup(tmp);
741 static gint procheader_scan_date_string(const gchar *str,
742 gchar *weekday, gint *day,
743 gchar *month, gint *year,
744 gint *hh, gint *mm, gint *ss,
749 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
750 weekday, day, month, year, hh, mm, ss, zone);
751 if (result == 8) return 0;
753 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
754 weekday, day, month, year, hh, mm, ss, zone);
755 if (result == 8) return 0;
757 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
758 day, month, year, hh, mm, ss, zone);
759 if (result == 7) return 0;
762 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
763 weekday, day, month, year, hh, mm, ss);
764 if (result == 7) return 0;
767 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
768 weekday, day, month, year, hh, mm, zone);
769 if (result == 7) return 0;
771 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
772 day, month, year, hh, mm, zone);
773 if (result == 6) return 0;
779 * Hiro, most UNIXen support this function:
780 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
782 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
784 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
796 memset(t, 0, sizeof *t);
798 if (procheader_scan_date_string(src, weekday, &day, month, &year,
799 &hh, &mm, &ss, zone) < 0) {
800 g_warning("Invalid date: %s\n", src);
804 /* Y2K compliant :) */
813 if ((p = strstr(monthstr, month)) != NULL)
814 dmonth = (gint)(p - monthstr) / 3 + 1;
816 g_warning("Invalid month: %s\n", month);
817 dmonth = G_DATE_BAD_MONTH;
824 t->tm_mon = dmonth - 1;
825 t->tm_year = year - 1900;
835 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
837 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
844 GDateMonth dmonth = G_DATE_BAD_MONTH;
850 if (procheader_scan_date_string(src, weekday, &day, month, &year,
851 &hh, &mm, &ss, zone) < 0) {
852 g_warning("Invalid date: %s\n", src);
854 strncpy2(dest, src, len);
858 /* Y2K compliant :) */
867 for (p = monthstr; *p != '\0'; p += 3) {
868 if (!strncasecmp(p, month, 3)) {
869 dmonth = (gint)(p - monthstr) / 3 + 1;
874 g_warning("Invalid month: %s\n", month);
880 t.tm_mon = dmonth - 1;
881 t.tm_year = year - 1900;
887 tz_offset = remote_tzoffset_sec(zone);
889 timer += tzoffset_sec(&timer) - tz_offset;
892 procheader_date_get_localtime(dest, len, timer);
897 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
900 gchar *default_format = "%y/%m/%d(%a) %H:%M";
902 lt = localtime(&timer);
904 if (prefs_common.date_format)
905 strftime(dest, len, prefs_common.date_format, lt);
907 strftime(dest, len, default_format, lt);
910 gint get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
914 HeaderEntry hentry[]={ { header, NULL, TRUE },
915 { NULL, NULL, FALSE } };
918 g_return_val_if_fail(msginfo != NULL, -1);
919 file = procmsg_get_message_file_path(msginfo);
920 if ((fp = fopen(file, "rb")) == NULL) {
921 FILE_OP_ERROR(file, "fopen");
925 val = procheader_get_one_field(buf,len, fp, hentry);
926 if (fclose(fp) == EOF) {
927 FILE_OP_ERROR(file, "fclose");