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);
504 H_DISPOSITION_NOTIFICATION_TO = 15,
505 H_RETURN_RECEIPT_TO = 16
508 static HeaderEntry hentry_full[] = {{"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 {"X-Face:", NULL, FALSE},
523 {"Disposition-Notification-To:", NULL, FALSE},
524 {"Return-Receipt-To:", NULL, FALSE},
525 {NULL, NULL, FALSE}};
527 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
528 {"From:", NULL, TRUE},
531 {"Newsgroups:", NULL, TRUE},
532 {"Subject:", NULL, TRUE},
533 {"Message-Id:", NULL, FALSE},
534 {"References:", NULL, FALSE},
535 {"In-Reply-To:", NULL, FALSE},
536 {"Content-Type:", NULL, FALSE},
537 {"Seen:", NULL, FALSE},
538 {"Status:", NULL, FALSE},
539 {"X-Status:", NULL, FALSE},
540 {"From ", NULL, FALSE},
541 {NULL, NULL, FALSE}};
543 HeaderEntry* procheader_get_headernames(gboolean full)
545 return full ? hentry_full : hentry_short;
548 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
551 return parse_stream(fp, FALSE, flags, full, decrypted);
554 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
555 gboolean full, gboolean decrypted)
558 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
559 gchar *reference = NULL;
564 get_one_field_func get_one_field =
565 isstring ? (get_one_field_func)string_get_one_field
566 : (get_one_field_func)procheader_get_one_field;
568 hentry = procheader_get_headernames(full);
570 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
571 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
575 msginfo = procmsg_msginfo_new();
577 if (flags.tmp_flags || flags.perm_flags)
578 msginfo->flags = flags;
580 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
583 MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MIME);
585 msginfo->inreplyto = NULL;
587 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
589 hp = buf + strlen(hentry[hnum].name);
590 while (*hp == ' ' || *hp == '\t') hp++;
594 if (msginfo->date) break;
596 procheader_date_parse(NULL, hp, 0);
597 msginfo->date = g_strdup(hp);
600 if (msginfo->from) break;
601 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
602 msginfo->from = g_strdup(tmp);
603 msginfo->fromname = procheader_get_fromname(tmp);
606 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
610 g_strconcat(p, ", ", tmp, NULL);
613 msginfo->to = g_strdup(tmp);
616 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
620 g_strconcat(p, ", ", tmp, NULL);
623 msginfo->cc = g_strdup(tmp);
626 if (msginfo->newsgroups) {
627 p = msginfo->newsgroups;
628 msginfo->newsgroups =
629 g_strconcat(p, ",", hp, NULL);
632 msginfo->newsgroups = g_strdup(hp);
635 if (msginfo->subject) break;
636 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
637 msginfo->subject = g_strdup(tmp);
640 if (msginfo->msgid) break;
642 extract_parenthesis(hp, '<', '>');
644 msginfo->msgid = g_strdup(hp);
649 msginfo->references = g_strdup(hp);
650 eliminate_parenthesis(hp, '(', ')');
651 if ((p = strrchr(hp, '<')) != NULL &&
652 strchr(p + 1, '>') != NULL) {
653 extract_parenthesis(p, '<', '>');
656 reference = g_strdup(p);
662 if (!strncasecmp(hp, "multipart", 9)) {
663 if (strncasecmp(hp, "multipart/signed", 16)) {
664 MSG_SET_TMP_FLAGS(msginfo->flags,
667 MSG_SET_TMP_FLAGS(msginfo->flags,
672 else if (!strncasecmp(hp, "multipart/encrypted", 19)) {
673 MSG_SET_TMP_FLAGS(msginfo->flags,
676 else if (!strncasecmp(hp, "multipart", 9) &&
677 !strncasecmp(hp, "multipart/signed", 16)) {
678 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_SIGNED);
680 else if (!strncasecmp(hp, "multipart", 9))
681 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
683 #ifdef ALLOW_HEADER_HINT
685 /* mnews Seen header */
686 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
690 if (msginfo->xface) break;
691 msginfo->xface = g_strdup(hp);
693 case H_DISPOSITION_NOTIFICATION_TO:
694 if (msginfo->dispositionnotificationto) break;
695 msginfo->dispositionnotificationto = g_strdup(hp);
697 case H_RETURN_RECEIPT_TO:
698 if (msginfo->returnreceiptto) break;
699 msginfo->returnreceiptto = g_strdup(hp);
701 #ifdef ALLOW_HEADER_HINT
703 if (strchr(hp, 'R') != NULL)
704 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
705 if (strchr(hp, 'O') != NULL)
706 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
707 if (strchr(hp, 'U') != NULL)
708 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
711 if (strchr(hp, 'D') != NULL)
712 MSG_SET_PERM_FLAGS(msginfo->flags,
714 if (strchr(hp, 'F') != NULL)
715 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
716 if (strchr(hp, 'd') != NULL)
717 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
718 if (strchr(hp, 'r') != NULL)
719 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
720 if (strchr(hp, 'f') != NULL)
721 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
725 if (msginfo->fromspace) break;
726 msginfo->fromspace = g_strdup(hp);
732 msginfo->inreplyto = reference;
737 gchar *procheader_get_fromname(const gchar *str)
741 Xstrdup_a(tmp, str, return NULL);
744 extract_quote(tmp, '\"');
746 } else if (strchr(tmp, '<')) {
747 eliminate_parenthesis(tmp, '<', '>');
751 extract_parenthesis(tmp, '<', '>');
754 } else if (strchr(tmp, '(')) {
755 extract_parenthesis(tmp, '(', ')');
760 name = g_strdup(str);
762 name = g_strdup(tmp);
767 static gint procheader_scan_date_string(const gchar *str,
768 gchar *weekday, gint *day,
769 gchar *month, gint *year,
770 gint *hh, gint *mm, gint *ss,
775 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
776 weekday, day, month, year, hh, mm, ss, zone);
777 if (result == 8) return 0;
779 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
780 weekday, day, month, year, hh, mm, ss, zone);
781 if (result == 8) return 0;
783 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
784 day, month, year, hh, mm, ss, zone);
785 if (result == 7) return 0;
788 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
789 weekday, day, month, year, hh, mm, ss);
790 if (result == 7) return 0;
793 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
794 weekday, day, month, year, hh, mm, zone);
795 if (result == 7) return 0;
797 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
798 day, month, year, hh, mm, zone);
799 if (result == 6) return 0;
805 * Hiro, most UNIXen support this function:
806 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
808 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
810 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
822 memset(t, 0, sizeof *t);
824 if (procheader_scan_date_string(src, weekday, &day, month, &year,
825 &hh, &mm, &ss, zone) < 0) {
826 g_warning("Invalid date: %s\n", src);
830 /* Y2K compliant :) */
839 if ((p = strstr(monthstr, month)) != NULL)
840 dmonth = (gint)(p - monthstr) / 3 + 1;
842 g_warning("Invalid month: %s\n", month);
843 dmonth = G_DATE_BAD_MONTH;
850 t->tm_mon = dmonth - 1;
851 t->tm_year = year - 1900;
861 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
863 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
870 GDateMonth dmonth = G_DATE_BAD_MONTH;
876 if (procheader_scan_date_string(src, weekday, &day, month, &year,
877 &hh, &mm, &ss, zone) < 0) {
878 g_warning("Invalid date: %s\n", src);
880 strncpy2(dest, src, len);
884 /* Y2K compliant :) */
893 for (p = monthstr; *p != '\0'; p += 3) {
894 if (!strncasecmp(p, month, 3)) {
895 dmonth = (gint)(p - monthstr) / 3 + 1;
900 g_warning("Invalid month: %s\n", month);
906 t.tm_mon = dmonth - 1;
907 t.tm_year = year - 1900;
913 tz_offset = remote_tzoffset_sec(zone);
915 timer += tzoffset_sec(&timer) - tz_offset;
918 procheader_date_get_localtime(dest, len, timer);
923 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
926 gchar *default_format = "%y/%m/%d(%a) %H:%M";
928 lt = localtime(&timer);
930 if (prefs_common.date_format)
931 strftime(dest, len, prefs_common.date_format, lt);
933 strftime(dest, len, default_format, lt);
939 const gchar *src_codeset, *dest_codeset;
941 src_codeset = conv_get_current_charset_str();
942 dest_codeset = CS_UTF_8;
943 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
945 g_snprintf(dest, len, "%s", str);
946 strncpy2(dest, str, len);
953 /* Added by Mel Hadasht on 27 Aug 2001 */
954 /* Get a header from msginfo */
955 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
959 HeaderEntry hentry[]={ { header, NULL, TRUE },
960 { NULL, NULL, FALSE } };
963 g_return_val_if_fail(msginfo != NULL, -1);
964 file = procmsg_get_message_file_path(msginfo);
965 if ((fp = fopen(file, "rb")) == NULL) {
966 FILE_OP_ERROR(file, "fopen");
970 val = procheader_get_one_field(buf,len, fp, hentry);
971 if (fclose(fp) == EOF) {
972 FILE_OP_ERROR(file, "fclose");