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] == ':') || (hdr1[len1 - 1] == ' '))
353 if ((hdr2[len2 - 1] == ':') || (hdr2[len2 - 1] == ' '))
357 return (g_strncasecmp(hdr1, hdr2, len1) == 0);
361 parse headers, for example :
362 From: dinh@enseirb.fr becomes :
363 header->name = "From:"
364 header->body = "dinh@enseirb.fr"
367 Header * procheader_parse_header(gchar * buf)
373 if ((*buf == ':') || (*buf == ' '))
376 for (p = buf; *p ; p++) {
377 if ((*p == ':') || (*p == ' ')) {
378 header = g_new(Header, 1);
379 header->name = g_strndup(buf, p - buf + 1);
381 while (*p == ' ' || *p == '\t') p++;
382 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
383 header->body = g_strdup(tmp);
390 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
397 if (hentry == NULL) return;
399 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
403 p = buf + strlen(hp->name);
404 while (*p == ' ' || *p == '\t') p++;
406 if (hp->body == NULL)
407 hp->body = g_strdup(p);
408 else if (procheader_headername_equal(hp->name, "To") ||
409 procheader_headername_equal(hp->name, "Cc")) {
410 gchar *tp = hp->body;
411 hp->body = g_strconcat(tp, ", ", p, NULL);
431 H_DISPOSITION_NOTIFICATION_TO = 12,
432 H_RETURN_RECEIPT_TO = 13
435 MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
437 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
438 {"From:", NULL, TRUE},
441 {"Newsgroups:", NULL, TRUE},
442 {"Subject:", NULL, TRUE},
443 {"Message-Id:", NULL, FALSE},
444 {"References:", NULL, FALSE},
445 {"In-Reply-To:", NULL, FALSE},
446 {"Content-Type:", NULL, FALSE},
447 {"Seen:", NULL, FALSE},
448 {"X-Face:", NULL, FALSE},
449 {"Disposition-Notification-To:", NULL, FALSE},
450 {"Return-Receipt-To:", NULL, FALSE},
451 {NULL, NULL, FALSE}};
453 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
454 {"From:", NULL, TRUE},
457 {"Newsgroups:", NULL, TRUE},
458 {"Subject:", NULL, TRUE},
459 {"Message-Id:", NULL, FALSE},
460 {"References:", NULL, FALSE},
461 {"In-Reply-To:", NULL, FALSE},
462 {"Content-Type:", NULL, FALSE},
463 {"Seen:", NULL, FALSE},
464 {NULL, NULL, FALSE}};
468 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
469 gchar *reference = NULL;
475 hentry = full ? hentry_full : hentry_short;
477 if ((fp = fopen(file, "r")) == NULL) {
478 FILE_OP_ERROR(file, "fopen");
481 if (MSG_IS_QUEUED(flags)) {
482 while (fgets(buf, sizeof(buf), fp) != NULL)
483 if (buf[0] == '\r' || buf[0] == '\n') break;
486 msginfo = g_new0(MsgInfo, 1);
487 msginfo->flags = flags != 0 ? flags : MSG_NEW|MSG_UNREAD;
488 msginfo->inreplyto = NULL;
490 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
492 hp = buf + strlen(hentry[hnum].name);
493 while (*hp == ' ' || *hp == '\t') hp++;
497 if (msginfo->date) break;
499 procheader_date_parse(NULL, hp, 0);
500 msginfo->date = g_strdup(hp);
503 if (msginfo->from) break;
504 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
505 msginfo->from = g_strdup(tmp);
506 msginfo->fromname = procheader_get_fromname(tmp);
509 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
513 g_strconcat(p, ", ", tmp, NULL);
516 msginfo->to = g_strdup(tmp);
519 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
523 g_strconcat(p, ", ", tmp, NULL);
526 msginfo->cc = g_strdup(tmp);
529 if (msginfo->newsgroups) {
530 p = msginfo->newsgroups;
531 msginfo->newsgroups =
532 g_strconcat(p, ",", hp, NULL);
535 msginfo->newsgroups = g_strdup(buf + 12);
538 if (msginfo->subject) break;
539 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
540 msginfo->subject = g_strdup(tmp);
543 if (msginfo->msgid) break;
545 extract_parenthesis(hp, '<', '>');
547 msginfo->msgid = g_strdup(hp);
551 msginfo->references = g_strdup(hp);
552 eliminate_parenthesis(hp, '(', ')');
553 if ((p = strrchr(hp, '<')) != NULL &&
554 strchr(p + 1, '>') != NULL) {
555 extract_parenthesis(p, '<', '>');
558 reference = g_strdup(p);
564 eliminate_parenthesis(hp, '(', ')');
565 extract_parenthesis(hp, '<', '>');
568 reference = g_strdup(hp);
572 if (!strncasecmp(hp, "multipart", 9))
573 msginfo->flags |= MSG_MIME;
576 /* mnews Seen header */
577 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
580 if (msginfo->xface) break;
581 msginfo->xface = g_strdup(hp);
583 case H_DISPOSITION_NOTIFICATION_TO:
584 if (msginfo->dispositionnotificationto) break;
585 msginfo->dispositionnotificationto = g_strdup(hp);
587 case H_RETURN_RECEIPT_TO:
588 if (msginfo->returnreceiptto) break;
589 msginfo->returnreceiptto = g_strdup(hp);
594 msginfo->inreplyto = reference;
601 gchar *procheader_get_fromname(const gchar *str)
605 Xalloca(tmp, strlen(str) + 1, return NULL);
609 extract_quote(tmp, '\"');
611 } else if (strchr(tmp, '<')) {
612 eliminate_parenthesis(tmp, '<', '>');
616 extract_parenthesis(tmp, '<', '>');
619 } else if (strchr(tmp, '(')) {
620 extract_parenthesis(tmp, '(', ')');
625 name = g_strdup(str);
627 name = g_strdup(tmp);
632 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
634 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
647 /* parsing date field... */
648 result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
649 weekday, &day, month, &year, &hh, &mm, &ss, zone);
651 result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
652 &day, month, &year, &hh, &mm, &ss, zone);
655 result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
656 weekday, &day, month, &year, &hh, &mm, zone);
658 result = sscanf(src, "%d %3s %d %2d:%2d %5s",
659 &day, month, &year, &hh, &mm,
662 g_warning("Invalid date: %s\n", src);
664 strncpy2(dest, src, len);
671 /* Y2K compliant :) */
679 if ((p = strstr(monthstr, month)) != NULL)
680 dmonth = (gint)(p - monthstr) / 3 + 1;
682 g_warning("Invalid month: %s\n", month);
683 dmonth = G_DATE_BAD_MONTH;
690 t.tm_mon = dmonth - 1;
691 t.tm_year = year - 1900;
697 timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
700 procheader_date_get_localtime(dest, len, timer);
705 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
708 gchar *default_format = "%y/%m/%d(%a) %H:%M";
710 lt = localtime(&timer);
712 if (prefs_common.date_format)
713 strftime(dest, len, prefs_common.date_format, lt);
715 strftime(dest, len, default_format, lt);