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"
38 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
43 HeaderEntry *hp = NULL;
46 /* skip non-required headers */
49 if (fgets(buf, len, fp) == NULL)
51 if (buf[0] == '\r' || buf[0] == '\n')
53 } while (buf[0] == ' ' || buf[0] == '\t');
55 for (hp = hentry, hnum = 0; hp->name != NULL;
57 if (!strncasecmp(hp->name, buf,
61 } while (hp->name == NULL);
63 if (fgets(buf, len, fp) == NULL) return -1;
64 if (buf[0] == '\r' || buf[0] == '\n') return -1;
67 /* unfold the specified folded line */
68 if (hp && hp->unfold) {
69 gboolean folded = FALSE;
70 gchar *bufp = buf + strlen(buf);
76 if (nexthead == ' ' || nexthead == '\t')
78 else if (nexthead == EOF)
80 else if (folded == TRUE) {
81 /* concatenate next line */
82 if ((len - (bufp - buf)) <= 2) break;
84 /* replace return code on the tail end
89 if (nexthead == '\r' || nexthead == '\n') {
93 if (fgets(bufp, len - (bufp - buf), fp)
104 /* remove trailing return code */
111 nexthead = fgetc(fp);
112 if (nexthead == ' ' || nexthead == '\t') {
113 size_t buflen = strlen(buf);
115 /* concatenate next line */
116 if ((len - buflen) > 2) {
117 gchar *p = buf + buflen;
122 if (fgets(p, len - buflen, fp) == NULL)
128 ungetc(nexthead, fp);
133 /* remove trailing return code */
139 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
141 gboolean folded = FALSE;
145 if (fgets(buf, len, fp) == NULL) return NULL;
146 if (buf[0] == '\r' || buf[0] == '\n') return NULL;
147 bufp = buf + strlen(buf);
150 nexthead = fgetc(fp);
153 if (nexthead == ' ' || nexthead == '\t')
155 else if (nexthead == EOF)
157 else if (folded == TRUE) {
158 /* concatenate next line */
159 if ((len - (bufp - buf)) <= 2) break;
161 /* replace return code on the tail end
166 if (nexthead == '\r' || nexthead == '\n') {
170 if (fgets(bufp, len - (bufp - buf), fp)
172 bufp += strlen(bufp);
176 ungetc(nexthead, fp);
181 /* remove trailing return code */
187 GSList *procheader_get_header_list(const gchar *file)
190 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
192 GSList *hlist = NULL;
195 if ((fp = fopen(file, "r")) == NULL) {
196 FILE_OP_ERROR(file, "fopen");
200 while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
201 if (*buf == ':') continue;
202 for (p = buf; *p && *p != ' '; p++) {
204 header = g_new(Header, 1);
205 header->name = g_strndup(buf, p - buf);
207 while (*p == ' ' || *p == '\t') p++;
208 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
209 header->body = g_strdup(tmp);
211 hlist = g_slist_append(hlist, header);
221 void procheader_header_list_destroy(GSList *hlist)
225 while (hlist != NULL) {
226 header = hlist->data;
228 g_free(header->name);
229 g_free(header->body);
231 hlist = g_slist_remove(hlist, header);
235 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
242 if (hentry == NULL) return;
244 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
248 p = buf + strlen(hp->name);
249 while (*p == ' ' || *p == '\t') p++;
251 if (hp->body == NULL)
252 hp->body = g_strdup(p);
253 else if (!strcasecmp(hp->name, "To:") ||
254 !strcasecmp(hp->name, "Cc:")) {
255 gchar *tp = hp->body;
256 hp->body = g_strconcat(tp, ", ", p, NULL);
277 MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
279 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
280 {"From:", NULL, TRUE},
282 {"Newsgroups:", NULL, TRUE},
283 {"Subject:", NULL, TRUE},
284 {"Message-Id:", NULL, FALSE},
285 {"References:", NULL, FALSE},
286 {"In-Reply-To:", NULL, FALSE},
287 {"Content-Type:", NULL, FALSE},
288 {"Seen:", NULL, FALSE},
289 {"X-Face:", NULL, FALSE},
290 {NULL, NULL, FALSE}};
292 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
293 {"From:", NULL, TRUE},
295 {"Newsgroups:", NULL, TRUE},
296 {"Subject:", NULL, TRUE},
297 {"Message-Id:", NULL, FALSE},
298 {"References:", NULL, FALSE},
299 {"In-Reply-To:", NULL, FALSE},
300 {"Content-Type:", NULL, FALSE},
301 {"Seen:", NULL, FALSE},
302 {NULL, NULL, FALSE}};
306 gchar buf[BUFFSIZE], tmp[BUFFSIZE];
307 gchar *reference = NULL;
313 hentry = full ? hentry_full : hentry_short;
315 if ((fp = fopen(file, "r")) == NULL) {
316 FILE_OP_ERROR(file, "fopen");
319 if (MSG_IS_QUEUED(flags)) {
320 while (fgets(buf, sizeof(buf), fp) != NULL)
321 if (buf[0] == '\r' || buf[0] == '\n') break;
324 msginfo = g_new0(MsgInfo, 1);
325 msginfo->flags = flags != 0 ? flags : MSG_NEW|MSG_UNREAD;
326 msginfo->inreplyto = NULL;
328 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
330 hp = buf + strlen(hentry[hnum].name);
331 while (*hp == ' ' || *hp == '\t') hp++;
335 if (msginfo->date) break;
337 procheader_date_parse(NULL, hp, 0);
338 msginfo->date = g_strdup(hp);
341 if (msginfo->from) break;
342 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
343 msginfo->from = g_strdup(tmp);
344 msginfo->fromname = procheader_get_fromname(tmp);
347 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
351 g_strconcat(p, ", ", tmp, NULL);
354 msginfo->to = g_strdup(tmp);
357 if (msginfo->newsgroups) {
358 p = msginfo->newsgroups;
359 msginfo->newsgroups =
360 g_strconcat(p, ",", hp, NULL);
363 msginfo->newsgroups = g_strdup(buf + 12);
366 if (msginfo->subject) break;
367 conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
368 msginfo->subject = g_strdup(tmp);
371 if (msginfo->msgid) break;
373 extract_parenthesis(hp, '<', '>');
375 msginfo->msgid = g_strdup(hp);
379 eliminate_parenthesis(hp, '(', ')');
380 if ((p = strrchr(hp, '<')) != NULL &&
381 strchr(p + 1, '>') != NULL) {
382 extract_parenthesis(p, '<', '>');
385 reference = g_strdup(p);
391 eliminate_parenthesis(hp, '(', ')');
392 extract_parenthesis(hp, '<', '>');
395 reference = g_strdup(hp);
399 if (!strncasecmp(hp, "multipart", 9))
400 msginfo->flags |= MSG_MIME;
403 /* mnews Seen header */
404 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
407 if (msginfo->xface) break;
408 msginfo->xface = g_strdup(hp);
413 msginfo->inreplyto = reference;
420 gchar *procheader_get_fromname(const gchar *str)
424 Xalloca(tmp, strlen(str) + 1, return NULL);
428 extract_quote(tmp, '\"');
430 } else if (strchr(tmp, '<')) {
431 eliminate_parenthesis(tmp, '<', '>');
435 extract_parenthesis(tmp, '<', '>');
438 } else if (strchr(tmp, '(')) {
439 extract_parenthesis(tmp, '(', ')');
444 name = g_strdup(str);
446 name = g_strdup(tmp);
451 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
453 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
466 /* parsing date field... */
467 result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
468 weekday, &day, month, &year, &hh, &mm, &ss, zone);
470 result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
471 &day, month, &year, &hh, &mm, &ss, zone);
474 result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
475 weekday, &day, month, &year, &hh, &mm, zone);
477 result = sscanf(src, "%d %3s %d %2d:%2d %5s",
478 &day, month, &year, &hh, &mm,
481 g_warning("Invalid date: %s\n", src);
483 strncpy2(dest, src, len);
490 /* Y2K compliant :) */
498 if ((p = strstr(monthstr, month)) != NULL)
499 dmonth = (gint)(p - monthstr) / 3 + 1;
501 g_warning("Invalid month: %s\n", month);
502 dmonth = G_DATE_BAD_MONTH;
509 t.tm_mon = dmonth - 1;
510 t.tm_year = year - 1900;
516 timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
519 procheader_date_get_localtime(dest, len, timer);
524 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
526 static gchar *wdaystr = N_("SunMonTueWedThuFriSat");
527 static gchar *tr_wday = NULL;
533 tr_wday = g_strdup(gettext(wdaystr));
535 lt = localtime(&timer);
537 wdlen = strlen(tr_wday) / 7;
538 if ((wday = alloca(wdlen + 1))) {
539 strncpy(wday, tr_wday + lt->tm_wday * wdlen, wdlen);
543 g_snprintf(dest, len, "%02d/%d/%d(%s) %02d:%02d",
544 lt->tm_year % 100, lt->tm_mon + 1, lt->tm_mday,
545 wday, lt->tm_hour, lt->tm_min);