2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 = g_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 = g_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,
517 H_DISPOSITION_NOTIFICATION_TO = 18,
518 H_RETURN_RECEIPT_TO = 19,
519 H_SC_PARTIALLY_RETRIEVED = 20,
520 H_SC_ACCOUNT_SERVER = 21,
521 H_SC_ACCOUNT_LOGIN = 22,
524 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
525 {"From:", NULL, TRUE},
528 {"Newsgroups:", NULL, TRUE},
529 {"Subject:", NULL, TRUE},
530 {"Message-ID:", NULL, FALSE},
531 {"References:", NULL, FALSE},
532 {"In-Reply-To:", NULL, FALSE},
533 {"Content-Type:", NULL, FALSE},
534 {"Seen:", NULL, FALSE},
535 {"Status:", NULL, FALSE},
536 {"X-Status:", NULL, FALSE},
537 {"From ", NULL, FALSE},
538 {"SC-Marked-For-Download:", NULL, FALSE},
539 {"SC-Message-Size:", NULL, FALSE},
540 {"Face:", NULL, FALSE},
541 {"X-Face:", NULL, FALSE},
542 {"Disposition-Notification-To:", NULL, FALSE},
543 {"Return-Receipt-To:", NULL, FALSE},
544 {"SC-Partially-Retrieved:", NULL, FALSE},
545 {"SC-Account-Server:", NULL, FALSE},
546 {"SC-Account-Login:",NULL, FALSE},
547 {NULL, NULL, FALSE}};
549 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
550 {"From:", NULL, TRUE},
553 {"Newsgroups:", NULL, TRUE},
554 {"Subject:", NULL, TRUE},
555 {"Message-ID:", NULL, FALSE},
556 {"References:", NULL, FALSE},
557 {"In-Reply-To:", NULL, FALSE},
558 {"Content-Type:", NULL, FALSE},
559 {"Seen:", NULL, FALSE},
560 {"Status:", NULL, FALSE},
561 {"X-Status:", NULL, FALSE},
562 {"From ", NULL, FALSE},
563 {"SC-Marked-For-Download:", NULL, FALSE},
564 {"SC-Message-Size:",NULL, FALSE},
565 {NULL, NULL, FALSE}};
567 HeaderEntry* procheader_get_headernames(gboolean full)
569 return full ? hentry_full : hentry_short;
572 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
575 return parse_stream(fp, FALSE, flags, full, decrypted);
578 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
579 gboolean full, gboolean decrypted)
587 get_one_field_func get_one_field =
588 isstring ? (get_one_field_func)string_get_one_field
589 : (get_one_field_func)procheader_get_one_field;
591 hentry = procheader_get_headernames(full);
593 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
594 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
598 msginfo = procmsg_msginfo_new();
600 if (flags.tmp_flags || flags.perm_flags)
601 msginfo->flags = flags;
603 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
605 msginfo->inreplyto = NULL;
607 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
609 hp = buf + strlen(hentry[hnum].name);
610 while (*hp == ' ' || *hp == '\t') hp++;
614 if (msginfo->date) break;
616 procheader_date_parse(NULL, hp, 0);
617 msginfo->date = g_strdup(hp);
620 if (msginfo->from) break;
621 msginfo->from = conv_unmime_header(hp, NULL);
622 msginfo->fromname = procheader_get_fromname(msginfo->from);
623 replace_returns(msginfo->from);
624 replace_returns(msginfo->fromname);
627 tmp = conv_unmime_header(hp, NULL);
631 g_strconcat(p, ", ", tmp, NULL);
634 msginfo->to = g_strdup(tmp);
638 tmp = conv_unmime_header(hp, NULL);
642 g_strconcat(p, ", ", tmp, NULL);
645 msginfo->cc = g_strdup(tmp);
649 if (msginfo->newsgroups) {
650 p = msginfo->newsgroups;
651 msginfo->newsgroups =
652 g_strconcat(p, ",", hp, NULL);
655 msginfo->newsgroups = g_strdup(hp);
658 if (msginfo->subject) break;
659 msginfo->subject = conv_unmime_header(hp, NULL);
660 replace_returns(msginfo->subject);
663 if (msginfo->msgid) break;
665 extract_parenthesis(hp, '<', '>');
667 msginfo->msgid = g_strdup(hp);
670 msginfo->references =
671 references_list_prepend(msginfo->references,
675 if (msginfo->inreplyto) break;
677 eliminate_parenthesis(hp, '(', ')');
678 if ((p = strrchr(hp, '<')) != NULL &&
679 strchr(p + 1, '>') != NULL) {
680 extract_parenthesis(p, '<', '>');
683 msginfo->inreplyto = g_strdup(p);
687 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
688 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
690 #ifdef ALLOW_HEADER_HINT
692 /* mnews Seen header */
693 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
697 if (msginfo->face) break;
698 msginfo->face = g_strdup(hp);
701 if (msginfo->xface) break;
702 msginfo->xface = g_strdup(hp);
704 case H_DISPOSITION_NOTIFICATION_TO:
705 if (msginfo->dispositionnotificationto) break;
706 msginfo->dispositionnotificationto = g_strdup(hp);
708 case H_RETURN_RECEIPT_TO:
709 if (msginfo->returnreceiptto) break;
710 msginfo->returnreceiptto = g_strdup(hp);
712 case H_SC_PARTIALLY_RETRIEVED:
713 if (msginfo->partial_recv) break;
714 msginfo->partial_recv = g_strdup(hp);
716 case H_SC_ACCOUNT_SERVER:
717 if (msginfo->account_server) break;
718 msginfo->account_server = g_strdup(hp);
720 case H_SC_ACCOUNT_LOGIN:
721 if (msginfo->account_login) break;
722 msginfo->account_login = g_strdup(hp);
724 case H_SC_MESSAGE_SIZE:
725 if (msginfo->total_size) break;
726 msginfo->total_size = atoi(hp);
728 case H_SC_PLANNED_DOWNLOAD:
729 msginfo->planned_download = atoi(hp);
731 #ifdef ALLOW_HEADER_HINT
733 if (strchr(hp, 'R') != NULL)
734 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
735 if (strchr(hp, 'O') != NULL)
736 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
737 if (strchr(hp, 'U') != NULL)
738 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
741 if (strchr(hp, 'D') != NULL)
742 MSG_SET_PERM_FLAGS(msginfo->flags,
744 if (strchr(hp, 'F') != NULL)
745 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
746 if (strchr(hp, 'd') != NULL)
747 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
748 if (strchr(hp, 'r') != NULL)
749 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
750 if (strchr(hp, 'f') != NULL)
751 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
755 if (msginfo->fromspace) break;
756 msginfo->fromspace = g_strdup(hp);
763 if (!msginfo->inreplyto && msginfo->references)
765 g_strdup((gchar *)msginfo->references->data);
770 gchar *procheader_get_fromname(const gchar *str)
774 Xstrdup_a(tmp, str, return NULL);
777 extract_quote(tmp, '\"');
779 } else if (strchr(tmp, '<')) {
780 eliminate_parenthesis(tmp, '<', '>');
784 extract_parenthesis(tmp, '<', '>');
787 } else if (strchr(tmp, '(')) {
788 extract_parenthesis(tmp, '(', ')');
793 name = g_strdup(str);
795 name = g_strdup(tmp);
800 static gint procheader_scan_date_string(const gchar *str,
801 gchar *weekday, gint *day,
802 gchar *month, gint *year,
803 gint *hh, gint *mm, gint *ss,
808 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
809 weekday, day, month, year, hh, mm, ss, zone);
810 if (result == 8) return 0;
812 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
813 weekday, day, month, year, hh, mm, ss, zone);
814 if (result == 8) return 0;
816 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
817 day, month, year, hh, mm, ss, zone);
818 if (result == 7) return 0;
821 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
822 weekday, day, month, year, hh, mm, ss);
823 if (result == 7) return 0;
825 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
826 day, month, year, hh, mm, ss);
827 if (result == 6) return 0;
830 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
831 weekday, day, month, year, hh, mm, zone);
832 if (result == 7) return 0;
834 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
835 day, month, year, hh, mm, zone);
836 if (result == 6) return 0;
839 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
840 weekday, day, month, year, hh, mm);
841 if (result == 6) return 0;
843 result = sscanf(str, "%d %9s %d %2d:%2d",
844 day, month, year, hh, mm);
845 if (result == 5) return 0;
851 * Hiro, most UNIXen support this function:
852 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
854 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
856 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
868 memset(t, 0, sizeof *t);
870 if (procheader_scan_date_string(src, weekday, &day, month, &year,
871 &hh, &mm, &ss, zone) < 0) {
872 g_warning("Invalid date: %s\n", src);
876 /* Y2K compliant :) */
885 if ((p = strstr(monthstr, month)) != NULL)
886 dmonth = (gint)(p - monthstr) / 3 + 1;
888 g_warning("Invalid month: %s\n", month);
889 dmonth = G_DATE_BAD_MONTH;
896 t->tm_mon = dmonth - 1;
897 t->tm_year = year - 1900;
907 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
909 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
916 GDateMonth dmonth = G_DATE_BAD_MONTH;
922 if (procheader_scan_date_string(src, weekday, &day, month, &year,
923 &hh, &mm, &ss, zone) < 0) {
925 strncpy2(dest, src, len);
929 /* Y2K compliant :) */
938 for (p = monthstr; *p != '\0'; p += 3) {
939 if (!g_ascii_strncasecmp(p, month, 3)) {
940 dmonth = (gint)(p - monthstr) / 3 + 1;
949 t.tm_mon = dmonth - 1;
950 t.tm_year = year - 1900;
956 tz_offset = remote_tzoffset_sec(zone);
958 timer += tzoffset_sec(&timer) - tz_offset;
961 procheader_date_get_localtime(dest, len, timer);
966 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
969 gchar *default_format = "%y/%m/%d(%a) %H:%M";
971 const gchar *src_codeset, *dest_codeset;
973 lt = localtime(&timer);
975 if (prefs_common.date_format)
976 strftime(dest, len, prefs_common.date_format, lt);
978 strftime(dest, len, default_format, lt);
980 src_codeset = conv_get_locale_charset_str();
981 dest_codeset = CS_UTF_8;
982 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
984 g_snprintf(dest, len, "%s", str);
985 strncpy2(dest, str, len);
990 /* Added by Mel Hadasht on 27 Aug 2001 */
991 /* Get a header from msginfo */
992 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
996 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
997 { NULL, NULL, FALSE } };
1000 hentry[0].name = header;
1002 g_return_val_if_fail(msginfo != NULL, -1);
1003 file = procmsg_get_message_file_path(msginfo);
1004 if ((fp = g_fopen(file, "rb")) == NULL) {
1005 FILE_OP_ERROR(file, "fopen");
1009 val = procheader_get_one_field(buf,len, fp, hentry);
1010 if (fclose(fp) == EOF) {
1011 FILE_OP_ERROR(file, "fclose");