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