fixed typo that broke forwarding
[claws.git] / src / procheader.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <time.h>
29
30 #include "intl.h"
31 #include "procheader.h"
32 #include "procmsg.h"
33 #include "codeconv.h"
34 #include "prefs_common.h"
35 #include "utils.h"
36
37 #define BUFFSIZE        8192
38
39 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
40                               HeaderEntry hentry[])
41 {
42         gint nexthead;
43         gint hnum = 0;
44         HeaderEntry *hp = NULL;
45
46         if (hentry != NULL) {
47                 /* skip non-required headers */
48                 do {
49                         do {
50                                 if (fgets(buf, len, fp) == NULL)
51                                         return -1;
52                                 if (buf[0] == '\r' || buf[0] == '\n')
53                                         return -1;
54                         } while (buf[0] == ' ' || buf[0] == '\t');
55
56                         for (hp = hentry, hnum = 0; hp->name != NULL;
57                              hp++, hnum++) {
58                                 if (!strncasecmp(hp->name, buf,
59                                                  strlen(hp->name))) {
60                                         break;
61                                 }
62                         }
63                 } while (hp->name == NULL);
64         } else {
65                 if (fgets(buf, len, fp) == NULL) return -1;
66                 if (buf[0] == '\r' || buf[0] == '\n') return -1;
67         }
68
69         /* unfold the specified folded line */
70         if (hp && hp->unfold) {
71                 gboolean folded = FALSE;
72                 gchar *bufp = buf + strlen(buf);
73
74                 while (1) {
75                         nexthead = fgetc(fp);
76
77                         /* folded */
78                         if (nexthead == ' ' || nexthead == '\t')
79                                 folded = TRUE;
80                         else if (nexthead == EOF)
81                                 break;
82                         else if (folded == TRUE) {
83                                 /* concatenate next line */
84                                 if ((len - (bufp - buf)) <= 2) break;
85
86                                 /* replace return code on the tail end
87                                    with space */
88                                 *(bufp - 1) = ' ';
89                                 *bufp++ = nexthead;
90                                 *bufp = '\0';
91                                 if (nexthead == '\r' || nexthead == '\n') {
92                                         folded = FALSE;
93                                         continue;
94                                 }
95                                 if (fgets(bufp, len - (bufp - buf), fp)
96                                     == NULL) break;
97                                 bufp += strlen(bufp);
98
99                                 folded = FALSE;
100                         } else {
101                                 ungetc(nexthead, fp);
102                                 break;
103                         }
104                 }
105
106                 /* remove trailing return code */
107                 strretchomp(buf);
108
109                 return hnum;
110         }
111
112         while (1) {
113                 nexthead = fgetc(fp);
114                 if (nexthead == ' ' || nexthead == '\t') {
115                         size_t buflen = strlen(buf);
116
117                         /* concatenate next line */
118                         if ((len - buflen) > 2) {
119                                 gchar *p = buf + buflen;
120
121                                 *p++ = nexthead;
122                                 *p = '\0';
123                                 buflen++;
124                                 if (fgets(p, len - buflen, fp) == NULL)
125                                         break;
126                         } else
127                                 break;
128                 } else {
129                         if (nexthead != EOF)
130                                 ungetc(nexthead, fp);
131                         break;
132                 }
133         }
134
135         /* remove trailing return code */
136         strretchomp(buf);
137
138         return hnum;
139 }
140
141 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
142 {
143         gboolean folded = FALSE;
144         gint nexthead;
145         gchar *bufp;
146
147         if (fgets(buf, len, fp) == NULL) return NULL;
148         if (buf[0] == '\r' || buf[0] == '\n') return NULL;
149         bufp = buf + strlen(buf);
150
151         while (1) {
152                 nexthead = fgetc(fp);
153
154                 /* folded */
155                 if (nexthead == ' ' || nexthead == '\t')
156                         folded = TRUE;
157                 else if (nexthead == EOF)
158                         break;
159                 else if (folded == TRUE) {
160                         /* concatenate next line */
161                         if ((len - (bufp - buf)) <= 2) break;
162
163                         /* replace return code on the tail end
164                            with space */
165                         *(bufp - 1) = ' ';
166                         *bufp++ = nexthead;
167                         *bufp = '\0';
168                         if (nexthead == '\r' || nexthead == '\n') {
169                                 folded = FALSE;
170                                 continue;
171                         }
172                         if (fgets(bufp, len - (bufp - buf), fp)
173                             == NULL) break;
174                         bufp += strlen(bufp);
175
176                         folded = FALSE;
177                 } else {
178                         ungetc(nexthead, fp);
179                         break;
180                 }
181         }
182
183         /* remove trailing return code */
184         strretchomp(buf);
185
186         return buf;
187 }
188
189 GSList *procheader_get_header_list_from_file(const gchar *file)
190 {
191         FILE *fp;
192         GSList *hlist;
193
194         if ((fp = fopen(file, "r")) == NULL) {
195                 FILE_OP_ERROR(file, "fopen");
196                 return NULL;
197         }
198
199         hlist = procheader_get_header_list(fp);
200
201         fclose(fp);
202         return hlist;
203 }
204
205 GSList *procheader_get_header_list(FILE *fp)
206 {
207         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
208         gchar *p;
209         GSList *hlist = NULL;
210         Header *header;
211
212         g_return_val_if_fail(fp != NULL, NULL);
213
214         while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
215                 if (header = procheader_parse_header(buf))
216                         hlist = g_slist_append(hlist, header);
217                 /*
218                 if (*buf == ':') continue;
219                 for (p = buf; *p && *p != ' '; p++) {
220                         if (*p == ':') {
221                                 header = g_new(Header, 1);
222                                 header->name = g_strndup(buf, p - buf);
223                                 p++;
224                                 while (*p == ' ' || *p == '\t') p++;
225                                 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
226                                 header->body = g_strdup(tmp);
227
228                                 hlist = g_slist_append(hlist, header);
229                                 break;
230                         }
231                 }
232                 */
233         }
234
235         return hlist;
236 }
237
238 GPtrArray *procheader_get_header_array(FILE *fp)
239 {
240         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
241         gchar *p;
242         GPtrArray *headers;
243         Header *header;
244
245         g_return_val_if_fail(fp != NULL, NULL);
246
247         headers = g_ptr_array_new();
248
249         while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
250                 if (header = procheader_parse_header(buf))
251                         g_ptr_array_add(headers, header);
252                 /*
253                 if (*buf == ':') continue;
254                 for (p = buf; *p && *p != ' '; p++) {
255                         if (*p == ':') {
256                                 header = g_new(Header, 1);
257                                 header->name = g_strndup(buf, p - buf);
258                                 p++;
259                                 while (*p == ' ' || *p == '\t') p++;
260                                 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
261                                 header->body = g_strdup(tmp);
262
263                                 g_ptr_array_add(headers, header);
264                                 break;
265                         }
266                 }
267                 */
268         }
269
270         return headers;
271 }
272
273 GPtrArray *procheader_get_header_array_asis(FILE *fp)
274 {
275         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
276         gchar *p;
277         GPtrArray *headers;
278         Header *header;
279
280         g_return_val_if_fail(fp != NULL, NULL);
281
282         headers = g_ptr_array_new();
283
284         while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
285                 if (header = procheader_parse_header(buf))
286                         g_ptr_array_add(headers, header);
287                         /*
288                 if (*buf == ':') continue;
289                 for (p = buf; *p && *p != ' '; p++) {
290                         if (*p == ':') {
291                                 header = g_new(Header, 1);
292                                 header->name = g_strndup(buf, p - buf);
293                                 p++;
294                                 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
295                                 header->body = g_strdup(tmp);
296
297                                 g_ptr_array_add(headers, header);
298                                 break;
299                         }
300                 }
301                         */
302         }
303
304         return headers;
305 }
306
307 void procheader_header_list_destroy(GSList *hlist)
308 {
309         Header *header;
310
311         while (hlist != NULL) {
312                 header = hlist->data;
313                 procheader_header_free(header);
314                 hlist = g_slist_remove(hlist, header);
315         }
316 }
317
318 void procheader_header_array_destroy(GPtrArray *harray)
319 {
320         gint i;
321         Header *header;
322
323         for (i = 0; i < harray->len; i++) {
324                 header = g_ptr_array_index(harray, i);
325                 procheader_header_free(header);
326         }
327
328         g_ptr_array_free(harray, TRUE);
329 }
330
331 void procheader_header_free(Header *header)
332 {
333         if (!header) return;
334
335         g_free(header->name);
336         g_free(header->body);
337         g_free(header);
338 }
339
340 /*
341   tests whether two headers' names are equal
342   remove the trailing ':' or ' ' before comparing
343 */
344
345 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
346 {
347         int len1;
348         int len2;
349
350         len1 = strlen(hdr1);
351         len2 = strlen(hdr2);
352         if (hdr1[len1 - 1] == ':')
353                 len1--;
354         if (hdr2[len2 - 1] == ':')
355                 len2--;
356         if (len1 != len2)
357                 return 0;
358
359         return (g_strncasecmp(hdr1, hdr2, len1) == 0);
360 }
361
362 /*
363   parse headers, for example :
364   From: dinh@enseirb.fr becomes :
365   header->name = "From:"
366   header->body = "dinh@enseirb.fr"
367  */
368
369 Header * procheader_parse_header(gchar * buf)
370 {
371         gchar tmp[BUFFSIZE];
372         gchar *p = buf;
373         Header * header;
374
375         if ((*buf == ':') || (*buf == ' '))
376                 return NULL;
377
378         for (p = buf; *p ; p++) {
379                 if ((*p == ':') || (*p == ' ')) {
380                         header = g_new(Header, 1);
381                         header->name = g_strndup(buf, p - buf + 1);
382                         p++;
383                         while (*p == ' ' || *p == '\t') p++;
384                         conv_unmime_header(tmp, sizeof(tmp), p, NULL);
385                         header->body = g_strdup(tmp);
386                         return header;
387                 }
388         }
389         return NULL;
390 }
391
392 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
393 {
394         gchar buf[BUFFSIZE];
395         HeaderEntry *hp;
396         gint hnum;
397         gchar *p;
398
399         if (hentry == NULL) return;
400
401         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
402                != -1) {
403                 hp = hentry + hnum;
404
405                 p = buf + strlen(hp->name);
406                 while (*p == ' ' || *p == '\t') p++;
407
408                 if (hp->body == NULL)
409                         hp->body = g_strdup(p);
410                 else if (procheader_headername_equal(hp->name, "To") ||
411                          procheader_headername_equal(hp->name, "Cc")) {
412                         gchar *tp = hp->body;
413                         hp->body = g_strconcat(tp, ", ", p, NULL);
414                         g_free(tp);
415                 }
416         }
417 }
418
419 enum
420 {
421         H_DATE          = 0,
422         H_FROM          = 1,
423         H_TO            = 2,
424         H_CC            = 3,
425         H_NEWSGROUPS    = 4,
426         H_SUBJECT       = 5,
427         H_MSG_ID        = 6,
428         H_REFERENCES    = 7,
429         H_IN_REPLY_TO   = 8,
430         H_CONTENT_TYPE  = 9,
431         H_SEEN          = 10,
432         H_STATUS        = 11,
433         H_X_STATUS      = 12,
434         H_FROM_SPACE    = 13,
435         H_X_FACE        = 14,
436         H_DISPOSITION_NOTIFICATION_TO = 15,
437         H_RETURN_RECEIPT_TO = 16
438 };
439
440 MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
441 {
442         FILE *fp;
443         MsgInfo *msginfo;
444
445         if ((fp = fopen(file, "r")) == NULL) {
446                 FILE_OP_ERROR(file, "fopen");
447                 return NULL;
448         }
449
450         msginfo = procheader_file_parse(fp, flags, full);
451
452         fclose(fp);
453
454         return msginfo;
455 }
456
457 MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
458                                gboolean full)
459 {
460         static HeaderEntry hentry_full[] = {{"Date:",           NULL, FALSE},
461                                            {"From:",            NULL, TRUE},
462                                            {"To:",              NULL, TRUE},
463                                            {"Cc:",              NULL, TRUE},
464                                            {"Newsgroups:",      NULL, TRUE},
465                                            {"Subject:",         NULL, TRUE},
466                                            {"Message-Id:",      NULL, FALSE},
467                                            {"References:",      NULL, FALSE},
468                                            {"In-Reply-To:",     NULL, FALSE},
469                                            {"Content-Type:",    NULL, FALSE},
470                                            {"Seen:",            NULL, FALSE},
471                                            {"Status:",          NULL, FALSE},
472                                            {"X-Status:",        NULL, FALSE},
473                                            {"From ",            NULL, FALSE},
474                                            {"X-Face:",          NULL, FALSE},
475                                            {"Disposition-Notification-To:", NULL, FALSE},
476                                            {"Return-Receipt-To:", NULL, FALSE},
477                                            {NULL,               NULL, FALSE}};
478
479         static HeaderEntry hentry_short[] = {{"Date:",          NULL, FALSE},
480                                             {"From:",           NULL, TRUE},
481                                             {"To:",             NULL, TRUE},
482                                             {"Cc:",             NULL, TRUE},
483                                             {"Newsgroups:",     NULL, TRUE},
484                                             {"Subject:",        NULL, TRUE},
485                                             {"Message-Id:",     NULL, FALSE},
486                                             {"References:",     NULL, FALSE},
487                                             {"In-Reply-To:",    NULL, FALSE},
488                                             {"Content-Type:",   NULL, FALSE},
489                                             {"Seen:",           NULL, FALSE},
490                                             {"Status:",         NULL, FALSE},
491                                             {"X-Status:",       NULL, FALSE},
492                                             {"From ",           NULL, FALSE},
493                                             {NULL,              NULL, FALSE}};
494         
495         MsgInfo *msginfo;
496         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
497         gchar *reference = NULL;
498         gchar *p;
499         gchar *hp;
500         HeaderEntry *hentry;
501         gint hnum;
502
503         hentry = full ? hentry_full : hentry_short;
504
505         if (MSG_IS_QUEUED(flags)) {
506                 while (fgets(buf, sizeof(buf), fp) != NULL)
507                         if (buf[0] == '\r' || buf[0] == '\n') break;
508         }
509
510         msginfo = g_new0(MsgInfo, 1);
511         msginfo->flags = flags != 0 ? flags : MSG_NEW|MSG_UNREAD;
512         msginfo->inreplyto = NULL;
513
514         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
515                != -1) {
516                 hp = buf + strlen(hentry[hnum].name);
517
518                 while (*hp == ' ' || *hp == '\t') hp++;
519
520                 switch (hnum) {
521                 case H_DATE:
522                         if (msginfo->date) break;
523                         msginfo->date_t =
524                                 procheader_date_parse(NULL, hp, 0);
525                         msginfo->date = g_strdup(hp);
526                         break;
527                 case H_FROM:
528                         if (msginfo->from) break;
529                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
530                         msginfo->from = g_strdup(tmp);
531                         msginfo->fromname = procheader_get_fromname(tmp);
532                         break;
533                 case H_TO:
534                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
535                         if (msginfo->to) {
536                                 p = msginfo->to;
537                                 msginfo->to =
538                                         g_strconcat(p, ", ", tmp, NULL);
539                                 g_free(p);
540                         } else
541                                 msginfo->to = g_strdup(tmp);
542                         break;
543                 case H_CC:
544                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
545                         if (msginfo->cc) {
546                                 p = msginfo->cc;
547                                 msginfo->cc =
548                                         g_strconcat(p, ", ", tmp, NULL);
549                                 g_free(p);
550                         } else
551                                 msginfo->cc = g_strdup(tmp);
552                         break;
553                 case H_NEWSGROUPS:
554                         if (msginfo->newsgroups) {
555                                 p = msginfo->newsgroups;
556                                 msginfo->newsgroups =
557                                         g_strconcat(p, ",", hp, NULL);
558                                 g_free(p);
559                         } else
560                                 msginfo->newsgroups = g_strdup(buf + 12);
561                         break;
562                 case H_SUBJECT:
563                         if (msginfo->subject) break;
564                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
565                         msginfo->subject = g_strdup(tmp);
566                         break;
567                 case H_MSG_ID:
568                         if (msginfo->msgid) break;
569
570                         extract_parenthesis(hp, '<', '>');
571                         remove_space(hp);
572                         msginfo->msgid = g_strdup(hp);
573                         break;
574                 case H_REFERENCES:
575                         if (!reference) {
576                                 msginfo->references = g_strdup(hp);
577                                 eliminate_parenthesis(hp, '(', ')');
578                                 if ((p = strrchr(hp, '<')) != NULL &&
579                                     strchr(p + 1, '>') != NULL) {
580                                         extract_parenthesis(p, '<', '>');
581                                         remove_space(p);
582                                         if (*p != '\0')
583                                                 reference = g_strdup(p);
584                                 }
585                         }
586                         break;
587                 case H_IN_REPLY_TO:
588                         if (!reference) {
589                                 eliminate_parenthesis(hp, '(', ')');
590                                 extract_parenthesis(hp, '<', '>');
591                                 remove_space(hp);
592                                 if (*hp != '\0')
593                                         reference = g_strdup(hp);
594                         }
595                         break;
596                 case H_CONTENT_TYPE:
597                         if (!strncasecmp(hp, "multipart", 9))
598                                 msginfo->flags |= MSG_MIME;
599                         break;
600                 case H_SEEN:
601                         /* mnews Seen header */
602                         MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
603                         break;
604                 case H_X_FACE:
605                         if (msginfo->xface) break;
606                         msginfo->xface = g_strdup(hp);
607                         break;
608                 case H_DISPOSITION_NOTIFICATION_TO:
609                         if (msginfo->dispositionnotificationto) break;
610                         msginfo->dispositionnotificationto = g_strdup(hp);
611                         break;
612                 case H_RETURN_RECEIPT_TO:
613                         if (msginfo->returnreceiptto) break;
614                         msginfo->returnreceiptto = g_strdup(hp);
615                         break;
616                 case H_STATUS:
617                         if (strchr(hp, 'R') != NULL)
618                                 MSG_UNSET_FLAGS(msginfo->flags, MSG_UNREAD);
619                         if (strchr(hp, 'O') != NULL)
620                                 MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW);
621                         if (strchr(hp, 'U') != NULL)
622                                 MSG_SET_FLAGS(msginfo->flags, MSG_UNREAD);
623                         break;
624                 case H_X_STATUS:
625                         if (strchr(hp, 'D') != NULL)
626                                 MSG_SET_FLAGS(msginfo->flags,
627                                               MSG_REALLY_DELETED);
628                         if (strchr(hp, 'F') != NULL)
629                                 MSG_SET_FLAGS(msginfo->flags, MSG_MARKED);
630                         if (strchr(hp, 'd') != NULL)
631                                 MSG_SET_FLAGS(msginfo->flags, MSG_DELETED);
632                         if (strchr(hp, 'r') != NULL)
633                                 MSG_SET_FLAGS(msginfo->flags, MSG_REPLIED);
634                         if (strchr(hp, 'f') != NULL)
635                                 MSG_SET_FLAGS(msginfo->flags, MSG_FORWARDED);
636                         break;
637                 case H_FROM_SPACE:
638                         if (msginfo->fromspace) break;
639                         msginfo->fromspace = g_strdup(hp);
640                         break;
641                 default:
642                 }
643         }
644         msginfo->inreplyto = reference;
645
646         return msginfo;
647 }
648
649 gchar *procheader_get_fromname(const gchar *str)
650 {
651         gchar *tmp, *name;
652
653         Xalloca(tmp, strlen(str) + 1, return NULL);
654         strcpy(tmp, str);
655
656         if (*tmp == '\"') {
657                 extract_quote(tmp, '\"');
658                 g_strstrip(tmp);
659         } else if (strchr(tmp, '<')) {
660                 eliminate_parenthesis(tmp, '<', '>');
661                 g_strstrip(tmp);
662                 if (*tmp == '\0') {
663                         strcpy(tmp, str);
664                         extract_parenthesis(tmp, '<', '>');
665                         g_strstrip(tmp);
666                 }
667         } else if (strchr(tmp, '(')) {
668                 extract_parenthesis(tmp, '(', ')');
669                 g_strstrip(tmp);
670         }
671
672         if (*tmp == '\0')
673                 name = g_strdup(str);
674         else
675                 name = g_strdup(tmp);
676
677         return name;
678 }
679
680 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
681 {
682         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
683         gchar weekday[4];
684         gint day;
685         gchar month[4];
686         gint year;
687         gint hh, mm, ss;
688         gchar zone[6];
689         gint result;
690         GDateMonth dmonth;
691         struct tm t;
692         gchar *p;
693         time_t timer;
694
695         /* parsing date field... */
696         result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
697                         weekday, &day, month, &year, &hh, &mm, &ss, zone);
698         if (result != 8) {
699                 result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
700                                 &day, month, &year, &hh, &mm, &ss, zone);
701                 if (result != 7) {
702                         ss = 0;
703                         result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
704                                         weekday, &day, month, &year, &hh, &mm, zone);
705                         if (result != 7) {
706                                 result = sscanf(src, "%d %3s %d %2d:%2d %5s",
707                                                 &day, month, &year, &hh, &mm,
708                                                 zone);
709                                 if (result != 6) {
710                                         g_warning("Invalid date: %s\n", src);
711                                         if (dest && len > 0)
712                                                 strncpy2(dest, src, len);
713                                         return 0;
714                                 }
715                         }
716                 }
717         }
718
719         /* Y2K compliant :) */
720         if (year < 100) {
721                 if (year < 70)
722                         year += 2000;
723                 else
724                         year += 1900;
725         }
726
727         if ((p = strstr(monthstr, month)) != NULL)
728                 dmonth = (gint)(p - monthstr) / 3 + 1;
729         else {
730                 g_warning("Invalid month: %s\n", month);
731                 dmonth = G_DATE_BAD_MONTH;
732         }
733
734         t.tm_sec = ss;
735         t.tm_min = mm;
736         t.tm_hour = hh;
737         t.tm_mday = day;
738         t.tm_mon = dmonth - 1;
739         t.tm_year = year - 1900;
740         t.tm_wday = 0;
741         t.tm_yday = 0;
742         t.tm_isdst = -1;
743
744         timer = mktime(&t);
745         timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
746
747         if (dest)
748                 procheader_date_get_localtime(dest, len, timer);
749
750         return timer;
751 }
752
753 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
754 {
755         struct tm *lt;
756         gchar *default_format = "%y/%m/%d(%a) %H:%M";
757
758         lt = localtime(&timer);
759
760         if (prefs_common.date_format)
761                 strftime(dest, len, prefs_common.date_format, lt);
762         else
763                 strftime(dest, len, default_format, lt);
764 }