sync with sylpheed 0.6.4cvs6
[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
458 /* FIXME: we should not allow headers in messages to change the sylpheed marks
459  * so we're currently disabling setting any MsgFlags when detecting X-Seen,
460  * Seen, X-Status, Status. See macro ALLOW_HEADER_HINT */  
461
462 #define ALLOW_HEADER_HINT
463
464 MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
465                                gboolean full)
466 {
467         static HeaderEntry hentry_full[] = {{"Date:",           NULL, FALSE},
468                                            {"From:",            NULL, TRUE},
469                                            {"To:",              NULL, TRUE},
470                                            {"Cc:",              NULL, TRUE},
471                                            {"Newsgroups:",      NULL, TRUE},
472                                            {"Subject:",         NULL, TRUE},
473                                            {"Message-Id:",      NULL, FALSE},
474                                            {"References:",      NULL, FALSE},
475                                            {"In-Reply-To:",     NULL, FALSE},
476                                            {"Content-Type:",    NULL, FALSE},
477                                            {"Seen:",            NULL, FALSE},
478                                            {"Status:",          NULL, FALSE},
479                                            {"X-Status:",        NULL, FALSE},
480                                            {"From ",            NULL, FALSE},
481                                            {"X-Face:",          NULL, FALSE},
482                                            {"Disposition-Notification-To:", NULL, FALSE},
483                                            {"Return-Receipt-To:", NULL, FALSE},
484                                            {NULL,               NULL, FALSE}};
485
486         static HeaderEntry hentry_short[] = {{"Date:",          NULL, FALSE},
487                                             {"From:",           NULL, TRUE},
488                                             {"To:",             NULL, TRUE},
489                                             {"Cc:",             NULL, TRUE},
490                                             {"Newsgroups:",     NULL, TRUE},
491                                             {"Subject:",        NULL, TRUE},
492                                             {"Message-Id:",     NULL, FALSE},
493                                             {"References:",     NULL, FALSE},
494                                             {"In-Reply-To:",    NULL, FALSE},
495                                             {"Content-Type:",   NULL, FALSE},
496                                             {"Seen:",           NULL, FALSE},
497                                             {"Status:",         NULL, FALSE},
498                                             {"X-Status:",       NULL, FALSE},
499                                             {"From ",           NULL, FALSE},
500                                             {NULL,              NULL, FALSE}};
501         
502         MsgInfo *msginfo;
503         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
504         gchar *reference = NULL;
505         gchar *p;
506         gchar *hp;
507         HeaderEntry *hentry;
508         gint hnum;
509
510         hentry = full ? hentry_full : hentry_short;
511
512         if (MSG_IS_QUEUED(flags)) {
513                 while (fgets(buf, sizeof(buf), fp) != NULL)
514                         if (buf[0] == '\r' || buf[0] == '\n') break;
515         }
516
517         msginfo = g_new0(MsgInfo, 1);
518         
519         if (flags.tmp_flags || flags.perm_flags) 
520                 msginfo->flags = flags;
521         else 
522                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
523         
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
530                 while (*hp == ' ' || *hp == '\t') hp++;
531
532                 switch (hnum) {
533                 case H_DATE:
534                         if (msginfo->date) break;
535                         msginfo->date_t =
536                                 procheader_date_parse(NULL, hp, 0);
537                         msginfo->date = g_strdup(hp);
538                         break;
539                 case H_FROM:
540                         if (msginfo->from) break;
541                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
542                         msginfo->from = g_strdup(tmp);
543                         msginfo->fromname = procheader_get_fromname(tmp);
544                         break;
545                 case H_TO:
546                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
547                         if (msginfo->to) {
548                                 p = msginfo->to;
549                                 msginfo->to =
550                                         g_strconcat(p, ", ", tmp, NULL);
551                                 g_free(p);
552                         } else
553                                 msginfo->to = g_strdup(tmp);
554                         break;
555                 case H_CC:
556                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
557                         if (msginfo->cc) {
558                                 p = msginfo->cc;
559                                 msginfo->cc =
560                                         g_strconcat(p, ", ", tmp, NULL);
561                                 g_free(p);
562                         } else
563                                 msginfo->cc = g_strdup(tmp);
564                         break;
565                 case H_NEWSGROUPS:
566                         if (msginfo->newsgroups) {
567                                 p = msginfo->newsgroups;
568                                 msginfo->newsgroups =
569                                         g_strconcat(p, ",", hp, NULL);
570                                 g_free(p);
571                         } else
572                                 msginfo->newsgroups = g_strdup(buf + 12);
573                         break;
574                 case H_SUBJECT:
575                         if (msginfo->subject) break;
576                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
577                         msginfo->subject = g_strdup(tmp);
578                         break;
579                 case H_MSG_ID:
580                         if (msginfo->msgid) break;
581
582                         extract_parenthesis(hp, '<', '>');
583                         remove_space(hp);
584                         msginfo->msgid = g_strdup(hp);
585                         break;
586                 case H_REFERENCES:
587                 case H_IN_REPLY_TO:
588                         if (!reference) {
589                                 msginfo->references = g_strdup(hp);
590                                 eliminate_parenthesis(hp, '(', ')');
591                                 if ((p = strrchr(hp, '<')) != NULL &&
592                                     strchr(p + 1, '>') != NULL) {
593                                         extract_parenthesis(p, '<', '>');
594                                         remove_space(p);
595                                         if (*p != '\0')
596                                                 reference = g_strdup(p);
597                                 }
598                         }
599                         break;
600                 case H_CONTENT_TYPE:
601                         if (!strncasecmp(hp, "multipart", 9))
602                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
603                         break;
604 #ifdef ALLOW_HEADER_HINT                        
605                 case H_SEEN:
606                         /* mnews Seen header */
607                         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
608                         break;
609 #endif                  
610                 case H_X_FACE:
611                         if (msginfo->xface) break;
612                         msginfo->xface = g_strdup(hp);
613                         break;
614                 case H_DISPOSITION_NOTIFICATION_TO:
615                         if (msginfo->dispositionnotificationto) break;
616                         msginfo->dispositionnotificationto = g_strdup(hp);
617                         break;
618                 case H_RETURN_RECEIPT_TO:
619                         if (msginfo->returnreceiptto) break;
620                         msginfo->returnreceiptto = g_strdup(hp);
621                         break;
622 #ifdef ALLOW_HEADER_HINT                        
623                 case H_STATUS:
624                         if (strchr(hp, 'R') != NULL)
625                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
626                         if (strchr(hp, 'O') != NULL)
627                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
628                         if (strchr(hp, 'U') != NULL)
629                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
630                         break;
631                 case H_X_STATUS:
632                         if (strchr(hp, 'D') != NULL)
633                                 MSG_SET_PERM_FLAGS(msginfo->flags,
634                                               MSG_REALLY_DELETED);
635                         if (strchr(hp, 'F') != NULL)
636                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
637                         if (strchr(hp, 'd') != NULL)
638                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
639                         if (strchr(hp, 'r') != NULL)
640                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
641                         if (strchr(hp, 'f') != NULL)
642                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
643                         break;
644 #endif                  
645                 case H_FROM_SPACE:
646                         if (msginfo->fromspace) break;
647                         msginfo->fromspace = g_strdup(hp);
648                         break;
649                 default:
650                         break;
651                 }
652         }
653         msginfo->inreplyto = reference;
654
655         return msginfo;
656 }
657
658 gchar *procheader_get_fromname(const gchar *str)
659 {
660         gchar *tmp, *name;
661
662         Xalloca(tmp, strlen(str) + 1, return NULL);
663         strcpy(tmp, str);
664
665         if (*tmp == '\"') {
666                 extract_quote(tmp, '\"');
667                 g_strstrip(tmp);
668         } else if (strchr(tmp, '<')) {
669                 eliminate_parenthesis(tmp, '<', '>');
670                 g_strstrip(tmp);
671                 if (*tmp == '\0') {
672                         strcpy(tmp, str);
673                         extract_parenthesis(tmp, '<', '>');
674                         g_strstrip(tmp);
675                 }
676         } else if (strchr(tmp, '(')) {
677                 extract_parenthesis(tmp, '(', ')');
678                 g_strstrip(tmp);
679         }
680
681         if (*tmp == '\0')
682                 name = g_strdup(str);
683         else
684                 name = g_strdup(tmp);
685
686         return name;
687 }
688
689 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
690 {
691         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
692         gchar weekday[4];
693         gint day;
694         gchar month[4];
695         gint year;
696         gint hh, mm, ss;
697         gchar zone[6];
698         gint result;
699         GDateMonth dmonth;
700         struct tm t;
701         gchar *p;
702         time_t timer;
703
704         /* parsing date field... */
705         result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
706                         weekday, &day, month, &year, &hh, &mm, &ss, zone);
707         if (result != 8) {
708                 result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
709                                 &day, month, &year, &hh, &mm, &ss, zone);
710                 if (result != 7) {
711                         ss = 0;
712                         result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
713                                         weekday, &day, month, &year, &hh, &mm, zone);
714                         if (result != 7) {
715                                 result = sscanf(src, "%d %3s %d %2d:%2d %5s",
716                                                 &day, month, &year, &hh, &mm,
717                                                 zone);
718                                 if (result != 6) {
719                                         g_warning("Invalid date: %s\n", src);
720                                         if (dest && len > 0)
721                                                 strncpy2(dest, src, len);
722                                         return 0;
723                                 }
724                         }
725                 }
726         }
727
728         /* Y2K compliant :) */
729         if (year < 100) {
730                 if (year < 70)
731                         year += 2000;
732                 else
733                         year += 1900;
734         }
735
736         if ((p = strstr(monthstr, month)) != NULL)
737                 dmonth = (gint)(p - monthstr) / 3 + 1;
738         else {
739                 g_warning("Invalid month: %s\n", month);
740                 dmonth = G_DATE_BAD_MONTH;
741         }
742
743         t.tm_sec = ss;
744         t.tm_min = mm;
745         t.tm_hour = hh;
746         t.tm_mday = day;
747         t.tm_mon = dmonth - 1;
748         t.tm_year = year - 1900;
749         t.tm_wday = 0;
750         t.tm_yday = 0;
751         t.tm_isdst = -1;
752
753         timer = mktime(&t);
754         timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
755
756         if (dest)
757                 procheader_date_get_localtime(dest, len, timer);
758
759         return timer;
760 }
761
762 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
763 {
764         struct tm *lt;
765         gchar *default_format = "%y/%m/%d(%a) %H:%M";
766
767         lt = localtime(&timer);
768
769         if (prefs_common.date_format)
770                 strftime(dest, len, prefs_common.date_format, lt);
771         else
772                 strftime(dest, len, default_format, lt);
773 }
774
775 gint get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len,gchar *header)
776 {
777        gchar *file;
778        FILE *fp;
779        HeaderEntry hentry[]={{header,NULL,TRUE},
780                                     {NULL,NULL,FALSE}};
781        gint val;
782        g_return_if_fail(msginfo != NULL);
783        file = procmsg_get_message_file_path(msginfo);
784        if ((fp = fopen(file, "r")) == NULL) {
785                FILE_OP_ERROR(file, "fopen");
786                g_free(file);
787                return;
788         }
789        val=procheader_get_one_field(buf,len, fp, hentry);
790         if (fclose(fp) == EOF) {
791                 FILE_OP_ERROR(file, "fclose");
792                 unlink(file);
793                 return -1;
794         }
795        if (val == -1)
796           return -1;
797        return 0;
798 }