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,
363 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
364 {"From:", NULL, TRUE},
367 {"Newsgroups:", NULL, TRUE},
368 {"Subject:", NULL, TRUE},
369 {"Message-ID:", NULL, FALSE},
370 {"References:", NULL, FALSE},
371 {"In-Reply-To:", NULL, FALSE},
372 {"Content-Type:", NULL, FALSE},
373 {"Seen:", NULL, FALSE},
374 {"Status:", NULL, FALSE},
375 {"X-Status:", NULL, FALSE},
376 {"From ", NULL, FALSE},
377 {"SC-Marked-For-Download:", NULL, FALSE},
378 {"SC-Message-Size:", NULL, FALSE},
379 {"Face:", NULL, FALSE},
380 {"X-Face:", NULL, FALSE},
381 {"Disposition-Notification-To:", NULL, FALSE},
382 {"Return-Receipt-To:", NULL, FALSE},
383 {"SC-Partially-Retrieved:", NULL, FALSE},
384 {"SC-Account-Server:", NULL, FALSE},
385 {"SC-Account-Login:",NULL, FALSE},
386 {NULL, NULL, FALSE}};
388 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
389 {"From:", NULL, TRUE},
392 {"Newsgroups:", NULL, TRUE},
393 {"Subject:", NULL, TRUE},
394 {"Message-ID:", NULL, FALSE},
395 {"References:", NULL, FALSE},
396 {"In-Reply-To:", NULL, FALSE},
397 {"Content-Type:", NULL, FALSE},
398 {"Seen:", NULL, FALSE},
399 {"Status:", NULL, FALSE},
400 {"X-Status:", NULL, FALSE},
401 {"From ", NULL, FALSE},
402 {"SC-Marked-For-Download:", NULL, FALSE},
403 {"SC-Message-Size:",NULL, FALSE},
404 {NULL, NULL, FALSE}};
406 HeaderEntry* procheader_get_headernames(gboolean full)
408 return full ? hentry_full : hentry_short;
411 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
414 return parse_stream(fp, FALSE, flags, full, decrypted);
417 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
418 gboolean full, gboolean decrypted)
426 get_one_field_func get_one_field =
427 isstring ? (get_one_field_func)string_get_one_field
428 : (get_one_field_func)procheader_get_one_field;
430 hentry = procheader_get_headernames(full);
432 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
433 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
437 msginfo = procmsg_msginfo_new();
439 if (flags.tmp_flags || flags.perm_flags)
440 msginfo->flags = flags;
442 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
444 msginfo->inreplyto = NULL;
446 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
448 hp = buf + strlen(hentry[hnum].name);
449 while (*hp == ' ' || *hp == '\t') hp++;
453 if (msginfo->date) break;
455 procheader_date_parse(NULL, hp, 0);
456 if (g_utf8_validate(hp, -1, NULL)) {
457 msginfo->date = g_strdup(hp);
459 gchar *utf = conv_codeset_strdup(
461 conv_get_locale_charset_str_no_utf8(),
464 !g_utf8_validate(utf, -1, NULL)) {
466 utf = g_malloc(strlen(buf)*2+1);
467 conv_localetodisp(utf,
474 if (msginfo->from) break;
475 msginfo->from = conv_unmime_header(hp, NULL);
476 msginfo->fromname = procheader_get_fromname(msginfo->from);
477 replace_returns(msginfo->from);
478 replace_returns(msginfo->fromname);
481 tmp = conv_unmime_header(hp, NULL);
485 g_strconcat(p, ", ", tmp, NULL);
488 msginfo->to = g_strdup(tmp);
492 tmp = conv_unmime_header(hp, NULL);
496 g_strconcat(p, ", ", tmp, NULL);
499 msginfo->cc = g_strdup(tmp);
503 if (msginfo->newsgroups) {
504 p = msginfo->newsgroups;
505 msginfo->newsgroups =
506 g_strconcat(p, ",", hp, NULL);
509 msginfo->newsgroups = g_strdup(hp);
512 if (msginfo->subject) break;
513 msginfo->subject = conv_unmime_header(hp, NULL);
514 replace_returns(msginfo->subject);
517 if (msginfo->msgid) break;
519 extract_parenthesis(hp, '<', '>');
521 msginfo->msgid = g_strdup(hp);
524 msginfo->references =
525 references_list_prepend(msginfo->references,
529 if (msginfo->inreplyto) break;
531 eliminate_parenthesis(hp, '(', ')');
532 if ((p = strrchr(hp, '<')) != NULL &&
533 strchr(p + 1, '>') != NULL) {
534 extract_parenthesis(p, '<', '>');
537 msginfo->inreplyto = g_strdup(p);
541 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
542 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
544 #ifdef ALLOW_HEADER_HINT
546 /* mnews Seen header */
547 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
551 if (msginfo->face) break;
552 msginfo->face = g_strdup(hp);
555 if (msginfo->xface) break;
556 msginfo->xface = g_strdup(hp);
558 case H_DISPOSITION_NOTIFICATION_TO:
559 if (msginfo->dispositionnotificationto) break;
560 msginfo->dispositionnotificationto = g_strdup(hp);
562 case H_RETURN_RECEIPT_TO:
563 if (msginfo->returnreceiptto) break;
564 msginfo->returnreceiptto = g_strdup(hp);
566 case H_SC_PARTIALLY_RETRIEVED:
567 if (msginfo->partial_recv) break;
568 msginfo->partial_recv = g_strdup(hp);
570 case H_SC_ACCOUNT_SERVER:
571 if (msginfo->account_server) break;
572 msginfo->account_server = g_strdup(hp);
574 case H_SC_ACCOUNT_LOGIN:
575 if (msginfo->account_login) break;
576 msginfo->account_login = g_strdup(hp);
578 case H_SC_MESSAGE_SIZE:
579 if (msginfo->total_size) break;
580 msginfo->total_size = atoi(hp);
582 case H_SC_PLANNED_DOWNLOAD:
583 msginfo->planned_download = atoi(hp);
585 #ifdef ALLOW_HEADER_HINT
587 if (strchr(hp, 'R') != NULL)
588 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
589 if (strchr(hp, 'O') != NULL)
590 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
591 if (strchr(hp, 'U') != NULL)
592 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
595 if (strchr(hp, 'D') != NULL)
596 MSG_SET_PERM_FLAGS(msginfo->flags,
598 if (strchr(hp, 'F') != NULL)
599 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
600 if (strchr(hp, 'd') != NULL)
601 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
602 if (strchr(hp, 'r') != NULL)
603 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
604 if (strchr(hp, 'f') != NULL)
605 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
609 if (msginfo->fromspace) break;
610 msginfo->fromspace = g_strdup(hp);
617 if (!msginfo->inreplyto && msginfo->references)
619 g_strdup((gchar *)msginfo->references->data);
624 gchar *procheader_get_fromname(const gchar *str)
628 Xstrdup_a(tmp, str, return NULL);
631 extract_quote(tmp, '\"');
633 } else if (strchr(tmp, '<')) {
634 eliminate_parenthesis(tmp, '<', '>');
638 extract_parenthesis(tmp, '<', '>');
641 } else if (strchr(tmp, '(')) {
642 extract_parenthesis(tmp, '(', ')');
647 name = g_strdup(str);
649 name = g_strdup(tmp);
654 static gint procheader_scan_date_string(const gchar *str,
655 gchar *weekday, gint *day,
656 gchar *month, gint *year,
657 gint *hh, gint *mm, gint *ss,
662 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
663 weekday, day, month, year, hh, mm, ss, zone);
664 if (result == 8) return 0;
666 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
667 weekday, day, month, year, hh, mm, ss, zone);
668 if (result == 8) return 0;
670 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
671 day, month, year, hh, mm, ss, zone);
672 if (result == 7) return 0;
675 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
676 weekday, day, month, year, hh, mm, ss);
677 if (result == 7) return 0;
679 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
680 day, month, year, hh, mm, ss);
681 if (result == 6) return 0;
684 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
685 weekday, day, month, year, hh, mm, zone);
686 if (result == 7) return 0;
688 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
689 day, month, year, hh, mm, zone);
690 if (result == 6) return 0;
693 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
694 weekday, day, month, year, hh, mm);
695 if (result == 6) return 0;
697 result = sscanf(str, "%d %9s %d %2d:%2d",
698 day, month, year, hh, mm);
699 if (result == 5) return 0;
705 * Hiro, most UNIXen support this function:
706 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
708 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
710 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
722 memset(t, 0, sizeof *t);
724 if (procheader_scan_date_string(src, weekday, &day, month, &year,
725 &hh, &mm, &ss, zone) < 0) {
726 g_warning("Invalid date: %s\n", src);
730 /* Y2K compliant :) */
739 if ((p = strstr(monthstr, month)) != NULL)
740 dmonth = (gint)(p - monthstr) / 3 + 1;
742 g_warning("Invalid month: %s\n", month);
743 dmonth = G_DATE_BAD_MONTH;
750 t->tm_mon = dmonth - 1;
751 t->tm_year = year - 1900;
761 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
763 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
770 GDateMonth dmonth = G_DATE_BAD_MONTH;
776 if (procheader_scan_date_string(src, weekday, &day, month, &year,
777 &hh, &mm, &ss, zone) < 0) {
779 strncpy2(dest, src, len);
783 /* Y2K compliant :) */
792 for (p = monthstr; *p != '\0'; p += 3) {
793 if (!g_ascii_strncasecmp(p, month, 3)) {
794 dmonth = (gint)(p - monthstr) / 3 + 1;
803 t.tm_mon = dmonth - 1;
804 t.tm_year = year - 1900;
810 tz_offset = remote_tzoffset_sec(zone);
812 timer += tzoffset_sec(&timer) - tz_offset;
815 procheader_date_get_localtime(dest, len, timer);
820 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
823 gchar *default_format = "%y/%m/%d(%a) %H:%M";
825 const gchar *src_codeset, *dest_codeset;
827 lt = localtime(&timer);
829 if (prefs_common.date_format)
830 strftime(dest, len, prefs_common.date_format, lt);
832 strftime(dest, len, default_format, lt);
834 src_codeset = conv_get_locale_charset_str();
835 dest_codeset = CS_UTF_8;
836 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
838 g_snprintf(dest, len, "%s", str);
839 strncpy2(dest, str, len);
844 /* Added by Mel Hadasht on 27 Aug 2001 */
845 /* Get a header from msginfo */
846 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
850 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
851 { NULL, NULL, FALSE } };
854 hentry[0].name = header;
856 g_return_val_if_fail(msginfo != NULL, -1);
857 file = procmsg_get_message_file_path(msginfo);
858 if ((fp = g_fopen(file, "rb")) == NULL) {
859 FILE_OP_ERROR(file, "fopen");
863 val = procheader_get_one_field(buf,len, fp, hentry);
864 if (fclose(fp) == EOF) {
865 FILE_OP_ERROR(file, "fclose");