2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 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 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
44 HeaderEntry *hp = NULL;
47 /* skip non-required headers */
50 if (fgets(buf, len, fp) == NULL)
52 if (buf[0] == '\r' || buf[0] == '\n')
54 } while (buf[0] == ' ' || buf[0] == '\t');
56 for (hp = hentry, hnum = 0; hp->name != NULL;
58 if (!strncasecmp(hp->name, buf,
63 } while (hp->name == NULL);
65 if (fgets(buf, len, fp) == NULL) return -1;
66 if (buf[0] == '\r' || buf[0] == '\n') return -1;
69 /* unfold the specified folded line */
70 if (hp && hp->unfold) {
71 gboolean folded = FALSE;
72 gchar *bufp = buf + strlen(buf);
78 if (nexthead == ' ' || nexthead == '\t')
80 else if (nexthead == EOF)
82 else if (folded == TRUE) {
83 /* concatenate next line */
84 if ((len - (bufp - buf)) <= 2) break;
86 /* replace return code on the tail end
91 if (nexthead == '\r' || nexthead == '\n') {
95 if (fgets(bufp, len - (bufp - buf), fp)
101 ungetc(nexthead, fp);
106 /* remove trailing return code */
113 nexthead = fgetc(fp);
114 if (nexthead == ' ' || nexthead == '\t') {
115 size_t buflen = strlen(buf);
117 /* concatenate next line */
118 if ((len - buflen) > 2) {
119 gchar *p = buf + buflen;
124 if (fgets(p, len - buflen, fp) == NULL)
130 ungetc(nexthead, fp);
135 /* remove trailing return code */
141 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
143 gboolean folded = FALSE;
147 if (fgets(buf, len, fp) == NULL) return NULL;
148 if (buf[0] == '\r' || buf[0] == '\n') return NULL;
149 bufp = buf + strlen(buf);
152 nexthead = fgetc(fp);
155 if (nexthead == ' ' || nexthead == '\t')
157 else if (nexthead == EOF)
159 else if (folded == TRUE) {
160 /* concatenate next line */
161 if ((len - (bufp - buf)) <= 2) break;
163 /* replace return code on the tail end
168 if (nexthead == '\r' || nexthead == '\n') {
172 if (fgets(bufp, len - (bufp - buf), fp)
174 bufp += strlen(bufp);
178 ungetc(nexthead, fp);
183 /* remove trailing return code */
189 GSList *procheader_get_header_list_from_file(const gchar *file)
194 if ((fp = fopen(file, "rb")) == NULL) {
195 FILE_OP_ERROR(file, "fopen");
199 hlist = procheader_get_header_list(fp);
205 GSList *procheader_get_header_list(FILE *fp)
207 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
209 GSList *hlist = NULL;
212 g_return_val_if_fail(fp != NULL, NULL);
214 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
215 if (header = procheader_parse_header(buf))
216 hlist = g_slist_append(hlist, header);
218 if (*buf == ':') continue;
219 for (p = buf; *p && *p != ' '; p++) {
221 header = g_new(Header, 1);
222 header->name = g_strndup(buf, p - buf);
224 while (*p == ' ' || *p == '\t') p++;
225 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
226 header->body = g_strdup(tmp);
228 hlist = g_slist_append(hlist, header);
238 GPtrArray *procheader_get_header_array(FILE *fp)
240 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
245 g_return_val_if_fail(fp != NULL, NULL);
247 headers = g_ptr_array_new();
249 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
250 if (header = procheader_parse_header(buf))
251 g_ptr_array_add(headers, header);
253 if (*buf == ':') continue;
254 for (p = buf; *p && *p != ' '; p++) {
256 header = g_new(Header, 1);
257 header->name = g_strndup(buf, p - buf);
259 while (*p == ' ' || *p == '\t') p++;
260 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
261 header->body = g_strdup(tmp);
263 g_ptr_array_add(headers, header);
273 GPtrArray *procheader_get_header_array_asis(FILE *fp)
275 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
280 g_return_val_if_fail(fp != NULL, NULL);
282 headers = g_ptr_array_new();
284 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
285 if (header = procheader_parse_header(buf))
286 g_ptr_array_add(headers, header);
288 if (*buf == ':') continue;
289 for (p = buf; *p && *p != ' '; p++) {
291 header = g_new(Header, 1);
292 header->name = g_strndup(buf, p - buf);
294 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
295 header->body = g_strdup(tmp);
297 g_ptr_array_add(headers, header);
307 void procheader_header_list_destroy(GSList *hlist)
311 while (hlist != NULL) {
312 header = hlist->data;
313 procheader_header_free(header);
314 hlist = g_slist_remove(hlist, header);
318 void procheader_header_array_destroy(GPtrArray *harray)
323 for (i = 0; i < harray->len; i++) {
324 header = g_ptr_array_index(harray, i);
325 procheader_header_free(header);
328 g_ptr_array_free(harray, TRUE);
331 void procheader_header_free(Header *header)
335 g_free(header->name);
336 g_free(header->body);
341 tests whether two headers' names are equal
342 remove the trailing ':' or ' ' before comparing
345 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
352 if (hdr1[len1 - 1] == ':')
354 if (hdr2[len2 - 1] == ':')
359 return (g_strncasecmp(hdr1, hdr2, len1) == 0);
363 parse headers, for example :
364 From: dinh@enseirb.fr becomes :
365 header->name = "From:"
366 header->body = "dinh@enseirb.fr"
369 Header * procheader_parse_header(gchar * buf)
375 if ((*buf == ':') || (*buf == ' '))
378 for (p = buf; *p ; p++) {
379 if ((*p == ':') || (*p == ' ')) {
380 header = g_new(Header, 1);
381 header->name = g_strndup(buf, p - buf + 1);
383 while (*p == ' ' || *p == '\t') p++;
384 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
385 header->body = g_strdup(tmp);
392 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
399 if (hentry == NULL) return;
401 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
405 p = buf + strlen(hp->name);
406 while (*p == ' ' || *p == '\t') p++;
408 if (hp->body == NULL)
409 hp->body = g_strdup(p);
410 else if (procheader_headername_equal(hp->name, "To") ||
411 procheader_headername_equal(hp->name, "Cc")) {
412 gchar *tp = hp->body;
413 hp->body = g_strconcat(tp, ", ", p, NULL);
436 H_DISPOSITION_NOTIFICATION_TO = 15,
437 H_RETURN_RECEIPT_TO = 16
440 MsgInfo *procheader_parse(const gchar *file, MsgFlags flags,
441 gboolean full, gboolean decrypted)
446 if ((fp = fopen(file, "rb")) == NULL) {
447 FILE_OP_ERROR(file, "fopen");
451 msginfo = procheader_file_parse(fp, flags, full, decrypted);
459 /* FIXME: we should not allow headers in messages to change the sylpheed marks
460 * so we're currently disabling setting any MsgFlags when detecting X-Seen,
461 * Seen, X-Status, Status. See macro ALLOW_HEADER_HINT */
463 /* #define ALLOW_HEADER_HINT */
465 MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
466 gboolean full, gboolean decrypted)
468 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
469 {"From:", NULL, TRUE},
472 {"Newsgroups:", NULL, TRUE},
473 {"Subject:", NULL, TRUE},
474 {"Message-Id:", NULL, FALSE},
475 {"References:", NULL, FALSE},
476 {"In-Reply-To:", NULL, FALSE},
477 {"Content-Type:", NULL, FALSE},
478 {"Seen:", NULL, FALSE},
479 {"Status:", NULL, FALSE},
480 {"X-Status:", NULL, FALSE},
481 {"From ", NULL, FALSE},
482 {"X-Face:", NULL, FALSE},
483 {"Disposition-Notification-To:", NULL, FALSE},
484 {"Return-Receipt-To:", NULL, FALSE},
485 {NULL, NULL, FALSE}};
487 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
488 {"From:", NULL, TRUE},
491 {"Newsgroups:", NULL, TRUE},
492 {"Subject:", NULL, TRUE},
493 {"Message-Id:", NULL, FALSE},
494 {"References:", NULL, FALSE},
495 {"In-Reply-To:", NULL, FALSE},
496 {"Content-Type:", NULL, FALSE},
497 {"Seen:", NULL, FALSE},
498 {"Status:", NULL, FALSE},
499 {"X-Status:", NULL, FALSE},
500 {"From ", NULL, FALSE},
501 {NULL, NULL, FALSE}};
504 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
505 gchar *reference = NULL;
511 hentry = full ? hentry_full : hentry_short;
513 if (MSG_IS_QUEUED(flags)) {
514 while (fgets(buf, sizeof(buf), fp) != NULL)
515 if (buf[0] == '\r' || buf[0] == '\n') break;
518 msginfo = g_new0(MsgInfo, 1);
520 if (flags.tmp_flags || flags.perm_flags)
521 msginfo->flags = flags;
523 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
526 MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MIME);
528 msginfo->inreplyto = NULL;
530 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
532 hp = buf + strlen(hentry[hnum].name);
534 while (*hp == ' ' || *hp == '\t') hp++;
538 if (msginfo->date) break;
540 procheader_date_parse(NULL, hp, 0);
541 msginfo->date = g_strdup(hp);
544 if (msginfo->from) break;
545 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
546 msginfo->from = g_strdup(tmp);
547 msginfo->fromname = procheader_get_fromname(tmp);
550 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
554 g_strconcat(p, ", ", tmp, NULL);
557 msginfo->to = g_strdup(tmp);
560 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
564 g_strconcat(p, ", ", tmp, NULL);
567 msginfo->cc = g_strdup(tmp);
570 if (msginfo->newsgroups) {
571 p = msginfo->newsgroups;
572 msginfo->newsgroups =
573 g_strconcat(p, ",", hp, NULL);
576 msginfo->newsgroups = g_strdup(buf + 12);
579 if (msginfo->subject) break;
580 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
581 msginfo->subject = g_strdup(tmp);
584 if (msginfo->msgid) break;
586 extract_parenthesis(hp, '<', '>');
588 msginfo->msgid = g_strdup(hp);
593 msginfo->references = g_strdup(hp);
594 eliminate_parenthesis(hp, '(', ')');
595 if ((p = strrchr(hp, '<')) != NULL &&
596 strchr(p + 1, '>') != NULL) {
597 extract_parenthesis(p, '<', '>');
600 reference = g_strdup(p);
606 if (!strncasecmp(hp, "multipart", 9) &&
607 strncasecmp(hp, "multipart/signed", 16)) {
608 MSG_SET_TMP_FLAGS(msginfo->flags,
612 else if (!strncasecmp(hp, "multipart/encrypted", 19)) {
613 MSG_SET_TMP_FLAGS(msginfo->flags,
616 else if (!strncasecmp(hp, "multipart", 9))
617 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
619 #ifdef ALLOW_HEADER_HINT
621 /* mnews Seen header */
622 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
626 if (msginfo->xface) break;
627 msginfo->xface = g_strdup(hp);
629 case H_DISPOSITION_NOTIFICATION_TO:
630 if (msginfo->dispositionnotificationto) break;
631 msginfo->dispositionnotificationto = g_strdup(hp);
632 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_RETRCPT_PENDING);
634 case H_RETURN_RECEIPT_TO:
635 if (msginfo->returnreceiptto) break;
636 msginfo->returnreceiptto = g_strdup(hp);
637 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_RETRCPT_PENDING);
639 #ifdef ALLOW_HEADER_HINT
641 if (strchr(hp, 'R') != NULL)
642 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
643 if (strchr(hp, 'O') != NULL)
644 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
645 if (strchr(hp, 'U') != NULL)
646 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
649 if (strchr(hp, 'D') != NULL)
650 MSG_SET_PERM_FLAGS(msginfo->flags,
652 if (strchr(hp, 'F') != NULL)
653 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
654 if (strchr(hp, 'd') != NULL)
655 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
656 if (strchr(hp, 'r') != NULL)
657 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
658 if (strchr(hp, 'f') != NULL)
659 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
663 if (msginfo->fromspace) break;
664 msginfo->fromspace = g_strdup(hp);
670 msginfo->inreplyto = reference;
675 gchar *procheader_get_fromname(const gchar *str)
679 Xstrdup_a(tmp, str, return NULL);
682 extract_quote(tmp, '\"');
684 } else if (strchr(tmp, '<')) {
685 eliminate_parenthesis(tmp, '<', '>');
689 extract_parenthesis(tmp, '<', '>');
692 } else if (strchr(tmp, '(')) {
693 extract_parenthesis(tmp, '(', ')');
698 name = g_strdup(str);
700 name = g_strdup(tmp);
705 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
707 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
720 /* parsing date field... */
721 result = sscanf(src, "%10s %d %9s %d %2d:%2d:%2d %5s",
722 weekday, &day, month, &year, &hh, &mm, &ss, zone);
724 result = sscanf(src, "%d %9s %d %2d:%2d:%2d %5s",
725 &day, month, &year, &hh, &mm, &ss, zone);
728 result = sscanf(src, "%10s %d %9s %d %2d:%2d %5s",
729 weekday, &day, month, &year, &hh, &mm, zone);
731 result = sscanf(src, "%d %9s %d %2d:%2d %5s",
732 &day, month, &year, &hh, &mm,
735 g_warning("Invalid date: %s\n", src);
737 strncpy2(dest, src, len);
744 /* Y2K compliant :) */
753 if ((p = strstr(monthstr, month)) != NULL)
754 dmonth = (gint)(p - monthstr) / 3 + 1;
756 g_warning("Invalid month: %s\n", month);
757 dmonth = G_DATE_BAD_MONTH;
764 t.tm_mon = dmonth - 1;
765 t.tm_year = year - 1900;
771 timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
774 procheader_date_get_localtime(dest, len, timer);
779 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
782 gchar *default_format = "%y/%m/%d(%a) %H:%M";
784 lt = localtime(&timer);
786 if (prefs_common.date_format)
787 strftime(dest, len, prefs_common.date_format, lt);
789 strftime(dest, len, default_format, lt);
792 gint get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len,gchar *header)
796 HeaderEntry hentry[]={{header,NULL,TRUE},
799 g_return_if_fail(msginfo != NULL);
800 file = procmsg_get_message_file_path(msginfo);
801 if ((fp = fopen(file, "rb")) == NULL) {
802 FILE_OP_ERROR(file, "fopen");
806 val=procheader_get_one_field(buf,len, fp, hentry);
807 if (fclose(fp) == EOF) {
808 FILE_OP_ERROR(file, "fclose");