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,
62 } while (hp->name == NULL);
64 if (fgets(buf, len, fp) == NULL) return -1;
65 if (buf[0] == '\r' || buf[0] == '\n') return -1;
68 /* unfold the specified folded line */
69 if (hp && hp->unfold) {
70 gboolean folded = FALSE;
71 gchar *bufp = buf + strlen(buf);
77 if (nexthead == ' ' || nexthead == '\t')
79 else if (nexthead == EOF)
81 else if (folded == TRUE) {
82 /* concatenate next line */
83 if ((len - (bufp - buf)) <= 2) break;
85 /* replace return code on the tail end
90 if (nexthead == '\r' || nexthead == '\n') {
94 if (fgets(bufp, len - (bufp - buf), fp)
100 ungetc(nexthead, fp);
105 /* remove trailing return code */
112 nexthead = fgetc(fp);
113 if (nexthead == ' ' || nexthead == '\t') {
114 size_t buflen = strlen(buf);
116 /* concatenate next line */
117 if ((len - buflen) > 2) {
118 gchar *p = buf + buflen;
123 if (fgets(p, len - buflen, fp) == NULL)
129 ungetc(nexthead, fp);
134 /* remove trailing return code */
140 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
142 gboolean folded = FALSE;
146 if (fgets(buf, len, fp) == NULL) return NULL;
147 if (buf[0] == '\r' || buf[0] == '\n') return NULL;
148 bufp = buf + strlen(buf);
151 nexthead = fgetc(fp);
154 if (nexthead == ' ' || nexthead == '\t')
156 else if (nexthead == EOF)
158 else if (folded == TRUE) {
159 /* concatenate next line */
160 if ((len - (bufp - buf)) <= 2) break;
162 /* replace return code on the tail end
167 if (nexthead == '\r' || nexthead == '\n') {
171 if (fgets(bufp, len - (bufp - buf), fp)
173 bufp += strlen(bufp);
177 ungetc(nexthead, fp);
182 /* remove trailing return code */
188 GSList *procheader_get_header_list_from_file(const gchar *file)
193 if ((fp = fopen(file, "r")) == NULL) {
194 FILE_OP_ERROR(file, "fopen");
198 hlist = procheader_get_header_list(fp);
204 GSList *procheader_get_header_list(FILE *fp)
206 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
208 GSList *hlist = NULL;
211 g_return_val_if_fail(fp != NULL, NULL);
213 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
214 if (header = procheader_parse_header(buf))
215 hlist = g_slist_append(hlist, header);
217 if (*buf == ':') continue;
218 for (p = buf; *p && *p != ' '; p++) {
220 header = g_new(Header, 1);
221 header->name = g_strndup(buf, p - buf);
223 while (*p == ' ' || *p == '\t') p++;
224 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
225 header->body = g_strdup(tmp);
227 hlist = g_slist_append(hlist, header);
237 GPtrArray *procheader_get_header_array(FILE *fp)
239 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
244 g_return_val_if_fail(fp != NULL, NULL);
246 headers = g_ptr_array_new();
248 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
249 if (header = procheader_parse_header(buf))
250 g_ptr_array_add(headers, header);
252 if (*buf == ':') continue;
253 for (p = buf; *p && *p != ' '; p++) {
255 header = g_new(Header, 1);
256 header->name = g_strndup(buf, p - buf);
258 while (*p == ' ' || *p == '\t') p++;
259 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
260 header->body = g_strdup(tmp);
262 g_ptr_array_add(headers, header);
272 GPtrArray *procheader_get_header_array_asis(FILE *fp)
274 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
279 g_return_val_if_fail(fp != NULL, NULL);
281 headers = g_ptr_array_new();
283 while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
284 if (header = procheader_parse_header(buf))
285 g_ptr_array_add(headers, header);
287 if (*buf == ':') continue;
288 for (p = buf; *p && *p != ' '; p++) {
290 header = g_new(Header, 1);
291 header->name = g_strndup(buf, p - buf);
293 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
294 header->body = g_strdup(tmp);
296 g_ptr_array_add(headers, header);
306 void procheader_header_list_destroy(GSList *hlist)
310 while (hlist != NULL) {
311 header = hlist->data;
312 procheader_header_free(header);
313 hlist = g_slist_remove(hlist, header);
317 void procheader_header_array_destroy(GPtrArray *harray)
322 for (i = 0; i < harray->len; i++) {
323 header = g_ptr_array_index(harray, i);
324 procheader_header_free(header);
327 g_ptr_array_free(harray, TRUE);
330 void procheader_header_free(Header *header)
334 g_free(header->name);
335 g_free(header->body);
340 tests whether two headers' names are equal
341 remove the trailing ':' or ' ' before comparing
344 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
351 if (hdr1[len1 - 1] == ':')
353 if (hdr2[len2 - 1] == ':')
358 return (g_strncasecmp(hdr1, hdr2, len1) == 0);
362 parse headers, for example :
363 From: dinh@enseirb.fr becomes :
364 header->name = "From:"
365 header->body = "dinh@enseirb.fr"
368 Header * procheader_parse_header(gchar * buf)
374 if ((*buf == ':') || (*buf == ' '))
377 for (p = buf; *p ; p++) {
378 if ((*p == ':') || (*p == ' ')) {
379 header = g_new(Header, 1);
380 header->name = g_strndup(buf, p - buf + 1);
382 while (*p == ' ' || *p == '\t') p++;
383 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
384 header->body = g_strdup(tmp);
391 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
398 if (hentry == NULL) return;
400 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
404 p = buf + strlen(hp->name);
405 while (*p == ' ' || *p == '\t') p++;
407 if (hp->body == NULL)
408 hp->body = g_strdup(p);
409 else if (procheader_headername_equal(hp->name, "To") ||
410 procheader_headername_equal(hp->name, "Cc")) {
411 gchar *tp = hp->body;
412 hp->body = g_strconcat(tp, ", ", p, NULL);
435 H_DISPOSITION_NOTIFICATION_TO = 15,
436 H_RETURN_RECEIPT_TO = 16
439 MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
444 if ((fp = fopen(file, "r")) == NULL) {
445 FILE_OP_ERROR(file, "fopen");
449 msginfo = procheader_file_parse(fp, flags, full);
456 MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
459 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
460 {"From:", NULL, TRUE},
463 {"Newsgroups:", NULL, TRUE},
464 {"Subject:", NULL, TRUE},
465 {"Message-Id:", NULL, FALSE},
466 {"References:", NULL, FALSE},
467 {"In-Reply-To:", NULL, FALSE},
468 {"Content-Type:", NULL, FALSE},
469 {"Seen:", NULL, FALSE},
470 {"Status:", NULL, FALSE},
471 {"X-Status:", NULL, FALSE},
472 {"X-Face:", NULL, FALSE},
473 {"From ", NULL, FALSE},
474 {"Disposition-Notification-To:", NULL, FALSE},
475 {"Return-Receipt-To:", NULL, FALSE},
476 {NULL, NULL, FALSE}};
478 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
479 {"From:", NULL, TRUE},
482 {"Newsgroups:", NULL, TRUE},
483 {"Subject:", NULL, TRUE},
484 {"Message-Id:", NULL, FALSE},
485 {"References:", NULL, FALSE},
486 {"In-Reply-To:", NULL, FALSE},
487 {"Content-Type:", NULL, FALSE},
488 {"Seen:", NULL, FALSE},
489 {"Status:", NULL, FALSE},
490 {"X-Status:", NULL, FALSE},
491 {"From ", NULL, FALSE},
492 {NULL, NULL, FALSE}};
495 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
496 gchar *reference = NULL;
502 hentry = full ? hentry_full : hentry_short;
504 if (MSG_IS_QUEUED(flags)) {
505 while (fgets(buf, sizeof(buf), fp) != NULL)
506 if (buf[0] == '\r' || buf[0] == '\n') break;
509 msginfo = g_new0(MsgInfo, 1);
510 msginfo->flags = flags != 0 ? flags : MSG_NEW|MSG_UNREAD;
511 msginfo->inreplyto = NULL;
513 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
515 hp = buf + strlen(hentry[hnum].name);
517 while (*hp == ' ' || *hp == '\t') hp++;
521 if (msginfo->date) break;
523 procheader_date_parse(NULL, hp, 0);
524 msginfo->date = g_strdup(hp);
527 if (msginfo->from) break;
528 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
529 msginfo->from = g_strdup(tmp);
530 msginfo->fromname = procheader_get_fromname(tmp);
533 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
537 g_strconcat(p, ", ", tmp, NULL);
540 msginfo->to = g_strdup(tmp);
543 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
547 g_strconcat(p, ", ", tmp, NULL);
550 msginfo->cc = g_strdup(tmp);
553 if (msginfo->newsgroups) {
554 p = msginfo->newsgroups;
555 msginfo->newsgroups =
556 g_strconcat(p, ",", hp, NULL);
559 msginfo->newsgroups = g_strdup(buf + 12);
562 if (msginfo->subject) break;
563 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
564 msginfo->subject = g_strdup(tmp);
567 if (msginfo->msgid) break;
569 extract_parenthesis(hp, '<', '>');
571 msginfo->msgid = g_strdup(hp);
575 msginfo->references = g_strdup(hp);
576 eliminate_parenthesis(hp, '(', ')');
577 if ((p = strrchr(hp, '<')) != NULL &&
578 strchr(p + 1, '>') != NULL) {
579 extract_parenthesis(p, '<', '>');
582 reference = g_strdup(p);
588 eliminate_parenthesis(hp, '(', ')');
589 extract_parenthesis(hp, '<', '>');
592 reference = g_strdup(hp);
596 if (!strncasecmp(hp, "multipart", 9))
597 msginfo->flags |= MSG_MIME;
600 /* mnews Seen header */
601 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
604 if (msginfo->xface) break;
605 msginfo->xface = g_strdup(hp);
607 case H_DISPOSITION_NOTIFICATION_TO:
608 if (msginfo->dispositionnotificationto) break;
609 msginfo->dispositionnotificationto = g_strdup(hp);
611 case H_RETURN_RECEIPT_TO:
612 if (msginfo->returnreceiptto) break;
613 msginfo->returnreceiptto = g_strdup(hp);
616 if (strchr(hp, 'R') != NULL)
617 MSG_UNSET_FLAGS(msginfo->flags, MSG_UNREAD);
618 if (strchr(hp, 'O') != NULL)
619 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW);
620 if (strchr(hp, 'U') != NULL)
621 MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
624 if (strchr(hp, 'X') != NULL)
625 MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
628 if (msginfo->fromspace) break;
629 msginfo->fromspace = g_strdup(hp);
634 msginfo->inreplyto = reference;
639 gchar *procheader_get_fromname(const gchar *str)
643 Xalloca(tmp, strlen(str) + 1, return NULL);
647 extract_quote(tmp, '\"');
649 } else if (strchr(tmp, '<')) {
650 eliminate_parenthesis(tmp, '<', '>');
654 extract_parenthesis(tmp, '<', '>');
657 } else if (strchr(tmp, '(')) {
658 extract_parenthesis(tmp, '(', ')');
663 name = g_strdup(str);
665 name = g_strdup(tmp);
670 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
672 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
685 /* parsing date field... */
686 result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
687 weekday, &day, month, &year, &hh, &mm, &ss, zone);
689 result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
690 &day, month, &year, &hh, &mm, &ss, zone);
693 result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
694 weekday, &day, month, &year, &hh, &mm, zone);
696 result = sscanf(src, "%d %3s %d %2d:%2d %5s",
697 &day, month, &year, &hh, &mm,
700 g_warning("Invalid date: %s\n", src);
702 strncpy2(dest, src, len);
709 /* Y2K compliant :) */
717 if ((p = strstr(monthstr, month)) != NULL)
718 dmonth = (gint)(p - monthstr) / 3 + 1;
720 g_warning("Invalid month: %s\n", month);
721 dmonth = G_DATE_BAD_MONTH;
728 t.tm_mon = dmonth - 1;
729 t.tm_year = year - 1900;
735 timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
738 procheader_date_get_localtime(dest, len, timer);
743 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
746 gchar *default_format = "%y/%m/%d(%a) %H:%M";
748 lt = localtime(&timer);
750 if (prefs_common.date_format)
751 strftime(dest, len, prefs_common.date_format, lt);
753 strftime(dest, len, default_format, lt);