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 gint (*get_one_field_func)(gchar *, gint, void *, HeaderEntry[]);
43 static gint string_get_one_field(gchar *buf, gint len, char **str,
44 HeaderEntry hentry[]);
46 static char *string_getline(char *buf, int len, char **str);
47 static int string_peekchar(char **str);
48 static int fpeekchar(FILE *fp);
49 static gint generic_get_one_field(gchar *buf, gint len, void *data,
51 getlinefunc getline, peekcharfunc peekchar);
52 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
53 gboolean full, gboolean decrypted);
56 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
59 return generic_get_one_field(buf, len, fp, hentry,
60 (getlinefunc)fgets, (peekcharfunc)fpeekchar);
63 static gint string_get_one_field(gchar *buf, gint len, char **str,
66 return generic_get_one_field(buf, len, str, hentry,
67 (getlinefunc)string_getline,
68 (peekcharfunc)string_peekchar);
71 static char *string_getline(char *buf, int len, char **str)
76 for (; **str && len > 1; --len)
77 if ((*buf++ = *(*str)++) == '\n')
84 static int string_peekchar(char **str)
89 static int fpeekchar(FILE *fp)
91 return ungetc(getc(fp), fp);
94 static gint generic_get_one_field(gchar *buf, gint len, void *data,
96 getlinefunc getline, peekcharfunc peekchar)
100 HeaderEntry *hp = NULL;
102 if (hentry != NULL) {
103 /* skip non-required headers */
106 if (getline(buf, len, data) == NULL)
108 if (buf[0] == '\r' || buf[0] == '\n')
110 } while (buf[0] == ' ' || buf[0] == '\t');
112 for (hp = hentry, hnum = 0; hp->name != NULL;
114 if (!strncasecmp(hp->name, buf,
118 } while (hp->name == NULL);
120 if (getline(buf, len, data) == NULL) return -1;
121 if (buf[0] == '\r' || buf[0] == '\n') return -1;
126 nexthead = peekchar(data);
127 if (nexthead == ' ' || nexthead == '\t') {
128 size_t buflen = strlen(buf);
130 /* concatenate next line */
131 if ((len - buflen) > 2) {
132 if (getline(buf + buflen, len - buflen, data) == NULL)
135 if (hp && hp->unfold)
143 /* remove trailing new line */
150 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
152 gboolean folded = FALSE;
156 if (fgets(buf, len, fp) == NULL) return NULL;
157 if (buf[0] == '\r' || buf[0] == '\n') return NULL;
158 bufp = buf + strlen(buf);
161 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
166 nexthead = fgetc(fp);
169 if (nexthead == ' ' || nexthead == '\t')
171 else if (nexthead == EOF)
173 else if (folded == TRUE) {
174 if (nexthead == '\r' || nexthead == '\n') {
179 if ((len - (bufp - buf)) <= 2) break;
181 /* replace return code on the tail end
187 /* concatenate next line */
188 if (fgets(bufp, len - (bufp - buf), fp)
190 bufp += strlen(bufp);
193 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
199 ungetc(nexthead, fp);
204 /* remove trailing return code */
212 GSList *procheader_get_header_list_from_file(const gchar *file)
217 if ((fp = fopen(file, "rb")) == NULL) {
218 FILE_OP_ERROR(file, "fopen");
222 hlist = procheader_get_header_list(fp);
228 GSList *procheader_get_header_list(FILE *fp)
231 GSList *hlist = NULL;
234 g_return_val_if_fail(fp != NULL, NULL);
236 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
237 if ((header = procheader_parse_header(buf)) != NULL)
238 hlist = g_slist_append(hlist, header);
240 if (*buf == ':') continue;
241 for (p = buf; *p && *p != ' '; p++) {
243 header = g_new(Header, 1);
244 header->name = g_strndup(buf, p - buf);
246 while (*p == ' ' || *p == '\t') p++;
247 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
248 header->body = g_strdup(tmp);
250 hlist = g_slist_append(hlist, header);
262 GPtrArray *procheader_get_header_array(FILE *fp)
268 g_return_val_if_fail(fp != NULL, NULL);
270 headers = g_ptr_array_new();
272 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
273 if ((header = procheader_parse_header(buf)) != NULL)
274 g_ptr_array_add(headers, header);
276 if (*buf == ':') continue;
277 for (p = buf; *p && *p != ' '; p++) {
279 header = g_new(Header, 1);
280 header->name = g_strndup(buf, p - buf);
282 while (*p == ' ' || *p == '\t') p++;
283 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
284 header->body = g_strdup(tmp);
286 g_ptr_array_add(headers, header);
297 GPtrArray *procheader_get_header_array_asis(FILE *fp)
303 g_return_val_if_fail(fp != NULL, NULL);
305 headers = g_ptr_array_new();
307 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
308 if ((header = procheader_parse_header(buf)) != NULL)
309 g_ptr_array_add(headers, header);
311 if (*buf == ':') continue;
312 for (p = buf; *p && *p != ' '; p++) {
314 header = g_new(Header, 1);
315 header->name = g_strndup(buf, p - buf);
317 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
318 header->body = g_strdup(tmp);
320 g_ptr_array_add(headers, header);
331 void procheader_header_list_destroy(GSList *hlist)
335 while (hlist != NULL) {
336 header = hlist->data;
337 procheader_header_free(header);
338 hlist = g_slist_remove(hlist, header);
343 void procheader_header_array_destroy(GPtrArray *harray)
348 for (i = 0; i < harray->len; i++) {
349 header = g_ptr_array_index(harray, i);
350 procheader_header_free(header);
353 g_ptr_array_free(harray, TRUE);
356 void procheader_header_free(Header *header)
360 g_free(header->name);
361 g_free(header->body);
366 tests whether two headers' names are equal
367 remove the trailing ':' or ' ' before comparing
370 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
377 if (hdr1[len1 - 1] == ':')
379 if (hdr2[len2 - 1] == ':')
384 return (g_strncasecmp(hdr1, hdr2, len1) == 0);
388 parse headers, for example :
389 From: dinh@enseirb.fr becomes :
390 header->name = "From:"
391 header->body = "dinh@enseirb.fr"
394 Header * procheader_parse_header(gchar * buf)
400 if ((*buf == ':') || (*buf == ' '))
403 for (p = buf; *p ; p++) {
404 if ((*p == ':') || (*p == ' ')) {
405 header = g_new(Header, 1);
406 header->name = g_strndup(buf, p - buf + 1);
408 while (*p == ' ' || *p == '\t') p++;
409 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
411 header->body = g_strdup(p);
413 header->body = g_strdup(tmp);
420 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
427 if (hentry == NULL) return;
429 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
433 p = buf + strlen(hp->name);
434 while (*p == ' ' || *p == '\t') p++;
436 if (hp->body == NULL)
437 hp->body = g_strdup(p);
438 else if (procheader_headername_equal(hp->name, "To") ||
439 procheader_headername_equal(hp->name, "Cc")) {
440 gchar *tp = hp->body;
441 hp->body = g_strconcat(tp, ", ", p, NULL);
447 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
448 gboolean full, gboolean decrypted)
453 if ((fp = fopen(file, "rb")) == NULL) {
454 FILE_OP_ERROR(file, "fopen");
458 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
463 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
466 return parse_stream(&str, TRUE, flags, full, decrypted);
486 H_DISPOSITION_NOTIFICATION_TO = 15,
487 H_RETURN_RECEIPT_TO = 16
490 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
491 {"From:", NULL, TRUE},
494 {"Newsgroups:", NULL, TRUE},
495 {"Subject:", NULL, TRUE},
496 {"Message-Id:", NULL, FALSE},
497 {"References:", NULL, FALSE},
498 {"In-Reply-To:", NULL, FALSE},
499 {"Content-Type:", NULL, FALSE},
500 {"Seen:", NULL, FALSE},
501 {"Status:", NULL, FALSE},
502 {"X-Status:", NULL, FALSE},
503 {"From ", NULL, FALSE},
504 {"X-Face:", NULL, FALSE},
505 {"Disposition-Notification-To:", NULL, FALSE},
506 {"Return-Receipt-To:", NULL, FALSE},
507 {NULL, NULL, FALSE}};
509 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
510 {"From:", NULL, TRUE},
513 {"Newsgroups:", NULL, TRUE},
514 {"Subject:", NULL, TRUE},
515 {"Message-Id:", NULL, FALSE},
516 {"References:", NULL, FALSE},
517 {"In-Reply-To:", NULL, FALSE},
518 {"Content-Type:", NULL, FALSE},
519 {"Seen:", NULL, FALSE},
520 {"Status:", NULL, FALSE},
521 {"X-Status:", NULL, FALSE},
522 {"From ", NULL, FALSE},
523 {NULL, NULL, FALSE}};
525 HeaderEntry* procheader_get_headernames(gboolean full)
527 return full ? hentry_full : hentry_short;
530 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
533 return parse_stream(fp, FALSE, flags, full, decrypted);
536 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
537 gboolean full, gboolean decrypted)
540 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
541 gchar *reference = NULL;
546 get_one_field_func get_one_field =
547 isstring ? (get_one_field_func)string_get_one_field
548 : (get_one_field_func)procheader_get_one_field;
550 hentry = procheader_get_headernames(full);
552 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
553 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
557 msginfo = procmsg_msginfo_new();
559 if (flags.tmp_flags || flags.perm_flags)
560 msginfo->flags = flags;
562 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
565 MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MIME);
567 msginfo->inreplyto = NULL;
569 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
571 hp = buf + strlen(hentry[hnum].name);
572 while (*hp == ' ' || *hp == '\t') hp++;
576 if (msginfo->date) break;
578 procheader_date_parse(NULL, hp, 0);
579 msginfo->date = g_strdup(hp);
582 if (msginfo->from) break;
583 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
584 msginfo->from = g_strdup(tmp);
585 msginfo->fromname = procheader_get_fromname(tmp);
588 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
592 g_strconcat(p, ", ", tmp, NULL);
595 msginfo->to = g_strdup(tmp);
598 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
602 g_strconcat(p, ", ", tmp, NULL);
605 msginfo->cc = g_strdup(tmp);
608 if (msginfo->newsgroups) {
609 p = msginfo->newsgroups;
610 msginfo->newsgroups =
611 g_strconcat(p, ",", hp, NULL);
614 msginfo->newsgroups = g_strdup(hp);
617 if (msginfo->subject) break;
618 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
619 msginfo->subject = g_strdup(tmp);
622 if (msginfo->msgid) break;
624 extract_parenthesis(hp, '<', '>');
626 msginfo->msgid = g_strdup(hp);
631 msginfo->references = g_strdup(hp);
632 eliminate_parenthesis(hp, '(', ')');
633 if ((p = strrchr(hp, '<')) != NULL &&
634 strchr(p + 1, '>') != NULL) {
635 extract_parenthesis(p, '<', '>');
638 reference = g_strdup(p);
644 if (!strncasecmp(hp, "multipart", 9)) {
645 if (strncasecmp(hp, "multipart/signed", 16)) {
646 MSG_SET_TMP_FLAGS(msginfo->flags,
649 MSG_SET_TMP_FLAGS(msginfo->flags,
654 else if (!strncasecmp(hp, "multipart/encrypted", 19)) {
655 MSG_SET_TMP_FLAGS(msginfo->flags,
658 else if (!strncasecmp(hp, "multipart", 9) &&
659 !strncasecmp(hp, "multipart/signed", 16)) {
660 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_SIGNED);
662 else if (!strncasecmp(hp, "multipart", 9))
663 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
665 #ifdef ALLOW_HEADER_HINT
667 /* mnews Seen header */
668 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
672 if (msginfo->xface) break;
673 msginfo->xface = g_strdup(hp);
675 case H_DISPOSITION_NOTIFICATION_TO:
676 if (msginfo->dispositionnotificationto) break;
677 msginfo->dispositionnotificationto = g_strdup(hp);
679 case H_RETURN_RECEIPT_TO:
680 if (msginfo->returnreceiptto) break;
681 msginfo->returnreceiptto = g_strdup(hp);
683 #ifdef ALLOW_HEADER_HINT
685 if (strchr(hp, 'R') != NULL)
686 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
687 if (strchr(hp, 'O') != NULL)
688 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
689 if (strchr(hp, 'U') != NULL)
690 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
693 if (strchr(hp, 'D') != NULL)
694 MSG_SET_PERM_FLAGS(msginfo->flags,
696 if (strchr(hp, 'F') != NULL)
697 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
698 if (strchr(hp, 'd') != NULL)
699 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
700 if (strchr(hp, 'r') != NULL)
701 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
702 if (strchr(hp, 'f') != NULL)
703 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
707 if (msginfo->fromspace) break;
708 msginfo->fromspace = g_strdup(hp);
714 msginfo->inreplyto = reference;
719 gchar *procheader_get_fromname(const gchar *str)
723 Xstrdup_a(tmp, str, return NULL);
726 extract_quote(tmp, '\"');
728 } else if (strchr(tmp, '<')) {
729 eliminate_parenthesis(tmp, '<', '>');
733 extract_parenthesis(tmp, '<', '>');
736 } else if (strchr(tmp, '(')) {
737 extract_parenthesis(tmp, '(', ')');
742 name = g_strdup(str);
744 name = g_strdup(tmp);
749 static gint procheader_scan_date_string(const gchar *str,
750 gchar *weekday, gint *day,
751 gchar *month, gint *year,
752 gint *hh, gint *mm, gint *ss,
757 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
758 weekday, day, month, year, hh, mm, ss, zone);
759 if (result == 8) return 0;
761 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
762 weekday, day, month, year, hh, mm, ss, zone);
763 if (result == 8) return 0;
765 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
766 day, month, year, hh, mm, ss, zone);
767 if (result == 7) return 0;
770 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
771 weekday, day, month, year, hh, mm, ss);
772 if (result == 7) return 0;
775 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
776 weekday, day, month, year, hh, mm, zone);
777 if (result == 7) return 0;
779 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
780 day, month, year, hh, mm, zone);
781 if (result == 6) return 0;
787 * Hiro, most UNIXen support this function:
788 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
790 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
792 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
804 memset(t, 0, sizeof *t);
806 if (procheader_scan_date_string(src, weekday, &day, month, &year,
807 &hh, &mm, &ss, zone) < 0) {
808 g_warning("Invalid date: %s\n", src);
812 /* Y2K compliant :) */
821 if ((p = strstr(monthstr, month)) != NULL)
822 dmonth = (gint)(p - monthstr) / 3 + 1;
824 g_warning("Invalid month: %s\n", month);
825 dmonth = G_DATE_BAD_MONTH;
832 t->tm_mon = dmonth - 1;
833 t->tm_year = year - 1900;
843 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
845 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
852 GDateMonth dmonth = G_DATE_BAD_MONTH;
858 if (procheader_scan_date_string(src, weekday, &day, month, &year,
859 &hh, &mm, &ss, zone) < 0) {
860 g_warning("Invalid date: %s\n", src);
862 strncpy2(dest, src, len);
866 /* Y2K compliant :) */
875 for (p = monthstr; *p != '\0'; p += 3) {
876 if (!strncasecmp(p, month, 3)) {
877 dmonth = (gint)(p - monthstr) / 3 + 1;
882 g_warning("Invalid month: %s\n", month);
888 t.tm_mon = dmonth - 1;
889 t.tm_year = year - 1900;
895 tz_offset = remote_tzoffset_sec(zone);
897 timer += tzoffset_sec(&timer) - tz_offset;
900 procheader_date_get_localtime(dest, len, timer);
905 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
908 gchar *default_format = "%y/%m/%d(%a) %H:%M";
910 lt = localtime(&timer);
912 if (prefs_common.date_format)
913 strftime(dest, len, prefs_common.date_format, lt);
915 strftime(dest, len, default_format, lt);
918 /* Added by Mel Hadasht on 27 Aug 2001 */
919 /* Get a header from msginfo */
920 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
924 HeaderEntry hentry[]={ { header, NULL, TRUE },
925 { NULL, NULL, FALSE } };
928 g_return_val_if_fail(msginfo != NULL, -1);
929 file = procmsg_get_message_file_path(msginfo);
930 if ((fp = fopen(file, "rb")) == NULL) {
931 FILE_OP_ERROR(file, "fopen");
935 val = procheader_get_one_field(buf,len, fp, hentry);
936 if (fclose(fp) == EOF) {
937 FILE_OP_ERROR(file, "fclose");