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 typedef char *(*getlinefunc)(char *, int, void *);
40 typedef int (*peekcharfunc)(void *);
41 typedef int (*getcharfunc)(void *);
42 typedef gint (*get_one_field_func)(gchar *, gint, void *, HeaderEntry[]);
44 static gint string_get_one_field(gchar *buf, gint len, char **str,
45 HeaderEntry hentry[]);
47 static char *string_getline(char *buf, int len, char **str);
48 static int string_peekchar(char **str);
49 static int string_getchar(char **str);
50 static int file_peekchar(FILE *fp);
51 static int file_getchar(FILE *fp);
52 static gint generic_get_one_field(gchar *buf, gint len, void *data,
55 peekcharfunc peekchar,
57 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
58 gboolean full, gboolean decrypted);
61 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
64 return generic_get_one_field(buf, len, fp, hentry,
65 (getlinefunc)fgets, (peekcharfunc)file_peekchar,
66 (getcharfunc)file_getchar);
69 static gint string_get_one_field(gchar *buf, gint len, char **str,
72 return generic_get_one_field(buf, len, str, hentry,
73 (getlinefunc)string_getline,
74 (peekcharfunc)string_peekchar,
75 (getcharfunc)string_getchar);
78 static char *string_getline(char *buf, int len, char **str)
83 for (; **str && len > 1; --len)
84 if ((*buf++ = *(*str)++) == '\n')
91 static int string_peekchar(char **str)
96 static int string_getchar(char **str)
101 static int file_peekchar(FILE *fp)
103 return ungetc(getc(fp), fp);
106 static int file_getchar(FILE *fp)
111 static gint generic_get_one_field(gchar *buf, gint len, void *data,
113 getlinefunc getline, peekcharfunc peekchar,
114 getcharfunc getchar_)
118 HeaderEntry *hp = NULL;
120 if (hentry != NULL) {
121 /* skip non-required headers */
124 if (getline(buf, len, data) == NULL)
126 if (buf[0] == '\r' || buf[0] == '\n')
128 } while (buf[0] == ' ' || buf[0] == '\t');
130 for (hp = hentry, hnum = 0; hp->name != NULL;
132 if (!strncasecmp(hp->name, buf,
136 } while (hp->name == NULL);
138 if (getline(buf, len, data) == NULL) return -1;
139 if (buf[0] == '\r' || buf[0] == '\n') return -1;
144 nexthead = peekchar(data);
145 /* ([*WSP CRLF] 1*WSP) */
146 if (nexthead == ' ' || nexthead == '\t') {
148 /* trim previous trailing \n if requesting one header or
149 * unfolding was requested */
150 if (!hentry || (hp && hp->unfold))
153 buflen = strlen(buf);
155 /* concatenate next line */
156 if ((len - buflen) > 2) {
157 if (getline(buf + buflen, len - buflen, data) == NULL)
162 /* remove trailing new line */
171 gint procheader_get_one_field_asis(gchar *buf, gint len, FILE *fp)
175 if (fgets(buf, len, fp) == NULL) return -1;
176 if (buf[0] == '\r' || buf[0] == '\n') return -1;
178 /* concatenate multi-line fields */
180 nexthead = file_peekchar(fp);
181 /* ([*WSP CRLF] 1*WSP) */
182 if (nexthead == ' ' || nexthead == '\t') {
183 size_t buflen = strlen(buf);
185 /* concatenate next line */
186 if ((len - buflen) > 2) {
187 if (fgets(buf + buflen, len - buflen, fp) == NULL)
192 /* remove trailing new line */
201 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
203 gboolean folded = FALSE;
207 if (fgets(buf, len, fp) == NULL) return NULL;
208 if (buf[0] == '\r' || buf[0] == '\n') return NULL;
209 bufp = buf + strlen(buf);
212 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
217 nexthead = fgetc(fp);
220 if (nexthead == ' ' || nexthead == '\t')
222 else if (nexthead == EOF)
224 else if (folded == TRUE) {
225 if (nexthead == '\r' || nexthead == '\n') {
230 if ((len - (bufp - buf)) <= 2) break;
232 /* replace return code on the tail end
238 /* concatenate next line */
239 if (fgets(bufp, len - (bufp - buf), fp)
241 bufp += strlen(bufp);
244 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
250 ungetc(nexthead, fp);
255 /* remove trailing return code */
263 GSList *procheader_get_header_list_from_file(const gchar *file)
268 if ((fp = fopen(file, "rb")) == NULL) {
269 FILE_OP_ERROR(file, "fopen");
273 hlist = procheader_get_header_list(fp);
279 GSList *procheader_get_header_list(FILE *fp)
282 GSList *hlist = NULL;
285 g_return_val_if_fail(fp != NULL, NULL);
287 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
288 if ((header = procheader_parse_header(buf)) != NULL)
289 hlist = g_slist_append(hlist, header);
291 if (*buf == ':') continue;
292 for (p = buf; *p && *p != ' '; p++) {
294 header = g_new(Header, 1);
295 header->name = g_strndup(buf, p - buf);
297 while (*p == ' ' || *p == '\t') p++;
298 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
299 header->body = g_strdup(tmp);
301 hlist = g_slist_append(hlist, header);
313 GPtrArray *procheader_get_header_array(FILE *fp)
319 g_return_val_if_fail(fp != NULL, NULL);
321 headers = g_ptr_array_new();
323 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
324 if ((header = procheader_parse_header(buf)) != NULL)
325 g_ptr_array_add(headers, header);
327 if (*buf == ':') continue;
328 for (p = buf; *p && *p != ' '; p++) {
330 header = g_new(Header, 1);
331 header->name = g_strndup(buf, p - buf);
333 while (*p == ' ' || *p == '\t') p++;
334 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
335 header->body = g_strdup(tmp);
337 g_ptr_array_add(headers, header);
348 GPtrArray *procheader_get_header_array_asis(FILE *fp)
354 g_return_val_if_fail(fp != NULL, NULL);
356 headers = g_ptr_array_new();
358 while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
359 if ((header = procheader_parse_header(buf)) != NULL)
360 g_ptr_array_add(headers, header);
362 if (*buf == ':') continue;
363 for (p = buf; *p && *p != ' '; p++) {
365 header = g_new(Header, 1);
366 header->name = g_strndup(buf, p - buf);
368 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
369 header->body = g_strdup(tmp);
371 g_ptr_array_add(headers, header);
382 void procheader_header_list_destroy(GSList *hlist)
386 while (hlist != NULL) {
387 header = hlist->data;
388 procheader_header_free(header);
389 hlist = g_slist_remove(hlist, header);
394 void procheader_header_array_destroy(GPtrArray *harray)
399 for (i = 0; i < harray->len; i++) {
400 header = g_ptr_array_index(harray, i);
401 procheader_header_free(header);
404 g_ptr_array_free(harray, TRUE);
407 void procheader_header_free(Header *header)
411 g_free(header->name);
412 g_free(header->body);
417 tests whether two headers' names are equal
418 remove the trailing ':' or ' ' before comparing
421 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
428 if (hdr1[len1 - 1] == ':')
430 if (hdr2[len2 - 1] == ':')
435 return (g_strncasecmp(hdr1, hdr2, len1) == 0);
439 parse headers, for example :
440 From: dinh@enseirb.fr becomes :
441 header->name = "From:"
442 header->body = "dinh@enseirb.fr"
445 Header * procheader_parse_header(gchar * buf)
451 if ((*buf == ':') || (*buf == ' '))
454 for (p = buf; *p ; p++) {
455 if ((*p == ':') || (*p == ' ')) {
456 header = g_new(Header, 1);
457 header->name = g_strndup(buf, p - buf + 1);
459 while (*p == ' ' || *p == '\t') p++;
460 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
462 header->body = g_strdup(p);
464 header->body = g_strdup(tmp);
471 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
478 if (hentry == NULL) return;
480 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
484 p = buf + strlen(hp->name);
485 while (*p == ' ' || *p == '\t') p++;
487 if (hp->body == NULL)
488 hp->body = g_strdup(p);
489 else if (procheader_headername_equal(hp->name, "To") ||
490 procheader_headername_equal(hp->name, "Cc")) {
491 gchar *tp = hp->body;
492 hp->body = g_strconcat(tp, ", ", p, NULL);
498 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
499 gboolean full, gboolean decrypted)
504 if ((fp = fopen(file, "rb")) == NULL) {
505 FILE_OP_ERROR(file, "fopen");
509 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
514 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
517 return parse_stream(&str, TRUE, flags, full, decrypted);
537 H_DISPOSITION_NOTIFICATION_TO = 15,
538 H_RETURN_RECEIPT_TO = 16
541 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
542 {"From:", NULL, TRUE},
545 {"Newsgroups:", NULL, TRUE},
546 {"Subject:", NULL, TRUE},
547 {"Message-Id:", NULL, FALSE},
548 {"References:", NULL, FALSE},
549 {"In-Reply-To:", NULL, FALSE},
550 {"Content-Type:", NULL, FALSE},
551 {"Seen:", NULL, FALSE},
552 {"Status:", NULL, FALSE},
553 {"X-Status:", NULL, FALSE},
554 {"From ", NULL, FALSE},
555 {"X-Face:", NULL, FALSE},
556 {"Disposition-Notification-To:", NULL, FALSE},
557 {"Return-Receipt-To:", NULL, FALSE},
558 {NULL, NULL, FALSE}};
560 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
561 {"From:", NULL, TRUE},
564 {"Newsgroups:", NULL, TRUE},
565 {"Subject:", NULL, TRUE},
566 {"Message-Id:", NULL, FALSE},
567 {"References:", NULL, FALSE},
568 {"In-Reply-To:", NULL, FALSE},
569 {"Content-Type:", NULL, FALSE},
570 {"Seen:", NULL, FALSE},
571 {"Status:", NULL, FALSE},
572 {"X-Status:", NULL, FALSE},
573 {"From ", NULL, FALSE},
574 {NULL, NULL, FALSE}};
576 HeaderEntry* procheader_get_headernames(gboolean full)
578 return full ? hentry_full : hentry_short;
581 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
584 return parse_stream(fp, FALSE, flags, full, decrypted);
587 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
588 gboolean full, gboolean decrypted)
591 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
592 gchar *reference = NULL;
597 get_one_field_func get_one_field =
598 isstring ? (get_one_field_func)string_get_one_field
599 : (get_one_field_func)procheader_get_one_field;
601 hentry = procheader_get_headernames(full);
603 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
604 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
608 msginfo = procmsg_msginfo_new();
610 if (flags.tmp_flags || flags.perm_flags)
611 msginfo->flags = flags;
613 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
616 MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MIME);
618 msginfo->inreplyto = NULL;
620 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
622 hp = buf + strlen(hentry[hnum].name);
623 while (*hp == ' ' || *hp == '\t') hp++;
627 if (msginfo->date) break;
629 procheader_date_parse(NULL, hp, 0);
630 msginfo->date = g_strdup(hp);
633 if (msginfo->from) break;
634 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
635 msginfo->from = g_strdup(tmp);
636 msginfo->fromname = procheader_get_fromname(tmp);
639 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
643 g_strconcat(p, ", ", tmp, NULL);
646 msginfo->to = g_strdup(tmp);
649 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
653 g_strconcat(p, ", ", tmp, NULL);
656 msginfo->cc = g_strdup(tmp);
659 if (msginfo->newsgroups) {
660 p = msginfo->newsgroups;
661 msginfo->newsgroups =
662 g_strconcat(p, ",", hp, NULL);
665 msginfo->newsgroups = g_strdup(hp);
668 if (msginfo->subject) break;
669 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
670 msginfo->subject = g_strdup(tmp);
673 if (msginfo->msgid) break;
675 extract_parenthesis(hp, '<', '>');
677 msginfo->msgid = g_strdup(hp);
682 msginfo->references = g_strdup(hp);
683 eliminate_parenthesis(hp, '(', ')');
684 if ((p = strrchr(hp, '<')) != NULL &&
685 strchr(p + 1, '>') != NULL) {
686 extract_parenthesis(p, '<', '>');
689 reference = g_strdup(p);
695 if (!strncasecmp(hp, "multipart", 9)) {
696 if (strncasecmp(hp, "multipart/signed", 16)) {
697 MSG_SET_TMP_FLAGS(msginfo->flags,
700 MSG_SET_TMP_FLAGS(msginfo->flags,
705 else if (!strncasecmp(hp, "multipart/encrypted", 19)) {
706 MSG_SET_TMP_FLAGS(msginfo->flags,
709 else if (!strncasecmp(hp, "multipart", 9) &&
710 !strncasecmp(hp, "multipart/signed", 16)) {
711 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_SIGNED);
713 else if (!strncasecmp(hp, "multipart", 9))
714 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
716 #ifdef ALLOW_HEADER_HINT
718 /* mnews Seen header */
719 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
723 if (msginfo->xface) break;
724 msginfo->xface = g_strdup(hp);
726 case H_DISPOSITION_NOTIFICATION_TO:
727 if (msginfo->dispositionnotificationto) break;
728 msginfo->dispositionnotificationto = g_strdup(hp);
730 case H_RETURN_RECEIPT_TO:
731 if (msginfo->returnreceiptto) break;
732 msginfo->returnreceiptto = g_strdup(hp);
734 #ifdef ALLOW_HEADER_HINT
736 if (strchr(hp, 'R') != NULL)
737 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
738 if (strchr(hp, 'O') != NULL)
739 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
740 if (strchr(hp, 'U') != NULL)
741 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
744 if (strchr(hp, 'D') != NULL)
745 MSG_SET_PERM_FLAGS(msginfo->flags,
747 if (strchr(hp, 'F') != NULL)
748 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
749 if (strchr(hp, 'd') != NULL)
750 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
751 if (strchr(hp, 'r') != NULL)
752 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
753 if (strchr(hp, 'f') != NULL)
754 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
758 if (msginfo->fromspace) break;
759 msginfo->fromspace = g_strdup(hp);
765 msginfo->inreplyto = reference;
770 gchar *procheader_get_fromname(const gchar *str)
774 Xstrdup_a(tmp, str, return NULL);
777 extract_quote(tmp, '\"');
779 } else if (strchr(tmp, '<')) {
780 eliminate_parenthesis(tmp, '<', '>');
784 extract_parenthesis(tmp, '<', '>');
787 } else if (strchr(tmp, '(')) {
788 extract_parenthesis(tmp, '(', ')');
793 name = g_strdup(str);
795 name = g_strdup(tmp);
800 static gint procheader_scan_date_string(const gchar *str,
801 gchar *weekday, gint *day,
802 gchar *month, gint *year,
803 gint *hh, gint *mm, gint *ss,
808 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
809 weekday, day, month, year, hh, mm, ss, zone);
810 if (result == 8) return 0;
812 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
813 weekday, day, month, year, hh, mm, ss, zone);
814 if (result == 8) return 0;
816 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
817 day, month, year, hh, mm, ss, zone);
818 if (result == 7) return 0;
821 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
822 weekday, day, month, year, hh, mm, ss);
823 if (result == 7) return 0;
826 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
827 weekday, day, month, year, hh, mm, zone);
828 if (result == 7) return 0;
830 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
831 day, month, year, hh, mm, zone);
832 if (result == 6) return 0;
838 * Hiro, most UNIXen support this function:
839 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
841 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
843 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
855 memset(t, 0, sizeof *t);
857 if (procheader_scan_date_string(src, weekday, &day, month, &year,
858 &hh, &mm, &ss, zone) < 0) {
859 g_warning("Invalid date: %s\n", src);
863 /* Y2K compliant :) */
872 if ((p = strstr(monthstr, month)) != NULL)
873 dmonth = (gint)(p - monthstr) / 3 + 1;
875 g_warning("Invalid month: %s\n", month);
876 dmonth = G_DATE_BAD_MONTH;
883 t->tm_mon = dmonth - 1;
884 t->tm_year = year - 1900;
894 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
896 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
903 GDateMonth dmonth = G_DATE_BAD_MONTH;
909 if (procheader_scan_date_string(src, weekday, &day, month, &year,
910 &hh, &mm, &ss, zone) < 0) {
911 g_warning("Invalid date: %s\n", src);
913 strncpy2(dest, src, len);
917 /* Y2K compliant :) */
926 for (p = monthstr; *p != '\0'; p += 3) {
927 if (!strncasecmp(p, month, 3)) {
928 dmonth = (gint)(p - monthstr) / 3 + 1;
933 g_warning("Invalid month: %s\n", month);
939 t.tm_mon = dmonth - 1;
940 t.tm_year = year - 1900;
946 tz_offset = remote_tzoffset_sec(zone);
948 timer += tzoffset_sec(&timer) - tz_offset;
951 procheader_date_get_localtime(dest, len, timer);
956 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
959 gchar *default_format = "%y/%m/%d(%a) %H:%M";
961 lt = localtime(&timer);
963 if (prefs_common.date_format)
964 strftime(dest, len, prefs_common.date_format, lt);
966 strftime(dest, len, default_format, lt);
969 /* Added by Mel Hadasht on 27 Aug 2001 */
970 /* Get a header from msginfo */
971 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
975 HeaderEntry hentry[]={ { header, NULL, TRUE },
976 { NULL, NULL, FALSE } };
979 g_return_val_if_fail(msginfo != NULL, -1);
980 file = procmsg_get_message_file_path(msginfo);
981 if ((fp = fopen(file, "rb")) == NULL) {
982 FILE_OP_ERROR(file, "fopen");
986 val = procheader_get_one_field(buf,len, fp, hentry);
987 if (fclose(fp) == EOF) {
988 FILE_OP_ERROR(file, "fclose");