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,
168 GPtrArray *procheader_get_header_array_asis(FILE *fp)
174 g_return_val_if_fail(fp != NULL, NULL);
176 headers = g_ptr_array_new();
178 while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
179 if ((header = procheader_parse_header(buf)) != NULL)
180 g_ptr_array_add(headers, header);
182 if (*buf == ':') continue;
183 for (p = buf; *p && *p != ' '; p++) {
185 header = g_new(Header, 1);
186 header->name = g_strndup(buf, p - buf);
188 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
189 header->body = g_strdup(tmp);
191 g_ptr_array_add(headers, header);
201 void procheader_header_array_destroy(GPtrArray *harray)
206 for (i = 0; i < harray->len; i++) {
207 header = g_ptr_array_index(harray, i);
208 procheader_header_free(header);
211 g_ptr_array_free(harray, TRUE);
214 void procheader_header_free(Header *header)
218 g_free(header->name);
219 g_free(header->body);
224 tests whether two headers' names are equal
225 remove the trailing ':' or ' ' before comparing
228 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
235 if (hdr1[len1 - 1] == ':')
237 if (hdr2[len2 - 1] == ':')
242 return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
246 parse headers, for example :
247 From: dinh@enseirb.fr becomes :
248 header->name = "From:"
249 header->body = "dinh@enseirb.fr"
252 Header * procheader_parse_header(gchar * buf)
257 if ((*buf == ':') || (*buf == ' '))
260 for (p = buf; *p ; p++) {
261 if ((*p == ':') || (*p == ' ')) {
262 header = g_new(Header, 1);
263 header->name = g_strndup(buf, p - buf + 1);
265 while (*p == ' ' || *p == '\t') p++;
266 header->body = conv_unmime_header(p, NULL);
273 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
280 if (hentry == NULL) return;
282 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
286 p = buf + strlen(hp->name);
287 while (*p == ' ' || *p == '\t') p++;
289 if (hp->body == NULL)
290 hp->body = g_strdup(p);
291 else if (procheader_headername_equal(hp->name, "To") ||
292 procheader_headername_equal(hp->name, "Cc")) {
293 gchar *tp = hp->body;
294 hp->body = g_strconcat(tp, ", ", p, NULL);
300 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
301 gboolean full, gboolean decrypted)
307 if (stat(file, &s) < 0) {
308 FILE_OP_ERROR(file, "stat");
311 if (!S_ISREG(s.st_mode))
314 if ((fp = g_fopen(file, "rb")) == NULL) {
315 FILE_OP_ERROR(file, "fopen");
319 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
323 msginfo->size = s.st_size;
324 msginfo->mtime = s.st_mtime;
330 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
333 return parse_stream(&str, TRUE, flags, full, decrypted);
352 H_SC_PLANNED_DOWNLOAD = 14,
353 H_SC_MESSAGE_SIZE = 15,
356 H_DISPOSITION_NOTIFICATION_TO = 18,
357 H_RETURN_RECEIPT_TO = 19,
358 H_SC_PARTIALLY_RETRIEVED = 20,
359 H_SC_ACCOUNT_SERVER = 21,
360 H_SC_ACCOUNT_LOGIN = 22,
362 H_LIST_SUBSCRIBE = 24,
363 H_LIST_UNSUBSCRIBE = 25,
369 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
370 {"From:", NULL, TRUE},
373 {"Newsgroups:", NULL, TRUE},
374 {"Subject:", NULL, TRUE},
375 {"Message-ID:", NULL, FALSE},
376 {"References:", NULL, FALSE},
377 {"In-Reply-To:", NULL, FALSE},
378 {"Content-Type:", NULL, FALSE},
379 {"Seen:", NULL, FALSE},
380 {"Status:", NULL, FALSE},
381 {"X-Status:", NULL, FALSE},
382 {"From ", NULL, FALSE},
383 {"SC-Marked-For-Download:", NULL, FALSE},
384 {"SC-Message-Size:", NULL, FALSE},
385 {"Face:", NULL, FALSE},
386 {"X-Face:", NULL, FALSE},
387 {"Disposition-Notification-To:", NULL, FALSE},
388 {"Return-Receipt-To:", NULL, FALSE},
389 {"SC-Partially-Retrieved:", NULL, FALSE},
390 {"SC-Account-Server:", NULL, FALSE},
391 {"SC-Account-Login:",NULL, FALSE},
392 {"List-Post:", NULL, TRUE},
393 {"List-Subscribe:", NULL, TRUE},
394 {"List-Unsubscribe:",NULL, TRUE},
395 {"List-Help:", NULL, TRUE},
396 {"List-Archive:", NULL, TRUE},
397 {"List-Owner:", NULL, TRUE},
398 {NULL, NULL, FALSE}};
400 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
401 {"From:", NULL, TRUE},
404 {"Newsgroups:", NULL, TRUE},
405 {"Subject:", NULL, TRUE},
406 {"Message-ID:", NULL, FALSE},
407 {"References:", NULL, FALSE},
408 {"In-Reply-To:", NULL, FALSE},
409 {"Content-Type:", NULL, FALSE},
410 {"Seen:", NULL, FALSE},
411 {"Status:", NULL, FALSE},
412 {"X-Status:", NULL, FALSE},
413 {"From ", NULL, FALSE},
414 {"SC-Marked-For-Download:", NULL, FALSE},
415 {"SC-Message-Size:",NULL, FALSE},
416 {NULL, NULL, FALSE}};
418 HeaderEntry* procheader_get_headernames(gboolean full)
420 return full ? hentry_full : hentry_short;
423 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
426 return parse_stream(fp, FALSE, flags, full, decrypted);
429 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
430 gboolean full, gboolean decrypted)
438 void *orig_data = data;
440 get_one_field_func get_one_field =
441 isstring ? (get_one_field_func)string_get_one_field
442 : (get_one_field_func)procheader_get_one_field;
444 hentry = procheader_get_headernames(full);
446 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
447 while (get_one_field(buf, sizeof(buf), data, NULL) != -1) {
448 if (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
449 strlen("X-Sylpheed-End-Special-Headers:")))
451 /* from other mailers */
452 if (!strncmp(buf, "Date: ", 6)
453 || !strncmp(buf, "To: ", 4)
454 || !strncmp(buf, "From: ", 6)
455 || !strncmp(buf, "Subject: ", 9)) {
459 rewind((FILE *)data);
465 msginfo = procmsg_msginfo_new();
467 if (flags.tmp_flags || flags.perm_flags)
468 msginfo->flags = flags;
470 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
472 msginfo->inreplyto = NULL;
474 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
476 hp = buf + strlen(hentry[hnum].name);
477 while (*hp == ' ' || *hp == '\t') hp++;
481 if (msginfo->date) break;
483 procheader_date_parse(NULL, hp, 0);
484 if (g_utf8_validate(hp, -1, NULL)) {
485 msginfo->date = g_strdup(hp);
487 gchar *utf = conv_codeset_strdup(
489 conv_get_locale_charset_str_no_utf8(),
492 !g_utf8_validate(utf, -1, NULL)) {
494 utf = g_malloc(strlen(buf)*2+1);
495 conv_localetodisp(utf,
502 if (msginfo->from) break;
503 msginfo->from = conv_unmime_header(hp, NULL);
504 msginfo->fromname = procheader_get_fromname(msginfo->from);
505 remove_return(msginfo->from);
506 remove_return(msginfo->fromname);
509 tmp = conv_unmime_header(hp, NULL);
514 g_strconcat(p, ", ", tmp, NULL);
517 msginfo->to = g_strdup(tmp);
521 tmp = conv_unmime_header(hp, NULL);
526 g_strconcat(p, ", ", tmp, NULL);
529 msginfo->cc = g_strdup(tmp);
533 if (msginfo->newsgroups) {
534 p = msginfo->newsgroups;
535 msginfo->newsgroups =
536 g_strconcat(p, ",", hp, NULL);
539 msginfo->newsgroups = g_strdup(hp);
542 if (msginfo->subject) break;
543 msginfo->subject = conv_unmime_header(hp, NULL);
544 remove_return(msginfo->subject);
547 if (msginfo->msgid) break;
549 extract_parenthesis(hp, '<', '>');
551 msginfo->msgid = g_strdup(hp);
554 msginfo->references =
555 references_list_prepend(msginfo->references,
559 if (msginfo->inreplyto) break;
561 eliminate_parenthesis(hp, '(', ')');
562 if ((p = strrchr(hp, '<')) != NULL &&
563 strchr(p + 1, '>') != NULL) {
564 extract_parenthesis(p, '<', '>');
567 msginfo->inreplyto = g_strdup(p);
571 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
572 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
574 #ifdef ALLOW_HEADER_HINT
576 /* mnews Seen header */
577 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
581 if (!msginfo->extradata)
582 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
583 if (msginfo->extradata->face) break;
584 msginfo->extradata->face = g_strdup(hp);
587 if (!msginfo->extradata)
588 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
589 if (msginfo->extradata->xface) break;
590 msginfo->extradata->xface = g_strdup(hp);
592 case H_DISPOSITION_NOTIFICATION_TO:
593 if (!msginfo->extradata)
594 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
595 if (msginfo->extradata->dispositionnotificationto) break;
596 msginfo->extradata->dispositionnotificationto = g_strdup(hp);
598 case H_RETURN_RECEIPT_TO:
599 if (!msginfo->extradata)
600 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
601 if (msginfo->extradata->returnreceiptto) break;
602 msginfo->extradata->returnreceiptto = g_strdup(hp);
604 /* partial download infos */
605 case H_SC_PARTIALLY_RETRIEVED:
606 if (!msginfo->extradata)
607 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
608 if (msginfo->extradata->partial_recv) break;
609 msginfo->extradata->partial_recv = g_strdup(hp);
611 case H_SC_ACCOUNT_SERVER:
612 if (!msginfo->extradata)
613 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
614 if (msginfo->extradata->account_server) break;
615 msginfo->extradata->account_server = g_strdup(hp);
617 case H_SC_ACCOUNT_LOGIN:
618 if (!msginfo->extradata)
619 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
620 if (msginfo->extradata->account_login) break;
621 msginfo->extradata->account_login = g_strdup(hp);
623 case H_SC_MESSAGE_SIZE:
624 if (msginfo->total_size) break;
625 msginfo->total_size = atoi(hp);
627 case H_SC_PLANNED_DOWNLOAD:
628 msginfo->planned_download = atoi(hp);
630 /* end partial download infos */
631 #ifdef ALLOW_HEADER_HINT
633 if (strchr(hp, 'R') != NULL)
634 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
635 if (strchr(hp, 'O') != NULL)
636 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
637 if (strchr(hp, 'U') != NULL)
638 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
641 if (strchr(hp, 'D') != NULL)
642 MSG_SET_PERM_FLAGS(msginfo->flags,
644 if (strchr(hp, 'F') != NULL)
645 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
646 if (strchr(hp, 'd') != NULL)
647 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
648 if (strchr(hp, 'r') != NULL)
649 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
650 if (strchr(hp, 'f') != NULL)
651 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
655 if (msginfo->fromspace) break;
656 msginfo->fromspace = g_strdup(hp);
657 remove_return(msginfo->fromspace);
661 if (!msginfo->extradata)
662 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
663 if (msginfo->extradata->list_post) break;
664 msginfo->extradata->list_post = g_strdup(hp);
666 case H_LIST_SUBSCRIBE:
667 if (!msginfo->extradata)
668 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
669 if (msginfo->extradata->list_subscribe) break;
670 msginfo->extradata->list_subscribe = g_strdup(hp);
672 case H_LIST_UNSUBSCRIBE:
673 if (!msginfo->extradata)
674 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
675 if (msginfo->extradata->list_unsubscribe) break;
676 msginfo->extradata->list_unsubscribe = g_strdup(hp);
679 if (!msginfo->extradata)
680 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
681 if (msginfo->extradata->list_help) break;
682 msginfo->extradata->list_help = g_strdup(hp);
685 if (!msginfo->extradata)
686 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
687 if (msginfo->extradata->list_archive) break;
688 msginfo->extradata->list_archive = g_strdup(hp);
691 if (!msginfo->extradata)
692 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
693 if (msginfo->extradata->list_owner) break;
694 msginfo->extradata->list_owner = g_strdup(hp);
702 if (!msginfo->inreplyto && msginfo->references)
704 g_strdup((gchar *)msginfo->references->data);
709 gchar *procheader_get_fromname(const gchar *str)
713 Xstrdup_a(tmp, str, return NULL);
716 extract_quote(tmp, '\"');
718 } else if (strchr(tmp, '<')) {
719 eliminate_parenthesis(tmp, '<', '>');
723 extract_parenthesis(tmp, '<', '>');
726 } else if (strchr(tmp, '(')) {
727 extract_parenthesis(tmp, '(', ')');
732 name = g_strdup(str);
734 name = g_strdup(tmp);
739 static gint procheader_scan_date_string(const gchar *str,
740 gchar *weekday, gint *day,
741 gchar *month, gint *year,
742 gint *hh, gint *mm, gint *ss,
747 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
748 weekday, day, month, year, hh, mm, ss, zone);
749 if (result == 8) return 0;
751 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
752 weekday, day, month, year, hh, mm, ss, zone);
753 if (result == 8) return 0;
755 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
756 day, month, year, hh, mm, ss, zone);
757 if (result == 7) return 0;
760 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
761 weekday, day, month, year, hh, mm, ss);
762 if (result == 7) return 0;
764 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
765 day, month, year, hh, mm, ss);
766 if (result == 6) return 0;
769 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
770 weekday, day, month, year, hh, mm, zone);
771 if (result == 7) return 0;
773 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
774 day, month, year, hh, mm, zone);
775 if (result == 6) return 0;
778 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
779 weekday, day, month, year, hh, mm);
780 if (result == 6) return 0;
782 result = sscanf(str, "%d %9s %d %2d:%2d",
783 day, month, year, hh, mm);
784 if (result == 5) return 0;
790 * Hiro, most UNIXen support this function:
791 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
793 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
795 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
807 memset(t, 0, sizeof *t);
809 if (procheader_scan_date_string(src, weekday, &day, month, &year,
810 &hh, &mm, &ss, zone) < 0) {
811 g_warning("Invalid date: %s\n", src);
815 /* Y2K compliant :) */
824 if ((p = strstr(monthstr, month)) != NULL)
825 dmonth = (gint)(p - monthstr) / 3 + 1;
827 g_warning("Invalid month: %s\n", month);
828 dmonth = G_DATE_BAD_MONTH;
835 t->tm_mon = dmonth - 1;
836 t->tm_year = year - 1900;
846 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
848 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
855 GDateMonth dmonth = G_DATE_BAD_MONTH;
861 if (procheader_scan_date_string(src, weekday, &day, month, &year,
862 &hh, &mm, &ss, zone) < 0) {
864 strncpy2(dest, src, len);
868 /* Y2K compliant :) */
877 for (p = monthstr; *p != '\0'; p += 3) {
878 if (!g_ascii_strncasecmp(p, month, 3)) {
879 dmonth = (gint)(p - monthstr) / 3 + 1;
888 t.tm_mon = dmonth - 1;
889 t.tm_year = year - 1900;
895 tz_offset = remote_tzoffset_sec(zone);
897 timer += tzoffset_sec(&timer) - tz_offset;
900 procheader_date_get_localtime(dest, len, timer);
905 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
908 gchar *default_format = "%y/%m/%d(%a) %H:%M";
910 const gchar *src_codeset, *dest_codeset;
912 lt = localtime(&timer);
914 if (prefs_common.date_format)
915 fast_strftime(dest, len, prefs_common.date_format, lt);
917 fast_strftime(dest, len, default_format, lt);
919 if (!g_utf8_validate(dest, -1, NULL)) {
920 src_codeset = conv_get_locale_charset_str_no_utf8();
921 dest_codeset = CS_UTF_8;
922 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
924 strncpy2(dest, str, len);
930 /* Added by Mel Hadasht on 27 Aug 2001 */
931 /* Get a header from msginfo */
932 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
936 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
937 { NULL, NULL, FALSE } };
940 hentry[0].name = header;
942 g_return_val_if_fail(msginfo != NULL, -1);
943 file = procmsg_get_message_file_path(msginfo);
944 if ((fp = g_fopen(file, "rb")) == NULL) {
945 FILE_OP_ERROR(file, "fopen");
949 val = procheader_get_one_field(buf,len, fp, hentry);
950 if (fclose(fp) == EOF) {
951 FILE_OP_ERROR(file, "fclose");