Statistics
| Revision:

root / src / procheader.c @ 1

History | View | Annotate | Download (16.2 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 1999-2004 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
#include <sys/stat.h>
30

    
31
#include "intl.h"
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, gint 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, gint 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)
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, NULL);
319
                                header->body = g_strdup(tmp);
320

    
321
                                g_ptr_array_add(headers, header);
322
                                break;
323
                        }
324
                }
325
        }
326

    
327
        return headers;
328
}
329

    
330
GPtrArray *procheader_get_header_array_asis(FILE *fp)
331
{
332
        gchar buf[BUFFSIZE], tmp[BUFFSIZE];
333
        gchar *p;
334
        GPtrArray *headers;
335
        Header *header;
336

    
337
        g_return_val_if_fail(fp != NULL, NULL);
338

    
339
        headers = g_ptr_array_new();
340

    
341
        while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
342
                if (*buf == ':') continue;
343
                for (p = buf; *p && *p != ' '; p++) {
344
                        if (*p == ':') {
345
                                header = g_new(Header, 1);
346
                                header->name = g_strndup(buf, p - buf);
347
                                p++;
348
                                conv_unmime_header(tmp, sizeof(tmp), p, NULL);
349
                                header->body = g_strdup(tmp);
350

    
351
                                g_ptr_array_add(headers, header);
352
                                break;
353
                        }
354
                }
355
        }
356

    
357
        return headers;
358
}
359

    
360
void procheader_header_list_destroy(GSList *hlist)
361
{
362
        Header *header;
363

    
364
        while (hlist != NULL) {
365
                header = hlist->data;
366
                procheader_header_free(header);
367
                hlist = g_slist_remove(hlist, header);
368
        }
369
}
370

    
371
void procheader_header_array_destroy(GPtrArray *harray)
372
{
373
        gint i;
374
        Header *header;
375

    
376
        for (i = 0; i < harray->len; i++) {
377
                header = g_ptr_array_index(harray, i);
378
                procheader_header_free(header);
379
        }
380

    
381
        g_ptr_array_free(harray, TRUE);
382
}
383

    
384
void procheader_header_free(Header *header)
385
{
386
        if (!header) return;
387

    
388
        g_free(header->name);
389
        g_free(header->body);
390
        g_free(header);
391
}
392

    
393
void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
394
{
395
        gchar buf[BUFFSIZE];
396
        HeaderEntry *hp;
397
        gint hnum;
398
        gchar *p;
399

    
400
        if (hentry == NULL) return;
401

    
402
        while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
403
               != -1) {
404
                hp = hentry + hnum;
405

    
406
                p = buf + strlen(hp->name);
407
                while (*p == ' ' || *p == '\t') p++;
408

    
409
                if (hp->body == NULL)
410
                        hp->body = g_strdup(p);
411
                else if (!strcasecmp(hp->name, "To:") ||
412
                         !strcasecmp(hp->name, "Cc:")) {
413
                        gchar *tp = hp->body;
414
                        hp->body = g_strconcat(tp, ", ", p, NULL);
415
                        g_free(tp);
416
                }
417
        }
418
}
419

    
420
MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
421
                               gboolean full)
