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