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, "r")) == 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, gboolean full)
445 if ((fp = fopen(file, "r")) == NULL) {
446 FILE_OP_ERROR(file, "fopen");
450 msginfo = procheader_file_parse(fp, flags, full);
457 MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
460 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
461 {"From:", NULL, TRUE},
464 {"Newsgroups:", NULL, TRUE},
465 {"Subject:", NULL, TRUE},
466 {"Message-Id:", NULL, FALSE},
467 {"References:", NULL, FALSE},
468 {"In-Reply-To:", NULL, FALSE},
469 {"Content-Type:", NULL, FALSE},
470 {"Seen:", NULL, FALSE},
471 {"Status:", NULL, FALSE},
472 {"X-Status:", NULL, FALSE},
473 {"From ", NULL, FALSE},
474 {"X-Face:", NULL, FALSE},
475 {"Disposition-Notification-To:", NULL, FALSE},
476 {"Return-Receipt-To:", NULL, FALSE},
477 {NULL, NULL, FALSE}};
479 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
480 {"From:", NULL, TRUE},
483 {"Newsgroups:", NULL, TRUE},
484 {"Subject:", NULL, TRUE},
485 {"Message-Id:", NULL, FALSE},
486 {"References:", NULL, FALSE},
487 {"In-Reply-To:", NULL, FALSE},
488 {"Content-Type:", NULL, FALSE},
489 {"Seen:", NULL, FALSE},
490 {"Status:", NULL, FALSE},
491 {"X-Status:", NULL, FALSE},
492 {"From ", NULL, FALSE},
493 {NULL, NULL, FALSE}};
496 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
497 gchar *reference = NULL;
503 hentry = full ? hentry_full : hentry_short;
505 if (MSG_IS_QUEUED(flags)) {
506 while (fgets(buf, sizeof(buf), fp) != NULL)
507 if (buf[0] == '\r' || buf[0] == '\n') break;
510 msginfo = g_new0(MsgInfo, 1);
511 msginfo->flags = flags != 0 ? flags : MSG_NEW|MSG_UNREAD;
512 msginfo->inreplyto = NULL;
514 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
516 hp = buf + strlen(hentry[hnum].name);
518 while (*hp == ' ' || *hp == '\t') hp++;
522 if (msginfo->date) break;
524 procheader_date_parse(NULL, hp, 0);
525 msginfo->date = g_strdup(hp);
528 if (msginfo->from) break;
529 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
530 msginfo->from = g_strdup(tmp);
531 msginfo->fromname = procheader_get_fromname(tmp);
534 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
538 g_strconcat(p, ", ", tmp, NULL);
541 msginfo->to = g_strdup(tmp);
544 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
548 g_strconcat(p, ", ", tmp, NULL);
551 msginfo->cc = g_strdup(tmp);
554 if (msginfo->newsgroups) {
555 p = msginfo->newsgroups;
556 msginfo->newsgroups =
557 g_strconcat(p, ",", hp, NULL);
560 msginfo->newsgroups = g_strdup(buf + 12);
563 if (msginfo->subject) break;
564 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
565 msginfo->subject = g_strdup(tmp);
568 if (msginfo->msgid) break;
570 extract_parenthesis(hp, '<', '>');
572 msginfo->msgid = g_strdup(hp);
576 msginfo->references = g_strdup(hp);
577 eliminate_parenthesis(hp, '(', ')');
578 if ((p = strrchr(hp, '<')) != NULL &&
579 strchr(p + 1, '>') != NULL) {
580 extract_parenthesis(p, '<', '>');
583 reference = g_strdup(p);
589 eliminate_parenthesis(hp, '(', ')');
590 extract_parenthesis(hp, '<', '>');
593 reference = g_strdup(hp);
597 if (!strncasecmp(hp, "multipart", 9))
598 msginfo->flags |= MSG_MIME;
601 /* mnews Seen header */
602 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
605 if (msginfo->xface) break;
606 msginfo->xface = g_strdup(hp);
608 case H_DISPOSITION_NOTIFICATION_TO:
609 if (msginfo->dispositionnotificationto) break;
610 msginfo->dispositionnotificationto = g_strdup(hp);
612 case H_RETURN_RECEIPT_TO:
613 if (msginfo->returnreceiptto) break;
614 msginfo->returnreceiptto = g_strdup(hp);
617 if (strchr(hp, 'R') != NULL)
618 MSG_UNSET_FLAGS(msginfo->flags, MSG_UNREAD);
619 if (strchr(hp, 'O') != NULL)
620 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW);
621 if (strchr(hp, 'U') != NULL)
622 MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
625 if (strchr(hp, 'D') != NULL)
626 MSG_SET_FLAGS(msginfo->flags,
628 if (strchr(hp, 'F') != NULL)
629 MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
630 if (strchr(hp, 'd') != NULL)
631 MSG_SET_FLAGS(msginfo->flags, MSG_DELETED);
632 if (strchr(hp, 'r') != NULL)
633 MSG_SET_FLAGS(msginfo->flags, MSG_REPLIED);
634 if (strchr(hp, 'f') != NULL)
635 MSG_SET_FLAGS(msginfo->flags, MSG_FORWARDED);
638 if (msginfo->fromspace) break;
639 msginfo->fromspace = g_strdup(hp);
644 msginfo->inreplyto = reference;
649 gchar *procheader_get_fromname(const gchar *str)
653 Xalloca(tmp, strlen(str) + 1, return NULL);
657 extract_quote(tmp, '\"');
659 } else if (strchr(tmp, '<')) {
660 eliminate_parenthesis(tmp, '<', '>');
664 extract_parenthesis(tmp, '<', '>');
667 } else if (strchr(tmp, '(')) {
668 extract_parenthesis(tmp, '(', ')');
673 name = g_strdup(str);
675 name = g_strdup(tmp);
680 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
682 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
695 /* parsing date field... */
696 result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
697 weekday, &day, month, &year, &hh, &mm, &ss, zone);
699 result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
700 &day, month, &year, &hh, &mm, &ss, zone);
703 result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
704 weekday, &day, month, &year, &hh, &mm, zone);
706 result = sscanf(src, "%d %3s %d %2d:%2d %5s",
707 &day, month, &year, &hh, &mm,
710 g_warning("Invalid date: %s\n", src);
712 strncpy2(dest, src, len);
719 /* Y2K compliant :) */
727 if ((p = strstr(monthstr, month)) != NULL)
728 dmonth = (gint)(p - monthstr) / 3 + 1;
730 g_warning("Invalid month: %s\n", month);
731 dmonth = G_DATE_BAD_MONTH;
738 t.tm_mon = dmonth - 1;
739 t.tm_year = year - 1900;
745 timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
748 procheader_date_get_localtime(dest, len, timer);
753 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
756 gchar *default_format = "%y/%m/%d(%a) %H:%M";
758 lt = localtime(&timer);
760 if (prefs_common.date_format)
761 strftime(dest, len, prefs_common.date_format, lt);
763 strftime(dest, len, default_format, lt);