2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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-Claws-End-Special-Headers: 1",
453 strlen("X-Claws-End-Special-Headers:"))) ||
454 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
455 strlen("X-Sylpheed-End-Special-Headers:"))))
457 /* from other mailers */
458 if (!strncmp(buf, "Date: ", 6)
459 || !strncmp(buf, "To: ", 4)
460 || !strncmp(buf, "From: ", 6)
461 || !strncmp(buf, "Subject: ", 9)) {
465 rewind((FILE *)data);
471 msginfo = procmsg_msginfo_new();
473 if (flags.tmp_flags || flags.perm_flags)
474 msginfo->flags = flags;
476 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
478 msginfo->inreplyto = NULL;
480 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
482 hp = buf + strlen(hentry[hnum].name);
483 while (*hp == ' ' || *hp == '\t') hp++;
487 if (msginfo->date) break;
489 procheader_date_parse(NULL, hp, 0);
490 if (g_utf8_validate(hp, -1, NULL)) {
491 msginfo->date = g_strdup(hp);
493 gchar *utf = conv_codeset_strdup(
495 conv_get_locale_charset_str_no_utf8(),
498 !g_utf8_validate(utf, -1, NULL)) {
500 utf = g_malloc(strlen(buf)*2+1);
501 conv_localetodisp(utf,
508 if (msginfo->from) break;
509 msginfo->from = conv_unmime_header(hp, NULL);
510 msginfo->fromname = procheader_get_fromname(msginfo->from);
511 remove_return(msginfo->from);
512 remove_return(msginfo->fromname);
515 tmp = conv_unmime_header(hp, NULL);
520 g_strconcat(p, ", ", tmp, NULL);
523 msginfo->to = g_strdup(tmp);
527 tmp = conv_unmime_header(hp, NULL);
532 g_strconcat(p, ", ", tmp, NULL);
535 msginfo->cc = g_strdup(tmp);
539 if (msginfo->newsgroups) {
540 p = msginfo->newsgroups;
541 msginfo->newsgroups =
542 g_strconcat(p, ",", hp, NULL);
545 msginfo->newsgroups = g_strdup(hp);
548 if (msginfo->subject) break;
549 msginfo->subject = conv_unmime_header(hp, NULL);
550 unfold_line(msginfo->subject);
553 if (msginfo->msgid) break;
555 extract_parenthesis(hp, '<', '>');
557 msginfo->msgid = g_strdup(hp);
560 msginfo->references =
561 references_list_prepend(msginfo->references,
565 if (msginfo->inreplyto) break;
567 eliminate_parenthesis(hp, '(', ')');
568 if ((p = strrchr(hp, '<')) != NULL &&
569 strchr(p + 1, '>') != NULL) {
570 extract_parenthesis(p, '<', '>');
573 msginfo->inreplyto = g_strdup(p);
577 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
578 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
580 #ifdef ALLOW_HEADER_HINT
582 /* mnews Seen header */
583 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
587 if (!msginfo->extradata)
588 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
589 if (msginfo->extradata->face) break;
590 msginfo->extradata->face = g_strdup(hp);
593 if (!msginfo->extradata)
594 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
595 if (msginfo->extradata->xface) break;
596 msginfo->extradata->xface = g_strdup(hp);
598 case H_DISPOSITION_NOTIFICATION_TO:
599 if (!msginfo->extradata)
600 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
601 if (msginfo->extradata->dispositionnotificationto) break;
602 msginfo->extradata->dispositionnotificationto = g_strdup(hp);
604 case H_RETURN_RECEIPT_TO:
605 if (!msginfo->extradata)
606 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
607 if (msginfo->extradata->returnreceiptto) break;
608 msginfo->extradata->returnreceiptto = g_strdup(hp);
610 /* partial download infos */
611 case H_SC_PARTIALLY_RETRIEVED:
612 if (!msginfo->extradata)
613 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
614 if (msginfo->extradata->partial_recv) break;
615 msginfo->extradata->partial_recv = g_strdup(hp);
617 case H_SC_ACCOUNT_SERVER:
618 if (!msginfo->extradata)
619 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
620 if (msginfo->extradata->account_server) break;
621 msginfo->extradata->account_server = g_strdup(hp);
623 case H_SC_ACCOUNT_LOGIN:
624 if (!msginfo->extradata)
625 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
626 if (msginfo->extradata->account_login) break;
627 msginfo->extradata->account_login = g_strdup(hp);
629 case H_SC_MESSAGE_SIZE:
630 if (msginfo->total_size) break;
631 msginfo->total_size = atoi(hp);
633 case H_SC_PLANNED_DOWNLOAD:
634 msginfo->planned_download = atoi(hp);
636 /* end partial download infos */
637 #ifdef ALLOW_HEADER_HINT
639 if (strchr(hp, 'R') != NULL)
640 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
641 if (strchr(hp, 'O') != NULL)
642 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
643 if (strchr(hp, 'U') != NULL)
644 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
647 if (strchr(hp, 'D') != NULL)
648 MSG_SET_PERM_FLAGS(msginfo->flags,
650 if (strchr(hp, 'F') != NULL)
651 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
652 if (strchr(hp, 'd') != NULL)
653 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
654 if (strchr(hp, 'r') != NULL)
655 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
656 if (strchr(hp, 'f') != NULL)
657 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
661 if (msginfo->fromspace) break;
662 msginfo->fromspace = g_strdup(hp);
663 remove_return(msginfo->fromspace);
667 if (!msginfo->extradata)
668 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
669 if (msginfo->extradata->list_post) break;
670 msginfo->extradata->list_post = g_strdup(hp);
672 case H_LIST_SUBSCRIBE:
673 if (!msginfo->extradata)
674 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
675 if (msginfo->extradata->list_subscribe) break;
676 msginfo->extradata->list_subscribe = g_strdup(hp);
678 case H_LIST_UNSUBSCRIBE:
679 if (!msginfo->extradata)
680 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
681 if (msginfo->extradata->list_unsubscribe) break;
682 msginfo->extradata->list_unsubscribe = g_strdup(hp);
685 if (!msginfo->extradata)
686 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
687 if (msginfo->extradata->list_help) break;
688 msginfo->extradata->list_help = g_strdup(hp);
691 if (!msginfo->extradata)
692 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
693 if (msginfo->extradata->list_archive) break;
694 msginfo->extradata->list_archive = g_strdup(hp);
697 if (!msginfo->extradata)
698 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
699 if (msginfo->extradata->list_owner) break;
700 msginfo->extradata->list_owner = g_strdup(hp);
708 if (!msginfo->inreplyto && msginfo->references)
710 g_strdup((gchar *)msginfo->references->data);
715 gchar *procheader_get_fromname(const gchar *str)
719 Xstrdup_a(tmp, str, return NULL);
722 extract_quote(tmp, '\"');
724 } else if (strchr(tmp, '<')) {
725 eliminate_parenthesis(tmp, '<', '>');
729 extract_parenthesis(tmp, '<', '>');
732 } else if (strchr(tmp, '(')) {
733 extract_parenthesis(tmp, '(', ')');
738 name = g_strdup(str);
740 name = g_strdup(tmp);
745 static gint procheader_scan_date_string(const gchar *str,
746 gchar *weekday, gint *day,
747 gchar *month, gint *year,
748 gint *hh, gint *mm, gint *ss,
753 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
754 weekday, day, month, year, hh, mm, ss, zone);
755 if (result == 8) return 0;
757 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
758 weekday, day, month, year, hh, mm, ss, zone);
759 if (result == 8) return 0;
761 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
762 day, month, year, hh, mm, ss, zone);
763 if (result == 7) return 0;
766 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
767 weekday, day, month, year, hh, mm, ss);
768 if (result == 7) return 0;
770 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
771 day, month, year, hh, mm, ss);
772 if (result == 6) return 0;
775 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
776 weekday, day, month, year, hh, mm, zone);
777 if (result == 7) return 0;
779 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
780 day, month, year, hh, mm, zone);
781 if (result == 6) return 0;
784 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
785 weekday, day, month, year, hh, mm);
786 if (result == 6) return 0;
788 result = sscanf(str, "%d %9s %d %2d:%2d",
789 day, month, year, hh, mm);
790 if (result == 5) return 0;
796 * Hiro, most UNIXen support this function:
797 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
799 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
801 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
813 memset(t, 0, sizeof *t);
815 if (procheader_scan_date_string(src, weekday, &day, month, &year,
816 &hh, &mm, &ss, zone) < 0) {
817 g_warning("Invalid date: %s\n", src);
821 /* Y2K compliant :) */
830 if ((p = strstr(monthstr, month)) != NULL)
831 dmonth = (gint)(p - monthstr) / 3 + 1;
833 g_warning("Invalid month: %s\n", month);
834 dmonth = G_DATE_BAD_MONTH;
841 t->tm_mon = dmonth - 1;
842 t->tm_year = year - 1900;
852 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
854 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
861 GDateMonth dmonth = G_DATE_BAD_MONTH;
867 if (procheader_scan_date_string(src, weekday, &day, month, &year,
868 &hh, &mm, &ss, zone) < 0) {
870 strncpy2(dest, src, len);
874 /* Y2K compliant :) */
883 for (p = monthstr; *p != '\0'; p += 3) {
884 if (!g_ascii_strncasecmp(p, month, 3)) {
885 dmonth = (gint)(p - monthstr) / 3 + 1;
894 t.tm_mon = dmonth - 1;
895 t.tm_year = year - 1900;
901 tz_offset = remote_tzoffset_sec(zone);
903 timer += tzoffset_sec(&timer) - tz_offset;
906 procheader_date_get_localtime(dest, len, timer);
911 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
914 gchar *default_format = "%y/%m/%d(%a) %H:%M";
916 const gchar *src_codeset, *dest_codeset;
919 lt = localtime_r(&timer, &buf);
921 if (prefs_common.date_format)
922 fast_strftime(dest, len, prefs_common.date_format, lt);
924 fast_strftime(dest, len, default_format, lt);
926 if (!g_utf8_validate(dest, -1, NULL)) {
927 src_codeset = conv_get_locale_charset_str_no_utf8();
928 dest_codeset = CS_UTF_8;
929 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
931 strncpy2(dest, str, len);
937 /* Added by Mel Hadasht on 27 Aug 2001 */
938 /* Get a header from msginfo */
939 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
943 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
944 { NULL, NULL, FALSE } };
947 hentry[0].name = header;
949 g_return_val_if_fail(msginfo != NULL, -1);
950 file = procmsg_get_message_file_path(msginfo);
951 if ((fp = g_fopen(file, "rb")) == NULL) {
952 FILE_OP_ERROR(file, "fopen");
956 val = procheader_get_one_field(buf,len, fp, hentry);
957 if (fclose(fp) == EOF) {
958 FILE_OP_ERROR(file, "fclose");