2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2003 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.
31 #include "procheader.h"
34 #include "prefs_common.h"
39 typedef char *(*getlinefunc)(char *, int, void *);
40 typedef int (*peekcharfunc)(void *);
41 typedef int (*getcharfunc)(void *);
42 typedef gint (*get_one_field_func)(gchar *, gint, void *, HeaderEntry[]);
44 static gint string_get_one_field(gchar *buf, gint len, char **str,
45 HeaderEntry hentry[]);
47 static char *string_getline(char *buf, int len, char **str);
48 static int string_peekchar(char **str);
49 static int string_getchar(char **str);
50 static int file_peekchar(FILE *fp);
51 static int file_getchar(FILE *fp);
52 static gint generic_get_one_field(gchar *buf, gint len, void *data,
55 peekcharfunc peekchar,
57 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
58 gboolean full, gboolean decrypted);
61 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
64 return generic_get_one_field(buf, len, fp, hentry,
65 (getlinefunc)fgets, (peekcharfunc)file_peekchar,
66 (getcharfunc)file_getchar);
69 static gint string_get_one_field(gchar *buf, gint len, char **str,
72 return generic_get_one_field(buf, len, str, hentry,
73 (getlinefunc)string_getline,
74 (peekcharfunc)string_peekchar,
75 (getcharfunc)string_getchar);
78 static char *string_getline(char *buf, int len, char **str)
83 for (; **str && len > 1; --len)
84 if ((*buf++ = *(*str)++) == '\n')
91 static int string_peekchar(char **str)
96 static int string_getchar(char **str)
101 static int file_peekchar(FILE *fp)
103 return ungetc(getc(fp), fp);
106 static int file_getchar(FILE *fp)
111 static gint generic_get_one_field(gchar *buf, gint len, void *data,
113 getlinefunc getline, peekcharfunc peekchar,
114 getcharfunc getchar_)
118 HeaderEntry *hp = NULL;
120 if (hentry != NULL) {
121 /* skip non-required headers */
124 if (getline(buf, len, data) == NULL)
126 if (buf[0] == '\r' || buf[0] == '\n')
128 } while (buf[0] == ' ' || buf[0] == '\t');
130 for (hp = hentry, hnum = 0; hp->name != NULL;
132 if (!strncasecmp(hp->name, buf,
136 } while (hp->name == NULL);
138 if (getline(buf, len, data) == NULL) return -1;
139 if (buf[0] == '\r' || buf[0] == '\n') return -1;
142 /* remove trailing new line */
145 #define UNFOLD_LINE() \
146 (!hentry || (hp && hp->unfold))
150 nexthead = peekchar(data);
151 /* ([*WSP CRLF] 1*WSP) */
152 if (nexthead == ' ' || nexthead == '\t') {
153 size_t buflen = strlen(buf);
155 /* concatenate next line */
156 if ((len - buflen) > 2) {
157 if (getline(buf + buflen, len - buflen, data) == NULL)
159 /* trim trailing \n if requesting one header or
160 * unfolding was requested */
174 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
176 gboolean folded = FALSE;
180 if (fgets(buf, len, fp) == NULL) return NULL;
181 if (buf[0] == '\r' || buf[0] == '\n') return NULL;
182 bufp = buf + strlen(buf);
185 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
190 nexthead = fgetc(fp);
193 if (nexthead == ' ' || nexthead == '\t')
195 else if (nexthead == EOF)
197 else if (folded == TRUE) {
198 if (nexthead == '\r' || nexthead == '\n') {
203 if ((len - (bufp - buf)) <= 2) break;
205 /* replace return code on the tail end
211 /* concatenate next line */
212 if (fgets(bufp, len - (bufp - buf), fp)
214 bufp += strlen(bufp);
217 (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
223 ungetc(nexthead, fp);
228 /* remove trailing return code */
236 GSList *procheader_get_header_list_from_file(const gchar *file)
241 if ((fp = fopen(file, "rb")) == NULL) {
242 FILE_OP_ERROR(file, "fopen");
246 hlist = procheader_get_header_list(fp);
252 GSList *procheader_get_header_list(FILE *fp)
255 GSList *hlist = NULL;
258 g_return_val_if_fail(fp != NULL, NULL);
260 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
261 if ((header = procheader_parse_header(buf)) != NULL)
262 hlist = g_slist_append(hlist, header);
264 if (*buf == ':') continue;
265 for (p = buf; *p && *p != ' '; p++) {
267 header = g_new(Header, 1);
268 header->name = g_strndup(buf, p - buf);
270 while (*p == ' ' || *p == '\t') p++;
271 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
272 header->body = g_strdup(tmp);
274 hlist = g_slist_append(hlist, header);
286 GPtrArray *procheader_get_header_array(FILE *fp)
292 g_return_val_if_fail(fp != NULL, NULL);
294 headers = g_ptr_array_new();
296 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
297 if ((header = procheader_parse_header(buf)) != NULL)
298 g_ptr_array_add(headers, header);
300 if (*buf == ':') continue;
301 for (p = buf; *p && *p != ' '; p++) {
303 header = g_new(Header, 1);
304 header->name = g_strndup(buf, p - buf);
306 while (*p == ' ' || *p == '\t') p++;
307 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
308 header->body = g_strdup(tmp);
310 g_ptr_array_add(headers, header);
321 GPtrArray *procheader_get_header_array_asis(FILE *fp)
327 g_return_val_if_fail(fp != NULL, NULL);
329 headers = g_ptr_array_new();
331 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
332 if ((header = procheader_parse_header(buf)) != NULL)
333 g_ptr_array_add(headers, header);
335 if (*buf == ':') continue;
336 for (p = buf; *p && *p != ' '; p++) {
338 header = g_new(Header, 1);
339 header->name = g_strndup(buf, p - buf);
341 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
342 header->body = g_strdup(tmp);
344 g_ptr_array_add(headers, header);
355 void procheader_header_list_destroy(GSList *hlist)
359 while (hlist != NULL) {
360 header = hlist->data;
361 procheader_header_free(header);
362 hlist = g_slist_remove(hlist, header);
367 void procheader_header_array_destroy(GPtrArray *harray)
372 for (i = 0; i < harray->len; i++) {
373 header = g_ptr_array_index(harray, i);
374 procheader_header_free(header);
377 g_ptr_array_free(harray, TRUE);
380 void procheader_header_free(Header *header)
384 g_free(header->name);
385 g_free(header->body);
390 tests whether two headers' names are equal
391 remove the trailing ':' or ' ' before comparing
394 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
401 if (hdr1[len1 - 1] == ':')
403 if (hdr2[len2 - 1] == ':')
408 return (g_strncasecmp(hdr1, hdr2, len1) == 0);
412 parse headers, for example :
413 From: dinh@enseirb.fr becomes :
414 header->name = "From:"
415 header->body = "dinh@enseirb.fr"
418 Header * procheader_parse_header(gchar * buf)
424 if ((*buf == ':') || (*buf == ' '))
427 for (p = buf; *p ; p++) {
428 if ((*p == ':') || (*p == ' ')) {
429 header = g_new(Header, 1);
430 header->name = g_strndup(buf, p - buf + 1);
432 while (*p == ' ' || *p == '\t') p++;
433 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
435 header->body = g_strdup(p);
437 header->body = g_strdup(tmp);
444 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
451 if (hentry == NULL) return;
453 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
457 p = buf + strlen(hp->name);
458 while (*p == ' ' || *p == '\t') p++;
460 if (hp->body == NULL)
461 hp->body = g_strdup(p);
462 else if (procheader_headername_equal(hp->name, "To") ||
463 procheader_headername_equal(hp->name, "Cc")) {
464 gchar *tp = hp->body;
465 hp->body = g_strconcat(tp, ", ", p, NULL);
471 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
472 gboolean full, gboolean decrypted)
477 if ((fp = fopen(file, "rb")) == NULL) {
478 FILE_OP_ERROR(file, "fopen");
482 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
487 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
490 return parse_stream(&str, TRUE, flags, full, decrypted);
510 H_DISPOSITION_NOTIFICATION_TO = 15,
511 H_RETURN_RECEIPT_TO = 16
514 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
515 {"From:", NULL, TRUE},
518 {"Newsgroups:", NULL, TRUE},
519 {"Subject:", NULL, TRUE},
520 {"Message-Id:", NULL, FALSE},
521 {"References:", NULL, FALSE},
522 {"In-Reply-To:", NULL, FALSE},
523 {"Content-Type:", NULL, FALSE},
524 {"Seen:", NULL, FALSE},
525 {"Status:", NULL, FALSE},
526 {"X-Status:", NULL, FALSE},
527 {"From ", NULL, FALSE},
528 {"X-Face:", NULL, FALSE},
529 {"Disposition-Notification-To:", NULL, FALSE},
530 {"Return-Receipt-To:", NULL, FALSE},
531 {NULL, NULL, FALSE}};
533 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
534 {"From:", NULL, TRUE},
537 {"Newsgroups:", NULL, TRUE},
538 {"Subject:", NULL, TRUE},
539 {"Message-Id:", NULL, FALSE},
540 {"References:", NULL, FALSE},
541 {"In-Reply-To:", NULL, FALSE},
542 {"Content-Type:", NULL, FALSE},
543 {"Seen:", NULL, FALSE},
544 {"Status:", NULL, FALSE},
545 {"X-Status:", NULL, FALSE},
546 {"From ", NULL, FALSE},
547 {NULL, NULL, FALSE}};
549 HeaderEntry* procheader_get_headernames(gboolean full)
551 return full ? hentry_full : hentry_short;
554 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
557 return parse_stream(fp, FALSE, flags, full, decrypted);
560 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
561 gboolean full, gboolean decrypted)
564 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
565 gchar *reference = NULL;
570 get_one_field_func get_one_field =
571 isstring ? (get_one_field_func)string_get_one_field
572 : (get_one_field_func)procheader_get_one_field;
574 hentry = procheader_get_headernames(full);
576 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
577 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
581 msginfo = procmsg_msginfo_new();
583 if (flags.tmp_flags || flags.perm_flags)
584 msginfo->flags = flags;
586 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
589 MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MIME);
591 msginfo->inreplyto = NULL;
593 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
595 hp = buf + strlen(hentry[hnum].name);
596 while (*hp == ' ' || *hp == '\t') hp++;
600 if (msginfo->date) break;
602 procheader_date_parse(NULL, hp, 0);
603 msginfo->date = g_strdup(hp);
606 if (msginfo->from) break;
607 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
608 msginfo->from = g_strdup(tmp);
609 msginfo->fromname = procheader_get_fromname(tmp);
612 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
616 g_strconcat(p, ", ", tmp, NULL);
619 msginfo->to = g_strdup(tmp);
622 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
626 g_strconcat(p, ", ", tmp, NULL);
629 msginfo->cc = g_strdup(tmp);
632 if (msginfo->newsgroups) {
633 p = msginfo->newsgroups;
634 msginfo->newsgroups =
635 g_strconcat(p, ",", hp, NULL);
638 msginfo->newsgroups = g_strdup(hp);
641 if (msginfo->subject) break;
642 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
643 msginfo->subject = g_strdup(tmp);
646 if (msginfo->msgid) break;
648 extract_parenthesis(hp, '<', '>');
650 msginfo->msgid = g_strdup(hp);
655 msginfo->references = g_strdup(hp);
656 eliminate_parenthesis(hp, '(', ')');
657 if ((p = strrchr(hp, '<')) != NULL &&
658 strchr(p + 1, '>') != NULL) {
659 extract_parenthesis(p, '<', '>');
662 reference = g_strdup(p);
668 if (!strncasecmp(hp, "multipart", 9)) {
669 if (strncasecmp(hp, "multipart/signed", 16)) {
670 MSG_SET_TMP_FLAGS(msginfo->flags,
673 MSG_SET_TMP_FLAGS(msginfo->flags,
678 else if (!strncasecmp(hp, "multipart/encrypted", 19)) {
679 MSG_SET_TMP_FLAGS(msginfo->flags,
682 else if (!strncasecmp(hp, "multipart", 9) &&
683 !strncasecmp(hp, "multipart/signed", 16)) {
684 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_SIGNED);
686 else if (!strncasecmp(hp, "multipart", 9))
687 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
689 #ifdef ALLOW_HEADER_HINT
691 /* mnews Seen header */
692 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
696 if (msginfo->xface) break;
697 msginfo->xface = g_strdup(hp);
699 case H_DISPOSITION_NOTIFICATION_TO:
700 if (msginfo->dispositionnotificationto) break;
701 msginfo->dispositionnotificationto = g_strdup(hp);
703 case H_RETURN_RECEIPT_TO:
704 if (msginfo->returnreceiptto) break;
705 msginfo->returnreceiptto = g_strdup(hp);
707 #ifdef ALLOW_HEADER_HINT
709 if (strchr(hp, 'R') != NULL)
710 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
711 if (strchr(hp, 'O') != NULL)
712 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
713 if (strchr(hp, 'U') != NULL)
714 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
717 if (strchr(hp, 'D') != NULL)
718 MSG_SET_PERM_FLAGS(msginfo->flags,
720 if (strchr(hp, 'F') != NULL)
721 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
722 if (strchr(hp, 'd') != NULL)
723 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
724 if (strchr(hp, 'r') != NULL)
725 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
726 if (strchr(hp, 'f') != NULL)
727 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
731 if (msginfo->fromspace) break;
732 msginfo->fromspace = g_strdup(hp);
738 msginfo->inreplyto = reference;
743 gchar *procheader_get_fromname(const gchar *str)
747 Xstrdup_a(tmp, str, return NULL);
750 extract_quote(tmp, '\"');
752 } else if (strchr(tmp, '<')) {
753 eliminate_parenthesis(tmp, '<', '>');
757 extract_parenthesis(tmp, '<', '>');
760 } else if (strchr(tmp, '(')) {
761 extract_parenthesis(tmp, '(', ')');
766 name = g_strdup(str);
768 name = g_strdup(tmp);
773 static gint procheader_scan_date_string(const gchar *str,
774 gchar *weekday, gint *day,
775 gchar *month, gint *year,
776 gint *hh, gint *mm, gint *ss,
781 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
782 weekday, day, month, year, hh, mm, ss, zone);
783 if (result == 8) return 0;
785 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
786 weekday, day, month, year, hh, mm, ss, zone);
787 if (result == 8) return 0;
789 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
790 day, month, year, hh, mm, ss, zone);
791 if (result == 7) return 0;
794 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
795 weekday, day, month, year, hh, mm, ss);
796 if (result == 7) return 0;
799 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
800 weekday, day, month, year, hh, mm, zone);
801 if (result == 7) return 0;
803 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
804 day, month, year, hh, mm, zone);
805 if (result == 6) return 0;
811 * Hiro, most UNIXen support this function:
812 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
814 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
816 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
828 memset(t, 0, sizeof *t);
830 if (procheader_scan_date_string(src, weekday, &day, month, &year,
831 &hh, &mm, &ss, zone) < 0) {
832 g_warning("Invalid date: %s\n", src);
836 /* Y2K compliant :) */
845 if ((p = strstr(monthstr, month)) != NULL)
846 dmonth = (gint)(p - monthstr) / 3 + 1;
848 g_warning("Invalid month: %s\n", month);
849 dmonth = G_DATE_BAD_MONTH;
856 t->tm_mon = dmonth - 1;
857 t->tm_year = year - 1900;
867 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
869 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
876 GDateMonth dmonth = G_DATE_BAD_MONTH;
882 if (procheader_scan_date_string(src, weekday, &day, month, &year,
883 &hh, &mm, &ss, zone) < 0) {
884 g_warning("Invalid date: %s\n", src);
886 strncpy2(dest, src, len);
890 /* Y2K compliant :) */
899 for (p = monthstr; *p != '\0'; p += 3) {
900 if (!strncasecmp(p, month, 3)) {
901 dmonth = (gint)(p - monthstr) / 3 + 1;
906 g_warning("Invalid month: %s\n", month);
912 t.tm_mon = dmonth - 1;
913 t.tm_year = year - 1900;
919 tz_offset = remote_tzoffset_sec(zone);
921 timer += tzoffset_sec(&timer) - tz_offset;
924 procheader_date_get_localtime(dest, len, timer);
929 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
932 gchar *default_format = "%y/%m/%d(%a) %H:%M";
934 lt = localtime(&timer);
936 if (prefs_common.date_format)
937 strftime(dest, len, prefs_common.date_format, lt);
939 strftime(dest, len, default_format, lt);
942 /* Added by Mel Hadasht on 27 Aug 2001 */
943 /* Get a header from msginfo */
944 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
948 HeaderEntry hentry[]={ { header, NULL, TRUE },
949 { NULL, NULL, FALSE } };
952 g_return_val_if_fail(msginfo != NULL, -1);
953 file = procmsg_get_message_file_path(msginfo);
954 if ((fp = fopen(file, "rb")) == NULL) {
955 FILE_OP_ERROR(file, "fopen");
959 val = procheader_get_one_field(buf,len, fp, hentry);
960 if (fclose(fp) == EOF) {
961 FILE_OP_ERROR(file, "fclose");