Statistics
| Revision:

root / src / procheader.c @ 144

History | View | Annotate | Download (16.6 kB)

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