2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 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.
25 #include <glib/gi18n.h>
32 #include "procheader.h"
35 #include "prefs_common.h"
40 typedef char *(*getlinefunc)(char *, int, void *);
41 typedef int (*peekcharfunc)(void *);
42 typedef int (*getcharfunc)(void *);
43 typedef gint (*get_one_field_func)(gchar *, gint, void *, HeaderEntry[]);
45 static gint string_get_one_field(gchar *buf, gint len, char **str,
46 HeaderEntry hentry[]);
48 static char *string_getline(char *buf, int len, char **str);
49 static int string_peekchar(char **str);
50 static int file_peekchar(FILE *fp);
51 static gint generic_get_one_field(gchar *buf, gint len, void *data,
54 peekcharfunc peekchar,
56 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
57 gboolean full, gboolean decrypted);
60 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
63 return generic_get_one_field(buf, len, fp, hentry,
64 (getlinefunc)fgets, (peekcharfunc)file_peekchar,
68 static gint string_get_one_field(gchar *buf, gint len, char **str,
71 return generic_get_one_field(buf, len, str, hentry,
72 (getlinefunc)string_getline,
73 (peekcharfunc)string_peekchar,
77 static char *string_getline(char *buf, int len, char **str)
82 for (; **str && len > 1; --len)
83 if ((*buf++ = *(*str)++) == '\n')
90 static int string_peekchar(char **str)
95 static int file_peekchar(FILE *fp)
97 return ungetc(getc(fp), fp);
100 static gint generic_get_one_field(gchar *buf, gint len, void *data,
102 getlinefunc getline, peekcharfunc peekchar,
107 HeaderEntry *hp = NULL;
109 if (hentry != NULL) {
110 /* skip non-required headers */
113 if (getline(buf, len, data) == NULL)
115 if (buf[0] == '\r' || buf[0] == '\n')
117 } while (buf[0] == ' ' || buf[0] == '\t');
119 for (hp = hentry, hnum = 0; hp->name != NULL;
121 if (!g_ascii_strncasecmp(hp->name, buf,
125 } while (hp->name == NULL);
127 if (getline(buf, len, data) == NULL) return -1;
128 if (buf[0] == '\r' || buf[0] == '\n') return -1;
133 nexthead = peekchar(data);
134 /* ([*WSP CRLF] 1*WSP) */
135 if (nexthead == ' ' || nexthead == '\t') {
137 /* trim previous trailing \n if requesting one header or
138 * unfolding was requested */
139 if ((!hentry && unfold) || (hp && hp->unfold))
142 buflen = strlen(buf);
144 /* concatenate next line */
145 if ((len - buflen) > 2) {
146 if (getline(buf + buflen, len - buflen, data) == NULL)
151 /* remove trailing new line */
160 gint procheader_get_one_field_asis(gchar *buf, gint len, FILE *fp)
162 return generic_get_one_field(buf, len, fp, NULL,
164 (peekcharfunc)file_peekchar,
169 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
171 gboolean folded = FALSE;
175 if (fgets(buf, len, fp) == NULL) return NULL;
176 if (buf[0] == '\r' || buf[0] == '\n') return NULL;
177 bufp = buf + strlen(buf);
180 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
185 nexthead = fgetc(fp);
188 if (nexthead == ' ' || nexthead == '\t')
190 else if (nexthead == EOF)
192 else if (folded == TRUE) {
193 if (nexthead == '\r' || nexthead == '\n') {
198 if ((len - (bufp - buf)) <= 2) break;
200 /* replace return code on the tail end
206 /* concatenate next line */
207 if (fgets(bufp, len - (bufp - buf), fp)
209 bufp += strlen(bufp);
212 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
218 ungetc(nexthead, fp);
223 /* remove trailing return code */
231 GSList *procheader_get_header_list_from_file(const gchar *file)
236 if ((fp = fopen(file, "rb")) == NULL) {
237 FILE_OP_ERROR(file, "fopen");
241 hlist = procheader_get_header_list(fp);
247 GSList *procheader_get_header_list(FILE *fp)
250 GSList *hlist = NULL;
253 g_return_val_if_fail(fp != NULL, NULL);
255 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
256 if ((header = procheader_parse_header(buf)) != NULL)
257 hlist = g_slist_append(hlist, header);
259 if (*buf == ':') continue;
260 for (p = buf; *p && *p != ' '; p++) {
262 header = g_new(Header, 1);
263 header->name = g_strndup(buf, p - buf);
265 while (*p == ' ' || *p == '\t') p++;
266 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
267 header->body = g_strdup(tmp);
269 hlist = g_slist_append(hlist, header);
281 GPtrArray *procheader_get_header_array(FILE *fp)
287 g_return_val_if_fail(fp != NULL, NULL);
289 headers = g_ptr_array_new();
291 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
292 if ((header = procheader_parse_header(buf)) != NULL)
293 g_ptr_array_add(headers, header);
295 if (*buf == ':') continue;
296 for (p = buf; *p && *p != ' '; p++) {
298 header = g_new(Header, 1);
299 header->name = g_strndup(buf, p - buf);
301 while (*p == ' ' || *p == '\t') p++;
302 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
303 header->body = g_strdup(tmp);
305 g_ptr_array_add(headers, header);
316 GPtrArray *procheader_get_header_array_asis(FILE *fp)
322 g_return_val_if_fail(fp != NULL, NULL);
324 headers = g_ptr_array_new();
326 while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
327 if ((header = procheader_parse_header(buf)) != NULL)
328 g_ptr_array_add(headers, header);
330 if (*buf == ':') continue;
331 for (p = buf; *p && *p != ' '; p++) {
333 header = g_new(Header, 1);
334 header->name = g_strndup(buf, p - buf);
336 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
337 header->body = g_strdup(tmp);
339 g_ptr_array_add(headers, header);
350 void procheader_header_list_destroy(GSList *hlist)
354 while (hlist != NULL) {
355 header = hlist->data;
356 procheader_header_free(header);
357 hlist = g_slist_remove(hlist, header);
362 void procheader_header_array_destroy(GPtrArray *harray)
367 for (i = 0; i < harray->len; i++) {
368 header = g_ptr_array_index(harray, i);
369 procheader_header_free(header);
372 g_ptr_array_free(harray, TRUE);
375 void procheader_header_free(Header *header)
379 g_free(header->name);
380 g_free(header->body);
385 tests whether two headers' names are equal
386 remove the trailing ':' or ' ' before comparing
389 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
396 if (hdr1[len1 - 1] == ':')
398 if (hdr2[len2 - 1] == ':')
403 return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
407 parse headers, for example :
408 From: dinh@enseirb.fr becomes :
409 header->name = "From:"
410 header->body = "dinh@enseirb.fr"
413 Header * procheader_parse_header(gchar * buf)
419 if ((*buf == ':') || (*buf == ' '))
422 for (p = buf; *p ; p++) {
423 if ((*p == ':') || (*p == ' ')) {
424 header = g_new(Header, 1);
425 header->name = g_strndup(buf, p - buf + 1);
427 while (*p == ' ' || *p == '\t') p++;
428 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
430 header->body = g_strdup(p);
432 header->body = g_strdup(tmp);
439 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
446 if (hentry == NULL) return;
448 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
452 p = buf + strlen(hp->name);
453 while (*p == ' ' || *p == '\t') p++;
455 if (hp->body == NULL)
456 hp->body = g_strdup(p);
457 else if (procheader_headername_equal(hp->name, "To") ||
458 procheader_headername_equal(hp->name, "Cc")) {
459 gchar *tp = hp->body;
460 hp->body = g_strconcat(tp, ", ", p, NULL);
466 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
467 gboolean full, gboolean decrypted)
473 if (stat(file, &s) < 0) {
474 FILE_OP_ERROR(file, "stat");
477 if (!S_ISREG(s.st_mode))
480 if ((fp = fopen(file, "rb")) == NULL) {
481 FILE_OP_ERROR(file, "fopen");
485 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
489 msginfo->size = s.st_size;
490 msginfo->mtime = s.st_mtime;
496 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
499 return parse_stream(&str, TRUE, flags, full, decrypted);
518 H_SC_PLANNED_DOWNLOAD = 14,
519 H_SC_MESSAGE_SIZE = 15,
521 H_DISPOSITION_NOTIFICATION_TO = 17,
522 H_RETURN_RECEIPT_TO = 18,
523 H_SC_PARTIALLY_RETRIEVED = 19,
524 H_SC_ACCOUNT_SERVER = 20,
525 H_SC_ACCOUNT_LOGIN = 21,
528 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
529 {"From:", NULL, TRUE},
532 {"Newsgroups:", NULL, TRUE},
533 {"Subject:", NULL, TRUE},
534 {"Message-ID:", NULL, FALSE},
535 {"References:", NULL, FALSE},
536 {"In-Reply-To:", NULL, FALSE},
537 {"Content-Type:", NULL, FALSE},
538 {"Seen:", NULL, FALSE},
539 {"Status:", NULL, FALSE},
540 {"X-Status:", NULL, FALSE},
541 {"From ", NULL, FALSE},
542 {"SC-Marked-For-Download:", NULL, FALSE},
543 {"SC-Message-Size:", NULL, FALSE},
544 {"X-Face:", NULL, FALSE},
545 {"Disposition-Notification-To:", NULL, FALSE},
546 {"Return-Receipt-To:", NULL, FALSE},
547 {"SC-Partially-Retrieved:", NULL, FALSE},
548 {"SC-Account-Server:", NULL, FALSE},
549 {"SC-Account-Login:",NULL, FALSE},
550 {NULL, NULL, FALSE}};
552 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
553 {"From:", NULL, TRUE},
556 {"Newsgroups:", NULL, TRUE},
557 {"Subject:", NULL, TRUE},
558 {"Message-ID:", NULL, FALSE},
559 {"References:", NULL, FALSE},
560 {"In-Reply-To:", NULL, FALSE},
561 {"Content-Type:", NULL, FALSE},
562 {"Seen:", NULL, FALSE},
563 {"Status:", NULL, FALSE},
564 {"X-Status:", NULL, FALSE},
565 {"From ", NULL, FALSE},
566 {"SC-Marked-For-Download:", NULL, FALSE},
567 {"SC-Message-Size:",NULL, FALSE},
568 {NULL, NULL, FALSE}};
570 HeaderEntry* procheader_get_headernames(gboolean full)
572 return full ? hentry_full : hentry_short;
575 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
578 return parse_stream(fp, FALSE, flags, full, decrypted);
581 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
582 gboolean full, gboolean decrypted)
585 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
586 gchar *reference = NULL;
591 get_one_field_func get_one_field =
592 isstring ? (get_one_field_func)string_get_one_field
593 : (get_one_field_func)procheader_get_one_field;
595 hentry = procheader_get_headernames(full);
597 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
598 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
602 msginfo = procmsg_msginfo_new();
604 if (flags.tmp_flags || flags.perm_flags)
605 msginfo->flags = flags;
607 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
609 msginfo->inreplyto = NULL;
611 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
613 hp = buf + strlen(hentry[hnum].name);
614 while (*hp == ' ' || *hp == '\t') hp++;
618 if (msginfo->date) break;
620 procheader_date_parse(NULL, hp, 0);
621 msginfo->date = g_strdup(hp);
624 if (msginfo->from) break;
625 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
626 msginfo->from = g_strdup(tmp);
627 msginfo->fromname = procheader_get_fromname(tmp);
630 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
634 g_strconcat(p, ", ", tmp, NULL);
637 msginfo->to = g_strdup(tmp);
640 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
644 g_strconcat(p, ", ", tmp, NULL);
647 msginfo->cc = g_strdup(tmp);
650 if (msginfo->newsgroups) {
651 p = msginfo->newsgroups;
652 msginfo->newsgroups =
653 g_strconcat(p, ",", hp, NULL);
656 msginfo->newsgroups = g_strdup(hp);
659 if (msginfo->subject) break;
660 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
661 msginfo->subject = g_strdup(tmp);
664 if (msginfo->msgid) break;
666 extract_parenthesis(hp, '<', '>');
668 msginfo->msgid = g_strdup(hp);
673 msginfo->references = g_strdup(hp);
674 eliminate_parenthesis(hp, '(', ')');
675 if ((p = strrchr(hp, '<')) != NULL &&
676 strchr(p + 1, '>') != NULL) {
677 extract_parenthesis(p, '<', '>');
680 reference = g_strdup(p);
685 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
686 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
688 #ifdef ALLOW_HEADER_HINT
690 /* mnews Seen header */
691 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
695 if (msginfo->xface) break;
696 msginfo->xface = g_strdup(hp);
698 case H_DISPOSITION_NOTIFICATION_TO:
699 if (msginfo->dispositionnotificationto) break;
700 msginfo->dispositionnotificationto = g_strdup(hp);
702 case H_RETURN_RECEIPT_TO:
703 if (msginfo->returnreceiptto) break;
704 msginfo->returnreceiptto = g_strdup(hp);
706 case H_SC_PARTIALLY_RETRIEVED:
707 if (msginfo->partial_recv) break;
708 msginfo->partial_recv = g_strdup(hp);
710 case H_SC_ACCOUNT_SERVER:
711 if (msginfo->account_server) break;
712 msginfo->account_server = g_strdup(hp);
714 case H_SC_ACCOUNT_LOGIN:
715 if (msginfo->account_login) break;
716 msginfo->account_login = g_strdup(hp);
718 case H_SC_MESSAGE_SIZE:
719 if (msginfo->total_size) break;
720 msginfo->total_size = atoi(hp);
722 case H_SC_PLANNED_DOWNLOAD:
723 msginfo->planned_download = atoi(hp);
725 #ifdef ALLOW_HEADER_HINT
727 if (strchr(hp, 'R') != NULL)
728 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
729 if (strchr(hp, 'O') != NULL)
730 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
731 if (strchr(hp, 'U') != NULL)
732 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
735 if (strchr(hp, 'D') != NULL)
736 MSG_SET_PERM_FLAGS(msginfo->flags,
738 if (strchr(hp, 'F') != NULL)
739 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
740 if (strchr(hp, 'd') != NULL)
741 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
742 if (strchr(hp, 'r') != NULL)
743 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
744 if (strchr(hp, 'f') != NULL)
745 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
749 if (msginfo->fromspace) break;
750 msginfo->fromspace = g_strdup(hp);
756 msginfo->inreplyto = reference;
761 gchar *procheader_get_fromname(const gchar *str)
765 Xstrdup_a(tmp, str, return NULL);
768 extract_quote(tmp, '\"');
770 } else if (strchr(tmp, '<')) {
771 eliminate_parenthesis(tmp, '<', '>');
775 extract_parenthesis(tmp, '<', '>');
778 } else if (strchr(tmp, '(')) {
779 extract_parenthesis(tmp, '(', ')');
784 name = g_strdup(str);
786 name = g_strdup(tmp);
791 static gint procheader_scan_date_string(const gchar *str,
792 gchar *weekday, gint *day,
793 gchar *month, gint *year,
794 gint *hh, gint *mm, gint *ss,
799 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
800 weekday, day, month, year, hh, mm, ss, zone);
801 if (result == 8) return 0;
803 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
804 weekday, day, month, year, hh, mm, ss, zone);
805 if (result == 8) return 0;
807 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
808 day, month, year, hh, mm, ss, zone);
809 if (result == 7) return 0;
812 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
813 weekday, day, month, year, hh, mm, ss);
814 if (result == 7) return 0;
817 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
818 weekday, day, month, year, hh, mm, zone);
819 if (result == 7) return 0;
821 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
822 day, month, year, hh, mm, zone);
823 if (result == 6) return 0;
829 * Hiro, most UNIXen support this function:
830 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
832 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
834 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
846 memset(t, 0, sizeof *t);
848 if (procheader_scan_date_string(src, weekday, &day, month, &year,
849 &hh, &mm, &ss, zone) < 0) {
850 g_warning("Invalid date: %s\n", src);
854 /* Y2K compliant :) */
863 if ((p = strstr(monthstr, month)) != NULL)
864 dmonth = (gint)(p - monthstr) / 3 + 1;
866 g_warning("Invalid month: %s\n", month);
867 dmonth = G_DATE_BAD_MONTH;
874 t->tm_mon = dmonth - 1;
875 t->tm_year = year - 1900;
885 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
887 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
894 GDateMonth dmonth = G_DATE_BAD_MONTH;
900 if (procheader_scan_date_string(src, weekday, &day, month, &year,
901 &hh, &mm, &ss, zone) < 0) {
902 g_warning("Invalid date: %s\n", src);
904 strncpy2(dest, src, len);
908 /* Y2K compliant :) */
917 for (p = monthstr; *p != '\0'; p += 3) {
918 if (!g_ascii_strncasecmp(p, month, 3)) {
919 dmonth = (gint)(p - monthstr) / 3 + 1;
924 g_warning("Invalid month: %s\n", month);
930 t.tm_mon = dmonth - 1;
931 t.tm_year = year - 1900;
937 tz_offset = remote_tzoffset_sec(zone);
939 timer += tzoffset_sec(&timer) - tz_offset;
942 procheader_date_get_localtime(dest, len, timer);
947 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
950 gchar *default_format = "%y/%m/%d(%a) %H:%M";
952 const gchar *src_codeset, *dest_codeset;
954 lt = localtime(&timer);
956 if (prefs_common.date_format)
957 strftime(dest, len, prefs_common.date_format, lt);
959 strftime(dest, len, default_format, lt);
961 src_codeset = conv_get_locale_charset_str();
962 dest_codeset = CS_UTF_8;
963 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
965 g_snprintf(dest, len, "%s", str);
966 strncpy2(dest, str, len);
971 /* Added by Mel Hadasht on 27 Aug 2001 */
972 /* Get a header from msginfo */
973 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
977 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
978 { NULL, NULL, FALSE } };
981 hentry[0].name = header;
983 g_return_val_if_fail(msginfo != NULL, -1);
984 file = procmsg_get_message_file_path(msginfo);
985 if ((fp = fopen(file, "rb")) == NULL) {
986 FILE_OP_ERROR(file, "fopen");
990 val = procheader_get_one_field(buf,len, fp, hentry);
991 if (fclose(fp) == EOF) {
992 FILE_OP_ERROR(file, "fclose");