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