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