526413ecebcb47f470c815c7166e692bf104ffe7
[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                         break;
551                 case H_MSG_ID:
552                         if (msginfo->msgid) break;
553
554                         extract_parenthesis(hp, '<', '>');
555                         remove_space(hp);
556                         msginfo->msgid = g_strdup(hp);
557                         break;
558                 case H_REFERENCES:
559                         msginfo->references =
560                                 references_list_prepend(msginfo->references,
561                                                         hp);
562                         break;
563                 case H_IN_REPLY_TO:
564                         if (msginfo->inreplyto) break;
565
566                         eliminate_parenthesis(hp, '(', ')');
567                         if ((p = strrchr(hp, '<')) != NULL &&
568                             strchr(p + 1, '>') != NULL) {
569                                 extract_parenthesis(p, '<', '>');
570                                 remove_space(p);
571                                 if (*p != '\0')
572                                         msginfo->inreplyto = g_strdup(p);
573                         }
574                         break;
575                 case H_CONTENT_TYPE:
576                         if (!g_ascii_strncasecmp(hp, "multipart/", 10))
577                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
578                         break;
579 #ifdef ALLOW_HEADER_HINT                        
580                 case H_SEEN:
581                         /* mnews Seen header */
582                         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
583                         break;
584 #endif                  
585                 case H_FACE:
586                         if (!msginfo->extradata)
587                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
588                         if (msginfo->extradata->face) break;
589                         msginfo->extradata->face = g_strdup(hp);
590                         break;
591                 case H_X_FACE:
592                         if (!msginfo->extradata)
593                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
594                         if (msginfo->extradata->xface) break;
595                         msginfo->extradata->xface = g_strdup(hp);
596                         break;
597                 case H_DISPOSITION_NOTIFICATION_TO:
598                         if (!msginfo->extradata)
599                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
600                         if (msginfo->extradata->dispositionnotificationto) break;
601                         msginfo->extradata->dispositionnotificationto = g_strdup(hp);
602                         break;
603                 case H_RETURN_RECEIPT_TO:
604                         if (!msginfo->extradata)
605                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
606                         if (msginfo->extradata->returnreceiptto) break;
607                         msginfo->extradata->returnreceiptto = g_strdup(hp);
608                         break;
609 /* partial download infos */                    
610                 case H_SC_PARTIALLY_RETRIEVED:
611                         if (!msginfo->extradata)
612                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
613                         if (msginfo->extradata->partial_recv) break;
614                         msginfo->extradata->partial_recv = g_strdup(hp);
615                         break;
616                 case H_SC_ACCOUNT_SERVER:
617                         if (!msginfo->extradata)
618                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
619                         if (msginfo->extradata->account_server) break;
620                         msginfo->extradata->account_server = g_strdup(hp);
621                         break;
622                 case H_SC_ACCOUNT_LOGIN:
623                         if (!msginfo->extradata)
624                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
625                         if (msginfo->extradata->account_login) break;
626                         msginfo->extradata->account_login = g_strdup(hp);
627                         break;
628                 case H_SC_MESSAGE_SIZE:
629                         if (msginfo->total_size) break;
630                         msginfo->total_size = atoi(hp);
631                         break;
632                 case H_SC_PLANNED_DOWNLOAD:
633                         msginfo->planned_download = atoi(hp);
634                         break;
635 /* end partial download infos */
636 #ifdef ALLOW_HEADER_HINT                        
637                 case H_STATUS:
638                         if (strchr(hp, 'R') != NULL)
639                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
640                         if (strchr(hp, 'O') != NULL)
641                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
642                         if (strchr(hp, 'U') != NULL)
643                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
644                         break;
645                 case H_X_STATUS:
646                         if (strchr(hp, 'D') != NULL)
647                                 MSG_SET_PERM_FLAGS(msginfo->flags,
648                                               MSG_REALLY_DELETED);
649                         if (strchr(hp, 'F') != NULL)
650                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
651                         if (strchr(hp, 'd') != NULL)
652                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
653                         if (strchr(hp, 'r') != NULL)
654                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
655                         if (strchr(hp, 'f') != NULL)
656                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
657                         break;
658 #endif                  
659                 case H_FROM_SPACE:
660                         if (msginfo->fromspace) break;
661                         msginfo->fromspace = g_strdup(hp);
662                         remove_return(msginfo->fromspace);
663                         break;
664 /* list infos */
665                 case H_LIST_POST:
666                         if (!msginfo->extradata)
667                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
668                         if (msginfo->extradata->list_post) break;
669                         msginfo->extradata->list_post = g_strdup(hp);
670                         break;
671                 case H_LIST_SUBSCRIBE:
672                         if (!msginfo->extradata)
673                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
674                         if (msginfo->extradata->list_subscribe) break;
675                         msginfo->extradata->list_subscribe = g_strdup(hp);
676                         break;
677                 case H_LIST_UNSUBSCRIBE:
678                         if (!msginfo->extradata)
679                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
680                         if (msginfo->extradata->list_unsubscribe) break;
681                         msginfo->extradata->list_unsubscribe = g_strdup(hp);
682                         break;
683                 case H_LIST_HELP:
684                         if (!msginfo->extradata)
685                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
686                         if (msginfo->extradata->list_help) break;
687                         msginfo->extradata->list_help = g_strdup(hp);
688                         break;
689                 case H_LIST_ARCHIVE:
690                         if (!msginfo->extradata)
691                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
692                         if (msginfo->extradata->list_archive) break;
693                         msginfo->extradata->list_archive = g_strdup(hp);
694                         break;
695                 case H_LIST_OWNER:
696                         if (!msginfo->extradata)
697                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
698                         if (msginfo->extradata->list_owner) break;
699                         msginfo->extradata->list_owner = g_strdup(hp);
700                         break;
701 /* end list infos */
702                 default:
703                         break;
704                 }
705         }
706
707         if (!msginfo->inreplyto && msginfo->references)
708                 msginfo->inreplyto =
709                         g_strdup((gchar *)msginfo->references->data);
710
711         return msginfo;
712 }
713
714 gchar *procheader_get_fromname(const gchar *str)
715 {
716         gchar *tmp, *name;
717
718         Xstrdup_a(tmp, str, return NULL);
719
720         if (*tmp == '\"') {
721                 extract_quote(tmp, '\"');
722                 g_strstrip(tmp);
723         } else if (strchr(tmp, '<')) {
724                 eliminate_parenthesis(tmp, '<', '>');
725                 g_strstrip(tmp);
726                 if (*tmp == '\0') {
727                         strcpy(tmp, str);
728                         extract_parenthesis(tmp, '<', '>');
729                         g_strstrip(tmp);
730                 }
731         } else if (strchr(tmp, '(')) {
732                 extract_parenthesis(tmp, '(', ')');
733                 g_strstrip(tmp);
734         }
735
736         if (*tmp == '\0')
737                 name = g_strdup(str);
738         else
739                 name = g_strdup(tmp);
740
741         return name;
742 }
743
744 static gint procheader_scan_date_string(const gchar *str,
745                                         gchar *weekday, gint *day,
746                                         gchar *month, gint *year,
747                                         gint *hh, gint *mm, gint *ss,
748                                         gchar *zone)
749 {
750         gint result;
751
752         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
753                         weekday, day, month, year, hh, mm, ss, zone);
754         if (result == 8) return 0;
755
756         result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
757                         weekday, day, month, year, hh, mm, ss, zone);
758         if (result == 8) return 0;
759
760         result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
761                         day, month, year, hh, mm, ss, zone);
762         if (result == 7) return 0;
763
764         *zone = '\0';
765         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
766                         weekday, day, month, year, hh, mm, ss);
767         if (result == 7) return 0;
768
769         result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
770                         day, month, year, hh, mm, ss);
771         if (result == 6) return 0;
772
773         *ss = 0;
774         result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
775                         weekday, day, month, year, hh, mm, zone);
776         if (result == 7) return 0;
777
778         result = sscanf(str, "%d %9s %d %2d:%2d %5s",
779                         day, month, year, hh, mm, zone);
780         if (result == 6) return 0;
781
782         *zone = '\0';
783         result = sscanf(str, "%10s %d %9s %d %2d:%2d",
784                         weekday, day, month, year, hh, mm);
785         if (result == 6) return 0;
786
787         result = sscanf(str, "%d %9s %d %2d:%2d",
788                         day, month, year, hh, mm);
789         if (result == 5) return 0;
790
791         return -1;
792 }
793
794 /*
795  * Hiro, most UNIXen support this function:
796  * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
797  */
798 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
799 {
800         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
801         gchar weekday[11];
802         gint day;
803         gchar month[10];
804         gint year;
805         gint hh, mm, ss;
806         GDateMonth dmonth;
807         gchar *p;
808
809         if (!t)
810                 return FALSE;
811         
812         memset(t, 0, sizeof *t);        
813
814         if (procheader_scan_date_string(src, weekday, &day, month, &year,
815                                         &hh, &mm, &ss, zone) < 0) {
816                 g_warning("Invalid date: %s\n", src);
817                 return FALSE;
818         }
819
820         /* Y2K compliant :) */
821         if (year < 100) {
822                 if (year < 70)
823                         year += 2000;
824                 else
825                         year += 1900;
826         }
827
828         month[3] = '\0';
829         if ((p = strstr(monthstr, month)) != NULL)
830                 dmonth = (gint)(p - monthstr) / 3 + 1;
831         else {
832                 g_warning("Invalid month: %s\n", month);
833                 dmonth = G_DATE_BAD_MONTH;
834         }
835
836         t->tm_sec = ss;
837         t->tm_min = mm;
838         t->tm_hour = hh;
839         t->tm_mday = day;
840         t->tm_mon = dmonth - 1;
841         t->tm_year = year - 1900;
842         t->tm_wday = 0;
843         t->tm_yday = 0;
844         t->tm_isdst = -1;
845
846         mktime(t);
847
848         return TRUE;
849 }
850
851 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
852 {
853         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
854         gchar weekday[11];
855         gint day;
856         gchar month[10];
857         gint year;
858         gint hh, mm, ss;
859         gchar zone[6];
860         GDateMonth dmonth = G_DATE_BAD_MONTH;
861         struct tm t;
862         gchar *p;
863         time_t timer;
864         time_t tz_offset;
865
866         if (procheader_scan_date_string(src, weekday, &day, month, &year,
867                                         &hh, &mm, &ss, zone) < 0) {
868                 if (dest && len > 0)
869                         strncpy2(dest, src, len);
870                 return 0;
871         }
872
873         /* Y2K compliant :) */
874         if (year < 1000) {
875                 if (year < 50)
876                         year += 2000;
877                 else
878                         year += 1900;
879         }
880
881         month[3] = '\0';
882         for (p = monthstr; *p != '\0'; p += 3) {
883                 if (!g_ascii_strncasecmp(p, month, 3)) {
884                         dmonth = (gint)(p - monthstr) / 3 + 1;
885                         break;
886                 }
887         }
888
889         t.tm_sec = ss;
890         t.tm_min = mm;
891         t.tm_hour = hh;
892         t.tm_mday = day;
893         t.tm_mon = dmonth - 1;
894         t.tm_year = year - 1900;
895         t.tm_wday = 0;
896         t.tm_yday = 0;
897         t.tm_isdst = -1;
898
899         timer = mktime(&t);
900         tz_offset = remote_tzoffset_sec(zone);
901         if (tz_offset != -1)
902                 timer += tzoffset_sec(&timer) - tz_offset;
903
904         if (dest)
905                 procheader_date_get_localtime(dest, len, timer);
906
907         return timer;
908 }
909
910 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
911 {
912         struct tm *lt;
913         gchar *default_format = "%y/%m/%d(%a) %H:%M";
914         gchar *str;
915         const gchar *src_codeset, *dest_codeset;
916
917         lt = localtime(&timer);
918
919         if (prefs_common.date_format)
920                 fast_strftime(dest, len, prefs_common.date_format, lt);
921         else
922                 fast_strftime(dest, len, default_format, lt);
923
924         if (!g_utf8_validate(dest, -1, NULL)) {
925                 src_codeset = conv_get_locale_charset_str_no_utf8();
926                 dest_codeset = CS_UTF_8;
927                 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
928                 if (str) {
929                         strncpy2(dest, str, len);
930                         g_free(str);
931                 }
932         }
933 }
934
935 /* Added by Mel Hadasht on 27 Aug 2001 */
936 /* Get a header from msginfo */
937 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
938 {
939         gchar *file;
940         FILE *fp;
941         HeaderEntry hentry[]={ { NULL, NULL, TRUE  },
942                                { NULL, NULL, FALSE } };
943         gint val;
944
945         hentry[0].name = header;
946        
947         g_return_val_if_fail(msginfo != NULL, -1);
948         file = procmsg_get_message_file_path(msginfo);
949         if ((fp = g_fopen(file, "rb")) == NULL) {
950                FILE_OP_ERROR(file, "fopen");
951                g_free(file);
952                return -1;
953         }
954         val = procheader_get_one_field(buf,len, fp, hentry);
955         if (fclose(fp) == EOF) {
956                 FILE_OP_ERROR(file, "fclose");
957                 g_unlink(file);
958                 g_free(file);
959                 return -1;
960         }
961
962         g_free(file);
963         if (val == -1)
964                 return -1;
965
966         return 0;
967 }