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