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