422
{
423
        struct stat s;
424
        FILE *fp;
425
        MsgInfo *msginfo;
426

    
427
        if (stat(file, &s) < 0) {
428
                FILE_OP_ERROR(file, "stat");
429
                return NULL;
430
        }
431
        if (!S_ISREG(s.st_mode))
432
                return NULL;
433

    
434
        if ((fp = fopen(file, "rb")) == NULL) {
435
                FILE_OP_ERROR(file, "fopen");
436
                return NULL;
437
        }
438

    
439
        msginfo = procheader_parse_stream(fp, flags, full);
440
        fclose(fp);
441

    
442
        if (msginfo) {
443
                msginfo->size = s.st_size;
444
                msginfo->mtime = s.st_mtime;
445
        }
446

    
447
        return msginfo;
448
}
449

    
450
MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full)
451
{
452
        FILE *fp;
453
        MsgInfo *msginfo;
454

    
455
        if ((fp = str_open_as_stream(str)) == NULL)
456
                return NULL;
457

    
458
        msginfo = procheader_parse_stream(fp, flags, full);
459
        fclose(fp);
460
        return msginfo;
461
}
462

    
463
enum
464
{
465
        H_DATE                = 0,
466
        H_FROM                = 1,
467
        H_TO                = 2,
468
        H_NEWSGROUPS        = 3,
469
        H_SUBJECT        = 4,
470
        H_MSG_ID        = 5,
471
        H_REFERENCES        = 6,
472
        H_IN_REPLY_TO        = 7,
473
        H_CONTENT_TYPE        = 8,
474
        H_SEEN                = 9,
475
        H_CC                = 10,
476
        H_X_FACE        = 11
477
};
478

    
479
MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full)
480
{
481
        static HeaderEntry hentry_full[] = {{"Date:",                NULL, FALSE},
482
                                           {"From:",                NULL, TRUE},
483
                                           {"To:",                NULL, TRUE},
484
                                           {"Newsgroups:",        NULL, TRUE},
485
                                           {"Subject:",                NULL, TRUE},
486
                                           {"Message-Id:",        NULL, FALSE},
487
                                           {"References:",        NULL, FALSE},
488
                                           {"In-Reply-To:",        NULL, FALSE},
489
                                           {"Content-Type:",        NULL, FALSE},
490
                                           {"Seen:",                NULL, FALSE},
491
                                           {"Cc:",                NULL, TRUE},
492
                                           {"X-Face:",                NULL, FALSE},
493
                                           {NULL,                NULL, FALSE}};
494

    
495
        static HeaderEntry hentry_short[] = {{"Date:",                NULL, FALSE},
496
                                            {"From:",                NULL, TRUE},
497
                                            {"To:",                NULL, TRUE},
498
                                            {"Newsgroups:",        NULL, TRUE},
499
                                            {"Subject:",        NULL, TRUE},
500
                                            {"Message-Id:",        NULL, FALSE},
501
                                            {"References:",        NULL, FALSE},
502
                                            {"In-Reply-To:",        NULL, FALSE},
503
                                            {"Content-Type:",        NULL, FALSE},
504
                                            {"Seen:",                NULL, FALSE},
505
                                            {NULL,                NULL, FALSE}};
506

    
507
        MsgInfo *msginfo;
508
        gchar buf[BUFFSIZE], tmp[BUFFSIZE];
509
        gchar *reference = NULL;
510
        gchar *p;
511
        gchar *hp;
512
        HeaderEntry *hentry;
513
        gint hnum;
514

    
515
        hentry = full ? hentry_full : hentry_short;
516

    
517
        if (MSG_IS_QUEUED(flags)) {
518
                while (fgets(buf, sizeof(buf), fp) != NULL)
519
                        if (buf[0] == '\r' || buf[0] == '\n') break;
520
        }
521

    
522
        msginfo = g_new0(MsgInfo, 1);
523
        msginfo->flags = flags;
524
        msginfo->inreplyto = NULL;
525

    
526
        while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
527
               != -1) {
528
                hp = buf + strlen(hentry[hnum].name);
529
                while (*hp == ' ' || *hp == '\t') hp++;
530

    
531
                switch (hnum) {
532
                case H_DATE:
533
                        if (msginfo->date) break;
534
                        msginfo->date_t =
535
                                procheader_date_parse(NULL, hp, 0);
536
                        msginfo->date = g_strdup(hp);
537
                        break;
538
                case H_FROM:
539
                        if (msginfo->from) break;
540
                        conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
541
                        msginfo->from = g_strdup(tmp);
542
                        msginfo->fromname = procheader_get_fromname(tmp);
543
                        break;
544
                case H_TO:
545
                        conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
546
                        if (msginfo->to) {
547
                                p = msginfo->to;
548
                                msginfo->to =
549
                                        g_strconcat(p, ", ", tmp, NULL);
550
                                g_free(p);
551
                        } else
552
                                msginfo->to = g_strdup(tmp);
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
                        conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
566
                        msginfo->subject = g_strdup(tmp);
567
                        break;
568
                case H_MSG_ID:
569
                        if (msginfo->msgid) break;
570

    
571
                        extract_parenthesis(hp, '<', '>');
572
                        remove_space(hp);
573
                        msginfo->msgid = g_strdup(hp);
574
                        break;
575
                case H_REFERENCES:
576
                case H_IN_REPLY_TO:
577
                        if (!reference) {
578
                                eliminate_parenthesis(hp, '(', ')');
579
                                if ((p = strrchr(hp, '<')) != NULL &&
580
                                    strchr(p + 1, '>') != NULL) {
581
                                        extract_parenthesis(p, '<', '>');
582
                                        remove_space(p);
583
                                        if (*p != '\0')
584
                                                reference = g_strdup(p);
585
                                }
586
                        }
587
                        break;
588
                case H_CONTENT_TYPE:
589
                        if (!strncasecmp(hp, "multipart", 9))
590
                                MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
591
                        break;
592
                case H_SEEN:
593
                        /* mnews Seen header */
594
                        MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
595
                        break;
596
                case H_CC:
597
                        conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
598
                        if (msginfo->cc) {
599
                                p = msginfo->cc;
600
                                msginfo->cc =
601
                                        g_strconcat(p, ", ", tmp, NULL);
602
                                g_free(p);
603
                        } else
604
                                msginfo->cc = g_strdup(tmp);
605
                        break;
606
                case H_X_FACE:
607
                        if (msginfo->xface) break;
608
                        msginfo->xface = g_strdup(hp);
609
                        break;
610
                default:
611
                        break;
612
                }
613
        }
