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