2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2005 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 *, size_t, void *);
41 typedef int (*peekcharfunc) (void *);
42 typedef int (*getcharfunc) (void *);
43 typedef gint (*get_one_field_func) (gchar *, size_t, void *, HeaderEntry[]);
45 static gint string_get_one_field(gchar *buf, size_t len, char **str,
46 HeaderEntry hentry[]);
48 static char *string_getline(char *buf, size_t 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, size_t 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, size_t 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, size_t 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, size_t 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, size_t 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, size_t 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, size_t 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 ((len - (bufp - buf)) <= 2) break;
195 if (nexthead == '\n') {
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)
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 header->body = conv_unmime_header(p, NULL);
434 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
441 if (hentry == NULL) return;
443 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
447 p = buf + strlen(hp->name);
448 while (*p == ' ' || *p == '\t') p++;
450 if (hp->body == NULL)
451 hp->body = g_strdup(p);
452 else if (procheader_headername_equal(hp->name, "To") ||
453 procheader_headername_equal(hp->name, "Cc")) {
454 gchar *tp = hp->body;
455 hp->body = g_strconcat(tp, ", ", p, NULL);
461 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
462 gboolean full, gboolean decrypted)
468 if (stat(file, &s) < 0) {
469 FILE_OP_ERROR(file, "stat");
472 if (!S_ISREG(s.st_mode))
475 if ((fp = fopen(file, "rb")) == NULL) {
476 FILE_OP_ERROR(file, "fopen");
480 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
484 msginfo->size = s.st_size;
485 msginfo->mtime = s.st_mtime;
491 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
494 return parse_stream(&str, TRUE, flags, full, decrypted);
513 H_SC_PLANNED_DOWNLOAD = 14,
514 H_SC_MESSAGE_SIZE = 15,
516 H_DISPOSITION_NOTIFICATION_TO = 17,
517 H_RETURN_RECEIPT_TO = 18,
518 H_SC_PARTIALLY_RETRIEVED = 19,
519 H_SC_ACCOUNT_SERVER = 20,
520 H_SC_ACCOUNT_LOGIN = 21,
523 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
524 {"From:", NULL, TRUE},
527 {"Newsgroups:", NULL, TRUE},
528 {"Subject:", NULL, TRUE},
529 {"Message-ID:", NULL, FALSE},
530 {"References:", NULL, FALSE},
531 {"In-Reply-To:", NULL, FALSE},
532 {"Content-Type:", NULL, FALSE},
533 {"Seen:", NULL, FALSE},
534 {"Status:", NULL, FALSE},
535 {"X-Status:", NULL, FALSE},
536 {"From ", NULL, FALSE},
537 {"SC-Marked-For-Download:", NULL, FALSE},
538 {"SC-Message-Size:", NULL, FALSE},
539 {"X-Face:", NULL, FALSE},
540 {"Disposition-Notification-To:", NULL, FALSE},
541 {"Return-Receipt-To:", NULL, FALSE},
542 {"SC-Partially-Retrieved:", NULL, FALSE},
543 {"SC-Account-Server:", NULL, FALSE},
544 {"SC-Account-Login:",NULL, FALSE},
545 {NULL, NULL, FALSE}};
547 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
548 {"From:", NULL, TRUE},
551 {"Newsgroups:", NULL, TRUE},
552 {"Subject:", NULL, TRUE},
553 {"Message-ID:", NULL, FALSE},
554 {"References:", NULL, FALSE},
555 {"In-Reply-To:", NULL, FALSE},
556 {"Content-Type:", NULL, FALSE},
557 {"Seen:", NULL, FALSE},
558 {"Status:", NULL, FALSE},
559 {"X-Status:", NULL, FALSE},
560 {"From ", NULL, FALSE},
561 {"SC-Marked-For-Download:", NULL, FALSE},
562 {"SC-Message-Size:",NULL, FALSE},
563 {NULL, NULL, FALSE}};
565 HeaderEntry* procheader_get_headernames(gboolean full)
567 return full ? hentry_full : hentry_short;
570 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
573 return parse_stream(fp, FALSE, flags, full, decrypted);
576 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
577 gboolean full, gboolean decrypted)
585 get_one_field_func get_one_field =
586 isstring ? (get_one_field_func)string_get_one_field
587 : (get_one_field_func)procheader_get_one_field;
589 hentry = procheader_get_headernames(full);
591 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
592 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
596 msginfo = procmsg_msginfo_new();
598 if (flags.tmp_flags || flags.perm_flags)
599 msginfo->flags = flags;
601 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
603 msginfo->inreplyto = NULL;
605 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
607 hp = buf + strlen(hentry[hnum].name);
608 while (*hp == ' ' || *hp == '\t') hp++;
612 if (msginfo->date) break;
614 procheader_date_parse(NULL, hp, 0);
615 msginfo->date = g_strdup(hp);
618 if (msginfo->from) break;
619 msginfo->from = conv_unmime_header(hp, NULL);
620 msginfo->fromname = procheader_get_fromname(msginfo->from);
623 tmp = conv_unmime_header(hp, NULL);
627 g_strconcat(p, ", ", tmp, NULL);
630 msginfo->to = g_strdup(tmp);
634 tmp = conv_unmime_header(hp, NULL);
638 g_strconcat(p, ", ", tmp, NULL);
641 msginfo->cc = g_strdup(tmp);
645 if (msginfo->newsgroups) {
646 p = msginfo->newsgroups;
647 msginfo->newsgroups =
648 g_strconcat(p, ",", hp, NULL);
651 msginfo->newsgroups = g_strdup(hp);
654 if (msginfo->subject) break;
655 msginfo->subject = conv_unmime_header(hp, NULL);
658 if (msginfo->msgid) break;
660 extract_parenthesis(hp, '<', '>');
662 msginfo->msgid = g_strdup(hp);
665 msginfo->references =
666 references_list_prepend(msginfo->references,
670 if (msginfo->inreplyto) break;
672 eliminate_parenthesis(hp, '(', ')');
673 if ((p = strrchr(hp, '<')) != NULL &&
674 strchr(p + 1, '>') != NULL) {
675 extract_parenthesis(p, '<', '>');
678 msginfo->inreplyto = g_strdup(p);
682 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
683 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
685 #ifdef ALLOW_HEADER_HINT
687 /* mnews Seen header */
688 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
692 if (msginfo->xface) break;
693 msginfo->xface = g_strdup(hp);
695 case H_DISPOSITION_NOTIFICATION_TO:
696 if (msginfo->dispositionnotificationto) break;
697 msginfo->dispositionnotificationto = g_strdup(hp);
699 case H_RETURN_RECEIPT_TO:
700 if (msginfo->returnreceiptto) break;
701 msginfo->returnreceiptto = g_strdup(hp);
703 case H_SC_PARTIALLY_RETRIEVED:
704 if (msginfo->partial_recv) break;
705 msginfo->partial_recv = g_strdup(hp);
707 case H_SC_ACCOUNT_SERVER:
708 if (msginfo->account_server) break;
709 msginfo->account_server = g_strdup(hp);
711 case H_SC_ACCOUNT_LOGIN:
712 if (msginfo->account_login) break;
713 msginfo->account_login = g_strdup(hp);
715 case H_SC_MESSAGE_SIZE:
716 if (msginfo->total_size) break;
717 msginfo->total_size = atoi(hp);
719 case H_SC_PLANNED_DOWNLOAD:
720 msginfo->planned_download = atoi(hp);
722 #ifdef ALLOW_HEADER_HINT
724 if (strchr(hp, 'R') != NULL)
725 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
726 if (strchr(hp, 'O') != NULL)
727 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
728 if (strchr(hp, 'U') != NULL)
729 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
732 if (strchr(hp, 'D') != NULL)
733 MSG_SET_PERM_FLAGS(msginfo->flags,
735 if (strchr(hp, 'F') != NULL)
736 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
737 if (strchr(hp, 'd') != NULL)
738 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
739 if (strchr(hp, 'r') != NULL)
740 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
741 if (strchr(hp, 'f') != NULL)
742 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
746 if (msginfo->fromspace) break;
747 msginfo->fromspace = g_strdup(hp);
754 if (!msginfo->inreplyto && msginfo->references)
756 g_strdup((gchar *)msginfo->references->data);
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;
816 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
817 day, month, year, hh, mm, ss);
818 if (result == 6) return 0;
821 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
822 weekday, day, month, year, hh, mm, zone);
823 if (result == 7) return 0;
825 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
826 day, month, year, hh, mm, zone);
827 if (result == 6) return 0;
830 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
831 weekday, day, month, year, hh, mm);
832 if (result == 6) return 0;
834 result = sscanf(str, "%d %9s %d %2d:%2d",
835 day, month, year, hh, mm);
836 if (result == 5) return 0;
842 * Hiro, most UNIXen support this function:
843 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
845 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
847 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
859 memset(t, 0, sizeof *t);
861 if (procheader_scan_date_string(src, weekday, &day, month, &year,
862 &hh, &mm, &ss, zone) < 0) {
863 g_warning("Invalid date: %s\n", src);
867 /* Y2K compliant :) */
876 if ((p = strstr(monthstr, month)) != NULL)
877 dmonth = (gint)(p - monthstr) / 3 + 1;
879 g_warning("Invalid month: %s\n", month);
880 dmonth = G_DATE_BAD_MONTH;
887 t->tm_mon = dmonth - 1;
888 t->tm_year = year - 1900;
898 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
900 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
907 GDateMonth dmonth = G_DATE_BAD_MONTH;
913 if (procheader_scan_date_string(src, weekday, &day, month, &year,
914 &hh, &mm, &ss, zone) < 0) {
915 g_warning("Invalid date: %s\n", src);
917 strncpy2(dest, src, len);
921 /* Y2K compliant :) */
930 for (p = monthstr; *p != '\0'; p += 3) {
931 if (!g_ascii_strncasecmp(p, month, 3)) {
932 dmonth = (gint)(p - monthstr) / 3 + 1;
937 g_warning("Invalid month: %s\n", month);
943 t.tm_mon = dmonth - 1;
944 t.tm_year = year - 1900;
950 tz_offset = remote_tzoffset_sec(zone);
952 timer += tzoffset_sec(&timer) - tz_offset;
955 procheader_date_get_localtime(dest, len, timer);
960 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
963 gchar *default_format = "%y/%m/%d(%a) %H:%M";
965 const gchar *src_codeset, *dest_codeset;
967 lt = localtime(&timer);
969 if (prefs_common.date_format)
970 strftime(dest, len, prefs_common.date_format, lt);
972 strftime(dest, len, default_format, lt);
974 src_codeset = conv_get_locale_charset_str();
975 dest_codeset = CS_UTF_8;
976 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
978 g_snprintf(dest, len, "%s", str);
979 strncpy2(dest, str, len);
984 /* Added by Mel Hadasht on 27 Aug 2001 */
985 /* Get a header from msginfo */
986 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
990 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
991 { NULL, NULL, FALSE } };
994 hentry[0].name = header;
996 g_return_val_if_fail(msginfo != NULL, -1);
997 file = procmsg_get_message_file_path(msginfo);
998 if ((fp = fopen(file, "rb")) == NULL) {
999 FILE_OP_ERROR(file, "fopen");
1003 val = procheader_get_one_field(buf,len, fp, hentry);
1004 if (fclose(fp) == EOF) {
1005 FILE_OP_ERROR(file, "fclose");