614
        msginfo->inreplyto = reference;
615

    
616
        return msginfo;
617
}
618

    
619
gchar *procheader_get_fromname(const gchar *str)
620
{
621
        gchar *tmp, *name;
622

    
623
        Xstrdup_a(tmp, str, return NULL);
624

    
625
        if (*tmp == '\"') {
626
                extract_quote(tmp, '\"');
627
                g_strstrip(tmp);
628
        } else if (strchr(tmp, '<')) {
629
                eliminate_parenthesis(tmp, '<', '>');
630
                g_strstrip(tmp);
631
                if (*tmp == '\0') {
632
                        strcpy(tmp, str);
633
                        extract_parenthesis(tmp, '<', '>');
634
                        g_strstrip(tmp);
635
                }
636
        } else if (strchr(tmp, '(')) {
637
                extract_parenthesis(tmp, '(', ')');
638
                g_strstrip(tmp);
639
        }
640

    
641
        if (*tmp == '\0')
642
                name = g_strdup(str);
643
        else
644
                name = g_strdup(tmp);
645

    
646
        return name;
647
}
648

    
649
static gint procheader_scan_date_string(const gchar *str,
650
                                        gchar *weekday, gint *day,
651
                                        gchar *month, gint *year,
652
                                        gint *hh, gint *mm, gint *ss,
653
                                        gchar *zone)
654
{
655
        gint result;
656

    
657
        result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
658
                        weekday, day, month, year, hh, mm, ss, zone);
659
        if (result == 8) return 0;
660

    
661
        result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
662
                        weekday, day, month, year, hh, mm, ss, zone);
663
        if (result == 8) return 0;
664

    
665
        result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
666
                        day, month, year, hh, mm, ss, zone);
667
        if (result == 7) return 0;
668

    
669
        *zone = '\0';
670
        result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
671
                        weekday, day, month, year, hh, mm, ss);
672
        if (result == 7) return 0;
673

    
674
        *ss = 0;
675
        result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
676
                        weekday, day, month, year, hh, mm, zone);
677
        if (result == 7) return 0;
678

    
679
        result = sscanf(str, "%d %9s %d %2d:%2d %5s",
680
                        day, month, year, hh, mm, zone);
681
        if (result == 6) return 0;
682

    
683
        return -1;
684
}
685

    
686
time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
687
{
688
        static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
689
        gchar weekday[11];
690
        gint day;
691
        gchar month[10];
692
        gint year;
693
        gint hh, mm, ss;
694
        gchar zone[6];
695
        GDateMonth dmonth = G_DATE_BAD_MONTH;
696
        struct tm t;
697
        gchar *p;
698
        time_t timer;
699
        time_t tz_offset;
700

    
701
        if (procheader_scan_date_string(src, weekday, &day, month, &year,
702
                                        &hh, &mm, &ss, zone) < 0) {
703
                g_warning("Invalid date: %s\n", src);
704
                if (dest && len > 0)
705
                        strncpy2(dest, src, len);
706
                return 0;
707
        }
708

    
709
        /* Y2K compliant :) */
710
        if (year < 1000) {
711
                if (year < 50)
712
                        year += 2000;
713
                else
714
                        year += 1900;
715
        }
716

    
717
        month[3] = '\0';
718
        for (p = monthstr; *p != '\0'; p += 3) {
719
                if (!strncasecmp(p, month, 3)) {
720
                        dmonth = (gint)(p - monthstr) / 3 + 1;
721
                        break;
722
                }
723
        }
724
        if (*p == '\0')
725
                g_warning("Invalid month: %s\n", month);
726

    
727
        t.tm_sec = ss;
728
        t.tm_min = mm;
729
        t.tm_hour = hh;
730
        t.tm_mday = day;
731
        t.tm_mon = dmonth - 1;
732
        t.tm_year = year - 1900;
733
        t.tm_wday = 0;
734
        t.tm_yday = 0;
735
        t.tm_isdst = -1;
736

    
737
        timer = mktime(&t);
738
        tz_offset = remote_tzoffset_sec(zone);
739
        if (tz_offset != -1)
740
                timer += tzoffset_sec(&timer) - tz_offset;
741

    
742
        if (dest)
743
                procheader_date_get_localtime(dest, len, timer);
744

    
745
        return timer;
746
}
747

    
748
void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
749
{
750
        struct tm *lt;
751
        gchar *default_format = "%y/%m/%d(%a) %H:%M";
752
        gchar *tmp;
753

    
754
        Xalloca(tmp, len + 1, dest[0] = '\0'; return;);
755

    
756
        lt = localtime(&timer);
757

    
758
        if (prefs_common.date_format)
759
                strftime(tmp, len, prefs_common.date_format, lt);
760
        else
761
                strftime(tmp, len, default_format, lt);
762

    
763
        conv_localetodisp(dest, len, tmp);
764
}