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"
40 procheader_get_one_field
41 - reads fp and puts the header and the corresponding content into buf
42 if one of these is one of hentry table.
43 - if hentry is NULL, ignores no headers
46 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
51 HeaderEntry *hp = NULL;
54 /* skip non-required headers */
57 if (fgets(buf, len, fp) == NULL)
59 if (buf[0] == '\r' || buf[0] == '\n')
61 } while (buf[0] == ' ' || buf[0] == '\t');
63 for (hp = hentry, hnum = 0; hp->name != NULL;
65 if (!strncasecmp(hp->name, buf,
69 } while (hp->name == NULL);
71 if (fgets(buf, len, fp) == NULL) return -1;
72 if (buf[0] == '\r' || buf[0] == '\n') return -1;
75 /* unfold the specified folded line */
76 if (hp && hp->unfold) {
77 gboolean folded = FALSE;
78 gchar *bufp = buf + strlen(buf);
84 if (nexthead == ' ' || nexthead == '\t')
86 else if (nexthead == EOF)
88 else if (folded == TRUE) {
89 /* concatenate next line */
90 if ((len - (bufp - buf)) <= 2) break;
92 /* replace return code on the tail end
97 if (nexthead == '\r' || nexthead == '\n') {
101 if (fgets(bufp, len - (bufp - buf), fp)
103 bufp += strlen(bufp);
107 ungetc(nexthead, fp);
112 /* remove trailing return code */
119 nexthead = fgetc(fp);
120 if (nexthead == ' ' || nexthead == '\t') {
121 size_t buflen = strlen(buf);
123 /* concatenate next line */
124 if ((len - buflen) > 2) {
125 gchar *p = buf + buflen;
130 if (fgets(p, len - buflen, fp) == NULL)
136 ungetc(nexthead, fp);
141 /* remove trailing return code */
147 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
149 gboolean folded = FALSE;
153 if (fgets(buf, len, fp) == NULL) return NULL;
154 if (buf[0] == '\r' || buf[0] == '\n') return NULL;
155 bufp = buf + strlen(buf);
158 nexthead = fgetc(fp);
161 if (nexthead == ' ' || nexthead == '\t')
163 else if (nexthead == EOF)
165 else if (folded == TRUE) {
166 /* concatenate next line */
167 if ((len - (bufp - buf)) <= 2) break;
169 /* replace return code on the tail end
174 if (nexthead == '\r' || nexthead == '\n') {
178 if (fgets(bufp, len - (bufp - buf), fp)
180 bufp += strlen(bufp);
184 ungetc(nexthead, fp);
189 /* remove trailing return code */
196 tests whether two headers' names are equal
197 remove the trailing ':' or ' ' before comparing
200 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
207 if ((hdr1[len1 - 1] == ':') || (hdr1[len1 - 1] == ' '))
209 if ((hdr2[len2 - 1] == ':') || (hdr2[len2 - 1] == ' '))
213 return (strncasecmp(hdr1, hdr2, len1) == 0);
216 void procheader_header_free(Header * header)
218 g_free(header->name);
219 g_free(header->body);
224 parse headers, for example :
225 From: dinh@enseirb.fr becomes :
226 header->name = "From:"
227 header->body = "dinh@enseirb.fr"
230 Header * procheader_parse_header(gchar * buf)
236 if ((*buf == ':') || (*buf == ' '))
239 for (p = buf; *p ; p++) {
240 if ((*p == ':') || (*p == ' ')) {
241 header = g_new(Header, 1);
242 header->name = g_strndup(buf, p - buf + 1);
244 while (*p == ' ' || *p == '\t') p++;
245 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
246 header->body = g_strdup(tmp);
253 GSList *procheader_get_header_list(const gchar *file)
256 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
258 GSList *hlist = NULL;
261 if ((fp = fopen(file, "r")) == NULL) {
262 FILE_OP_ERROR(file, "fopen");
266 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
267 header = procheader_parse_header(buf);
269 hlist = g_slist_append(hlist, header);
276 void procheader_header_list_destroy(GSList *hlist)
280 while (hlist != NULL) {
281 header = hlist->data;
283 procheader_header_free(header);
284 hlist = g_slist_remove(hlist, header);
288 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
295 if (hentry == NULL) return;
297 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
301 p = buf + strlen(hp->name);
302 while (*p == ' ' || *p == '\t') p++;
304 if (hp->body == NULL)
305 hp->body = g_strdup(p);
306 else if (!strcasecmp(hp->name, "To:") ||
307 !strcasecmp(hp->name, "Cc:")) {
308 gchar *tp = hp->body;
309 hp->body = g_strconcat(tp, ", ", p, NULL);
329 H_DISPOSITION_NOTIFICATION_TO = 12
332 MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
334 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
335 {"From:", NULL, TRUE},
338 {"Newsgroups:", NULL, TRUE},
339 {"Subject:", NULL, TRUE},
340 {"Message-Id:", NULL, FALSE},
341 {"References:", NULL, FALSE},
342 {"In-Reply-To:", NULL, FALSE},
343 {"Content-Type:", NULL, FALSE},
344 {"Seen:", NULL, FALSE},
345 {"X-Face:", NULL, FALSE},
346 {"Disposition-Notification-To:",NULL, FALSE},
347 {NULL, NULL, FALSE}};
349 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
350 {"From:", NULL, TRUE},
353 {"Newsgroups:", NULL, TRUE},
354 {"Subject:", NULL, TRUE},
355 {"Message-Id:", NULL, FALSE},
356 {"References:", NULL, FALSE},
357 {"In-Reply-To:", NULL, FALSE},
358 {"Content-Type:", NULL, FALSE},
359 {"Seen:", NULL, FALSE},
360 {NULL, NULL, FALSE}};
364 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
365 gchar *reference = NULL;
371 hentry = full ? hentry_full : hentry_short;
373 if ((fp = fopen(file, "r")) == NULL) {
374 FILE_OP_ERROR(file, "fopen");
377 if (MSG_IS_QUEUED(flags)) {
378 while (fgets(buf, sizeof(buf), fp) != NULL)
379 if (buf[0] == '\r' || buf[0] == '\n') break;
382 msginfo = g_new0(MsgInfo, 1);
383 msginfo->flags = flags != 0 ? flags : MSG_NEW|MSG_UNREAD;
384 msginfo->inreplyto = NULL;
386 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
388 hp = buf + strlen(hentry[hnum].name);
389 while (*hp == ' ' || *hp == '\t') hp++;
393 if (msginfo->date) break;
395 procheader_date_parse(NULL, hp, 0);
396 msginfo->date = g_strdup(hp);
399 if (msginfo->from) break;
400 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
401 msginfo->from = g_strdup(tmp);
402 msginfo->fromname = procheader_get_fromname(tmp);
405 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
409 g_strconcat(p, ", ", tmp, NULL);
412 msginfo->to = g_strdup(tmp);
415 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
419 g_strconcat(p, ", ", tmp, NULL);
422 msginfo->cc = g_strdup(tmp);
425 if (msginfo->newsgroups) {
426 p = msginfo->newsgroups;
427 msginfo->newsgroups =
428 g_strconcat(p, ",", hp, NULL);
431 msginfo->newsgroups = g_strdup(buf + 12);
434 if (msginfo->subject) break;
435 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
436 msginfo->subject = g_strdup(tmp);
439 if (msginfo->msgid) break;
441 extract_parenthesis(hp, '<', '>');
443 msginfo->msgid = g_strdup(hp);
447 eliminate_parenthesis(hp, '(', ')');
448 if ((p = strrchr(hp, '<')) != NULL &&
449 strchr(p + 1, '>') != NULL) {
450 extract_parenthesis(p, '<', '>');
453 reference = g_strdup(p);
459 eliminate_parenthesis(hp, '(', ')');
460 extract_parenthesis(hp, '<', '>');
463 reference = g_strdup(hp);
467 if (!strncasecmp(hp, "multipart", 9))
468 msginfo->flags |= MSG_MIME;
471 /* mnews Seen header */
472 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
475 if (msginfo->xface) break;
476 msginfo->xface = g_strdup(hp);
478 case H_DISPOSITION_NOTIFICATION_TO:
479 if (msginfo->dispositionnotificationto) break;
480 msginfo->dispositionnotificationto = g_strdup(hp);
485 msginfo->inreplyto = reference;
492 gchar *procheader_get_fromname(const gchar *str)
496 Xalloca(tmp, strlen(str) + 1, return NULL);
500 extract_quote(tmp, '\"');
502 } else if (strchr(tmp, '<')) {
503 eliminate_parenthesis(tmp, '<', '>');
507 extract_parenthesis(tmp, '<', '>');
510 } else if (strchr(tmp, '(')) {
511 extract_parenthesis(tmp, '(', ')');
516 name = g_strdup(str);
518 name = g_strdup(tmp);
523 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
525 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
538 /* parsing date field... */
539 result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
540 weekday, &day, month, &year, &hh, &mm, &ss, zone);
542 result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
543 &day, month, &year, &hh, &mm, &ss, zone);
546 result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
547 weekday, &day, month, &year, &hh, &mm, zone);
549 result = sscanf(src, "%d %3s %d %2d:%2d %5s",
550 &day, month, &year, &hh, &mm,
553 g_warning("Invalid date: %s\n", src);
555 strncpy2(dest, src, len);
562 /* Y2K compliant :) */
570 if ((p = strstr(monthstr, month)) != NULL)
571 dmonth = (gint)(p - monthstr) / 3 + 1;
573 g_warning("Invalid month: %s\n", month);
574 dmonth = G_DATE_BAD_MONTH;
581 t.tm_mon = dmonth - 1;
582 t.tm_year = year - 1900;
588 timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
591 procheader_date_get_localtime(dest, len, timer);
596 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
599 gchar *default_format = "%y/%m/%d(%a) %H:%M";
601 lt = localtime(&timer);
603 if (prefs_common.date_format)
604 strftime(dest, len, prefs_common.date_format, lt);
606 strftime(dest, len, default_format, lt);