2005-01-25 [paul] 1.0.0cvs14
[claws.git] / src / procheader.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2004 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 #include <sys/stat.h>
30
31 #include "intl.h"
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 *, int, void *);
41 typedef int (*peekcharfunc)(void *);
42 typedef int (*getcharfunc)(void *);
43 typedef gint (*get_one_field_func)(gchar *, gint, void *, HeaderEntry[]);
44
45 static gint string_get_one_field(gchar *buf, gint len, char **str,
46                                  HeaderEntry hentry[]);
47
48 static char *string_getline(char *buf, int 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, gint 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, gint 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, gint 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, int len, char **str)
78 {
79         if (!**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, gint 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_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, gint 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, gint 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 (nexthead == '\r' || nexthead == '\n') {
194                                 folded = FALSE;
195                                 continue;
196                         }
197
198                         if ((len - (bufp - buf)) <= 2) break;
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 = 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_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 tmp[BUFFSIZE];
416         gchar *p = buf;
417         Header * header;
418
419         if ((*buf == ':') || (*buf == ' '))
420                 return NULL;
421
422         for (p = buf; *p ; p++) {
423                 if ((*p == ':') || (*p == ' ')) {
424                         header = g_new(Header, 1);
425                         header->name = g_strndup(buf, p - buf + 1);
426                         p++;
427                         while (*p == ' ' || *p == '\t') p++;
428                         conv_unmime_header(tmp, sizeof(tmp), p, NULL);
429                         if(tmp == NULL) 
430                                 header->body = g_strdup(p);
431                         else    
432                                 header->body = g_strdup(tmp);
433                         return header;
434                 }
435         }
436         return NULL;
437 }
438
439 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
440 {
441         gchar buf[BUFFSIZE];
442         HeaderEntry *hp;
443         gint hnum;
444         gchar *p;
445
446         if (hentry == NULL) return;
447
448         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
449                != -1) {
450                 hp = hentry + hnum;
451
452                 p = buf + strlen(hp->name);
453                 while (*p == ' ' || *p == '\t') p++;
454
455                 if (hp->body == NULL)
456                         hp->body = g_strdup(p);
457                 else if (procheader_headername_equal(hp->name, "To") ||
458                          procheader_headername_equal(hp->name, "Cc")) {
459                         gchar *tp = hp->body;
460                         hp->body = g_strconcat(tp, ", ", p, NULL);
461                         g_free(tp);
462                 }
463         }
464 }
465
466 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
467                                gboolean full, gboolean decrypted)
468 {
469         struct stat s;
470         FILE *fp;
471         MsgInfo *msginfo;
472
473         if (stat(file, &s) < 0) {
474                 FILE_OP_ERROR(file, "stat");
475                 return NULL;
476         }
477         if (!S_ISREG(s.st_mode))
478                 return NULL;
479
480         if ((fp = fopen(file, "rb")) == NULL) {
481                 FILE_OP_ERROR(file, "fopen");
482                 return NULL;
483         }
484
485         msginfo = procheader_parse_stream(fp, flags, full, decrypted);
486         fclose(fp);
487
488         if (msginfo) {
489                 msginfo->size = s.st_size;
490                 msginfo->mtime = s.st_mtime;
491         }
492
493         return msginfo;
494 }
495
496 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
497                               gboolean decrypted)
498 {
499         return parse_stream(&str, TRUE, flags, full, decrypted);
500 }
501
502 enum
503 {
504         H_DATE          = 0,
505         H_FROM          = 1,
506         H_TO            = 2,
507         H_CC            = 3,
508         H_NEWSGROUPS    = 4,
509         H_SUBJECT       = 5,
510         H_MSG_ID        = 6,
511         H_REFERENCES    = 7,
512         H_IN_REPLY_TO   = 8,
513         H_CONTENT_TYPE  = 9,
514         H_SEEN          = 10,
515         H_STATUS        = 11,
516         H_X_STATUS      = 12,
517         H_FROM_SPACE    = 13,
518         H_SC_PLANNED_DOWNLOAD = 14,
519         H_SC_MESSAGE_SIZE = 15,
520         H_X_FACE        = 16,
521         H_DISPOSITION_NOTIFICATION_TO = 17,
522         H_RETURN_RECEIPT_TO = 18,
523         H_SC_PARTIALLY_RETRIEVED = 19,
524         H_SC_ACCOUNT_SERVER = 20,
525         H_SC_ACCOUNT_LOGIN = 21,
526 };
527
528 static HeaderEntry hentry_full[] = {{"Date:",           NULL, FALSE},
529                                    {"From:",            NULL, TRUE},
530                                    {"To:",              NULL, TRUE},
531                                    {"Cc:",              NULL, TRUE},
532                                    {"Newsgroups:",      NULL, TRUE},
533                                    {"Subject:",         NULL, TRUE},
534                                    {"Message-ID:",      NULL, FALSE},
535                                    {"References:",      NULL, FALSE},
536                                    {"In-Reply-To:",     NULL, FALSE},
537                                    {"Content-Type:",    NULL, FALSE},
538                                    {"Seen:",            NULL, FALSE},
539                                    {"Status:",          NULL, FALSE},
540                                    {"X-Status:",        NULL, FALSE},
541                                    {"From ",            NULL, FALSE},
542                                    {"SC-Marked-For-Download:", NULL, FALSE},
543                                    {"SC-Message-Size:", NULL, FALSE},
544                                    {"X-Face:",          NULL, FALSE},
545                                    {"Disposition-Notification-To:", NULL, FALSE},
546                                    {"Return-Receipt-To:", NULL, FALSE},
547                                    {"SC-Partially-Retrieved:", NULL, FALSE},
548                                    {"SC-Account-Server:", NULL, FALSE},
549                                    {"SC-Account-Login:",NULL, FALSE},
550                                    {NULL,               NULL, FALSE}};
551
552 static HeaderEntry hentry_short[] = {{"Date:",          NULL, FALSE},
553                                     {"From:",           NULL, TRUE},
554                                     {"To:",             NULL, TRUE},
555                                     {"Cc:",             NULL, TRUE},
556                                     {"Newsgroups:",     NULL, TRUE},
557                                     {"Subject:",        NULL, TRUE},
558                                     {"Message-ID:",     NULL, FALSE},
559                                     {"References:",     NULL, FALSE},
560                                     {"In-Reply-To:",    NULL, FALSE},
561                                     {"Content-Type:",   NULL, FALSE},
562                                     {"Seen:",           NULL, FALSE},
563                                     {"Status:",         NULL, FALSE},
564                                     {"X-Status:",       NULL, FALSE},
565                                     {"From ",           NULL, FALSE},
566                                     {"SC-Marked-For-Download:", NULL, FALSE},
567                                     {"SC-Message-Size:",NULL, FALSE},
568                                     {NULL,              NULL, FALSE}};
569
570 HeaderEntry* procheader_get_headernames(gboolean full)
571 {
572         return full ? hentry_full : hentry_short;
573 }
574
575 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
576                                  gboolean decrypted)
577 {
578         return parse_stream(fp, FALSE, flags, full, decrypted);
579 }
580
581 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
582                              gboolean full, gboolean decrypted)
583 {
584         MsgInfo *msginfo;
585         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
586         gchar *reference = NULL;
587         gchar *p;
588         gchar *hp;
589         HeaderEntry *hentry;
590         gint hnum;
591         get_one_field_func get_one_field =
592                 isstring ? (get_one_field_func)string_get_one_field
593                          : (get_one_field_func)procheader_get_one_field;
594
595         hentry = procheader_get_headernames(full);
596
597         if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
598                 while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
599                         ; /* loop */
600         }
601
602         msginfo = procmsg_msginfo_new();
603         
604         if (flags.tmp_flags || flags.perm_flags) 
605                 msginfo->flags = flags;
606         else 
607                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
608         
609         msginfo->inreplyto = NULL;
610
611         while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
612                != -1) {
613                 hp = buf + strlen(hentry[hnum].name);
614                 while (*hp == ' ' || *hp == '\t') hp++;
615
616                 switch (hnum) {
617                 case H_DATE:
618                         if (msginfo->date) break;
619                         msginfo->date_t =
620                                 procheader_date_parse(NULL, hp, 0);
621                         msginfo->date = g_strdup(hp);
622                         break;
623                 case H_FROM:
624                         if (msginfo->from) break;
625                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
626                         msginfo->from = g_strdup(tmp);
627                         msginfo->fromname = procheader_get_fromname(tmp);
628                         break;
629                 case H_TO:
630                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
631                         if (msginfo->to) {
632                                 p = msginfo->to;
633                                 msginfo->to =
634                                         g_strconcat(p, ", ", tmp, NULL);
635                                 g_free(p);
636                         } else
637                                 msginfo->to = g_strdup(tmp);
638                         break;
639                 case H_CC:
640                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
641                         if (msginfo->cc) {
642                                 p = msginfo->cc;
643                                 msginfo->cc =
644                                         g_strconcat(p, ", ", tmp, NULL);
645                                 g_free(p);
646                         } else
647                                 msginfo->cc = g_strdup(tmp);
648                         break;
649                 case H_NEWSGROUPS:
650                         if (msginfo->newsgroups) {
651                                 p = msginfo->newsgroups;
652                                 msginfo->newsgroups =
653                                         g_strconcat(p, ",", hp, NULL);
654                                 g_free(p);
655                         } else
656                                 msginfo->newsgroups = g_strdup(hp);
657                         break;
658                 case H_SUBJECT:
659                         if (msginfo->subject) break;
660                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
661                         msginfo->subject = g_strdup(tmp);
662                         break;
663                 case H_MSG_ID:
664                         if (msginfo->msgid) break;
665
666                         extract_parenthesis(hp, '<', '>');
667                         remove_space(hp);
668                         msginfo->msgid = g_strdup(hp);
669                         break;
670                 case H_REFERENCES:
671                 case H_IN_REPLY_TO:
672                         if (!reference) {
673                                 msginfo->references = g_strdup(hp);
674                                 eliminate_parenthesis(hp, '(', ')');
675                                 if ((p = strrchr(hp, '<')) != NULL &&
676                                     strchr(p + 1, '>') != NULL) {
677                                         extract_parenthesis(p, '<', '>');
678                                         remove_space(p);
679                                         if (*p != '\0')
680                                                 reference = g_strdup(p);
681                                 }
682                         }
683                         break;
684                 case H_CONTENT_TYPE:
685                         if (!g_strncasecmp(hp, "multipart/", 10))
686                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
687                         break;
688 #ifdef ALLOW_HEADER_HINT                        
689                 case H_SEEN:
690                         /* mnews Seen header */
691                         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
692                         break;
693 #endif                  
694                 case H_X_FACE:
695                         if (msginfo->xface) break;
696                         msginfo->xface = g_strdup(hp);
697                         break;
698                 case H_DISPOSITION_NOTIFICATION_TO:
699                         if (msginfo->dispositionnotificationto) break;
700                         msginfo->dispositionnotificationto = g_strdup(hp);
701                         break;
702                 case H_RETURN_RECEIPT_TO:
703                         if (msginfo->returnreceiptto) break;
704                         msginfo->returnreceiptto = g_strdup(hp);
705                         break;
706                 case H_SC_PARTIALLY_RETRIEVED:
707                         if (msginfo->partial_recv) break;
708                         msginfo->partial_recv = g_strdup(hp);
709                         break;
710                 case H_SC_ACCOUNT_SERVER:
711                         if (msginfo->account_server) break;
712                         msginfo->account_server = g_strdup(hp);
713                         break;
714                 case H_SC_ACCOUNT_LOGIN:
715                         if (msginfo->account_login) break;
716                         msginfo->account_login = g_strdup(hp);
717                         break;
718                 case H_SC_MESSAGE_SIZE:
719                         if (msginfo->total_size) break;
720                         msginfo->total_size = atoi(hp);
721                         break;
722                 case H_SC_PLANNED_DOWNLOAD:
723                         msginfo->planned_download = atoi(hp);
724                         break;
725 #ifdef ALLOW_HEADER_HINT                        
726                 case H_STATUS:
727                         if (strchr(hp, 'R') != NULL)
728                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
729                         if (strchr(hp, 'O') != NULL)
730                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
731                         if (strchr(hp, 'U') != NULL)
732                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
733                         break;
734                 case H_X_STATUS:
735                         if (strchr(hp, 'D') != NULL)
736                                 MSG_SET_PERM_FLAGS(msginfo->flags,
737                                               MSG_REALLY_DELETED);
738                         if (strchr(hp, 'F') != NULL)
739                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
740                         if (strchr(hp, 'd') != NULL)
741                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
742                         if (strchr(hp, 'r') != NULL)
743                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
744                         if (strchr(hp, 'f') != NULL)
745                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
746                         break;
747 #endif                  
748                 case H_FROM_SPACE:
749                         if (msginfo->fromspace) break;
750                         msginfo->fromspace = g_strdup(hp);
751                         break;
752                 default:
753                         break;
754                 }
755         }
756         msginfo->inreplyto = reference;
757
758         return msginfo;
759 }
760
761 gchar *procheader_get_fromname(const gchar *str)
762 {
763         gchar *tmp, *name;
764
765         Xstrdup_a(tmp, str, return NULL);
766
767         if (*tmp == '\"') {
768                 extract_quote(tmp, '\"');
769                 g_strstrip(tmp);
770         } else if (strchr(tmp, '<')) {
771                 eliminate_parenthesis(tmp, '<', '>');
772                 g_strstrip(tmp);
773                 if (*tmp == '\0') {
774                         strcpy(tmp, str);
775                         extract_parenthesis(tmp, '<', '>');
776                         g_strstrip(tmp);
777                 }
778         } else if (strchr(tmp, '(')) {
779                 extract_parenthesis(tmp, '(', ')');
780                 g_strstrip(tmp);
781         }
782
783         if (*tmp == '\0')
784                 name = g_strdup(str);
785         else
786                 name = g_strdup(tmp);
787
788         return name;
789 }
790
791 static gint procheader_scan_date_string(const gchar *str,
792                                         gchar *weekday, gint *day,
793                                         gchar *month, gint *year,
794                                         gint *hh, gint *mm, gint *ss,
795                                         gchar *zone)
796 {
797         gint result;
798
799         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
800                         weekday, day, month, year, hh, mm, ss, zone);
801         if (result == 8) return 0;
802
803         result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
804                         weekday, day, month, year, hh, mm, ss, zone);
805         if (result == 8) return 0;
806
807         result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
808                         day, month, year, hh, mm, ss, zone);
809         if (result == 7) return 0;
810
811         *zone = '\0';
812         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
813                         weekday, day, month, year, hh, mm, ss);
814         if (result == 7) return 0;
815
816         *ss = 0;
817         result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
818                         weekday, day, month, year, hh, mm, zone);
819         if (result == 7) return 0;
820
821         result = sscanf(str, "%d %9s %d %2d:%2d %5s",
822                         day, month, year, hh, mm, zone);
823         if (result == 6) return 0;
824
825         return -1;
826 }
827
828 /*
829  * Hiro, most UNIXen support this function:
830  * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
831  */
832 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
833 {
834         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
835         gchar weekday[11];
836         gint day;
837         gchar month[10];
838         gint year;
839         gint hh, mm, ss;
840         GDateMonth dmonth;
841         gchar *p;
842
843         if (!t)
844                 return FALSE;
845         
846         memset(t, 0, sizeof *t);        
847
848         if (procheader_scan_date_string(src, weekday, &day, month, &year,
849                                         &hh, &mm, &ss, zone) < 0) {
850                 g_warning("Invalid date: %s\n", src);
851                 return FALSE;
852         }
853
854         /* Y2K compliant :) */
855         if (year < 100) {
856                 if (year < 70)
857                         year += 2000;
858                 else
859                         year += 1900;
860         }
861
862         month[3] = '\0';
863         if ((p = strstr(monthstr, month)) != NULL)
864                 dmonth = (gint)(p - monthstr) / 3 + 1;
865         else {
866                 g_warning("Invalid month: %s\n", month);
867                 dmonth = G_DATE_BAD_MONTH;
868         }
869
870         t->tm_sec = ss;
871         t->tm_min = mm;
872         t->tm_hour = hh;
873         t->tm_mday = day;
874         t->tm_mon = dmonth - 1;
875         t->tm_year = year - 1900;
876         t->tm_wday = 0;
877         t->tm_yday = 0;
878         t->tm_isdst = -1;
879
880         mktime(t);
881
882         return TRUE;
883 }
884
885 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
886 {
887         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
888         gchar weekday[11];
889         gint day;
890         gchar month[10];
891         gint year;
892         gint hh, mm, ss;
893         gchar zone[6];
894         GDateMonth dmonth = G_DATE_BAD_MONTH;
895         struct tm t;
896         gchar *p;
897         time_t timer;
898         time_t tz_offset;
899
900         if (procheader_scan_date_string(src, weekday, &day, month, &year,
901                                         &hh, &mm, &ss, zone) < 0) {
902                 g_warning("Invalid date: %s\n", src);
903                 if (dest && len > 0)
904                         strncpy2(dest, src, len);
905                 return 0;
906         }
907
908         /* Y2K compliant :) */
909         if (year < 1000) {
910                 if (year < 50)
911                         year += 2000;
912                 else
913                         year += 1900;
914         }
915
916         month[3] = '\0';
917         for (p = monthstr; *p != '\0'; p += 3) {
918                 if (!g_strncasecmp(p, month, 3)) {
919                         dmonth = (gint)(p - monthstr) / 3 + 1;
920                         break;
921                 }
922         }
923         if (*p == '\0')
924                 g_warning("Invalid month: %s\n", month);
925
926         t.tm_sec = ss;
927         t.tm_min = mm;
928         t.tm_hour = hh;
929         t.tm_mday = day;
930         t.tm_mon = dmonth - 1;
931         t.tm_year = year - 1900;
932         t.tm_wday = 0;
933         t.tm_yday = 0;
934         t.tm_isdst = -1;
935
936         timer = mktime(&t);
937         tz_offset = remote_tzoffset_sec(zone);
938         if (tz_offset != -1)
939                 timer += tzoffset_sec(&timer) - tz_offset;
940
941         if (dest)
942                 procheader_date_get_localtime(dest, len, timer);
943
944         return timer;
945 }
946
947 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
948 {
949         struct tm *lt;
950         gchar *default_format = "%y/%m/%d(%a) %H:%M";
951
952         lt = localtime(&timer);
953
954         if (prefs_common.date_format)
955                 strftime(dest, len, prefs_common.date_format, lt);
956         else
957                 strftime(dest, len, default_format, lt);
958 }
959
960 /* Added by Mel Hadasht on 27 Aug 2001 */
961 /* Get a header from msginfo */
962 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
963 {
964         gchar *file;
965         FILE *fp;
966         HeaderEntry hentry[]={ { NULL, NULL, TRUE  },
967                                { NULL, NULL, FALSE } };
968         gint val;
969
970         hentry[0].name = header;
971        
972         g_return_val_if_fail(msginfo != NULL, -1);
973         file = procmsg_get_message_file_path(msginfo);
974         if ((fp = fopen(file, "rb")) == NULL) {
975                FILE_OP_ERROR(file, "fopen");
976                g_free(file);
977                return -1;
978         }
979         val = procheader_get_one_field(buf,len, fp, hentry);
980         if (fclose(fp) == EOF) {
981                 FILE_OP_ERROR(file, "fclose");
982                 unlink(file);
983                 g_free(file);
984                 return -1;
985         }
986
987         g_free(file);
988         if (val == -1)
989                 return -1;
990
991         return 0;
992 }