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