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