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 file_peekchar(FILE *fp);
50 static gint generic_get_one_field(gchar *buf, gint len, void *data,
53 peekcharfunc peekchar,
55 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
56 gboolean full, gboolean decrypted);
59 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
62 return generic_get_one_field(buf, len, fp, hentry,
63 (getlinefunc)fgets, (peekcharfunc)file_peekchar,
67 static gint string_get_one_field(gchar *buf, gint len, char **str,
70 return generic_get_one_field(buf, len, str, hentry,
71 (getlinefunc)string_getline,
72 (peekcharfunc)string_peekchar,
76 static char *string_getline(char *buf, int len, char **str)
81 for (; **str && len > 1; --len)
82 if ((*buf++ = *(*str)++) == '\n')
89 static int string_peekchar(char **str)
94 static int file_peekchar(FILE *fp)
96 return ungetc(getc(fp), fp);
99 static gint generic_get_one_field(gchar *buf, gint len, void *data,
101 getlinefunc getline, peekcharfunc peekchar,
106 HeaderEntry *hp = NULL;
108 if (hentry != NULL) {
109 /* skip non-required headers */
112 if (getline(buf, len, data) == NULL)
114 if (buf[0] == '\r' || buf[0] == '\n')
116 } while (buf[0] == ' ' || buf[0] == '\t');
118 for (hp = hentry, hnum = 0; hp->name != NULL;
120 if (!strncasecmp(hp->name, buf,
124 } while (hp->name == NULL);
126 if (getline(buf, len, data) == NULL) return -1;
127 if (buf[0] == '\r' || buf[0] == '\n') return -1;
132 nexthead = peekchar(data);
133 /* ([*WSP CRLF] 1*WSP) */
134 if (nexthead == ' ' || nexthead == '\t') {
136 /* trim previous trailing \n if requesting one header or
137 * unfolding was requested */
138 if ((!hentry && unfold) || (hp && hp->unfold))
141 buflen = strlen(buf);
143 /* concatenate next line */
144 if ((len - buflen) > 2) {
145 if (getline(buf + buflen, len - buflen, data) == NULL)
150 /* remove trailing new line */
159 gint procheader_get_one_field_asis(gchar *buf, gint len, FILE *fp)
161 return generic_get_one_field(buf, len, fp, NULL,
163 (peekcharfunc)file_peekchar,
168 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
170 gboolean folded = FALSE;
174 if (fgets(buf, len, fp) == NULL) return NULL;
175 if (buf[0] == '\r' || buf[0] == '\n') return NULL;
176 bufp = buf + strlen(buf);
179 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
184 nexthead = fgetc(fp);
187 if (nexthead == ' ' || nexthead == '\t')
189 else if (nexthead == EOF)
191 else if (folded == TRUE) {
192 if (nexthead == '\r' || nexthead == '\n') {
197 if ((len - (bufp - buf)) <= 2) break;
199 /* replace return code on the tail end
205 /* concatenate next line */
206 if (fgets(bufp, len - (bufp - buf), fp)
208 bufp += strlen(bufp);
211 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
217 ungetc(nexthead, fp);
222 /* remove trailing return code */
230 GSList *procheader_get_header_list_from_file(const gchar *file)
235 if ((fp = fopen(file, "rb")) == NULL) {
236 FILE_OP_ERROR(file, "fopen");
240 hlist = procheader_get_header_list(fp);
246 GSList *procheader_get_header_list(FILE *fp)
249 GSList *hlist = NULL;
252 g_return_val_if_fail(fp != NULL, NULL);
254 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
255 if ((header = procheader_parse_header(buf)) != NULL)
256 hlist = g_slist_append(hlist, header);
258 if (*buf == ':') continue;
259 for (p = buf; *p && *p != ' '; p++) {
261 header = g_new(Header, 1);
262 header->name = g_strndup(buf, p - buf);
264 while (*p == ' ' || *p == '\t') p++;
265 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
266 header->body = g_strdup(tmp);
268 hlist = g_slist_append(hlist, header);
280 GPtrArray *procheader_get_header_array(FILE *fp)
286 g_return_val_if_fail(fp != NULL, NULL);
288 headers = g_ptr_array_new();
290 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
291 if ((header = procheader_parse_header(buf)) != NULL)
292 g_ptr_array_add(headers, header);
294 if (*buf == ':') continue;
295 for (p = buf; *p && *p != ' '; p++) {
297 header = g_new(Header, 1);
298 header->name = g_strndup(buf, p - buf);
300 while (*p == ' ' || *p == '\t') p++;
301 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
302 header->body = g_strdup(tmp);
304 g_ptr_array_add(headers, header);
315 GPtrArray *procheader_get_header_array_asis(FILE *fp)
321 g_return_val_if_fail(fp != NULL, NULL);
323 headers = g_ptr_array_new();
325 while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
326 if ((header = procheader_parse_header(buf)) != NULL)
327 g_ptr_array_add(headers, header);
329 if (*buf == ':') continue;
330 for (p = buf; *p && *p != ' '; p++) {
332 header = g_new(Header, 1);
333 header->name = g_strndup(buf, p - buf);
335 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
336 header->body = g_strdup(tmp);
338 g_ptr_array_add(headers, header);
349 void procheader_header_list_destroy(GSList *hlist)
353 while (hlist != NULL) {
354 header = hlist->data;
355 procheader_header_free(header);
356 hlist = g_slist_remove(hlist, header);
361 void procheader_header_array_destroy(GPtrArray *harray)
366 for (i = 0; i < harray->len; i++) {
367 header = g_ptr_array_index(harray, i);
368 procheader_header_free(header);
371 g_ptr_array_free(harray, TRUE);
374 void procheader_header_free(Header *header)
378 g_free(header->name);
379 g_free(header->body);
384 tests whether two headers' names are equal
385 remove the trailing ':' or ' ' before comparing
388 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
395 if (hdr1[len1 - 1] == ':')
397 if (hdr2[len2 - 1] == ':')
402 return (g_strncasecmp(hdr1, hdr2, len1) == 0);
406 parse headers, for example :
407 From: dinh@enseirb.fr becomes :
408 header->name = "From:"
409 header->body = "dinh@enseirb.fr"
412 Header * procheader_parse_header(gchar * buf)
418 if ((*buf == ':') || (*buf == ' '))
421 for (p = buf; *p ; p++) {
422 if ((*p == ':') || (*p == ' ')) {
423 header = g_new(Header, 1);
424 header->name = g_strndup(buf, p - buf + 1);
426 while (*p == ' ' || *p == '\t') p++;
427 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
429 header->body = g_strdup(p);
431 header->body = g_strdup(tmp);
438 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
445 if (hentry == NULL) return;
447 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
451 p = buf + strlen(hp->name);
452 while (*p == ' ' || *p == '\t') p++;
454 if (hp->body == NULL)
455 hp->body = g_strdup(p);
456 else if (procheader_headername_equal(hp->name, "To") ||
457 procheader_headername_equal(hp->name, "Cc")) {
458 gchar *tp = hp->body;
459 hp->body = g_strconcat(tp, ", ", p, NULL);
465 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
466 gboolean full, gboolean decrypted)
471 if ((fp = fopen(file, "rb")) == NULL) {
472 FILE_OP_ERROR(file, "fopen");
476 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
481 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
484 return parse_stream(&str, TRUE, flags, full, decrypted);
503 H_SC_PLANNED_DOWNLOAD = 14,
505 H_DISPOSITION_NOTIFICATION_TO = 16,
506 H_RETURN_RECEIPT_TO = 17,
507 H_SC_PARTIALLY_RETRIEVED = 18,
508 H_SC_ACCOUNT_SERVER = 19,
509 H_SC_ACCOUNT_LOGIN = 20,
510 H_SC_MESSAGE_SIZE = 21
513 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
514 {"From:", NULL, TRUE},
517 {"Newsgroups:", NULL, TRUE},
518 {"Subject:", NULL, TRUE},
519 {"Message-ID:", NULL, FALSE},
520 {"References:", NULL, FALSE},
521 {"In-Reply-To:", NULL, FALSE},
522 {"Content-Type:", NULL, FALSE},
523 {"Seen:", NULL, FALSE},
524 {"Status:", NULL, FALSE},
525 {"X-Status:", NULL, FALSE},
526 {"From ", NULL, FALSE},
527 {"SC-Marked-For-Download:", NULL, FALSE},
528 {"X-Face:", NULL, FALSE},
529 {"Disposition-Notification-To:", NULL, FALSE},
530 {"Return-Receipt-To:", NULL, FALSE},
531 {"SC-Partially-Retrieved:", NULL, FALSE},
532 {"SC-Account-Server:", NULL, FALSE},
533 {"SC-Account-Login:",NULL, FALSE},
534 {"SC-Message-Size:", NULL, FALSE},
535 {NULL, NULL, FALSE}};
537 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
538 {"From:", NULL, TRUE},
541 {"Newsgroups:", NULL, TRUE},
542 {"Subject:", NULL, TRUE},
543 {"Message-ID:", NULL, FALSE},
544 {"References:", NULL, FALSE},
545 {"In-Reply-To:", NULL, FALSE},
546 {"Content-Type:", NULL, FALSE},
547 {"Seen:", NULL, FALSE},
548 {"Status:", NULL, FALSE},
549 {"X-Status:", NULL, FALSE},
550 {"From ", NULL, FALSE},
551 {"SC-Marked-For-Download:", NULL, FALSE},
552 {NULL, NULL, FALSE}};
554 HeaderEntry* procheader_get_headernames(gboolean full)
556 return full ? hentry_full : hentry_short;
559 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
562 return parse_stream(fp, FALSE, flags, full, decrypted);
565 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
566 gboolean full, gboolean decrypted)
569 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
570 gchar *reference = NULL;
575 get_one_field_func get_one_field =
576 isstring ? (get_one_field_func)string_get_one_field
577 : (get_one_field_func)procheader_get_one_field;
579 hentry = procheader_get_headernames(full);
581 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
582 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
586 msginfo = procmsg_msginfo_new();
588 if (flags.tmp_flags || flags.perm_flags)
589 msginfo->flags = flags;
591 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
593 msginfo->inreplyto = NULL;
595 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
597 hp = buf + strlen(hentry[hnum].name);
598 while (*hp == ' ' || *hp == '\t') hp++;
602 if (msginfo->date) break;
604 procheader_date_parse(NULL, hp, 0);
605 msginfo->date = g_strdup(hp);
608 if (msginfo->from) break;
609 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
610 msginfo->from = g_strdup(tmp);
611 msginfo->fromname = procheader_get_fromname(tmp);
614 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
618 g_strconcat(p, ", ", tmp, NULL);
621 msginfo->to = g_strdup(tmp);
624 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
628 g_strconcat(p, ", ", tmp, NULL);
631 msginfo->cc = g_strdup(tmp);
634 if (msginfo->newsgroups) {
635 p = msginfo->newsgroups;
636 msginfo->newsgroups =
637 g_strconcat(p, ",", hp, NULL);
640 msginfo->newsgroups = g_strdup(hp);
643 if (msginfo->subject) break;
644 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
645 msginfo->subject = g_strdup(tmp);
648 if (msginfo->msgid) break;
650 extract_parenthesis(hp, '<', '>');
652 msginfo->msgid = g_strdup(hp);
657 msginfo->references = g_strdup(hp);
658 eliminate_parenthesis(hp, '(', ')');
659 if ((p = strrchr(hp, '<')) != NULL &&
660 strchr(p + 1, '>') != NULL) {
661 extract_parenthesis(p, '<', '>');
664 reference = g_strdup(p);
669 if (!strncasecmp(hp, "multipart/", 10))
670 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
672 #ifdef ALLOW_HEADER_HINT
674 /* mnews Seen header */
675 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
679 if (msginfo->xface) break;
680 msginfo->xface = g_strdup(hp);
682 case H_DISPOSITION_NOTIFICATION_TO:
683 if (msginfo->dispositionnotificationto) break;
684 msginfo->dispositionnotificationto = g_strdup(hp);
686 case H_RETURN_RECEIPT_TO:
687 if (msginfo->returnreceiptto) break;
688 msginfo->returnreceiptto = g_strdup(hp);
690 case H_SC_PARTIALLY_RETRIEVED:
691 if (msginfo->partial_recv) break;
692 msginfo->partial_recv = g_strdup(hp);
694 case H_SC_ACCOUNT_SERVER:
695 if (msginfo->account_server) break;
696 msginfo->account_server = g_strdup(hp);
698 case H_SC_ACCOUNT_LOGIN:
699 if (msginfo->account_login) break;
700 msginfo->account_login = g_strdup(hp);
702 case H_SC_MESSAGE_SIZE:
703 if (msginfo->total_size) break;
704 msginfo->total_size = atoi(hp);
706 case H_SC_PLANNED_DOWNLOAD:
707 msginfo->planned_download = atoi(hp);
709 #ifdef ALLOW_HEADER_HINT
711 if (strchr(hp, 'R') != NULL)
712 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
713 if (strchr(hp, 'O') != NULL)
714 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
715 if (strchr(hp, 'U') != NULL)
716 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
719 if (strchr(hp, 'D') != NULL)
720 MSG_SET_PERM_FLAGS(msginfo->flags,
722 if (strchr(hp, 'F') != NULL)
723 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
724 if (strchr(hp, 'd') != NULL)
725 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
726 if (strchr(hp, 'r') != NULL)
727 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
728 if (strchr(hp, 'f') != NULL)
729 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
733 if (msginfo->fromspace) break;
734 msginfo->fromspace = g_strdup(hp);
740 msginfo->inreplyto = reference;
745 gchar *procheader_get_fromname(const gchar *str)
749 Xstrdup_a(tmp, str, return NULL);
752 extract_quote(tmp, '\"');
754 } else if (strchr(tmp, '<')) {
755 eliminate_parenthesis(tmp, '<', '>');
759 extract_parenthesis(tmp, '<', '>');
762 } else if (strchr(tmp, '(')) {
763 extract_parenthesis(tmp, '(', ')');
768 name = g_strdup(str);
770 name = g_strdup(tmp);
775 static gint procheader_scan_date_string(const gchar *str,
776 gchar *weekday, gint *day,
777 gchar *month, gint *year,
778 gint *hh, gint *mm, gint *ss,
783 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
784 weekday, day, month, year, hh, mm, ss, zone);
785 if (result == 8) return 0;
787 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
788 weekday, day, month, year, hh, mm, ss, zone);
789 if (result == 8) return 0;
791 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
792 day, month, year, hh, mm, ss, zone);
793 if (result == 7) return 0;
796 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
797 weekday, day, month, year, hh, mm, ss);
798 if (result == 7) return 0;
801 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
802 weekday, day, month, year, hh, mm, zone);
803 if (result == 7) return 0;
805 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
806 day, month, year, hh, mm, zone);
807 if (result == 6) return 0;
813 * Hiro, most UNIXen support this function:
814 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
816 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
818 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
830 memset(t, 0, sizeof *t);
832 if (procheader_scan_date_string(src, weekday, &day, month, &year,
833 &hh, &mm, &ss, zone) < 0) {
834 g_warning("Invalid date: %s\n", src);
838 /* Y2K compliant :) */
847 if ((p = strstr(monthstr, month)) != NULL)
848 dmonth = (gint)(p - monthstr) / 3 + 1;
850 g_warning("Invalid month: %s\n", month);
851 dmonth = G_DATE_BAD_MONTH;
858 t->tm_mon = dmonth - 1;
859 t->tm_year = year - 1900;
869 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
871 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
878 GDateMonth dmonth = G_DATE_BAD_MONTH;
884 if (procheader_scan_date_string(src, weekday, &day, month, &year,
885 &hh, &mm, &ss, zone) < 0) {
886 g_warning("Invalid date: %s\n", src);
888 strncpy2(dest, src, len);
892 /* Y2K compliant :) */
901 for (p = monthstr; *p != '\0'; p += 3) {
902 if (!strncasecmp(p, month, 3)) {
903 dmonth = (gint)(p - monthstr) / 3 + 1;
908 g_warning("Invalid month: %s\n", month);
914 t.tm_mon = dmonth - 1;
915 t.tm_year = year - 1900;
921 tz_offset = remote_tzoffset_sec(zone);
923 timer += tzoffset_sec(&timer) - tz_offset;
926 procheader_date_get_localtime(dest, len, timer);
931 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
934 gchar *default_format = "%y/%m/%d(%a) %H:%M";
936 lt = localtime(&timer);
938 if (prefs_common.date_format)
939 strftime(dest, len, prefs_common.date_format, lt);
941 strftime(dest, len, default_format, lt);
944 /* Added by Mel Hadasht on 27 Aug 2001 */
945 /* Get a header from msginfo */
946 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
950 HeaderEntry hentry[]={ { header, NULL, TRUE },
951 { NULL, NULL, FALSE } };
954 g_return_val_if_fail(msginfo != NULL, -1);
955 file = procmsg_get_message_file_path(msginfo);
956 if ((fp = fopen(file, "rb")) == NULL) {
957 FILE_OP_ERROR(file, "fopen");
961 val = procheader_get_one_field(buf,len, fp, hentry);
962 if (fclose(fp) == EOF) {
963 FILE_OP_ERROR(file, "fclose");