2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail 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 gboolean skiptab = (nexthead == '\t');
138 /* trim previous trailing \n if requesting one header or
139 * unfolding was requested */
140 if ((!hentry && unfold) || (hp && hp->unfold))
143 buflen = strlen(buf);
145 /* concatenate next line */
146 if ((len - buflen) > 2) {
147 if (getline(buf + buflen, len - buflen, data) == NULL)
149 if (skiptab) { /* replace tab with space */
150 *(buf + buflen) = ' ';
155 /* remove trailing new line */
164 gint procheader_get_one_field_asis(gchar *buf, size_t len, FILE *fp)
166 return generic_get_one_field(buf, len, fp, NULL,
168 (peekcharfunc)file_peekchar,
172 GPtrArray *procheader_get_header_array_asis(FILE *fp)
178 g_return_val_if_fail(fp != NULL, NULL);
180 headers = g_ptr_array_new();
182 while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
183 if ((header = procheader_parse_header(buf)) != NULL)
184 g_ptr_array_add(headers, header);
186 if (*buf == ':') continue;
187 for (p = buf; *p && *p != ' '; p++) {
189 header = g_new(Header, 1);
190 header->name = g_strndup(buf, p - buf);
192 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
193 header->body = g_strdup(tmp);
195 g_ptr_array_add(headers, header);
205 void procheader_header_array_destroy(GPtrArray *harray)
210 for (i = 0; i < harray->len; i++) {
211 header = g_ptr_array_index(harray, i);
212 procheader_header_free(header);
215 g_ptr_array_free(harray, TRUE);
218 void procheader_header_free(Header *header)
222 g_free(header->name);
223 g_free(header->body);
228 tests whether two headers' names are equal
229 remove the trailing ':' or ' ' before comparing
232 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
239 if (hdr1[len1 - 1] == ':')
241 if (hdr2[len2 - 1] == ':')
246 return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
250 parse headers, for example :
251 From: dinh@enseirb.fr becomes :
252 header->name = "From:"
253 header->body = "dinh@enseirb.fr"
256 Header * procheader_parse_header(gchar * buf)
261 if ((*buf == ':') || (*buf == ' '))
264 for (p = buf; *p ; p++) {
265 if ((*p == ':') || (*p == ' ')) {
266 header = g_new(Header, 1);
267 header->name = g_strndup(buf, p - buf + 1);
269 while (*p == ' ' || *p == '\t') p++;
270 header->body = conv_unmime_header(p, NULL);
277 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
284 if (hentry == NULL) return;
286 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
290 p = buf + strlen(hp->name);
291 while (*p == ' ' || *p == '\t') p++;
293 if (hp->body == NULL)
294 hp->body = g_strdup(p);
295 else if (procheader_headername_equal(hp->name, "To") ||
296 procheader_headername_equal(hp->name, "Cc")) {
297 gchar *tp = hp->body;
298 hp->body = g_strconcat(tp, ", ", p, NULL);
304 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
305 gboolean full, gboolean decrypted)
311 if (stat(file, &s) < 0) {
312 FILE_OP_ERROR(file, "stat");
315 if (!S_ISREG(s.st_mode))
318 if ((fp = g_fopen(file, "rb")) == NULL) {
319 FILE_OP_ERROR(file, "fopen");
323 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
327 msginfo->size = s.st_size;
328 msginfo->mtime = s.st_mtime;
334 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
337 return parse_stream(&str, TRUE, flags, full, decrypted);
356 H_SC_PLANNED_DOWNLOAD = 14,
357 H_SC_MESSAGE_SIZE = 15,
360 H_DISPOSITION_NOTIFICATION_TO = 18,
361 H_RETURN_RECEIPT_TO = 19,
362 H_SC_PARTIALLY_RETRIEVED = 20,
363 H_SC_ACCOUNT_SERVER = 21,
364 H_SC_ACCOUNT_LOGIN = 22,
366 H_LIST_SUBSCRIBE = 24,
367 H_LIST_UNSUBSCRIBE = 25,
373 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
374 {"From:", NULL, TRUE},
377 {"Newsgroups:", NULL, TRUE},
378 {"Subject:", NULL, TRUE},
379 {"Message-ID:", NULL, FALSE},
380 {"References:", NULL, FALSE},
381 {"In-Reply-To:", NULL, FALSE},
382 {"Content-Type:", NULL, FALSE},
383 {"Seen:", NULL, FALSE},
384 {"Status:", NULL, FALSE},
385 {"X-Status:", NULL, FALSE},
386 {"From ", NULL, FALSE},
387 {"SC-Marked-For-Download:", NULL, FALSE},
388 {"SC-Message-Size:", NULL, FALSE},
389 {"Face:", NULL, FALSE},
390 {"X-Face:", NULL, FALSE},
391 {"Disposition-Notification-To:", NULL, FALSE},
392 {"Return-Receipt-To:", NULL, FALSE},
393 {"SC-Partially-Retrieved:", NULL, FALSE},
394 {"SC-Account-Server:", NULL, FALSE},
395 {"SC-Account-Login:",NULL, FALSE},
396 {"List-Post:", NULL, TRUE},
397 {"List-Subscribe:", NULL, TRUE},
398 {"List-Unsubscribe:",NULL, TRUE},
399 {"List-Help:", NULL, TRUE},
400 {"List-Archive:", NULL, TRUE},
401 {"List-Owner:", NULL, TRUE},
402 {NULL, NULL, FALSE}};
404 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
405 {"From:", NULL, TRUE},
408 {"Newsgroups:", NULL, TRUE},
409 {"Subject:", NULL, TRUE},
410 {"Message-ID:", NULL, FALSE},
411 {"References:", NULL, FALSE},
412 {"In-Reply-To:", NULL, FALSE},
413 {"Content-Type:", NULL, FALSE},
414 {"Seen:", NULL, FALSE},
415 {"Status:", NULL, FALSE},
416 {"X-Status:", NULL, FALSE},
417 {"From ", NULL, FALSE},
418 {"SC-Marked-For-Download:", NULL, FALSE},
419 {"SC-Message-Size:",NULL, FALSE},
420 {NULL, NULL, FALSE}};
422 HeaderEntry* procheader_get_headernames(gboolean full)
424 return full ? hentry_full : hentry_short;
427 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
430 return parse_stream(fp, FALSE, flags, full, decrypted);
433 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
434 gboolean full, gboolean decrypted)
442 void *orig_data = data;
444 get_one_field_func get_one_field =
445 isstring ? (get_one_field_func)string_get_one_field
446 : (get_one_field_func)procheader_get_one_field;
448 hentry = procheader_get_headernames(full);
450 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
451 while (get_one_field(buf, sizeof(buf), data, NULL) != -1) {
452 if (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
453 strlen("X-Sylpheed-End-Special-Headers:")))
455 /* from other mailers */
456 if (!strncmp(buf, "Date: ", 6)
457 || !strncmp(buf, "To: ", 4)
458 || !strncmp(buf, "From: ", 6)
459 || !strncmp(buf, "Subject: ", 9)) {
463 rewind((FILE *)data);
469 msginfo = procmsg_msginfo_new();
471 if (flags.tmp_flags || flags.perm_flags)
472 msginfo->flags = flags;
474 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
476 msginfo->inreplyto = NULL;
478 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
480 hp = buf + strlen(hentry[hnum].name);
481 while (*hp == ' ' || *hp == '\t') hp++;
485 if (msginfo->date) break;
487 procheader_date_parse(NULL, hp, 0);
488 if (g_utf8_validate(hp, -1, NULL)) {
489 msginfo->date = g_strdup(hp);
491 gchar *utf = conv_codeset_strdup(
493 conv_get_locale_charset_str_no_utf8(),
496 !g_utf8_validate(utf, -1, NULL)) {
498 utf = g_malloc(strlen(buf)*2+1);
499 conv_localetodisp(utf,
506 if (msginfo->from) break;
507 msginfo->from = conv_unmime_header(hp, NULL);
508 msginfo->fromname = procheader_get_fromname(msginfo->from);
509 remove_return(msginfo->from);
510 remove_return(msginfo->fromname);
513 tmp = conv_unmime_header(hp, NULL);
518 g_strconcat(p, ", ", tmp, NULL);
521 msginfo->to = g_strdup(tmp);
525 tmp = conv_unmime_header(hp, NULL);
530 g_strconcat(p, ", ", tmp, NULL);
533 msginfo->cc = g_strdup(tmp);
537 if (msginfo->newsgroups) {
538 p = msginfo->newsgroups;
539 msginfo->newsgroups =
540 g_strconcat(p, ",", hp, NULL);
543 msginfo->newsgroups = g_strdup(hp);
546 if (msginfo->subject) break;
547 msginfo->subject = conv_unmime_header(hp, NULL);
550 if (msginfo->msgid) break;
552 extract_parenthesis(hp, '<', '>');
554 msginfo->msgid = g_strdup(hp);
557 msginfo->references =
558 references_list_prepend(msginfo->references,
562 if (msginfo->inreplyto) break;
564 eliminate_parenthesis(hp, '(', ')');
565 if ((p = strrchr(hp, '<')) != NULL &&
566 strchr(p + 1, '>') != NULL) {
567 extract_parenthesis(p, '<', '>');
570 msginfo->inreplyto = g_strdup(p);
574 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
575 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
577 #ifdef ALLOW_HEADER_HINT
579 /* mnews Seen header */
580 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
584 if (!msginfo->extradata)
585 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
586 if (msginfo->extradata->face) break;
587 msginfo->extradata->face = g_strdup(hp);
590 if (!msginfo->extradata)
591 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
592 if (msginfo->extradata->xface) break;
593 msginfo->extradata->xface = g_strdup(hp);
595 case H_DISPOSITION_NOTIFICATION_TO:
596 if (!msginfo->extradata)
597 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
598 if (msginfo->extradata->dispositionnotificationto) break;
599 msginfo->extradata->dispositionnotificationto = g_strdup(hp);
601 case H_RETURN_RECEIPT_TO:
602 if (!msginfo->extradata)
603 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
604 if (msginfo->extradata->returnreceiptto) break;
605 msginfo->extradata->returnreceiptto = g_strdup(hp);
607 /* partial download infos */
608 case H_SC_PARTIALLY_RETRIEVED:
609 if (!msginfo->extradata)
610 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
611 if (msginfo->extradata->partial_recv) break;
612 msginfo->extradata->partial_recv = g_strdup(hp);
614 case H_SC_ACCOUNT_SERVER:
615 if (!msginfo->extradata)
616 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
617 if (msginfo->extradata->account_server) break;
618 msginfo->extradata->account_server = g_strdup(hp);
620 case H_SC_ACCOUNT_LOGIN:
621 if (!msginfo->extradata)
622 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
623 if (msginfo->extradata->account_login) break;
624 msginfo->extradata->account_login = g_strdup(hp);
626 case H_SC_MESSAGE_SIZE:
627 if (msginfo->total_size) break;
628 msginfo->total_size = atoi(hp);
630 case H_SC_PLANNED_DOWNLOAD:
631 msginfo->planned_download = atoi(hp);
633 /* end partial download infos */
634 #ifdef ALLOW_HEADER_HINT
636 if (strchr(hp, 'R') != NULL)
637 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
638 if (strchr(hp, 'O') != NULL)
639 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
640 if (strchr(hp, 'U') != NULL)
641 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
644 if (strchr(hp, 'D') != NULL)
645 MSG_SET_PERM_FLAGS(msginfo->flags,
647 if (strchr(hp, 'F') != NULL)
648 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
649 if (strchr(hp, 'd') != NULL)
650 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
651 if (strchr(hp, 'r') != NULL)
652 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
653 if (strchr(hp, 'f') != NULL)
654 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
658 if (msginfo->fromspace) break;
659 msginfo->fromspace = g_strdup(hp);
660 remove_return(msginfo->fromspace);
664 if (!msginfo->extradata)
665 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
666 if (msginfo->extradata->list_post) break;
667 msginfo->extradata->list_post = g_strdup(hp);
669 case H_LIST_SUBSCRIBE:
670 if (!msginfo->extradata)
671 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
672 if (msginfo->extradata->list_subscribe) break;
673 msginfo->extradata->list_subscribe = g_strdup(hp);
675 case H_LIST_UNSUBSCRIBE:
676 if (!msginfo->extradata)
677 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
678 if (msginfo->extradata->list_unsubscribe) break;
679 msginfo->extradata->list_unsubscribe = g_strdup(hp);
682 if (!msginfo->extradata)
683 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
684 if (msginfo->extradata->list_help) break;
685 msginfo->extradata->list_help = g_strdup(hp);
688 if (!msginfo->extradata)
689 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
690 if (msginfo->extradata->list_archive) break;
691 msginfo->extradata->list_archive = g_strdup(hp);
694 if (!msginfo->extradata)
695 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
696 if (msginfo->extradata->list_owner) break;
697 msginfo->extradata->list_owner = g_strdup(hp);
705 if (!msginfo->inreplyto && msginfo->references)
707 g_strdup((gchar *)msginfo->references->data);
712 gchar *procheader_get_fromname(const gchar *str)
716 Xstrdup_a(tmp, str, return NULL);
719 extract_quote(tmp, '\"');
721 } else if (strchr(tmp, '<')) {
722 eliminate_parenthesis(tmp, '<', '>');
726 extract_parenthesis(tmp, '<', '>');
729 } else if (strchr(tmp, '(')) {
730 extract_parenthesis(tmp, '(', ')');
735 name = g_strdup(str);
737 name = g_strdup(tmp);
742 static gint procheader_scan_date_string(const gchar *str,
743 gchar *weekday, gint *day,
744 gchar *month, gint *year,
745 gint *hh, gint *mm, gint *ss,
750 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
751 weekday, day, month, year, hh, mm, ss, zone);
752 if (result == 8) return 0;
754 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
755 weekday, day, month, year, hh, mm, ss, zone);
756 if (result == 8) return 0;
758 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
759 day, month, year, hh, mm, ss, zone);
760 if (result == 7) return 0;
763 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
764 weekday, day, month, year, hh, mm, ss);
765 if (result == 7) return 0;
767 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
768 day, month, year, hh, mm, ss);
769 if (result == 6) return 0;
772 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
773 weekday, day, month, year, hh, mm, zone);
774 if (result == 7) return 0;
776 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
777 day, month, year, hh, mm, zone);
778 if (result == 6) return 0;
781 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
782 weekday, day, month, year, hh, mm);
783 if (result == 6) return 0;
785 result = sscanf(str, "%d %9s %d %2d:%2d",
786 day, month, year, hh, mm);
787 if (result == 5) return 0;
793 * Hiro, most UNIXen support this function:
794 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
796 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
798 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
810 memset(t, 0, sizeof *t);
812 if (procheader_scan_date_string(src, weekday, &day, month, &year,
813 &hh, &mm, &ss, zone) < 0) {
814 g_warning("Invalid date: %s\n", src);
818 /* Y2K compliant :) */
827 if ((p = strstr(monthstr, month)) != NULL)
828 dmonth = (gint)(p - monthstr) / 3 + 1;
830 g_warning("Invalid month: %s\n", month);
831 dmonth = G_DATE_BAD_MONTH;
838 t->tm_mon = dmonth - 1;
839 t->tm_year = year - 1900;
849 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
851 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
858 GDateMonth dmonth = G_DATE_BAD_MONTH;
864 if (procheader_scan_date_string(src, weekday, &day, month, &year,
865 &hh, &mm, &ss, zone) < 0) {
867 strncpy2(dest, src, len);
871 /* Y2K compliant :) */
880 for (p = monthstr; *p != '\0'; p += 3) {
881 if (!g_ascii_strncasecmp(p, month, 3)) {
882 dmonth = (gint)(p - monthstr) / 3 + 1;
891 t.tm_mon = dmonth - 1;
892 t.tm_year = year - 1900;
898 tz_offset = remote_tzoffset_sec(zone);
900 timer += tzoffset_sec(&timer) - tz_offset;
903 procheader_date_get_localtime(dest, len, timer);
908 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
911 gchar *default_format = "%y/%m/%d(%a) %H:%M";
913 const gchar *src_codeset, *dest_codeset;
915 lt = localtime(&timer);
917 if (prefs_common.date_format)
918 fast_strftime(dest, len, prefs_common.date_format, lt);
920 fast_strftime(dest, len, default_format, lt);
922 if (!g_utf8_validate(dest, -1, NULL)) {
923 src_codeset = conv_get_locale_charset_str_no_utf8();
924 dest_codeset = CS_UTF_8;
925 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
927 strncpy2(dest, str, len);
933 /* Added by Mel Hadasht on 27 Aug 2001 */
934 /* Get a header from msginfo */
935 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
939 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
940 { NULL, NULL, FALSE } };
943 hentry[0].name = header;
945 g_return_val_if_fail(msginfo != NULL, -1);
946 file = procmsg_get_message_file_path(msginfo);
947 if ((fp = g_fopen(file, "rb")) == NULL) {
948 FILE_OP_ERROR(file, "fopen");
952 val = procheader_get_one_field(buf,len, fp, hentry);
953 if (fclose(fp) == EOF) {
954 FILE_OP_ERROR(file, "fclose");