c2e151e2b7ba7eb0dae3aeb703222a5758277818
[claws.git] / src / procheader.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 typedef char *(*getlinefunc) (char *, size_t, void *);
41 typedef int (*peekcharfunc) (void *);
42 typedef int (*getcharfunc) (void *);
43 typedef gint (*get_one_field_func) (gchar *, size_t, void *, HeaderEntry[]);
44
45 static gint string_get_one_field(gchar *buf, size_t len, char **str,
46                                  HeaderEntry hentry[]);
47
48 static char *string_getline(char *buf, size_t len, char **str);
49 static int string_peekchar(char **str);
50 static int file_peekchar(FILE *fp);
51 static gint generic_get_one_field(gchar *buf, size_t len, void *data,
52                                   HeaderEntry hentry[],
53                                   getlinefunc getline, 
54                                   peekcharfunc peekchar,
55                                   gboolean unfold);
56 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
57                              gboolean full, gboolean decrypted);
58
59
60 gint procheader_get_one_field(gchar *buf, size_t len, FILE *fp,
61                               HeaderEntry hentry[])
62 {
63         return generic_get_one_field(buf, len, fp, hentry,
64                                      (getlinefunc)fgets, (peekcharfunc)file_peekchar,
65                                      TRUE);
66 }
67
68 static gint string_get_one_field(gchar *buf, size_t len, char **str,
69                                  HeaderEntry hentry[])
70 {
71         return generic_get_one_field(buf, len, str, hentry,
72                                      (getlinefunc)string_getline,
73                                      (peekcharfunc)string_peekchar,
74                                      TRUE);
75 }
76
77 static char *string_getline(char *buf, size_t len, char **str)
78 {
79         if (!*str || !**str)
80                 return NULL;
81
82         for (; **str && len > 1; --len)
83                 if ((*buf++ = *(*str)++) == '\n')
84                     break;
85         *buf = '\0';
86
87         return buf;
88 }
89
90 static int string_peekchar(char **str)
91 {
92         return **str;
93 }
94
95 static int file_peekchar(FILE *fp)
96 {
97         return ungetc(getc(fp), fp);
98 }
99
100 static gint generic_get_one_field(gchar *buf, size_t len, void *data,
101                           HeaderEntry *hentry,
102                           getlinefunc getline, peekcharfunc peekchar,
103                           gboolean unfold)
104 {
105         gint nexthead;
106         gint hnum = 0;
107         HeaderEntry *hp = NULL;
108
109         if (hentry != NULL) {
110                 /* skip non-required headers */
111                 do {
112                         do {
113                                 if (getline(buf, len, data) == NULL)
114                                         return -1;
115                                 if (buf[0] == '\r' || buf[0] == '\n')
116                                         return -1;
117                         } while (buf[0] == ' ' || buf[0] == '\t');
118
119                         for (hp = hentry, hnum = 0; hp->name != NULL;
120                              hp++, hnum++) {
121                                 if (!g_ascii_strncasecmp(hp->name, buf,
122                                                  strlen(hp->name)))
123                                         break;
124                         }
125                 } while (hp->name == NULL);
126         } else {
127                 if (getline(buf, len, data) == NULL) return -1;
128                 if (buf[0] == '\r' || buf[0] == '\n') return -1;
129         }
130
131         /* unfold line */
132         while (1) {
133                 nexthead = peekchar(data);
134                 /* ([*WSP CRLF] 1*WSP) */
135                 if (nexthead == ' ' || nexthead == '\t') {
136                         size_t buflen;
137                         /* trim previous trailing \n if requesting one header or
138                          * unfolding was requested */
139                         if ((!hentry && unfold) || (hp && hp->unfold))
140                                 strretchomp(buf);
141
142                         buflen = strlen(buf);
143                         
144                         /* concatenate next line */
145                         if ((len - buflen) > 2) {
146                                 if (getline(buf + buflen, len - buflen, data) == NULL)
147                                         break;
148                         } else
149                                 break;
150                 } else {
151                         /* remove trailing new line */
152                         strretchomp(buf);
153                         break;
154                 }
155         }
156
157         return hnum;
158 }
159
160 gint procheader_get_one_field_asis(gchar *buf, size_t len, FILE *fp)
161 {
162         return generic_get_one_field(buf, len, fp, NULL,
163                                      (getlinefunc)fgets, 
164                                      (peekcharfunc)file_peekchar,
165                                      FALSE);
166 }
167
168 GPtrArray *procheader_get_header_array_asis(FILE *fp)
169 {
170         gchar buf[BUFFSIZE];
171         GPtrArray *headers;
172         Header *header;
173
174         g_return_val_if_fail(fp != NULL, NULL);
175
176         headers = g_ptr_array_new();
177
178         while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
179                 if ((header = procheader_parse_header(buf)) != NULL)
180                         g_ptr_array_add(headers, header);
181                         /*
182                 if (*buf == ':') continue;
183                 for (p = buf; *p && *p != ' '; p++) {
184                         if (*p == ':') {
185                                 header = g_new(Header, 1);
186                                 header->name = g_strndup(buf, p - buf);
187                                 p++;
188                                 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
189                                 header->body = g_strdup(tmp);
190
191                                 g_ptr_array_add(headers, header);
192                                 break;
193                         }
194                 }
195                         */
196         }
197
198         return headers;
199 }
200
201 void procheader_header_array_destroy(GPtrArray *harray)
202 {
203         gint i;
204         Header *header;
205
206         for (i = 0; i < harray->len; i++) {
207                 header = g_ptr_array_index(harray, i);
208                 procheader_header_free(header);
209         }
210
211         g_ptr_array_free(harray, TRUE);
212 }
213
214 void procheader_header_free(Header *header)
215 {
216         if (!header) return;
217
218         g_free(header->name);
219         g_free(header->body);
220         g_free(header);
221 }
222
223 /*
224   tests whether two headers' names are equal
225   remove the trailing ':' or ' ' before comparing
226 */
227
228 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
229 {
230         int len1;
231         int len2;
232
233         len1 = strlen(hdr1);
234         len2 = strlen(hdr2);
235         if (hdr1[len1 - 1] == ':')
236                 len1--;
237         if (hdr2[len2 - 1] == ':')
238                 len2--;
239         if (len1 != len2)
240                 return 0;
241
242         return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
243 }
244
245 /*
246   parse headers, for example :
247   From: dinh@enseirb.fr becomes :
248   header->name = "From:"
249   header->body = "dinh@enseirb.fr"
250  */
251
252 Header * procheader_parse_header(gchar * buf)
253 {
254         gchar *p = buf;
255         Header * header;
256
257         if ((*buf == ':') || (*buf == ' '))
258                 return NULL;
259
260         for (p = buf; *p ; p++) {
261                 if ((*p == ':') || (*p == ' ')) {
262                         header = g_new(Header, 1);
263                         header->name = g_strndup(buf, p - buf + 1);
264                         p++;
265                         while (*p == ' ' || *p == '\t') p++;
266                         header->body = conv_unmime_header(p, NULL);
267                         return header;
268                 }
269         }
270         return NULL;
271 }
272
273 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
274 {
275         gchar buf[BUFFSIZE];
276         HeaderEntry *hp;
277         gint hnum;
278         gchar *p;
279
280         if (hentry == NULL) return;
281
282         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
283                != -1) {
284                 hp = hentry + hnum;
285
286                 p = buf + strlen(hp->name);
287                 while (*p == ' ' || *p == '\t') p++;
288
289                 if (hp->body == NULL)
290                         hp->body = g_strdup(p);
291                 else if (procheader_headername_equal(hp->name, "To") ||
292                          procheader_headername_equal(hp->name, "Cc")) {
293                         gchar *tp = hp->body;
294                         hp->body = g_strconcat(tp, ", ", p, NULL);
295                         g_free(tp);
296                 }
297         }
298 }
299
300 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
301                                gboolean full, gboolean decrypted)
302 {
303         struct stat s;
304         FILE *fp;
305         MsgInfo *msginfo;
306
307         if (stat(file, &s) < 0) {
308                 FILE_OP_ERROR(file, "stat");
309                 return NULL;
310         }
311         if (!S_ISREG(s.st_mode))
312                 return NULL;
313
314         if ((fp = g_fopen(file, "rb")) == NULL) {
315                 FILE_OP_ERROR(file, "fopen");
316                 return NULL;
317         }
318
319         msginfo = procheader_parse_stream(fp, flags, full, decrypted);
320         fclose(fp);
321
322         if (msginfo) {
323                 msginfo->size = s.st_size;
324                 msginfo->mtime = s.st_mtime;
325         }
326
327         return msginfo;
328 }
329
330 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
331                               gboolean decrypted)
332 {
333         return parse_stream(&str, TRUE, flags, full, decrypted);
334 }
335
336 enum
337 {
338         H_DATE          = 0,
339         H_FROM          = 1,
340         H_TO            = 2,
341         H_CC            = 3,
342         H_NEWSGROUPS    = 4,
343         H_SUBJECT       = 5,
344         H_MSG_ID        = 6,
345         H_REFERENCES    = 7,
346         H_IN_REPLY_TO   = 8,
347         H_CONTENT_TYPE  = 9,
348         H_SEEN          = 10,
349         H_STATUS        = 11,
350         H_X_STATUS      = 12,
351         H_FROM_SPACE    = 13,
352         H_SC_PLANNED_DOWNLOAD = 14,
353         H_SC_MESSAGE_SIZE = 15,
354         H_FACE          = 16,
355         H_X_FACE        = 17,
356         H_DISPOSITION_NOTIFICATION_TO = 18,
357         H_RETURN_RECEIPT_TO = 19,
358         H_SC_PARTIALLY_RETRIEVED = 20,
359         H_SC_ACCOUNT_SERVER = 21,
360         H_SC_ACCOUNT_LOGIN = 22,
361 };
362
363 static HeaderEntry hentry_full[] = {{"Date:",           NULL, FALSE},
364                                    {"From:",            NULL, TRUE},
365                                    {"To:",              NULL, TRUE},
366                                    {"Cc:",              NULL, TRUE},
367                                    {"Newsgroups:",      NULL, TRUE},
368                                    {"Subject:",         NULL, TRUE},
369                                    {"Message-ID:",      NULL, FALSE},
370                                    {"References:",      NULL, FALSE},
371                                    {"In-Reply-To:",     NULL, FALSE},
372                                    {"Content-Type:",    NULL, FALSE},
373                                    {"Seen:",            NULL, FALSE},
374                                    {"Status:",          NULL, FALSE},
375                                    {"X-Status:",        NULL, FALSE},
376                                    {"From ",            NULL, FALSE},
377                                    {"SC-Marked-For-Download:", NULL, FALSE},
378                                    {"SC-Message-Size:", NULL, FALSE},
379                                    {"Face:",            NULL, FALSE},
380                                    {"X-Face:",          NULL, FALSE},
381                                    {"Disposition-Notification-To:", NULL, FALSE},
382                                    {"Return-Receipt-To:", NULL, FALSE},
383                                    {"SC-Partially-Retrieved:", NULL, FALSE},
384                                    {"SC-Account-Server:", NULL, FALSE},
385                                    {"SC-Account-Login:",NULL, FALSE},
386                                    {NULL,               NULL, FALSE}};
387
388 static HeaderEntry hentry_short[] = {{"Date:",          NULL, FALSE},
389                                     {"From:",           NULL, TRUE},
390                                     {"To:",             NULL, TRUE},
391                                     {"Cc:",             NULL, TRUE},
392                                     {"Newsgroups:",     NULL, TRUE},
393                                     {"Subject:",        NULL, TRUE},
394                                     {"Message-ID:",     NULL, FALSE},
395                                     {"References:",     NULL, FALSE},
396                                     {"In-Reply-To:",    NULL, FALSE},
397                                     {"Content-Type:",   NULL, FALSE},
398                                     {"Seen:",           NULL, FALSE},
399                                     {"Status:",         NULL, FALSE},
400                                     {"X-Status:",       NULL, FALSE},
401                                     {"From ",           NULL, FALSE},
402                                     {"SC-Marked-For-Download:", NULL, FALSE},
403                                     {"SC-Message-Size:",NULL, FALSE},
404                                     {NULL,              NULL, FALSE}};
405
406 HeaderEntry* procheader_get_headernames(gboolean full)
407 {
408         return full ? hentry_full : hentry_short;
409 }
410
411 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
412                                  gboolean decrypted)
413 {
414         return parse_stream(fp, FALSE, flags, full, decrypted);
415 }
416
417 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
418                              gboolean full, gboolean decrypted)
419 {
420         MsgInfo *msginfo;
421         gchar buf[BUFFSIZE];
422         gchar *p, *tmp;
423         gchar *hp;
424         HeaderEntry *hentry;
425         gint hnum;
426         get_one_field_func get_one_field =
427                 isstring ? (get_one_field_func)string_get_one_field
428                          : (get_one_field_func)procheader_get_one_field;
429
430         hentry = procheader_get_headernames(full);
431
432         if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
433                 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
434                         ; /* loop */
435         }
436
437         msginfo = procmsg_msginfo_new();
438         
439         if (flags.tmp_flags || flags.perm_flags) 
440                 msginfo->flags = flags;
441         else 
442                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
443         
444         msginfo->inreplyto = NULL;
445
446         while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
447                != -1) {
448                 hp = buf + strlen(hentry[hnum].name);
449                 while (*hp == ' ' || *hp == '\t') hp++;
450
451                 switch (hnum) {
452                 case H_DATE:
453                         if (msginfo->date) break;
454                         msginfo->date_t =
455                                 procheader_date_parse(NULL, hp, 0);
456                         msginfo->date = g_strdup(hp);
457                         break;
458                 case H_FROM:
459                         if (msginfo->from) break;
460                         msginfo->from = conv_unmime_header(hp, NULL);
461                         msginfo->fromname = procheader_get_fromname(msginfo->from);
462                         replace_returns(msginfo->from);
463                         replace_returns(msginfo->fromname);
464                         break;
465                 case H_TO:
466                         tmp = conv_unmime_header(hp, NULL);
467                         if (msginfo->to) {
468                                 p = msginfo->to;
469                                 msginfo->to =
470                                         g_strconcat(p, ", ", tmp, NULL);
471                                 g_free(p);
472                         } else
473                                 msginfo->to = g_strdup(tmp);
474                         g_free(tmp);                                
475                         break;
476                 case H_CC:
477                         tmp = conv_unmime_header(hp, NULL);
478                         if (msginfo->cc) {
479                                 p = msginfo->cc;
480                                 msginfo->cc =
481                                         g_strconcat(p, ", ", tmp, NULL);
482                                 g_free(p);
483                         } else
484                                 msginfo->cc = g_strdup(tmp);
485                         g_free(tmp);                                
486                         break;
487                 case H_NEWSGROUPS:
488                         if (msginfo->newsgroups) {
489                                 p = msginfo->newsgroups;
490                                 msginfo->newsgroups =
491                                         g_strconcat(p, ",", hp, NULL);
492                                 g_free(p);
493                         } else
494                                 msginfo->newsgroups = g_strdup(hp);
495                         break;
496                 case H_SUBJECT:
497                         if (msginfo->subject) break;
498                         msginfo->subject = conv_unmime_header(hp, NULL);
499                         replace_returns(msginfo->subject);
500                         break;
501                 case H_MSG_ID:
502                         if (msginfo->msgid) break;
503
504                         extract_parenthesis(hp, '<', '>');
505                         remove_space(hp);
506                         msginfo->msgid = g_strdup(hp);
507                         break;
508                 case H_REFERENCES:
509                         msginfo->references =
510                                 references_list_prepend(msginfo->references,
511                                                         hp);
512                         break;
513                 case H_IN_REPLY_TO:
514                         if (msginfo->inreplyto) break;
515
516                         eliminate_parenthesis(hp, '(', ')');
517                         if ((p = strrchr(hp, '<')) != NULL &&
518                             strchr(p + 1, '>') != NULL) {
519                                 extract_parenthesis(p, '<', '>');
520                                 remove_space(p);
521                                 if (*p != '\0')
522                                         msginfo->inreplyto = g_strdup(p);
523                         }
524                         break;
525                 case H_CONTENT_TYPE:
526                         if (!g_ascii_strncasecmp(hp, "multipart/", 10))
527                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
528                         break;
529 #ifdef ALLOW_HEADER_HINT                        
530                 case H_SEEN:
531                         /* mnews Seen header */
532                         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
533                         break;
534 #endif                  
535                 case H_FACE:
536                         if (msginfo->face) break;
537                         msginfo->face = g_strdup(hp);
538                         break;
539                 case H_X_FACE:
540                         if (msginfo->xface) break;
541                         msginfo->xface = g_strdup(hp);
542                         break;
543                 case H_DISPOSITION_NOTIFICATION_TO:
544                         if (msginfo->dispositionnotificationto) break;
545                         msginfo->dispositionnotificationto = g_strdup(hp);
546                         break;
547                 case H_RETURN_RECEIPT_TO:
548                         if (msginfo->returnreceiptto) break;
549                         msginfo->returnreceiptto = g_strdup(hp);
550                         break;
551                 case H_SC_PARTIALLY_RETRIEVED:
552                         if (msginfo->partial_recv) break;
553                         msginfo->partial_recv = g_strdup(hp);
554                         break;
555                 case H_SC_ACCOUNT_SERVER:
556                         if (msginfo->account_server) break;
557                         msginfo->account_server = g_strdup(hp);
558                         break;
559                 case H_SC_ACCOUNT_LOGIN:
560                         if (msginfo->account_login) break;
561                         msginfo->account_login = g_strdup(hp);
562                         break;
563                 case H_SC_MESSAGE_SIZE:
564                         if (msginfo->total_size) break;
565                         msginfo->total_size = atoi(hp);
566                         break;
567                 case H_SC_PLANNED_DOWNLOAD:
568                         msginfo->planned_download = atoi(hp);
569                         break;
570 #ifdef ALLOW_HEADER_HINT                        
571                 case H_STATUS:
572                         if (strchr(hp, 'R') != NULL)
573                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
574                         if (strchr(hp, 'O') != NULL)
575                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
576                         if (strchr(hp, 'U') != NULL)
577                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
578                         break;
579                 case H_X_STATUS:
580                         if (strchr(hp, 'D') != NULL)
581                                 MSG_SET_PERM_FLAGS(msginfo->flags,
582                                               MSG_REALLY_DELETED);
583                         if (strchr(hp, 'F') != NULL)
584                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
585                         if (strchr(hp, 'd') != NULL)
586                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
587                         if (strchr(hp, 'r') != NULL)
588                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
589                         if (strchr(hp, 'f') != NULL)
590                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
591                         break;
592 #endif                  
593                 case H_FROM_SPACE:
594                         if (msginfo->fromspace) break;
595                         msginfo->fromspace = g_strdup(hp);
596                         break;
597                 default:
598                         break;
599                 }
600         }
601
602         if (!msginfo->inreplyto && msginfo->references)
603                 msginfo->inreplyto =
604                         g_strdup((gchar *)msginfo->references->data);
605
606         return msginfo;
607 }
608
609 gchar *procheader_get_fromname(const gchar *str)
610 {
611         gchar *tmp, *name;
612
613         Xstrdup_a(tmp, str, return NULL);
614
615         if (*tmp == '\"') {
616                 extract_quote(tmp, '\"');
617                 g_strstrip(tmp);
618         } else if (strchr(tmp, '<')) {
619                 eliminate_parenthesis(tmp, '<', '>');
620                 g_strstrip(tmp);
621                 if (*tmp == '\0') {
622                         strcpy(tmp, str);
623                         extract_parenthesis(tmp, '<', '>');
624                         g_strstrip(tmp);
625                 }
626         } else if (strchr(tmp, '(')) {
627                 extract_parenthesis(tmp, '(', ')');
628                 g_strstrip(tmp);
629         }
630
631         if (*tmp == '\0')
632                 name = g_strdup(str);
633         else
634                 name = g_strdup(tmp);
635
636         return name;
637 }
638
639 static gint procheader_scan_date_string(const gchar *str,
640                                         gchar *weekday, gint *day,
641                                         gchar *month, gint *year,
642                                         gint *hh, gint *mm, gint *ss,
643                                         gchar *zone)
644 {
645         gint result;
646
647         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
648                         weekday, day, month, year, hh, mm, ss, zone);
649         if (result == 8) return 0;
650
651         result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
652                         weekday, day, month, year, hh, mm, ss, zone);
653         if (result == 8) return 0;
654
655         result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
656                         day, month, year, hh, mm, ss, zone);
657         if (result == 7) return 0;
658
659         *zone = '\0';
660         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
661                         weekday, day, month, year, hh, mm, ss);
662         if (result == 7) return 0;
663
664         result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
665                         day, month, year, hh, mm, ss);
666         if (result == 6) return 0;
667
668         *ss = 0;
669         result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
670                         weekday, day, month, year, hh, mm, zone);
671         if (result == 7) return 0;
672
673         result = sscanf(str, "%d %9s %d %2d:%2d %5s",
674                         day, month, year, hh, mm, zone);
675         if (result == 6) return 0;
676
677         *zone = '\0';
678         result = sscanf(str, "%10s %d %9s %d %2d:%2d",
679                         weekday, day, month, year, hh, mm);
680         if (result == 6) return 0;
681
682         result = sscanf(str, "%d %9s %d %2d:%2d",
683                         day, month, year, hh, mm);
684         if (result == 5) return 0;
685
686         return -1;
687 }
688
689 /*
690  * Hiro, most UNIXen support this function:
691  * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
692  */
693 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
694 {
695         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
696         gchar weekday[11];
697         gint day;
698         gchar month[10];
699         gint year;
700         gint hh, mm, ss;
701         GDateMonth dmonth;
702         gchar *p;
703
704         if (!t)
705                 return FALSE;
706         
707         memset(t, 0, sizeof *t);        
708
709         if (procheader_scan_date_string(src, weekday, &day, month, &year,
710                                         &hh, &mm, &ss, zone) < 0) {
711                 g_warning("Invalid date: %s\n", src);
712                 return FALSE;
713         }
714
715         /* Y2K compliant :) */
716         if (year < 100) {
717                 if (year < 70)
718                         year += 2000;
719                 else
720                         year += 1900;
721         }
722
723         month[3] = '\0';
724         if ((p = strstr(monthstr, month)) != NULL)
725                 dmonth = (gint)(p - monthstr) / 3 + 1;
726         else {
727                 g_warning("Invalid month: %s\n", month);
728                 dmonth = G_DATE_BAD_MONTH;
729         }
730
731         t->tm_sec = ss;
732         t->tm_min = mm;
733         t->tm_hour = hh;
734         t->tm_mday = day;
735         t->tm_mon = dmonth - 1;
736         t->tm_year = year - 1900;
737         t->tm_wday = 0;
738         t->tm_yday = 0;
739         t->tm_isdst = -1;
740
741         mktime(t);
742
743         return TRUE;
744 }
745
746 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
747 {
748         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
749         gchar weekday[11];
750         gint day;
751         gchar month[10];
752         gint year;
753         gint hh, mm, ss;
754         gchar zone[6];
755         GDateMonth dmonth = G_DATE_BAD_MONTH;
756         struct tm t;
757         gchar *p;
758         time_t timer;
759         time_t tz_offset;
760
761         if (procheader_scan_date_string(src, weekday, &day, month, &year,
762                                         &hh, &mm, &ss, zone) < 0) {
763                 if (dest && len > 0)
764                         strncpy2(dest, src, len);
765                 return 0;
766         }
767
768         /* Y2K compliant :) */
769         if (year < 1000) {
770                 if (year < 50)
771                         year += 2000;
772                 else
773                         year += 1900;
774         }
775
776         month[3] = '\0';
777         for (p = monthstr; *p != '\0'; p += 3) {
778                 if (!g_ascii_strncasecmp(p, month, 3)) {
779                         dmonth = (gint)(p - monthstr) / 3 + 1;
780                         break;
781                 }
782         }
783
784         t.tm_sec = ss;
785         t.tm_min = mm;
786         t.tm_hour = hh;
787         t.tm_mday = day;
788         t.tm_mon = dmonth - 1;
789         t.tm_year = year - 1900;
790         t.tm_wday = 0;
791         t.tm_yday = 0;
792         t.tm_isdst = -1;
793
794         timer = mktime(&t);
795         tz_offset = remote_tzoffset_sec(zone);
796         if (tz_offset != -1)
797                 timer += tzoffset_sec(&timer) - tz_offset;
798
799         if (dest)
800                 procheader_date_get_localtime(dest, len, timer);
801
802         return timer;
803 }
804
805 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
806 {
807         struct tm *lt;
808         gchar *default_format = "%y/%m/%d(%a) %H:%M";
809         gchar *str;
810         const gchar *src_codeset, *dest_codeset;
811
812         lt = localtime(&timer);
813
814         if (prefs_common.date_format)
815                 strftime(dest, len, prefs_common.date_format, lt);
816         else
817                 strftime(dest, len, default_format, lt);
818
819         src_codeset = conv_get_locale_charset_str();
820         dest_codeset = CS_UTF_8;
821         str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
822         if (str) {
823                 g_snprintf(dest, len, "%s", str);
824                 strncpy2(dest, str, len);
825                 g_free(str);
826         }
827 }
828
829 /* Added by Mel Hadasht on 27 Aug 2001 */
830 /* Get a header from msginfo */
831 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
832 {
833         gchar *file;
834         FILE *fp;
835         HeaderEntry hentry[]={ { NULL, NULL, TRUE  },
836                                { NULL, NULL, FALSE } };
837         gint val;
838
839         hentry[0].name = header;
840        
841         g_return_val_if_fail(msginfo != NULL, -1);
842         file = procmsg_get_message_file_path(msginfo);
843         if ((fp = g_fopen(file, "rb")) == NULL) {
844                FILE_OP_ERROR(file, "fopen");
845                g_free(file);
846                return -1;
847         }
848         val = procheader_get_one_field(buf,len, fp, hentry);
849         if (fclose(fp) == EOF) {
850                 FILE_OP_ERROR(file, "fclose");
851                 g_unlink(file);
852                 g_free(file);
853                 return -1;
854         }
855
856         g_free(file);
857         if (val == -1)
858                 return -1;
859
860         return 0;
861 }