2006-07-07 [paul] 2.3.1cvs71
[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 GPtrArray *procheader_get_header_array_asis(FILE *fp)
169 {
170         gchar buf[BUFFSIZE];
171         GPtrArray *headers;
172         Header *header;
173
174         g_return_val_if_fail(fp != NULL, NULL);
175
176         headers = g_ptr_array_new();
177
178         while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
179                 if ((header = procheader_parse_header(buf)) != NULL)
180                         g_ptr_array_add(headers, header);
181                         /*
182                 if (*buf == ':') continue;
183                 for (p = buf; *p && *p != ' '; p++) {
184                         if (*p == ':') {
185                                 header = g_new(Header, 1);
186                                 header->name = g_strndup(buf, p - buf);
187                                 p++;
188                                 conv_unmime_header(tmp, sizeof(tmp), p, NULL);
189                                 header->body = g_strdup(tmp);
190
191                                 g_ptr_array_add(headers, header);
192                                 break;
193                         }
194                 }
195                         */
196         }
197
198         return headers;
199 }
200
201 void procheader_header_array_destroy(GPtrArray *harray)
202 {
203         gint i;
204         Header *header;
205
206         for (i = 0; i < harray->len; i++) {
207                 header = g_ptr_array_index(harray, i);
208                 procheader_header_free(header);
209         }
210
211         g_ptr_array_free(harray, TRUE);
212 }
213
214 void procheader_header_free(Header *header)
215 {
216         if (!header) return;
217
218         g_free(header->name);
219         g_free(header->body);
220         g_free(header);
221 }
222
223 /*
224   tests whether two headers' names are equal
225   remove the trailing ':' or ' ' before comparing
226 */
227
228 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
229 {
230         int len1;
231         int len2;
232
233         len1 = strlen(hdr1);
234         len2 = strlen(hdr2);
235         if (hdr1[len1 - 1] == ':')
236                 len1--;
237         if (hdr2[len2 - 1] == ':')
238                 len2--;
239         if (len1 != len2)
240                 return 0;
241
242         return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
243 }
244
245 /*
246   parse headers, for example :
247   From: dinh@enseirb.fr becomes :
248   header->name = "From:"
249   header->body = "dinh@enseirb.fr"
250  */
251
252 Header * procheader_parse_header(gchar * buf)
253 {
254         gchar *p = buf;
255         Header * header;
256
257         if ((*buf == ':') || (*buf == ' '))
258                 return NULL;
259
260         for (p = buf; *p ; p++) {
261                 if ((*p == ':') || (*p == ' ')) {
262                         header = g_new(Header, 1);
263                         header->name = g_strndup(buf, p - buf + 1);
264                         p++;
265                         while (*p == ' ' || *p == '\t') p++;
266                         header->body = conv_unmime_header(p, NULL);
267                         return header;
268                 }
269         }
270         return NULL;
271 }
272
273 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
274 {
275         gchar buf[BUFFSIZE];
276         HeaderEntry *hp;
277         gint hnum;
278         gchar *p;
279
280         if (hentry == NULL) return;
281
282         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
283                != -1) {
284                 hp = hentry + hnum;
285
286                 p = buf + strlen(hp->name);
287                 while (*p == ' ' || *p == '\t') p++;
288
289                 if (hp->body == NULL)
290                         hp->body = g_strdup(p);
291                 else if (procheader_headername_equal(hp->name, "To") ||
292                          procheader_headername_equal(hp->name, "Cc")) {
293                         gchar *tp = hp->body;
294                         hp->body = g_strconcat(tp, ", ", p, NULL);
295                         g_free(tp);
296                 }
297         }
298 }
299
300 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
301                                gboolean full, gboolean decrypted)
302 {
303         struct stat s;
304         FILE *fp;
305         MsgInfo *msginfo;
306
307         if (stat(file, &s) < 0) {
308                 FILE_OP_ERROR(file, "stat");
309                 return NULL;
310         }
311         if (!S_ISREG(s.st_mode))
312                 return NULL;
313
314         if ((fp = g_fopen(file, "rb")) == NULL) {
315                 FILE_OP_ERROR(file, "fopen");
316                 return NULL;
317         }
318
319         msginfo = procheader_parse_stream(fp, flags, full, decrypted);
320         fclose(fp);
321
322         if (msginfo) {
323                 msginfo->size = s.st_size;
324                 msginfo->mtime = s.st_mtime;
325         }
326
327         return msginfo;
328 }
329
330 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
331                               gboolean decrypted)
332 {
333         return parse_stream(&str, TRUE, flags, full, decrypted);
334 }
335
336 enum
337 {
338         H_DATE          = 0,
339         H_FROM          = 1,
340         H_TO            = 2,
341         H_CC            = 3,
342         H_NEWSGROUPS    = 4,
343         H_SUBJECT       = 5,
344         H_MSG_ID        = 6,
345         H_REFERENCES    = 7,
346         H_IN_REPLY_TO   = 8,
347         H_CONTENT_TYPE  = 9,
348         H_SEEN          = 10,
349         H_STATUS        = 11,
350         H_X_STATUS      = 12,
351         H_FROM_SPACE    = 13,
352         H_SC_PLANNED_DOWNLOAD = 14,
353         H_SC_MESSAGE_SIZE = 15,
354         H_FACE          = 16,
355         H_X_FACE        = 17,
356         H_DISPOSITION_NOTIFICATION_TO = 18,
357         H_RETURN_RECEIPT_TO = 19,
358         H_SC_PARTIALLY_RETRIEVED = 20,
359         H_SC_ACCOUNT_SERVER = 21,
360         H_SC_ACCOUNT_LOGIN = 22,
361         H_LIST_POST        = 23,
362         H_LIST_SUBSCRIBE   = 24,
363         H_LIST_UNSUBSCRIBE = 25,
364         H_LIST_HELP        = 26,
365         H_LIST_ARCHIVE     = 27,
366         H_LIST_OWNER       = 28,
367 };
368
369 static HeaderEntry hentry_full[] = {{"Date:",           NULL, FALSE},
370                                    {"From:",            NULL, TRUE},
371                                    {"To:",              NULL, TRUE},
372                                    {"Cc:",              NULL, TRUE},
373                                    {"Newsgroups:",      NULL, TRUE},
374                                    {"Subject:",         NULL, TRUE},
375                                    {"Message-ID:",      NULL, FALSE},
376                                    {"References:",      NULL, FALSE},
377                                    {"In-Reply-To:",     NULL, FALSE},
378                                    {"Content-Type:",    NULL, FALSE},
379                                    {"Seen:",            NULL, FALSE},
380                                    {"Status:",          NULL, FALSE},
381                                    {"X-Status:",        NULL, FALSE},
382                                    {"From ",            NULL, FALSE},
383                                    {"SC-Marked-For-Download:", NULL, FALSE},
384                                    {"SC-Message-Size:", NULL, FALSE},
385                                    {"Face:",            NULL, FALSE},
386                                    {"X-Face:",          NULL, FALSE},
387                                    {"Disposition-Notification-To:", NULL, FALSE},
388                                    {"Return-Receipt-To:", NULL, FALSE},
389                                    {"SC-Partially-Retrieved:", NULL, FALSE},
390                                    {"SC-Account-Server:", NULL, FALSE},
391                                    {"SC-Account-Login:",NULL, FALSE},
392                                    {"List-Post:",       NULL, TRUE},
393                                    {"List-Subscribe:",  NULL, TRUE},
394                                    {"List-Unsubscribe:",NULL, TRUE},
395                                    {"List-Help:",       NULL, TRUE},
396                                    {"List-Archive:",    NULL, TRUE},
397                                    {"List-Owner:",      NULL, TRUE},
398                                    {NULL,               NULL, FALSE}};
399
400 static HeaderEntry hentry_short[] = {{"Date:",          NULL, FALSE},
401                                     {"From:",           NULL, TRUE},
402                                     {"To:",             NULL, TRUE},
403                                     {"Cc:",             NULL, TRUE},
404                                     {"Newsgroups:",     NULL, TRUE},
405                                     {"Subject:",        NULL, TRUE},
406                                     {"Message-ID:",     NULL, FALSE},
407                                     {"References:",     NULL, FALSE},
408                                     {"In-Reply-To:",    NULL, FALSE},
409                                     {"Content-Type:",   NULL, FALSE},
410                                     {"Seen:",           NULL, FALSE},
411                                     {"Status:",         NULL, FALSE},
412                                     {"X-Status:",       NULL, FALSE},
413                                     {"From ",           NULL, FALSE},
414                                     {"SC-Marked-For-Download:", NULL, FALSE},
415                                     {"SC-Message-Size:",NULL, FALSE},
416                                     {NULL,              NULL, FALSE}};
417
418 HeaderEntry* procheader_get_headernames(gboolean full)
419 {
420         return full ? hentry_full : hentry_short;
421 }
422
423 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
424                                  gboolean decrypted)
425 {
426         return parse_stream(fp, FALSE, flags, full, decrypted);
427 }
428
429 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
430                              gboolean full, gboolean decrypted)
431 {
432         MsgInfo *msginfo;
433         gchar buf[BUFFSIZE];
434         gchar *p, *tmp;
435         gchar *hp;
436         HeaderEntry *hentry;
437         gint hnum;
438         void *orig_data = data;
439
440         get_one_field_func get_one_field =
441                 isstring ? (get_one_field_func)string_get_one_field
442                          : (get_one_field_func)procheader_get_one_field;
443
444         hentry = procheader_get_headernames(full);
445
446         if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
447                 while (get_one_field(buf, sizeof(buf), data, NULL) != -1) {
448                         if (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
449                                 strlen("X-Sylpheed-End-Special-Headers:")))
450                                 break;
451                         /* from other mailers */
452                         if (!strncmp(buf, "Date: ", 6)
453                         ||  !strncmp(buf, "To: ", 4)
454                         ||  !strncmp(buf, "From: ", 6)
455                         ||  !strncmp(buf, "Subject: ", 9)) {
456                                 if (isstring)
457                                         data = orig_data;
458                                 else 
459                                         rewind((FILE *)data);
460                                 break;
461                         }
462                 }
463         }
464
465         msginfo = procmsg_msginfo_new();
466         
467         if (flags.tmp_flags || flags.perm_flags) 
468                 msginfo->flags = flags;
469         else 
470                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
471         
472         msginfo->inreplyto = NULL;
473
474         while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
475                != -1) {
476                 hp = buf + strlen(hentry[hnum].name);
477                 while (*hp == ' ' || *hp == '\t') hp++;
478
479                 switch (hnum) {
480                 case H_DATE:
481                         if (msginfo->date) break;
482                         msginfo->date_t =
483                                 procheader_date_parse(NULL, hp, 0);
484                         if (g_utf8_validate(hp, -1, NULL)) {
485                                 msginfo->date = g_strdup(hp);
486                         } else {
487                                 gchar *utf = conv_codeset_strdup(
488                                         hp, 
489                                         conv_get_locale_charset_str_no_utf8(),
490                                         CS_INTERNAL);
491                                 if (utf == NULL || 
492                                     !g_utf8_validate(utf, -1, NULL)) {
493                                         g_free(utf);
494                                         utf = g_malloc(strlen(buf)*2+1);
495                                         conv_localetodisp(utf, 
496                                                 strlen(hp)*2+1, hp);
497                                 }
498                                 msginfo->date = utf;
499                         }
500                         break;
501                 case H_FROM:
502                         if (msginfo->from) break;
503                         msginfo->from = conv_unmime_header(hp, NULL);
504                         msginfo->fromname = procheader_get_fromname(msginfo->from);
505                         remove_return(msginfo->from);
506                         remove_return(msginfo->fromname);
507                         break;
508                 case H_TO:
509                         tmp = conv_unmime_header(hp, NULL);
510                         remove_return(tmp);
511                         if (msginfo->to) {
512                                 p = msginfo->to;
513                                 msginfo->to =
514                                         g_strconcat(p, ", ", tmp, NULL);
515                                 g_free(p);
516                         } else
517                                 msginfo->to = g_strdup(tmp);
518                         g_free(tmp);                                
519                         break;
520                 case H_CC:
521                         tmp = conv_unmime_header(hp, NULL);
522                         remove_return(tmp);
523                         if (msginfo->cc) {
524                                 p = msginfo->cc;
525                                 msginfo->cc =
526                                         g_strconcat(p, ", ", tmp, NULL);
527                                 g_free(p);
528                         } else
529                                 msginfo->cc = g_strdup(tmp);
530                         g_free(tmp);                                
531                         break;
532                 case H_NEWSGROUPS:
533                         if (msginfo->newsgroups) {
534                                 p = msginfo->newsgroups;
535                                 msginfo->newsgroups =
536                                         g_strconcat(p, ",", hp, NULL);
537                                 g_free(p);
538                         } else
539                                 msginfo->newsgroups = g_strdup(hp);
540                         break;
541                 case H_SUBJECT:
542                         if (msginfo->subject) break;
543                         msginfo->subject = conv_unmime_header(hp, NULL);
544                         remove_return(msginfo->subject);
545                         break;
546                 case H_MSG_ID:
547                         if (msginfo->msgid) break;
548
549                         extract_parenthesis(hp, '<', '>');
550                         remove_space(hp);
551                         msginfo->msgid = g_strdup(hp);
552                         break;
553                 case H_REFERENCES:
554                         msginfo->references =
555                                 references_list_prepend(msginfo->references,
556                                                         hp);
557                         break;
558                 case H_IN_REPLY_TO:
559                         if (msginfo->inreplyto) break;
560
561                         eliminate_parenthesis(hp, '(', ')');
562                         if ((p = strrchr(hp, '<')) != NULL &&
563                             strchr(p + 1, '>') != NULL) {
564                                 extract_parenthesis(p, '<', '>');
565                                 remove_space(p);
566                                 if (*p != '\0')
567                                         msginfo->inreplyto = g_strdup(p);
568                         }
569                         break;
570                 case H_CONTENT_TYPE:
571                         if (!g_ascii_strncasecmp(hp, "multipart/", 10))
572                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
573                         break;
574 #ifdef ALLOW_HEADER_HINT                        
575                 case H_SEEN:
576                         /* mnews Seen header */
577                         MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
578                         break;
579 #endif                  
580                 case H_FACE:
581                         if (msginfo->face) break;
582                         msginfo->face = g_strdup(hp);
583                         break;
584                 case H_X_FACE:
585                         if (msginfo->xface) break;
586                         msginfo->xface = g_strdup(hp);
587                         break;
588                 case H_DISPOSITION_NOTIFICATION_TO:
589                         if (msginfo->dispositionnotificationto) break;
590                         msginfo->dispositionnotificationto = g_strdup(hp);
591                         break;
592                 case H_RETURN_RECEIPT_TO:
593                         if (msginfo->returnreceiptto) break;
594                         msginfo->returnreceiptto = g_strdup(hp);
595                         break;
596                 case H_SC_PARTIALLY_RETRIEVED:
597                         if (msginfo->partial_recv) break;
598                         msginfo->partial_recv = g_strdup(hp);
599                         break;
600                 case H_SC_ACCOUNT_SERVER:
601                         if (msginfo->account_server) break;
602                         msginfo->account_server = g_strdup(hp);
603                         break;
604                 case H_SC_ACCOUNT_LOGIN:
605                         if (msginfo->account_login) break;
606                         msginfo->account_login = g_strdup(hp);
607                         break;
608                 case H_SC_MESSAGE_SIZE:
609                         if (msginfo->total_size) break;
610                         msginfo->total_size = atoi(hp);
611                         break;
612                 case H_SC_PLANNED_DOWNLOAD:
613                         msginfo->planned_download = atoi(hp);
614                         break;
615 #ifdef ALLOW_HEADER_HINT                        
616                 case H_STATUS:
617                         if (strchr(hp, 'R') != NULL)
618                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
619                         if (strchr(hp, 'O') != NULL)
620                                 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
621                         if (strchr(hp, 'U') != NULL)
622                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
623                         break;
624                 case H_X_STATUS:
625                         if (strchr(hp, 'D') != NULL)
626                                 MSG_SET_PERM_FLAGS(msginfo->flags,
627                                               MSG_REALLY_DELETED);
628                         if (strchr(hp, 'F') != NULL)
629                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
630                         if (strchr(hp, 'd') != NULL)
631                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
632                         if (strchr(hp, 'r') != NULL)
633                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
634                         if (strchr(hp, 'f') != NULL)
635                                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
636                         break;
637 #endif                  
638                 case H_FROM_SPACE:
639                         if (msginfo->fromspace) break;
640                         msginfo->fromspace = g_strdup(hp);
641                         remove_return(msginfo->fromspace);
642                         break;
643                 case H_LIST_POST:
644                         msginfo->list_post = g_strdup(hp);
645                         break;
646                 case H_LIST_SUBSCRIBE:
647                         msginfo->list_subscribe = g_strdup(hp);
648                         break;
649                 case H_LIST_UNSUBSCRIBE:
650                         msginfo->list_unsubscribe = g_strdup(hp);
651                         break;
652                 case H_LIST_HELP:
653                         msginfo->list_help = g_strdup(hp);
654                         break;
655                 case H_LIST_ARCHIVE:
656                         msginfo->list_archive = g_strdup(hp);
657                         break;
658                 case H_LIST_OWNER:
659                         msginfo->list_owner = g_strdup(hp);
660                 default:
661                         break;
662                 }
663         }
664
665         if (!msginfo->inreplyto && msginfo->references)
666                 msginfo->inreplyto =
667                         g_strdup((gchar *)msginfo->references->data);
668
669         return msginfo;
670 }
671
672 gchar *procheader_get_fromname(const gchar *str)
673 {
674         gchar *tmp, *name;
675
676         Xstrdup_a(tmp, str, return NULL);
677
678         if (*tmp == '\"') {
679                 extract_quote(tmp, '\"');
680                 g_strstrip(tmp);
681         } else if (strchr(tmp, '<')) {
682                 eliminate_parenthesis(tmp, '<', '>');
683                 g_strstrip(tmp);
684                 if (*tmp == '\0') {
685                         strcpy(tmp, str);
686                         extract_parenthesis(tmp, '<', '>');
687                         g_strstrip(tmp);
688                 }
689         } else if (strchr(tmp, '(')) {
690                 extract_parenthesis(tmp, '(', ')');
691                 g_strstrip(tmp);
692         }
693
694         if (*tmp == '\0')
695                 name = g_strdup(str);
696         else
697                 name = g_strdup(tmp);
698
699         return name;
700 }
701
702 static gint procheader_scan_date_string(const gchar *str,
703                                         gchar *weekday, gint *day,
704                                         gchar *month, gint *year,
705                                         gint *hh, gint *mm, gint *ss,
706                                         gchar *zone)
707 {
708         gint result;
709
710         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
711                         weekday, day, month, year, hh, mm, ss, zone);
712         if (result == 8) return 0;
713
714         result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
715                         weekday, day, month, year, hh, mm, ss, zone);
716         if (result == 8) return 0;
717
718         result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
719                         day, month, year, hh, mm, ss, zone);
720         if (result == 7) return 0;
721
722         *zone = '\0';
723         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
724                         weekday, day, month, year, hh, mm, ss);
725         if (result == 7) return 0;
726
727         result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
728                         day, month, year, hh, mm, ss);
729         if (result == 6) return 0;
730
731         *ss = 0;
732         result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
733                         weekday, day, month, year, hh, mm, zone);
734         if (result == 7) return 0;
735
736         result = sscanf(str, "%d %9s %d %2d:%2d %5s",
737                         day, month, year, hh, mm, zone);
738         if (result == 6) return 0;
739
740         *zone = '\0';
741         result = sscanf(str, "%10s %d %9s %d %2d:%2d",
742                         weekday, day, month, year, hh, mm);
743         if (result == 6) return 0;
744
745         result = sscanf(str, "%d %9s %d %2d:%2d",
746                         day, month, year, hh, mm);
747         if (result == 5) return 0;
748
749         return -1;
750 }
751
752 /*
753  * Hiro, most UNIXen support this function:
754  * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
755  */
756 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
757 {
758         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
759         gchar weekday[11];
760         gint day;
761         gchar month[10];
762         gint year;
763         gint hh, mm, ss;
764         GDateMonth dmonth;
765         gchar *p;
766
767         if (!t)
768                 return FALSE;
769         
770         memset(t, 0, sizeof *t);        
771
772         if (procheader_scan_date_string(src, weekday, &day, month, &year,
773                                         &hh, &mm, &ss, zone) < 0) {
774                 g_warning("Invalid date: %s\n", src);
775                 return FALSE;
776         }
777
778         /* Y2K compliant :) */
779         if (year < 100) {
780                 if (year < 70)
781                         year += 2000;
782                 else
783                         year += 1900;
784         }
785
786         month[3] = '\0';
787         if ((p = strstr(monthstr, month)) != NULL)
788                 dmonth = (gint)(p - monthstr) / 3 + 1;
789         else {
790                 g_warning("Invalid month: %s\n", month);
791                 dmonth = G_DATE_BAD_MONTH;
792         }
793
794         t->tm_sec = ss;
795         t->tm_min = mm;
796         t->tm_hour = hh;
797         t->tm_mday = day;
798         t->tm_mon = dmonth - 1;
799         t->tm_year = year - 1900;
800         t->tm_wday = 0;
801         t->tm_yday = 0;
802         t->tm_isdst = -1;
803
804         mktime(t);
805
806         return TRUE;
807 }
808
809 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
810 {
811         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
812         gchar weekday[11];
813         gint day;
814         gchar month[10];
815         gint year;
816         gint hh, mm, ss;
817         gchar zone[6];
818         GDateMonth dmonth = G_DATE_BAD_MONTH;
819         struct tm t;
820         gchar *p;
821         time_t timer;
822         time_t tz_offset;
823
824         if (procheader_scan_date_string(src, weekday, &day, month, &year,
825                                         &hh, &mm, &ss, zone) < 0) {
826                 if (dest && len > 0)
827                         strncpy2(dest, src, len);
828                 return 0;
829         }
830
831         /* Y2K compliant :) */
832         if (year < 1000) {
833                 if (year < 50)
834                         year += 2000;
835                 else
836                         year += 1900;
837         }
838
839         month[3] = '\0';
840         for (p = monthstr; *p != '\0'; p += 3) {
841                 if (!g_ascii_strncasecmp(p, month, 3)) {
842                         dmonth = (gint)(p - monthstr) / 3 + 1;
843                         break;
844                 }
845         }
846
847         t.tm_sec = ss;
848         t.tm_min = mm;
849         t.tm_hour = hh;
850         t.tm_mday = day;
851         t.tm_mon = dmonth - 1;
852         t.tm_year = year - 1900;
853         t.tm_wday = 0;
854         t.tm_yday = 0;
855         t.tm_isdst = -1;
856
857         timer = mktime(&t);
858         tz_offset = remote_tzoffset_sec(zone);
859         if (tz_offset != -1)
860                 timer += tzoffset_sec(&timer) - tz_offset;
861
862         if (dest)
863                 procheader_date_get_localtime(dest, len, timer);
864
865         return timer;
866 }
867
868 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
869 {
870         struct tm *lt;
871         gchar *default_format = "%y/%m/%d(%a) %H:%M";
872         gchar *str;
873         const gchar *src_codeset, *dest_codeset;
874
875         lt = localtime(&timer);
876
877         if (prefs_common.date_format)
878                 strftime(dest, len, prefs_common.date_format, lt);
879         else
880                 strftime(dest, len, default_format, lt);
881
882         if (!g_utf8_validate(dest, -1, NULL)) {
883                 src_codeset = conv_get_locale_charset_str_no_utf8();
884                 dest_codeset = CS_UTF_8;
885                 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
886                 if (str) {
887                         strncpy2(dest, str, len);
888                         g_free(str);
889                 }
890         }
891 }
892
893 /* Added by Mel Hadasht on 27 Aug 2001 */
894 /* Get a header from msginfo */
895 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
896 {
897         gchar *file;
898         FILE *fp;
899         HeaderEntry hentry[]={ { NULL, NULL, TRUE  },
900                                { NULL, NULL, FALSE } };
901         gint val;
902
903         hentry[0].name = header;
904        
905         g_return_val_if_fail(msginfo != NULL, -1);
906         file = procmsg_get_message_file_path(msginfo);
907         if ((fp = g_fopen(file, "rb")) == NULL) {
908                FILE_OP_ERROR(file, "fopen");
909                g_free(file);
910                return -1;
911         }
912         val = procheader_get_one_field(buf,len, fp, hentry);
913         if (fclose(fp) == EOF) {
914                 FILE_OP_ERROR(file, "fclose");
915                 g_unlink(file);
916                 g_free(file);
917                 return -1;
918         }
919
920         g_free(file);
921         if (val == -1)
922                 return -1;
923
924         return 0;
925 }