2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2005 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, 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,
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)
585 get_one_field_func get_one_field =
586 isstring ? (get_one_field_func)string_get_one_field
587 : (get_one_field_func)procheader_get_one_field;
589 hentry = procheader_get_headernames(full);
591 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
592 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
596 msginfo = procmsg_msginfo_new();
598 if (flags.tmp_flags || flags.perm_flags)
599 msginfo->flags = flags;
601 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
603 msginfo->inreplyto = NULL;
605 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
607 hp = buf + strlen(hentry[hnum].name);
608 while (*hp == ' ' || *hp == '\t') hp++;
612 if (msginfo->date) break;
614 procheader_date_parse(NULL, hp, 0);
615 msginfo->date = g_strdup(hp);
618 if (msginfo->from) break;
619 msginfo->from = conv_unmime_header(hp, NULL);
620 msginfo->fromname = procheader_get_fromname(msginfo->from);
621 replace_returns(msginfo->from);
622 replace_returns(msginfo->fromname);
625 tmp = conv_unmime_header(hp, NULL);
629 g_strconcat(p, ", ", tmp, NULL);
632 msginfo->to = g_strdup(tmp);
636 tmp = conv_unmime_header(hp, NULL);
640 g_strconcat(p, ", ", tmp, NULL);
643 msginfo->cc = g_strdup(tmp);
647 if (msginfo->newsgroups) {
648 p = msginfo->newsgroups;
649 msginfo->newsgroups =
650 g_strconcat(p, ",", hp, NULL);
653 msginfo->newsgroups = g_strdup(hp);
656 if (msginfo->subject) break;
657 msginfo->subject = conv_unmime_header(hp, NULL);
658 replace_returns(msginfo->subject);
661 if (msginfo->msgid) break;
663 extract_parenthesis(hp, '<', '>');
665 msginfo->msgid = g_strdup(hp);
668 msginfo->references =
669 references_list_prepend(msginfo->references,
673 if (msginfo->inreplyto) break;
675 eliminate_parenthesis(hp, '(', ')');
676 if ((p = strrchr(hp, '<')) != NULL &&
677 strchr(p + 1, '>') != NULL) {
678 extract_parenthesis(p, '<', '>');
681 msginfo->inreplyto = g_strdup(p);
685 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
686 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
688 #ifdef ALLOW_HEADER_HINT
690 /* mnews Seen header */
691 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
695 if (msginfo->xface) break;
696 msginfo->xface = g_strdup(hp);
698 case H_DISPOSITION_NOTIFICATION_TO:
699 if (msginfo->dispositionnotificationto) break;
700 msginfo->dispositionnotificationto = g_strdup(hp);
702 case H_RETURN_RECEIPT_TO:
703 if (msginfo->returnreceiptto) break;
704 msginfo->returnreceiptto = g_strdup(hp);
706 case H_SC_PARTIALLY_RETRIEVED:
707 if (msginfo->partial_recv) break;
708 msginfo->partial_recv = g_strdup(hp);
710 case H_SC_ACCOUNT_SERVER:
711 if (msginfo->account_server) break;
712 msginfo->account_server = g_strdup(hp);
714 case H_SC_ACCOUNT_LOGIN:
715 if (msginfo->account_login) break;
716 msginfo->account_login = g_strdup(hp);
718 case H_SC_MESSAGE_SIZE:
719 if (msginfo->total_size) break;
720 msginfo->total_size = atoi(hp);
722 case H_SC_PLANNED_DOWNLOAD:
723 msginfo->planned_download = atoi(hp);
725 #ifdef ALLOW_HEADER_HINT
727 if (strchr(hp, 'R') != NULL)
728 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
729 if (strchr(hp, 'O') != NULL)
730 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
731 if (strchr(hp, 'U') != NULL)
732 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
735 if (strchr(hp, 'D') != NULL)
736 MSG_SET_PERM_FLAGS(msginfo->flags,
738 if (strchr(hp, 'F') != NULL)
739 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
740 if (strchr(hp, 'd') != NULL)
741 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
742 if (strchr(hp, 'r') != NULL)
743 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
744 if (strchr(hp, 'f') != NULL)
745 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
749 if (msginfo->fromspace) break;
750 msginfo->fromspace = g_strdup(hp);
757 if (!msginfo->inreplyto && msginfo->references)
759 g_strdup((gchar *)msginfo->references->data);
764 gchar *procheader_get_fromname(const gchar *str)
768 Xstrdup_a(tmp, str, return NULL);
771 extract_quote(tmp, '\"');
773 } else if (strchr(tmp, '<')) {
774 eliminate_parenthesis(tmp, '<', '>');
778 extract_parenthesis(tmp, '<', '>');
781 } else if (strchr(tmp, '(')) {
782 extract_parenthesis(tmp, '(', ')');
787 name = g_strdup(str);
789 name = g_strdup(tmp);
794 static gint procheader_scan_date_string(const gchar *str,
795 gchar *weekday, gint *day,
796 gchar *month, gint *year,
797 gint *hh, gint *mm, gint *ss,
802 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
803 weekday, day, month, year, hh, mm, ss, zone);
804 if (result == 8) return 0;
806 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
807 weekday, day, month, year, hh, mm, ss, zone);
808 if (result == 8) return 0;
810 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
811 day, month, year, hh, mm, ss, zone);
812 if (result == 7) return 0;
815 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
816 weekday, day, month, year, hh, mm, ss);
817 if (result == 7) return 0;
819 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
820 day, month, year, hh, mm, ss);
821 if (result == 6) return 0;
824 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
825 weekday, day, month, year, hh, mm, zone);
826 if (result == 7) return 0;
828 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
829 day, month, year, hh, mm, zone);
830 if (result == 6) return 0;
833 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
834 weekday, day, month, year, hh, mm);
835 if (result == 6) return 0;
837 result = sscanf(str, "%d %9s %d %2d:%2d",
838 day, month, year, hh, mm);
839 if (result == 5) return 0;
845 * Hiro, most UNIXen support this function:
846 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
848 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
850 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
862 memset(t, 0, sizeof *t);
864 if (procheader_scan_date_string(src, weekday, &day, month, &year,
865 &hh, &mm, &ss, zone) < 0) {
866 g_warning("Invalid date: %s\n", src);
870 /* Y2K compliant :) */
879 if ((p = strstr(monthstr, month)) != NULL)
880 dmonth = (gint)(p - monthstr) / 3 + 1;
882 g_warning("Invalid month: %s\n", month);
883 dmonth = G_DATE_BAD_MONTH;
890 t->tm_mon = dmonth - 1;
891 t->tm_year = year - 1900;
901 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
903 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
910 GDateMonth dmonth = G_DATE_BAD_MONTH;
916 if (procheader_scan_date_string(src, weekday, &day, month, &year,
917 &hh, &mm, &ss, zone) < 0) {
919 strncpy2(dest, src, len);
923 /* Y2K compliant :) */
932 for (p = monthstr; *p != '\0'; p += 3) {
933 if (!g_ascii_strncasecmp(p, month, 3)) {
934 dmonth = (gint)(p - monthstr) / 3 + 1;
943 t.tm_mon = dmonth - 1;
944 t.tm_year = year - 1900;
950 tz_offset = remote_tzoffset_sec(zone);
952 timer += tzoffset_sec(&timer) - tz_offset;
955 procheader_date_get_localtime(dest, len, timer);
960 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
963 gchar *default_format = "%y/%m/%d(%a) %H:%M";
965 const gchar *src_codeset, *dest_codeset;
967 lt = localtime(&timer);
969 if (prefs_common.date_format)
970 strftime(dest, len, prefs_common.date_format, lt);
972 strftime(dest, len, default_format, lt);
974 src_codeset = conv_get_locale_charset_str();
975 dest_codeset = CS_UTF_8;
976 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
978 g_snprintf(dest, len, "%s", str);
979 strncpy2(dest, str, len);
984 /* Added by Mel Hadasht on 27 Aug 2001 */
985 /* Get a header from msginfo */
986 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
990 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
991 { NULL, NULL, FALSE } };
994 hentry[0].name = header;
996 g_return_val_if_fail(msginfo != NULL, -1);
997 file = procmsg_get_message_file_path(msginfo);
998 if ((fp = g_fopen(file, "rb")) == NULL) {
999 FILE_OP_ERROR(file, "fopen");
1003 val = procheader_get_one_field(buf,len, fp, hentry);
1004 if (fclose(fp) == EOF) {
1005 FILE_OP_ERROR(file, "fclose");