2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2004 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, gint 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 (nexthead == '\r' || nexthead == '\n') {
198 if ((len - (bufp - buf)) <= 2) break;
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)
581 gchar *reference = NULL;
586 get_one_field_func get_one_field =
587 isstring ? (get_one_field_func)string_get_one_field
588 : (get_one_field_func)procheader_get_one_field;
590 hentry = procheader_get_headernames(full);
592 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
593 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
597 msginfo = procmsg_msginfo_new();
599 if (flags.tmp_flags || flags.perm_flags)
600 msginfo->flags = flags;
602 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
604 msginfo->inreplyto = NULL;
606 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
608 hp = buf + strlen(hentry[hnum].name);
609 while (*hp == ' ' || *hp == '\t') hp++;
613 if (msginfo->date) break;
615 procheader_date_parse(NULL, hp, 0);
616 msginfo->date = g_strdup(hp);
619 if (msginfo->from) break;
620 msginfo->from = conv_unmime_header(hp, NULL);
621 msginfo->fromname = procheader_get_fromname(msginfo->from);
624 tmp = conv_unmime_header(hp, NULL);
628 g_strconcat(p, ", ", tmp, NULL);
631 msginfo->to = g_strdup(tmp);
635 tmp = conv_unmime_header(hp, NULL);
639 g_strconcat(p, ", ", tmp, NULL);
642 msginfo->cc = g_strdup(tmp);
646 if (msginfo->newsgroups) {
647 p = msginfo->newsgroups;
648 msginfo->newsgroups =
649 g_strconcat(p, ",", hp, NULL);
652 msginfo->newsgroups = g_strdup(hp);
655 if (msginfo->subject) break;
656 msginfo->subject = conv_unmime_header(hp, NULL);
659 if (msginfo->msgid) break;
661 extract_parenthesis(hp, '<', '>');
663 msginfo->msgid = g_strdup(hp);
668 msginfo->references = g_strdup(hp);
669 eliminate_parenthesis(hp, '(', ')');
670 if ((p = strrchr(hp, '<')) != NULL &&
671 strchr(p + 1, '>') != NULL) {
672 extract_parenthesis(p, '<', '>');
675 reference = g_strdup(p);
680 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
681 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
683 #ifdef ALLOW_HEADER_HINT
685 /* mnews Seen header */
686 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
690 if (msginfo->xface) break;
691 msginfo->xface = g_strdup(hp);
693 case H_DISPOSITION_NOTIFICATION_TO:
694 if (msginfo->dispositionnotificationto) break;
695 msginfo->dispositionnotificationto = g_strdup(hp);
697 case H_RETURN_RECEIPT_TO:
698 if (msginfo->returnreceiptto) break;
699 msginfo->returnreceiptto = g_strdup(hp);
701 case H_SC_PARTIALLY_RETRIEVED:
702 if (msginfo->partial_recv) break;
703 msginfo->partial_recv = g_strdup(hp);
705 case H_SC_ACCOUNT_SERVER:
706 if (msginfo->account_server) break;
707 msginfo->account_server = g_strdup(hp);
709 case H_SC_ACCOUNT_LOGIN:
710 if (msginfo->account_login) break;
711 msginfo->account_login = g_strdup(hp);
713 case H_SC_MESSAGE_SIZE:
714 if (msginfo->total_size) break;
715 msginfo->total_size = atoi(hp);
717 case H_SC_PLANNED_DOWNLOAD:
718 msginfo->planned_download = atoi(hp);
720 #ifdef ALLOW_HEADER_HINT
722 if (strchr(hp, 'R') != NULL)
723 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
724 if (strchr(hp, 'O') != NULL)
725 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
726 if (strchr(hp, 'U') != NULL)
727 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
730 if (strchr(hp, 'D') != NULL)
731 MSG_SET_PERM_FLAGS(msginfo->flags,
733 if (strchr(hp, 'F') != NULL)
734 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
735 if (strchr(hp, 'd') != NULL)
736 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
737 if (strchr(hp, 'r') != NULL)
738 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
739 if (strchr(hp, 'f') != NULL)
740 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
744 if (msginfo->fromspace) break;
745 msginfo->fromspace = g_strdup(hp);
751 msginfo->inreplyto = reference;
756 gchar *procheader_get_fromname(const gchar *str)
760 Xstrdup_a(tmp, str, return NULL);
763 extract_quote(tmp, '\"');
765 } else if (strchr(tmp, '<')) {
766 eliminate_parenthesis(tmp, '<', '>');
770 extract_parenthesis(tmp, '<', '>');
773 } else if (strchr(tmp, '(')) {
774 extract_parenthesis(tmp, '(', ')');
779 name = g_strdup(str);
781 name = g_strdup(tmp);
786 static gint procheader_scan_date_string(const gchar *str,
787 gchar *weekday, gint *day,
788 gchar *month, gint *year,
789 gint *hh, gint *mm, gint *ss,
794 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
795 weekday, day, month, year, hh, mm, ss, zone);
796 if (result == 8) return 0;
798 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
799 weekday, day, month, year, hh, mm, ss, zone);
800 if (result == 8) return 0;
802 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
803 day, month, year, hh, mm, ss, zone);
804 if (result == 7) return 0;
807 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
808 weekday, day, month, year, hh, mm, ss);
809 if (result == 7) return 0;
812 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
813 weekday, day, month, year, hh, mm, zone);
814 if (result == 7) return 0;
816 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
817 day, month, year, hh, mm, zone);
818 if (result == 6) return 0;
824 * Hiro, most UNIXen support this function:
825 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
827 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
829 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
841 memset(t, 0, sizeof *t);
843 if (procheader_scan_date_string(src, weekday, &day, month, &year,
844 &hh, &mm, &ss, zone) < 0) {
845 g_warning("Invalid date: %s\n", src);
849 /* Y2K compliant :) */
858 if ((p = strstr(monthstr, month)) != NULL)
859 dmonth = (gint)(p - monthstr) / 3 + 1;
861 g_warning("Invalid month: %s\n", month);
862 dmonth = G_DATE_BAD_MONTH;
869 t->tm_mon = dmonth - 1;
870 t->tm_year = year - 1900;
880 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
882 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
889 GDateMonth dmonth = G_DATE_BAD_MONTH;
895 if (procheader_scan_date_string(src, weekday, &day, month, &year,
896 &hh, &mm, &ss, zone) < 0) {
897 g_warning("Invalid date: %s\n", src);
899 strncpy2(dest, src, len);
903 /* Y2K compliant :) */
912 for (p = monthstr; *p != '\0'; p += 3) {
913 if (!g_ascii_strncasecmp(p, month, 3)) {
914 dmonth = (gint)(p - monthstr) / 3 + 1;
919 g_warning("Invalid month: %s\n", month);
925 t.tm_mon = dmonth - 1;
926 t.tm_year = year - 1900;
932 tz_offset = remote_tzoffset_sec(zone);
934 timer += tzoffset_sec(&timer) - tz_offset;
937 procheader_date_get_localtime(dest, len, timer);
942 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
945 gchar *default_format = "%y/%m/%d(%a) %H:%M";
947 const gchar *src_codeset, *dest_codeset;
949 lt = localtime(&timer);
951 if (prefs_common.date_format)
952 strftime(dest, len, prefs_common.date_format, lt);
954 strftime(dest, len, default_format, lt);
956 src_codeset = conv_get_locale_charset_str();
957 dest_codeset = CS_UTF_8;
958 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
960 g_snprintf(dest, len, "%s", str);
961 strncpy2(dest, str, len);
966 /* Added by Mel Hadasht on 27 Aug 2001 */
967 /* Get a header from msginfo */
968 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
972 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
973 { NULL, NULL, FALSE } };
976 hentry[0].name = header;
978 g_return_val_if_fail(msginfo != NULL, -1);
979 file = procmsg_get_message_file_path(msginfo);
980 if ((fp = fopen(file, "rb")) == NULL) {
981 FILE_OP_ERROR(file, "fopen");
985 val = procheader_get_one_field(buf,len, fp, hentry);
986 if (fclose(fp) == EOF) {
987 FILE_OP_ERROR(file, "fclose");