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