6b6159a03af9d99e6428a294c0458981aadfae44
[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         
512         if (flags.tmp_flags || flags.perm_flags) 
513                 msginfo->flags = flags;
514         else 
515                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
516         
517         msginfo->inreplyto = NULL;
518
519         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
520                != -1) {
521                 hp = buf + strlen(hentry[hnum].name);
522
523                 while (*hp == ' ' || *hp == '\t') hp++;
524
525                 switch (hnum) {
526                 case H_DATE:
527                         if (msginfo->date) break;
528                         msginfo->date_t =
529                                 procheader_date_parse(NULL, hp, 0);
530                         msginfo->date = g_strdup(hp);
531                         break;
532                 case H_FROM:
533                         if (msginfo->from) break;
534                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
535                         msginfo->from = g_strdup(tmp);
536                         msginfo->fromname = procheader_get_fromname(tmp);
537                         break;
538                 case H_TO:
539                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
540                         if (msginfo->to) {
541                                 p = msginfo->to;
542                                 msginfo->to =
543                                         g_strconcat(p, ", ", tmp, NULL);
544                                 g_free(p);
545                         } else
546                                 msginfo->to = g_strdup(tmp);
547                         break;
548                 case H_CC:
549                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
550                         if (msginfo->cc) {
551                                 p = msginfo->cc;
552                                 msginfo->cc =
553                                         g_strconcat(p, ", ", tmp, NULL);
554                                 g_free(p);
555                         } else
556                                 msginfo->cc = g_strdup(tmp);
557                         break;
558                 case H_NEWSGROUPS:
559                         if (msginfo->newsgroups) {
560                                 p = msginfo->newsgroups;
561                                 msginfo->newsgroups =
562                                         g_strconcat(p, ",", hp, NULL);
563                                 g_free(p);
564                         } else
565                                 msginfo->newsgroups = g_strdup(buf + 12);
566                         break;
567                 case H_SUBJECT:
568                         if (msginfo->subject) break;
569                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
570                         msginfo->subject = g_strdup(tmp);
571                         break;
572                 case H_MSG_ID:
573                         if (msginfo->msgid) break;
574
575                         extract_parenthesis(hp, '<', '>');
576                         remove_space(hp);
577                         msginfo->msgid = g_strdup(hp);
578                         break;
579                 case H_REFERENCES:
580                         if (!reference) {
581                                 msginfo->references = g_strdup(hp);
582                                 eliminate_parenthesis(hp, '(', ')');
583                                 if ((p = strrchr(hp, '<')) != NULL &&
584                                     strchr(p + 1, '>') != NULL) {
585                                         extract_parenthesis(p, '<', '>');
586                                         remove_space(p);
587                                         if (*p != '\0')
588                                                 reference = g_strdup(p);
589                                 }
590                         }
591                         break;
592                 case H_IN_REPLY_TO:
593                         if (!reference) {
594                                 eliminate_parenthesis(hp, '(', ')');
595                                 extract_parenthesis(hp, '<', '>');
596                                 remove_space(hp);
597                                 if (*hp != '\0')
598                                         reference = g_strdup(hp);
599                         }
600                         break;
601                 case H_CONTENT_TYPE:
602                         if (!strncasecmp(hp, "multipart", 9))
603                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
604                         break;
605                 case H_SEEN:
606                         /* mnews Seen header */
607                         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
608                         break;
609                 case H_X_FACE:
610                         if (msginfo->xface) break;
611                         msginfo->xface = g_strdup(hp);
612                         break;
613                 case H_DISPOSITION_NOTIFICATION_TO:
614                         if (msginfo->dispositionnotificationto) break;
615                         msginfo->dispositionnotificationto = g_strdup(hp);
616                         break;
617                 case H_RETURN_RECEIPT_TO:
618                         if (msginfo->returnreceiptto) break;
619                         msginfo->returnreceiptto = g_strdup(hp);
620                         break;
621                 case H_STATUS:
622                         if (strchr(hp, 'R') != NULL)
623                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
624                         if (strchr(hp, 'O') != NULL)
625                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
626                         if (strchr(hp, 'U') != NULL)
627                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
628                         break;
629                 case H_X_STATUS:
630                         if (strchr(hp, 'D') != NULL)
631                                 MSG_SET_PERM_FLAGS(msginfo->flags,
632                                               MSG_REALLY_DELETED);
633                         if (strchr(hp, 'F') != NULL)
634                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
635                         if (strchr(hp, 'd') != NULL)
636                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
637                         if (strchr(hp, 'r') != NULL)
638                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
639                         if (strchr(hp, 'f') != NULL)
640                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
641                         break;
642                 case H_FROM_SPACE:
643                         if (msginfo->fromspace) break;
644                         msginfo->fromspace = g_strdup(hp);
645                         break;
646                 default:
647                         break;
648                 }
649         }
650         msginfo->inreplyto = reference;
651
652         return msginfo;
653 }
654
655 gchar *procheader_get_fromname(const gchar *str)
656 {
657         gchar *tmp, *name;
658
659         Xalloca(tmp, strlen(str) + 1, return NULL);
660         strcpy(tmp, str);
661
662         if (*tmp == '\"') {
663                 extract_quote(tmp, '\"');
664                 g_strstrip(tmp);
665         } else if (strchr(tmp, '<')) {
666                 eliminate_parenthesis(tmp, '<', '>');
667                 g_strstrip(tmp);
668                 if (*tmp == '\0') {
669                         strcpy(tmp, str);
670                         extract_parenthesis(tmp, '<', '>');
671                         g_strstrip(tmp);
672                 }
673         } else if (strchr(tmp, '(')) {
674                 extract_parenthesis(tmp, '(', ')');
675                 g_strstrip(tmp);
676         }
677
678         if (*tmp == '\0')
679                 name = g_strdup(str);
680         else
681                 name = g_strdup(tmp);
682
683         return name;
684 }
685
686 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
687 {
688         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
689         gchar weekday[4];
690         gint day;
691         gchar month[4];
692         gint year;
693         gint hh, mm, ss;
694         gchar zone[6];
695         gint result;
696         GDateMonth dmonth;
697         struct tm t;
698         gchar *p;
699         time_t timer;
700
701         /* parsing date field... */
702         result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
703                         weekday, &day, month, &year, &hh, &mm, &ss, zone);
704         if (result != 8) {
705                 result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
706                                 &day, month, &year, &hh, &mm, &ss, zone);
707                 if (result != 7) {
708                         ss = 0;
709                         result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
710                                         weekday, &day, month, &year, &hh, &mm, zone);
711                         if (result != 7) {
712                                 result = sscanf(src, "%d %3s %d %2d:%2d %5s",
713                                                 &day, month, &year, &hh, &mm,
714                                                 zone);
715                                 if (result != 6) {
716                                         g_warning("Invalid date: %s\n", src);
717                                         if (dest && len > 0)
718                                                 strncpy2(dest, src, len);
719                                         return 0;
720                                 }
721                         }
722                 }
723         }
724
725         /* Y2K compliant :) */
726         if (year < 100) {
727                 if (year < 70)
728                         year += 2000;
729                 else
730                         year += 1900;
731         }
732
733         if ((p = strstr(monthstr, month)) != NULL)
734                 dmonth = (gint)(p - monthstr) / 3 + 1;
735         else {
736                 g_warning("Invalid month: %s\n", month);
737                 dmonth = G_DATE_BAD_MONTH;
738         }
739
740         t.tm_sec = ss;
741         t.tm_min = mm;
742         t.tm_hour = hh;
743         t.tm_mday = day;
744         t.tm_mon = dmonth - 1;
745         t.tm_year = year - 1900;
746         t.tm_wday = 0;
747         t.tm_yday = 0;
748         t.tm_isdst = -1;
749
750         timer = mktime(&t);
751         timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
752
753         if (dest)
754                 procheader_date_get_localtime(dest, len, timer);
755
756         return timer;
757 }
758
759 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
760 {
761         struct tm *lt;
762         gchar *default_format = "%y/%m/%d(%a) %H:%M";
763
764         lt = localtime(&timer);
765
766         if (prefs_common.date_format)
767                 strftime(dest, len, prefs_common.date_format, lt);
768         else
769                 strftime(dest, len, default_format, lt);
770 }