2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 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(const gchar *file)
191 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
193 GSList *hlist = NULL;
196 if ((fp = fopen(file, "r")) == NULL) {
197 FILE_OP_ERROR(file, "fopen");
201 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
202 if (*buf == ':') continue;
203 for (p = buf; *p && *p != ' '; p++) {
205 header = g_new(Header, 1);
206 header->name = g_strndup(buf, p - buf);
208 while (*p == ' ' || *p == '\t') p++;
209 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
210 header->body = g_strdup(tmp);
212 hlist = g_slist_append(hlist, header);
222 void procheader_header_list_destroy(GSList *hlist)
226 while (hlist != NULL) {
227 header = hlist->data;
229 g_free(header->name);
230 g_free(header->body);
232 hlist = g_slist_remove(hlist, header);
236 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
243 if (hentry == NULL) return;
245 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
249 p = buf + strlen(hp->name);
250 while (*p == ' ' || *p == '\t') p++;
252 if (hp->body == NULL)
253 hp->body = g_strdup(p);
254 else if (!strcasecmp(hp->name, "To:") ||
255 !strcasecmp(hp->name, "Cc:")) {
256 gchar *tp = hp->body;
257 hp->body = g_strconcat(tp, ", ", p, NULL);
278 MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
280 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
281 {"From:", NULL, TRUE},
283 {"Newsgroups:", NULL, TRUE},
284 {"Subject:", NULL, TRUE},
285 {"Message-Id:", NULL, FALSE},
286 {"References:", NULL, FALSE},
287 {"In-Reply-To:", NULL, FALSE},
288 {"Content-Type:", NULL, FALSE},
289 {"Seen:", NULL, FALSE},
290 {"X-Face:", NULL, FALSE},
291 {NULL, NULL, FALSE}};
293 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
294 {"From:", NULL, TRUE},
296 {"Newsgroups:", NULL, TRUE},
297 {"Subject:", NULL, TRUE},
298 {"Message-Id:", NULL, FALSE},
299 {"References:", NULL, FALSE},
300 {"In-Reply-To:", NULL, FALSE},
301 {"Content-Type:", NULL, FALSE},
302 {"Seen:", NULL, FALSE},
303 {NULL, NULL, FALSE}};
307 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
308 gchar *reference = NULL;
314 hentry = full ? hentry_full : hentry_short;
316 if ((fp = fopen(file, "r")) == NULL) {
317 FILE_OP_ERROR(file, "fopen");
320 if (MSG_IS_QUEUED(flags)) {
321 while (fgets(buf, sizeof(buf), fp) != NULL)
322 if (buf[0] == '\r' || buf[0] == '\n') break;
325 msginfo = g_new0(MsgInfo, 1);
326 msginfo->flags = flags != 0 ? flags : MSG_NEW|MSG_UNREAD;
327 msginfo->inreplyto = NULL;
329 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
331 hp = buf + strlen(hentry[hnum].name);
332 while (*hp == ' ' || *hp == '\t') hp++;
336 if (msginfo->date) break;
338 procheader_date_parse(NULL, hp, 0);
339 msginfo->date = g_strdup(hp);
342 if (msginfo->from) break;
343 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
344 msginfo->from = g_strdup(tmp);
345 msginfo->fromname = procheader_get_fromname(tmp);
348 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
352 g_strconcat(p, ", ", tmp, NULL);
355 msginfo->to = g_strdup(tmp);
358 if (msginfo->newsgroups) {
359 p = msginfo->newsgroups;
360 msginfo->newsgroups =
361 g_strconcat(p, ",", hp, NULL);
364 msginfo->newsgroups = g_strdup(buf + 12);
367 if (msginfo->subject) break;
368 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
369 msginfo->subject = g_strdup(tmp);
372 if (msginfo->msgid) break;
374 extract_parenthesis(hp, '<', '>');
376 msginfo->msgid = g_strdup(hp);
380 eliminate_parenthesis(hp, '(', ')');
381 if ((p = strrchr(hp, '<')) != NULL &&
382 strchr(p + 1, '>') != NULL) {
383 extract_parenthesis(p, '<', '>');
386 reference = g_strdup(p);
392 eliminate_parenthesis(hp, '(', ')');
393 extract_parenthesis(hp, '<', '>');
396 reference = g_strdup(hp);
400 if (!strncasecmp(hp, "multipart", 9))
401 msginfo->flags |= MSG_MIME;
404 /* mnews Seen header */
405 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
408 if (msginfo->xface) break;
409 msginfo->xface = g_strdup(hp);
414 msginfo->inreplyto = reference;
421 gchar *procheader_get_fromname(const gchar *str)
425 Xalloca(tmp, strlen(str) + 1, return NULL);
429 extract_quote(tmp, '\"');
431 } else if (strchr(tmp, '<')) {
432 eliminate_parenthesis(tmp, '<', '>');
436 extract_parenthesis(tmp, '<', '>');
439 } else if (strchr(tmp, '(')) {
440 extract_parenthesis(tmp, '(', ')');
445 name = g_strdup(str);
447 name = g_strdup(tmp);
452 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
454 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
467 /* parsing date field... */
468 result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
469 weekday, &day, month, &year, &hh, &mm, &ss, zone);
471 result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
472 &day, month, &year, &hh, &mm, &ss, zone);
475 result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
476 weekday, &day, month, &year, &hh, &mm, zone);
478 result = sscanf(src, "%d %3s %d %2d:%2d %5s",
479 &day, month, &year, &hh, &mm,
482 g_warning("Invalid date: %s\n", src);
484 strncpy2(dest, src, len);
491 /* Y2K compliant :) */
499 if ((p = strstr(monthstr, month)) != NULL)
500 dmonth = (gint)(p - monthstr) / 3 + 1;
502 g_warning("Invalid month: %s\n", month);
503 dmonth = G_DATE_BAD_MONTH;
510 t.tm_mon = dmonth - 1;
511 t.tm_year = year - 1900;
517 timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
520 procheader_date_get_localtime(dest, len, timer);
525 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
528 gchar *default_format = "%y/%m/%d(%a) %H:%M";
530 lt = localtime(&timer);
532 if (prefs_common.date_format)
533 strftime(dest, len, prefs_common.date_format, lt);
535 strftime(dest, len, default_format, lt);