2006-10-19 [colin] 2.5.6cvs2
[claws.git] / src / procheader.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <time.h>
30 #include <sys/stat.h>
31
32 #include "procheader.h"
33 #include "procmsg.h"
34 #include "codeconv.h"
35 #include "prefs_common.h"
36 #include "utils.h"
37
38 #define BUFFSIZE        8192
39
40 typedef char *(*getlinefunc) (char *, size_t, void *);
41 typedef int (*peekcharfunc) (void *);
42 typedef int (*getcharfunc) (void *);
43 typedef gint (*get_one_field_func) (gchar *, size_t, void *, HeaderEntry[]);
44
45 static gint string_get_one_field(gchar *buf, size_t len, char **str,
46                                  HeaderEntry hentry[]);
47
48 static char *string_getline(char *buf, size_t len, char **str);
49 static int string_peekchar(char **str);
50 static int file_peekchar(FILE *fp);
51 static gint generic_get_one_field(gchar *buf, size_t len, void *data,
52                                   HeaderEntry hentry[],
53                                   getlinefunc getline, 
54                                   peekcharfunc peekchar,
55                                   gboolean unfold);
56 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
57                              gboolean full, gboolean decrypted);
58
59
60 gint procheader_get_one_field(gchar *buf, size_t len, FILE *fp,
61                               HeaderEntry hentry[])
62 {
63         return generic_get_one_field(buf, len, fp, hentry,
64                                      (getlinefunc)fgets, (peekcharfunc)file_peekchar,
65                                      TRUE);
66 }
67
68 static gint string_get_one_field(gchar *buf, size_t len, char **str,
69                                  HeaderEntry hentry[])
70 {
71         return generic_get_one_field(buf, len, str, hentry,
72                                      (getlinefunc)string_getline,
73                                      (peekcharfunc)string_peekchar,
74                                      TRUE);
75 }
76
77 static char *string_getline(char *buf, size_t len, char **str)
78 {
79         if (!*str || !**str)
80                 return NULL;
81
82         for (; **str && len > 1; --len)
83                 if ((*buf++ = *(*str)++) == '\n')
84                     break;
85         *buf = '\0';
86
87         return buf;
88 }
89
90 static int string_peekchar(char **str)
91 {
92         return **str;
93 }
94
95 static int file_peekchar(FILE *fp)
96 {
97         return ungetc(getc(fp), fp);
98 }
99
100 static gint generic_get_one_field(gchar *buf, size_t len, void *data,
101                           HeaderEntry *hentry,
102                           getlinefunc getline, peekcharfunc peekchar,
103                           gboolean unfold)
104 {
105         gint nexthead;
106         gint hnum = 0;
107         HeaderEntry *hp = NULL;
108
109         if (hentry != NULL) {
110                 /* skip non-required headers */
111                 do {
112                         do {
113                                 if (getline(buf, len, data) == NULL)
114                                         return -1;
115                                 if (buf[0] == '\r' || buf[0] == '\n')
116                                         return -1;
117                         } while (buf[0] == ' ' || buf[0] == '\t');
118
119                         for (hp = hentry, hnum = 0; hp->name != NULL;
120                              hp++, hnum++) {
121                                 if (!g_ascii_strncasecmp(hp->name, buf,
122                                                  strlen(hp->name)))
123                                         break;
124                         }
125                 } while (hp->name == NULL);
126         } else {
127                 if (getline(buf, len, data) == NULL) return -1;
128                 if (buf[0] == '\r' || buf[0] == '\n') return -1;
129         }
130
131         /* unfold line */
132         while (1) {
133                 nexthead = peekchar(data);
134                 /* ([*WSP CRLF] 1*WSP) */
135                 if (nexthead == ' ' || nexthead == '\t') {
136                         size_t buflen;
137                         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-Sylpheed-End-Special-Headers: 1",
453                                 strlen("X-Sylpheed-End-Special-Headers:")))
454                                 break;
455                         /* from other mailers */
456                         if (!strncmp(buf, "Date: ", 6)
457                         ||  !strncmp(buf, "To: ", 4)
458                         ||  !strncmp(buf, "From: ", 6)
459                         ||  !strncmp(buf, "Subject: ", 9)) {
460                                 if (isstring)
461                                         data = orig_data;
462                                 else 
463                                         rewind((FILE *)data);
464                                 break;
465                         }
466                 }
467         }
468
469         msginfo = procmsg_msginfo_new();
470         
471         if (flags.tmp_flags || flags.perm_flags) 
472                 msginfo->flags = flags;
473         else 
474                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
475         
476         msginfo->inreplyto = NULL;
477
478         while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
479                != -1) {
480                 hp = buf + strlen(hentry[hnum].name);
481                 while (*hp == ' ' || *hp == '\t') hp++;
482
483                 switch (hnum) {
484                 case H_DATE:
485                         if (msginfo->date) break;
486                         msginfo->date_t =
487                                 procheader_date_parse(NULL, hp, 0);
488                         if (g_utf8_validate(hp, -1, NULL)) {
489                                 msginfo->date = g_strdup(hp);
490                         } else {
491                                 gchar *utf = conv_codeset_strdup(
492                                         hp, 
493                                         conv_get_locale_charset_str_no_utf8(),
494                                         CS_INTERNAL);
495                                 if (utf == NULL || 
496                                     !g_utf8_validate(utf, -1, NULL)) {
497                                         g_free(utf);
498                                         utf = g_malloc(strlen(buf)*2+1);
499                                         conv_localetodisp(utf, 
500                                                 strlen(hp)*2+1, hp);
501                                 }
502                                 msginfo->date = utf;
503                         }
504                         break;
505                 case H_FROM:
506                         if (msginfo->from) break;
507                         msginfo->from = conv_unmime_header(hp, NULL);
508                         msginfo->fromname = procheader_get_fromname(msginfo->from);
509                         remove_return(msginfo->from);
510                         remove_return(msginfo->fromname);
511                         break;
512                 case H_TO:
513                         tmp = conv_unmime_header(hp, NULL);
514                         remove_return(tmp);
515                         if (msginfo->to) {
516                                 p = msginfo->to;
517                                 msginfo->to =
518                                         g_strconcat(p, ", ", tmp, NULL);
519                                 g_free(p);
520                         } else
521                                 msginfo->to = g_strdup(tmp);
522                         g_free(tmp);                                
523                         break;
524                 case H_CC:
525                         tmp = conv_unmime_header(hp, NULL);
526                         remove_return(tmp);
527                         if (msginfo->cc) {
528                                 p = msginfo->cc;
529                                 msginfo->cc =
530                                         g_strconcat(p, ", ", tmp, NULL);
531                                 g_free(p);
532                         } else
533                                 msginfo->cc = g_strdup(tmp);
534                         g_free(tmp);                                
535                         break;
536                 case H_NEWSGROUPS:
537                         if (msginfo->newsgroups) {
538                                 p = msginfo->newsgroups;
539                                 msginfo->newsgroups =
540                                         g_strconcat(p, ",", hp, NULL);
541                                 g_free(p);
542                         } else
543                                 msginfo->newsgroups = g_strdup(hp);
544                         break;
545                 case H_SUBJECT:
546                         if (msginfo->subject) break;
547                         msginfo->subject = conv_unmime_header(hp, NULL);
548                         break;
549                 case H_MSG_ID:
550                         if (msginfo->msgid) break;
551
552                         extract_parenthesis(hp, '<', '>');
553                         remove_space(hp);
554                         msginfo->msgid = g_strdup(hp);
555                         break;
556                 case H_REFERENCES:
557                         msginfo->references =
558                                 references_list_prepend(msginfo->references,
559                                                         hp);
560                         break;
561                 case H_IN_REPLY_TO:
562                         if (msginfo->inreplyto) break;
563
564                         eliminate_parenthesis(hp, '(', ')');
565                         if ((p = strrchr(hp, '<')) != NULL &&
566                             strchr(p + 1, '>') != NULL) {
567                                 extract_parenthesis(p, '<', '>');
568                                 remove_space(p);
569                                 if (*p != '\0')
570                                         msginfo->inreplyto = g_strdup(p);
571                         }
572                         break;
573                 case H_CONTENT_TYPE:
574                         if (!g_ascii_strncasecmp(hp, "multipart/", 10))
575                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
576                         break;
577 #ifdef ALLOW_HEADER_HINT                        
578                 case H_SEEN:
579                         /* mnews Seen header */
580                         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
581                         break;
582 #endif                  
583                 case H_FACE:
584                         if (!msginfo->extradata)
585                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
586                         if (msginfo->extradata->face) break;
587                         msginfo->extradata->face = g_strdup(hp);
588                         break;
589                 case H_X_FACE:
590                         if (!msginfo->extradata)
591                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
592                         if (msginfo->extradata->xface) break;
593                         msginfo->extradata->xface = g_strdup(hp);
594                         break;
595                 case H_DISPOSITION_NOTIFICATION_TO:
596                         if (!msginfo->extradata)
597                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
598                         if (msginfo->extradata->dispositionnotificationto) break;
599                         msginfo->extradata->dispositionnotificationto = g_strdup(hp);
600                         break;
601                 case H_RETURN_RECEIPT_TO:
602                         if (!msginfo->extradata)
603                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
604                         if (msginfo->extradata->returnreceiptto) break;
605                         msginfo->extradata->returnreceiptto = g_strdup(hp);
606                         break;
607 /* partial download infos */                    
608                 case H_SC_PARTIALLY_RETRIEVED:
609                         if (!msginfo->extradata)
610                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
611                         if (msginfo->extradata->partial_recv) break;
612                         msginfo->extradata->partial_recv = g_strdup(hp);
613                         break;
614                 case H_SC_ACCOUNT_SERVER:
615                         if (!msginfo->extradata)
616                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
617                         if (msginfo->extradata->account_server) break;
618                         msginfo->extradata->account_server = g_strdup(hp);
619                         break;
620                 case H_SC_ACCOUNT_LOGIN:
621                         if (!msginfo->extradata)
622                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
623                         if (msginfo->extradata->account_login) break;
624                         msginfo->extradata->account_login = g_strdup(hp);
625                         break;
626                 case H_SC_MESSAGE_SIZE:
627                         if (msginfo->total_size) break;
628                         msginfo->total_size = atoi(hp);
629                         break;
630                 case H_SC_PLANNED_DOWNLOAD:
631                         msginfo->planned_download = atoi(hp);
632                         break;
633 /* end partial download infos */
634 #ifdef ALLOW_HEADER_HINT                        
635                 case H_STATUS:
636                         if (strchr(hp, 'R') != NULL)
637                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
638                         if (strchr(hp, 'O') != NULL)
639                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
640                         if (strchr(hp, 'U') != NULL)
641                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
642                         break;
643                 case H_X_STATUS:
644                         if (strchr(hp, 'D') != NULL)
645                                 MSG_SET_PERM_FLAGS(msginfo->flags,
646                                               MSG_REALLY_DELETED);
647                         if (strchr(hp, 'F') != NULL)
648                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
649                         if (strchr(hp, 'd') != NULL)
650                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
651                         if (strchr(hp, 'r') != NULL)
652                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
653                         if (strchr(hp, 'f') != NULL)
654                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
655                         break;
656 #endif                  
657                 case H_FROM_SPACE:
658                         if (msginfo->fromspace) break;
659                         msginfo->fromspace = g_strdup(hp);
660                         remove_return(msginfo->fromspace);
661                         break;
662 /* list infos */
663                 case H_LIST_POST:
664                         if (!msginfo->extradata)
665                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
666                         if (msginfo->extradata->list_post) break;
667                         msginfo->extradata->list_post = g_strdup(hp);
668                         break;
669                 case H_LIST_SUBSCRIBE:
670                         if (!msginfo->extradata)
671                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
672                         if (msginfo->extradata->list_subscribe) break;
673                         msginfo->extradata->list_subscribe = g_strdup(hp);
674                         break;
675                 case H_LIST_UNSUBSCRIBE:
676                         if (!msginfo->extradata)
677                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
678                         if (msginfo->extradata->list_unsubscribe) break;
679                         msginfo->extradata->list_unsubscribe = g_strdup(hp);
680                         break;
681                 case H_LIST_HELP:
682                         if (!msginfo->extradata)
683                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
684                         if (msginfo->extradata->list_help) break;
685                         msginfo->extradata->list_help = g_strdup(hp);
686                         break;
687                 case H_LIST_ARCHIVE:
688                         if (!msginfo->extradata)
689                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
690                         if (msginfo->extradata->list_archive) break;
691                         msginfo->extradata->list_archive = g_strdup(hp);
692                         break;
693                 case H_LIST_OWNER:
694                         if (!msginfo->extradata)
695                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
696                         if (msginfo->extradata->list_owner) break;
697                         msginfo->extradata->list_owner = g_strdup(hp);
698                         break;
699 /* end list infos */
700                 default:
701                         break;
702                 }
703         }
704
705         if (!msginfo->inreplyto && msginfo->references)
706                 msginfo->inreplyto =
707                         g_strdup((gchar *)msginfo->references->data);
708
709         return msginfo;
710 }
711
712 gchar *procheader_get_fromname(const gchar *str)
713 {
714         gchar *tmp, *name;
715
716         Xstrdup_a(tmp, str, return NULL);
717
718         if (*tmp == '\"') {
719                 extract_quote(tmp, '\"');
720                 g_strstrip(tmp);
721         } else if (strchr(tmp, '<')) {
722                 eliminate_parenthesis(tmp, '<', '>');
723                 g_strstrip(tmp);
724                 if (*tmp == '\0') {
725                         strcpy(tmp, str);
726                         extract_parenthesis(tmp, '<', '>');
727                         g_strstrip(tmp);
728                 }
729         } else if (strchr(tmp, '(')) {
730                 extract_parenthesis(tmp, '(', ')');
731                 g_strstrip(tmp);
732         }
733
734         if (*tmp == '\0')
735                 name = g_strdup(str);
736         else
737                 name = g_strdup(tmp);
738
739         return name;
740 }
741
742 static gint procheader_scan_date_string(const gchar *str,
743                                         gchar *weekday, gint *day,
744                                         gchar *month, gint *year,
745                                         gint *hh, gint *mm, gint *ss,
746                                         gchar *zone)
747 {
748         gint result;
749
750         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
751                         weekday, day, month, year, hh, mm, ss, zone);
752         if (result == 8) return 0;
753
754         result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
755                         weekday, day, month, year, hh, mm, ss, zone);
756         if (result == 8) return 0;
757
758         result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
759                         day, month, year, hh, mm, ss, zone);
760         if (result == 7) return 0;
761
762         *zone = '\0';
763         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
764                         weekday, day, month, year, hh, mm, ss);
765         if (result == 7) return 0;
766
767         result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
768                         day, month, year, hh, mm, ss);
769         if (result == 6) return 0;
770
771         *ss = 0;
772         result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
773                         weekday, day, month, year, hh, mm, zone);
774         if (result == 7) return 0;
775
776         result = sscanf(str, "%d %9s %d %2d:%2d %5s",
777                         day, month, year, hh, mm, zone);
778         if (result == 6) return 0;
779
780         *zone = '\0';
781         result = sscanf(str, "%10s %d %9s %d %2d:%2d",
782                         weekday, day, month, year, hh, mm);
783         if (result == 6) return 0;
784
785         result = sscanf(str, "%d %9s %d %2d:%2d",
786                         day, month, year, hh, mm);
787         if (result == 5) return 0;
788
789         return -1;
790 }
791
792 /*
793  * Hiro, most UNIXen support this function:
794  * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
795  */
796 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
797 {
798         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
799         gchar weekday[11];
800         gint day;
801         gchar month[10];
802         gint year;
803         gint hh, mm, ss;
804         GDateMonth dmonth;
805         gchar *p;
806
807         if (!t)
808                 return FALSE;
809         
810         memset(t, 0, sizeof *t);        
811
812         if (procheader_scan_date_string(src, weekday, &day, month, &year,
813                                         &hh, &mm, &ss, zone) < 0) {
814                 g_warning("Invalid date: %s\n", src);
815                 return FALSE;
816         }
817
818         /* Y2K compliant :) */
819         if (year < 100) {
820                 if (year < 70)
821                         year += 2000;
822                 else
823                         year += 1900;
824         }
825
826         month[3] = '\0';
827         if ((p = strstr(monthstr, month)) != NULL)
828                 dmonth = (gint)(p - monthstr) / 3 + 1;
829         else {
830                 g_warning("Invalid month: %s\n", month);
831                 dmonth = G_DATE_BAD_MONTH;
832         }
833
834         t->tm_sec = ss;
835         t->tm_min = mm;
836         t->tm_hour = hh;
837         t->tm_mday = day;
838         t->tm_mon = dmonth - 1;
839         t->tm_year = year - 1900;
840         t->tm_wday = 0;
841         t->tm_yday = 0;
842         t->tm_isdst = -1;
843
844         mktime(t);
845
846         return TRUE;
847 }
848
849 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
850 {
851         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
852         gchar weekday[11];
853         gint day;
854         gchar month[10];
855         gint year;
856         gint hh, mm, ss;
857         gchar zone[6];
858         GDateMonth dmonth = G_DATE_BAD_MONTH;
859         struct tm t;
860         gchar *p;
861         time_t timer;
862         time_t tz_offset;
863
864         if (procheader_scan_date_string(src, weekday, &day, month, &year,
865                                         &hh, &mm, &ss, zone) < 0) {
866                 if (dest && len > 0)
867                         strncpy2(dest, src, len);
868                 return 0;
869         }
870
871         /* Y2K compliant :) */
872         if (year < 1000) {
873                 if (year < 50)
874                         year += 2000;
875                 else
876                         year += 1900;
877         }
878
879         month[3] = '\0';
880         for (p = monthstr; *p != '\0'; p += 3) {
881                 if (!g_ascii_strncasecmp(p, month, 3)) {
882                         dmonth = (gint)(p - monthstr) / 3 + 1;
883                         break;
884                 }
885         }
886
887         t.tm_sec = ss;
888         t.tm_min = mm;
889         t.tm_hour = hh;
890         t.tm_mday = day;
891         t.tm_mon = dmonth - 1;
892         t.tm_year = year - 1900;
893         t.tm_wday = 0;
894         t.tm_yday = 0;
895         t.tm_isdst = -1;
896
897         timer = mktime(&t);
898         tz_offset = remote_tzoffset_sec(zone);
899         if (tz_offset != -1)
900                 timer += tzoffset_sec(&timer) - tz_offset;
901
902         if (dest)
903                 procheader_date_get_localtime(dest, len, timer);
904
905         return timer;
906 }
907
908 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
909 {
910         struct tm *lt;
911         gchar *default_format = "%y/%m/%d(%a) %H:%M";
912         gchar *str;
913         const gchar *src_codeset, *dest_codeset;
914
915         lt = localtime(&timer);
916
917         if (prefs_common.date_format)
918                 fast_strftime(dest, len, prefs_common.date_format, lt);
919         else
920                 fast_strftime(dest, len, default_format, lt);
921
922         if (!g_utf8_validate(dest, -1, NULL)) {
923                 src_codeset = conv_get_locale_charset_str_no_utf8();
924                 dest_codeset = CS_UTF_8;
925                 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
926                 if (str) {
927                         strncpy2(dest, str, len);
928                         g_free(str);
929                 }
930         }
931 }
932
933 /* Added by Mel Hadasht on 27 Aug 2001 */
934 /* Get a header from msginfo */
935 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
936 {
937         gchar *file;
938         FILE *fp;
939         HeaderEntry hentry[]={ { NULL, NULL, TRUE  },
940                                { NULL, NULL, FALSE } };
941         gint val;
942
943         hentry[0].name = header;
944        
945         g_return_val_if_fail(msginfo != NULL, -1);
946         file = procmsg_get_message_file_path(msginfo);
947         if ((fp = g_fopen(file, "rb")) == NULL) {
948                FILE_OP_ERROR(file, "fopen");
949                g_free(file);
950                return -1;
951         }
952         val = procheader_get_one_field(buf,len, fp, hentry);
953         if (fclose(fp) == EOF) {
954                 FILE_OP_ERROR(file, "fclose");
955                 g_unlink(file);
956                 g_free(file);
957                 return -1;
958         }
959
960         g_free(file);
961         if (val == -1)
962                 return -1;
963
964         return 0;
965 }