for release 3.17.5
[claws.git] / src / procheader.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail 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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <glib.h>
26 #include <glib/gi18n.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <time.h>
31 #include <sys/stat.h>
32
33 #ifdef G_OS_WIN32
34 #  include <w32lib.h>
35 #endif
36
37 #include "procheader.h"
38 #include "procmsg.h"
39 #include "codeconv.h"
40 #include "prefs_common.h"
41 #include "hooks.h"
42 #include "utils.h"
43 #include "defs.h"
44 #include "file-utils.h"
45
46 #define BUFFSIZE        8192
47
48 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
49
50 typedef char *(*getlinefunc) (char *, size_t, void *);
51 typedef int (*peekcharfunc) (void *);
52 typedef int (*getcharfunc) (void *);
53 typedef gint (*get_one_field_func) (gchar **, void *, HeaderEntry[]);
54
55 static gint string_get_one_field(gchar **buf, char **str,
56                                  HeaderEntry hentry[]);
57
58 static char *string_getline(char *buf, size_t len, char **str);
59 static int string_peekchar(char **str);
60 static int file_peekchar(FILE *fp);
61 static gint generic_get_one_field(gchar **bufptr, void *data,
62                                   HeaderEntry hentry[],
63                                   getlinefunc getline, 
64                                   peekcharfunc peekchar,
65                                   gboolean unfold);
66 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
67                              gboolean full, gboolean decrypted);
68
69
70 gint procheader_get_one_field(gchar **buf, FILE *fp,
71                               HeaderEntry hentry[])
72 {
73         return generic_get_one_field(buf, fp, hentry,
74                                      (getlinefunc)fgets_crlf, (peekcharfunc)file_peekchar,
75                                      TRUE);
76 }
77
78 static gint string_get_one_field(gchar **buf, char **str,
79                                  HeaderEntry hentry[])
80 {
81         return generic_get_one_field(buf, str, hentry,
82                                      (getlinefunc)string_getline,
83                                      (peekcharfunc)string_peekchar,
84                                      TRUE);
85 }
86
87 gboolean procheader_skip_headers(FILE *fp)
88 {
89         gchar *buf = g_malloc(BUFFSIZE);
90         do {
91                 if (fgets_crlf(buf, BUFFSIZE - 1, fp) == NULL) {
92                         g_free(buf);
93                         return FALSE;
94                 }
95                 if (buf[0] == '\r' || buf[0] == '\n') {
96                         break;
97                 }
98         } while (TRUE);
99         g_free(buf);
100
101         return TRUE;
102 }
103
104
105 static char *string_getline(char *buf, size_t len, char **str)
106 {
107         gboolean is_cr = FALSE;
108         gboolean last_was_cr = FALSE;
109
110         if (!*str || !**str)
111                 return NULL;
112
113         for (; **str && len > 1; --len) {
114                 is_cr = (**str == '\r');
115                 if ((*buf++ = *(*str)++) == '\n') {
116                     break;
117                 }
118                 if (last_was_cr) {
119                         *(--buf) = '\n';
120                         buf++;
121                     break;
122                 }
123                 last_was_cr = is_cr;
124         }
125                 
126         *buf = '\0';
127
128         return buf;
129 }
130
131 static int string_peekchar(char **str)
132 {
133         return **str;
134 }
135
136 static int file_peekchar(FILE *fp)
137 {
138         return ungetc(getc(fp), fp);
139 }
140
141 static gint generic_get_one_field(gchar **bufptr, void *data,
142                           HeaderEntry *hentry,
143                           getlinefunc getline, peekcharfunc peekchar,
144                           gboolean unfold)
145 {
146         /* returns -1 in case of failure of any kind, whatever it's a parsing error
147            or an allocation error. if returns -1, *bufptr is always NULL, and vice-versa,
148            and if returning 0 (OK), *bufptr is always non-NULL, so callers just have to
149            test the return value
150         */
151         gint nexthead;
152         gint hnum = 0;
153         HeaderEntry *hp = NULL;
154         size_t len;
155         gchar *buf;
156
157         cm_return_val_if_fail(bufptr != NULL, -1);
158
159         len = BUFFSIZE;
160         buf = g_malloc(len);
161
162         if (hentry != NULL) {
163                 /* skip non-required headers */
164                 /* and get hentry header line */
165                 do {
166                         do {
167                                 if (getline(buf, len, data) == NULL) {
168                                         debug_print("generic_get_one_field: getline\n");
169                                         g_free(buf);
170                                         *bufptr = NULL;
171                                         return -1;
172                                 }
173                                 if (buf[0] == '\r' || buf[0] == '\n') {
174                                         debug_print("generic_get_one_field: empty line\n");
175                                         g_free(buf);
176                                         *bufptr = NULL;
177                                         return -1;
178                                 }
179                         } while (buf[0] == ' ' || buf[0] == '\t');
180
181                         for (hp = hentry, hnum = 0; hp->name != NULL;
182                              hp++, hnum++) {
183                                 if (!g_ascii_strncasecmp(hp->name, buf,
184                                                  strlen(hp->name)))
185                                         break;
186                         }
187                 } while (hp->name == NULL);
188         } else {
189                 /* read first line */
190                 if (getline(buf, len, data) == NULL) {
191                         debug_print("generic_get_one_field: getline\n");
192                         g_free(buf);
193                         *bufptr = NULL;
194                         return -1;
195                 }
196                 if (buf[0] == '\r' || buf[0] == '\n') {
197                         debug_print("generic_get_one_field: empty line\n");
198                         g_free(buf);
199                         *bufptr = NULL;
200                         return -1;
201                 }
202         }
203         /* reduce initial buffer to its useful part */
204         len = strlen(buf)+1;
205         buf = g_realloc(buf, len);
206         if (buf == NULL) {
207                 debug_print("generic_get_one_field: reallocation error\n");
208                 *bufptr = NULL;
209                 return -1;
210         }
211
212         /* unfold line */
213         while (1) {
214                 nexthead = peekchar(data);
215                 /* ([*WSP CRLF] 1*WSP) */
216                 if (nexthead == ' ' || nexthead == '\t') {
217                         size_t buflen;
218                         gchar *tmpbuf;
219                         size_t tmplen;
220
221                         gboolean skiptab = (nexthead == '\t');
222                         /* trim previous trailing \n if requesting one header or
223                          * unfolding was requested */
224                         if ((!hentry && unfold) || (hp && hp->unfold))
225                                 strretchomp(buf);
226
227                         buflen = strlen(buf);
228                         
229                         /* read next line */
230                         tmpbuf = g_malloc(BUFFSIZE);
231
232                         if (getline(tmpbuf, BUFFSIZE, data) == NULL) {
233                                 g_free(tmpbuf);
234                                 break;
235                         }
236                         tmplen = strlen(tmpbuf)+1;
237
238                         /* extend initial buffer and concatenate next line */
239                         len += tmplen;
240                         buf = g_realloc(buf, len);
241                         if (buf == NULL) {
242                                 debug_print("generic_get_one_field: reallocation error\n");
243                                 g_free(buf);
244                                 *bufptr = NULL;
245                                 return -1;
246                         }
247                         memcpy(buf+buflen, tmpbuf, tmplen);
248                         g_free(tmpbuf);
249                         if (skiptab) { /* replace tab with space */
250                                 *(buf + buflen) = ' ';
251                         }
252                 } else {
253                         /* remove trailing new line */
254                         strretchomp(buf);
255                         break;
256                 }
257         }
258
259         *bufptr = buf;
260
261         return hnum;
262 }
263
264 gint procheader_get_one_field_asis(gchar **buf, FILE *fp)
265 {
266         return generic_get_one_field(buf, fp, NULL,
267                                      (getlinefunc)fgets_crlf, 
268                                      (peekcharfunc)file_peekchar,
269                                      FALSE);
270 }
271
272 GPtrArray *procheader_get_header_array_asis(FILE *fp)
273 {
274         gchar *buf = NULL;
275         GPtrArray *headers;
276         Header *header;
277
278         cm_return_val_if_fail(fp != NULL, NULL);
279
280         headers = g_ptr_array_new();
281
282         while (procheader_get_one_field_asis(&buf, fp) != -1) {
283                 if ((header = procheader_parse_header(buf)) != NULL)
284                         g_ptr_array_add(headers, header);
285                 g_free(buf);
286                 buf = NULL;
287         }
288
289         return headers;
290 }
291
292 void procheader_header_array_destroy(GPtrArray *harray)
293 {
294         gint i;
295         Header *header;
296
297         cm_return_if_fail(harray != NULL);
298
299         for (i = 0; i < harray->len; i++) {
300                 header = g_ptr_array_index(harray, i);
301                 procheader_header_free(header);
302         }
303
304         g_ptr_array_free(harray, TRUE);
305 }
306
307 void procheader_header_free(Header *header)
308 {
309         if (!header) return;
310
311         g_free(header->name);
312         g_free(header->body);
313         g_free(header);
314 }
315
316 /*
317   tests whether two headers' names are equal
318   remove the trailing ':' or ' ' before comparing
319 */
320
321 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
322 {
323         int len1;
324         int len2;
325
326         len1 = strlen(hdr1);
327         len2 = strlen(hdr2);
328         if (hdr1[len1 - 1] == ':')
329                 len1--;
330         if (hdr2[len2 - 1] == ':')
331                 len2--;
332         if (len1 != len2)
333                 return 0;
334
335         return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
336 }
337
338 /*
339   parse headers, for example :
340   From: dinh@enseirb.fr becomes :
341   header->name = "From:"
342   header->body = "dinh@enseirb.fr"
343  */
344 static gboolean header_is_addr_field(const gchar *hdr)
345 {
346         static char *addr_headers[] = {
347                                 "To:",
348                                 "Cc:",
349                                 "Bcc:",
350                                 "From:",
351                                 "Reply-To:",
352                                 "Followup-To:",
353                                 "Followup-and-Reply-To:",
354                                 "Disposition-Notification-To:",
355                                 "Return-Receipt-To:",
356                                 NULL};
357         int i;
358
359         if (!hdr)
360                 return FALSE;
361
362         for (i = 0; addr_headers[i] != NULL; i++)
363                 if (!strcasecmp(hdr, addr_headers[i]))
364                         return FALSE;
365
366         return FALSE;
367 }
368
369 Header * procheader_parse_header(gchar * buf)
370 {
371         gchar *p;
372         Header * header;
373         gboolean addr_field = FALSE;
374
375         cm_return_val_if_fail(buf != NULL, NULL);
376
377         if ((*buf == ':') || (*buf == ' '))
378                 return NULL;
379
380         for (p = buf; *p ; p++) {
381                 if ((*p == ':') || (*p == ' ')) {
382                         header = g_new(Header, 1);
383                         header->name = g_strndup(buf, p - buf + 1);
384                         addr_field = header_is_addr_field(header->name);
385                         p++;
386                         while (*p == ' ' || *p == '\t') p++;
387                         header->body = conv_unmime_header(p, NULL, addr_field);
388                         return header;
389                 }
390         }
391         return NULL;
392 }
393
394 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
395 {
396         gchar *buf = NULL;
397         HeaderEntry *hp;
398         gint hnum;
399         gchar *p;
400
401         if (hentry == NULL) return;
402
403         while ((hnum = procheader_get_one_field(&buf, fp, hentry)) != -1) {
404                 hp = hentry + hnum;
405
406                 p = buf + strlen(hp->name);
407                 while (*p == ' ' || *p == '\t') p++;
408
409                 if (hp->body == NULL)
410                         hp->body = g_strdup(p);
411                 else if (procheader_headername_equal(hp->name, "To") ||
412                          procheader_headername_equal(hp->name, "Cc")) {
413                         gchar *tp = hp->body;
414                         hp->body = g_strconcat(tp, ", ", p, NULL);
415                         g_free(tp);
416                 }
417                 g_free(buf);
418                 buf = NULL;
419         }
420 }
421
422 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
423                                gboolean full, gboolean decrypted)
424 {
425 #ifdef G_OS_WIN32
426         GFile *f;
427         GFileInfo *fi;
428         GTimeVal tv;
429         GError *error = NULL;
430 #else
431         GStatBuf s;
432 #endif
433         FILE *fp;
434         MsgInfo *msginfo;
435
436 #ifdef G_OS_WIN32
437         f = g_file_new_for_path(file);
438         fi = g_file_query_info(f, "standard::size,standard::type,time::modified",
439                         G_FILE_QUERY_INFO_NONE, NULL, &error);
440         if (error != NULL) {
441                 g_warning(error->message);
442                 g_error_free(error);
443                 g_object_unref(f);
444         }
445 #else
446         if (g_stat(file, &s) < 0) {
447                 FILE_OP_ERROR(file, "stat");
448                 return NULL;
449         }
450 #endif
451
452 #ifdef G_OS_WIN32
453         if (g_file_info_get_file_type(fi) != G_FILE_TYPE_REGULAR) {
454                 g_object_unref(fi);
455                 g_object_unref(f);
456                 return NULL;
457         }
458 #else
459         if (!S_ISREG(s.st_mode))
460                 return NULL;
461 #endif
462
463         if ((fp = claws_fopen(file, "rb")) == NULL) {
464                 FILE_OP_ERROR(file, "claws_fopen");
465                 return NULL;
466         }
467
468         msginfo = procheader_parse_stream(fp, flags, full, decrypted);
469         claws_fclose(fp);
470
471         if (msginfo) {
472 #ifdef G_OS_WIN32
473                 msginfo->size = g_file_info_get_size(fi);
474                 g_file_info_get_modification_time(fi, &tv);
475                 msginfo->mtime = tv.tv_sec;
476 #else
477                 msginfo->size = s.st_size;
478                 msginfo->mtime = s.st_mtime;
479 #endif
480         }
481
482 #ifdef G_OS_WIN32
483         g_object_unref(fi);
484         g_object_unref(f);
485 #endif
486
487         return msginfo;
488 }
489
490 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
491                               gboolean decrypted)
492 {
493         return parse_stream(&str, TRUE, flags, full, decrypted);
494 }
495
496 enum
497 {
498         H_DATE = 0,
499         H_FROM,
500         H_TO,
501         H_CC,
502         H_NEWSGROUPS,
503         H_SUBJECT,
504         H_MSG_ID,
505         H_REFERENCES,
506         H_IN_REPLY_TO,
507         H_CONTENT_TYPE,
508         H_SEEN,
509         H_STATUS,
510         H_FROM_SPACE,
511         H_SC_PLANNED_DOWNLOAD,
512         H_SC_MESSAGE_SIZE,
513         H_FACE,
514         H_X_FACE,
515         H_DISPOSITION_NOTIFICATION_TO,
516         H_RETURN_RECEIPT_TO,
517         H_SC_PARTIALLY_RETRIEVED,
518         H_SC_ACCOUNT_SERVER,
519         H_SC_ACCOUNT_LOGIN,
520         H_LIST_POST,
521         H_LIST_SUBSCRIBE,
522         H_LIST_UNSUBSCRIBE,
523         H_LIST_HELP,
524         H_LIST_ARCHIVE,
525         H_LIST_OWNER,
526         H_RESENT_FROM,
527 };
528
529 static HeaderEntry hentry_full[] = {
530                                    {"Date:",            NULL, FALSE},
531                                    {"From:",            NULL, TRUE},
532                                    {"To:",              NULL, TRUE},
533                                    {"Cc:",              NULL, TRUE},
534                                    {"Newsgroups:",      NULL, TRUE},
535                                    {"Subject:",         NULL, TRUE},
536                                    {"Message-ID:",      NULL, FALSE},
537                                    {"References:",      NULL, FALSE},
538                                    {"In-Reply-To:",     NULL, FALSE},
539                                    {"Content-Type:",    NULL, FALSE},
540                                    {"Seen:",            NULL, FALSE},
541                                    {"Status:",          NULL, FALSE},
542                                    {"From ",            NULL, FALSE},
543                                    {"SC-Marked-For-Download:", NULL, FALSE},
544                                    {"SC-Message-Size:", NULL, FALSE},
545                                    {"Face:",            NULL, FALSE},
546                                    {"X-Face:",          NULL, FALSE},
547                                    {"Disposition-Notification-To:", NULL, FALSE},
548                                    {"Return-Receipt-To:", NULL, FALSE},
549                                    {"SC-Partially-Retrieved:", NULL, FALSE},
550                                    {"SC-Account-Server:", NULL, FALSE},
551                                    {"SC-Account-Login:",NULL, FALSE},
552                                    {"List-Post:",       NULL, TRUE},
553                                    {"List-Subscribe:",  NULL, TRUE},
554                                    {"List-Unsubscribe:",NULL, TRUE},
555                                    {"List-Help:",       NULL, TRUE},
556                                    {"List-Archive:",    NULL, TRUE},
557                                    {"List-Owner:",      NULL, TRUE},
558                                    {"Resent-From:",     NULL, TRUE},
559                                    {NULL,               NULL, FALSE}};
560
561 static HeaderEntry hentry_short[] = {
562                                     {"Date:",           NULL, FALSE},
563                                     {"From:",           NULL, TRUE},
564                                     {"To:",             NULL, TRUE},
565                                     {"Cc:",             NULL, TRUE},
566                                     {"Newsgroups:",     NULL, TRUE},
567                                     {"Subject:",        NULL, TRUE},
568                                     {"Message-ID:",     NULL, FALSE},
569                                     {"References:",     NULL, FALSE},
570                                     {"In-Reply-To:",    NULL, FALSE},
571                                     {"Content-Type:",   NULL, FALSE},
572                                     {"Seen:",           NULL, FALSE},
573                                     {"Status:",         NULL, FALSE},
574                                     {"From ",           NULL, FALSE},
575                                     {"SC-Marked-For-Download:", NULL, FALSE},
576                                     {"SC-Message-Size:",NULL, FALSE},
577                                     {NULL,              NULL, FALSE}};
578
579 static HeaderEntry* procheader_get_headernames(gboolean full)
580 {
581         return full ? hentry_full : hentry_short;
582 }
583
584 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
585                                  gboolean decrypted)
586 {
587         return parse_stream(fp, FALSE, flags, full, decrypted);
588 }
589
590 static gboolean avatar_from_some_face(gpointer source, gpointer userdata)
591 {
592         AvatarCaptureData *acd = (AvatarCaptureData *)source;
593         
594         if (*(acd->content) == '\0') /* won't be null, but may be empty */
595                 return FALSE;
596
597         if (!strcmp(acd->header, hentry_full[H_FACE].name)) {
598                 debug_print("avatar_from_some_face: found 'Face' header\n");
599                 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_FACE, acd->content);
600         }
601 #if HAVE_LIBCOMPFACE
602         else if (!strcmp(acd->header, hentry_full[H_X_FACE].name)) {
603                 debug_print("avatar_from_some_face: found 'X-Face' header\n");
604                 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_XFACE, acd->content);
605         }
606 #endif
607         return FALSE;
608 }
609
610 static gulong avatar_hook_id = HOOK_NONE;
611
612 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
613                              gboolean full, gboolean decrypted)
614 {
615         MsgInfo *msginfo;
616         gchar *buf = NULL;
617         gchar *p, *tmp;
618         gchar *hp;
619         HeaderEntry *hentry;
620         gint hnum;
621         void *orig_data = data;
622
623         get_one_field_func get_one_field =
624                 isstring ? (get_one_field_func)string_get_one_field
625                          : (get_one_field_func)procheader_get_one_field;
626
627         hentry = procheader_get_headernames(full);
628
629         if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
630                 while (get_one_field(&buf, data, NULL) != -1) {
631                         if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
632                                 strlen("X-Claws-End-Special-Headers:"))) ||
633                             (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
634                                 strlen("X-Sylpheed-End-Special-Headers:")))) {
635                                 g_free(buf);
636                                 buf = NULL;
637                                 break;
638                         }
639                         /* from other mailers */
640                         if (!strncmp(buf, "Date: ", 6)
641                         ||  !strncmp(buf, "To: ", 4)
642                         ||  !strncmp(buf, "From: ", 6)
643                         ||  !strncmp(buf, "Subject: ", 9)) {
644                                 if (isstring)
645                                         data = orig_data;
646                                 else 
647                                         rewind((FILE *)data);
648                                 g_free(buf);
649                                 buf = NULL;
650                                 break;
651                         }
652                         g_free(buf);
653                         buf = NULL;
654                 }
655         }
656
657         msginfo = procmsg_msginfo_new();
658         
659         if (flags.tmp_flags || flags.perm_flags) 
660                 msginfo->flags = flags;
661         else 
662                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
663         
664         msginfo->inreplyto = NULL;
665
666         if (avatar_hook_id == HOOK_NONE && (prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
667                 avatar_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_from_some_face, NULL);
668         } else if (avatar_hook_id != HOOK_NONE && !(prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
669                 hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_hook_id);
670                 avatar_hook_id = HOOK_NONE;
671         }
672
673         while ((hnum = get_one_field(&buf, data, hentry)) != -1) {
674                 hp = buf + strlen(hentry[hnum].name);
675                 while (*hp == ' ' || *hp == '\t') hp++;
676
677                 switch (hnum) {
678                 case H_DATE:
679                         if (msginfo->date) break;
680                         msginfo->date_t =
681                                 procheader_date_parse(NULL, hp, 0);
682                         if (g_utf8_validate(hp, -1, NULL)) {
683                                 msginfo->date = g_strdup(hp);
684                         } else {
685                                 gchar *utf = conv_codeset_strdup(
686                                         hp, 
687                                         conv_get_locale_charset_str_no_utf8(),
688                                         CS_INTERNAL);
689                                 if (utf == NULL || 
690                                     !g_utf8_validate(utf, -1, NULL)) {
691                                         g_free(utf);
692                                         utf = g_malloc(strlen(buf)*2+1);
693                                         conv_localetodisp(utf, 
694                                                 strlen(hp)*2+1, hp);
695                                 }
696                                 msginfo->date = utf;
697                         }
698                         break;
699                 case H_FROM:
700                         if (msginfo->from) break;
701                         msginfo->from = conv_unmime_header(hp, NULL, TRUE);
702                         msginfo->fromname = procheader_get_fromname(msginfo->from);
703                         remove_return(msginfo->from);
704                         remove_return(msginfo->fromname);
705                         break;
706                 case H_TO:
707                         tmp = conv_unmime_header(hp, NULL, TRUE);
708                         remove_return(tmp);
709                         if (msginfo->to) {
710                                 p = msginfo->to;
711                                 msginfo->to =
712                                         g_strconcat(p, ", ", tmp, NULL);
713                                 g_free(p);
714                         } else
715                                 msginfo->to = g_strdup(tmp);
716                         g_free(tmp);                                
717                         break;
718                 case H_CC:
719                         tmp = conv_unmime_header(hp, NULL, TRUE);
720                         remove_return(tmp);
721                         if (msginfo->cc) {
722                                 p = msginfo->cc;
723                                 msginfo->cc =
724                                         g_strconcat(p, ", ", tmp, NULL);
725                                 g_free(p);
726                         } else
727                                 msginfo->cc = g_strdup(tmp);
728                         g_free(tmp);                                
729                         break;
730                 case H_NEWSGROUPS:
731                         if (msginfo->newsgroups) {
732                                 p = msginfo->newsgroups;
733                                 msginfo->newsgroups =
734                                         g_strconcat(p, ",", hp, NULL);
735                                 g_free(p);
736                         } else
737                                 msginfo->newsgroups = g_strdup(hp);
738                         break;
739                 case H_SUBJECT:
740                         if (msginfo->subject) break;
741                         msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
742                         unfold_line(msginfo->subject);
743                        break;
744                 case H_MSG_ID:
745                         if (msginfo->msgid) break;
746
747                         extract_parenthesis(hp, '<', '>');
748                         remove_space(hp);
749                         msginfo->msgid = g_strdup(hp);
750                         break;
751                 case H_REFERENCES:
752                         msginfo->references =
753                                 references_list_prepend(msginfo->references,
754                                                         hp);
755                         break;
756                 case H_IN_REPLY_TO:
757                         if (msginfo->inreplyto) break;
758
759                         eliminate_parenthesis(hp, '(', ')');
760                         if ((p = strrchr(hp, '<')) != NULL &&
761                             strchr(p + 1, '>') != NULL) {
762                                 extract_parenthesis(p, '<', '>');
763                                 remove_space(p);
764                                 if (*p != '\0')
765                                         msginfo->inreplyto = g_strdup(p);
766                         }
767                         break;
768                 case H_CONTENT_TYPE:
769                         if (!g_ascii_strncasecmp(hp, "multipart/", 10))
770                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
771                         break;
772                 case H_DISPOSITION_NOTIFICATION_TO:
773                         if (!msginfo->extradata)
774                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
775                         if (msginfo->extradata->dispositionnotificationto) break;
776                         msginfo->extradata->dispositionnotificationto = g_strdup(hp);
777                         break;
778                 case H_RETURN_RECEIPT_TO:
779                         if (!msginfo->extradata)
780                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
781                         if (msginfo->extradata->returnreceiptto) break;
782                         msginfo->extradata->returnreceiptto = g_strdup(hp);
783                         break;
784 /* partial download infos */                    
785                 case H_SC_PARTIALLY_RETRIEVED:
786                         if (!msginfo->extradata)
787                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
788                         if (msginfo->extradata->partial_recv) break;
789                         msginfo->extradata->partial_recv = g_strdup(hp);
790                         break;
791                 case H_SC_ACCOUNT_SERVER:
792                         if (!msginfo->extradata)
793                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
794                         if (msginfo->extradata->account_server) break;
795                         msginfo->extradata->account_server = g_strdup(hp);
796                         break;
797                 case H_SC_ACCOUNT_LOGIN:
798                         if (!msginfo->extradata)
799                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
800                         if (msginfo->extradata->account_login) break;
801                         msginfo->extradata->account_login = g_strdup(hp);
802                         break;
803                 case H_SC_MESSAGE_SIZE:
804                         if (msginfo->total_size) break;
805                         msginfo->total_size = atoi(hp);
806                         break;
807                 case H_SC_PLANNED_DOWNLOAD:
808                         msginfo->planned_download = atoi(hp);
809                         break;
810 /* end partial download infos */
811                 case H_FROM_SPACE:
812                         if (msginfo->fromspace) break;
813                         msginfo->fromspace = g_strdup(hp);
814                         remove_return(msginfo->fromspace);
815                         break;
816 /* list infos */
817                 case H_LIST_POST:
818                         if (!msginfo->extradata)
819                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
820                         if (msginfo->extradata->list_post) break;
821                         msginfo->extradata->list_post = g_strdup(hp);
822                         break;
823                 case H_LIST_SUBSCRIBE:
824                         if (!msginfo->extradata)
825                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
826                         if (msginfo->extradata->list_subscribe) break;
827                         msginfo->extradata->list_subscribe = g_strdup(hp);
828                         break;
829                 case H_LIST_UNSUBSCRIBE:
830                         if (!msginfo->extradata)
831                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
832                         if (msginfo->extradata->list_unsubscribe) break;
833                         msginfo->extradata->list_unsubscribe = g_strdup(hp);
834                         break;
835                 case H_LIST_HELP:
836                         if (!msginfo->extradata)
837                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
838                         if (msginfo->extradata->list_help) break;
839                         msginfo->extradata->list_help = g_strdup(hp);
840                         break;
841                 case H_LIST_ARCHIVE:
842                         if (!msginfo->extradata)
843                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
844                         if (msginfo->extradata->list_archive) break;
845                         msginfo->extradata->list_archive = g_strdup(hp);
846                         break;
847                 case H_LIST_OWNER:
848                         if (!msginfo->extradata)
849                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
850                         if (msginfo->extradata->list_owner) break;
851                         msginfo->extradata->list_owner = g_strdup(hp);
852                         break;
853                 case H_RESENT_FROM:
854                         if (!msginfo->extradata)
855                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
856                         if (msginfo->extradata->resent_from) break;
857                         msginfo->extradata->resent_from = g_strdup(hp);
858                         break;
859 /* end list infos */
860                 default:
861                         break;
862                 }
863                 /* to avoid performance penalty hooklist is invoked only for
864                    headers known to be able to generate avatars */
865                 if (hnum == H_FROM || hnum == H_X_FACE || hnum == H_FACE) {
866                         AvatarCaptureData *acd = g_new0(AvatarCaptureData, 1);
867                         /* no extra memory is wasted, hooks are expected to
868                            take care of copying members when needed */
869                         acd->msginfo = msginfo;
870                         acd->header  = hentry_full[hnum].name;
871                         acd->content = hp;
872                         hooks_invoke(AVATAR_HEADER_UPDATE_HOOKLIST, (gpointer)acd);
873                         g_free(acd);
874                 }
875                 g_free(buf);
876                 buf = NULL;
877         }
878
879         if (!msginfo->inreplyto && msginfo->references)
880                 msginfo->inreplyto =
881                         g_strdup((gchar *)msginfo->references->data);
882
883         return msginfo;
884 }
885
886 gchar *procheader_get_fromname(const gchar *str)
887 {
888         gchar *tmp, *name;
889
890         Xstrdup_a(tmp, str, return NULL);
891
892         if (*tmp == '\"') {
893                 extract_quote(tmp, '\"');
894                 g_strstrip(tmp);
895         } else if (strchr(tmp, '<')) {
896                 eliminate_parenthesis(tmp, '<', '>');
897                 g_strstrip(tmp);
898                 if (*tmp == '\0') {
899                         strcpy(tmp, str);
900                         extract_parenthesis(tmp, '<', '>');
901                         g_strstrip(tmp);
902                 }
903         } else if (strchr(tmp, '(')) {
904                 extract_parenthesis(tmp, '(', ')');
905                 g_strstrip(tmp);
906         }
907
908         if (*tmp == '\0')
909                 name = g_strdup(str);
910         else
911                 name = g_strdup(tmp);
912
913         return name;
914 }
915
916 static gint procheader_scan_date_string(const gchar *str,
917                                         gchar *weekday, gint *day,
918                                         gchar *month, gint *year,
919                                         gint *hh, gint *mm, gint *ss,
920                                         gchar *zone)
921 {
922         gint result;
923         gint month_n;
924         gint secfract;
925         gint zone1 = 0, zone2 = 0;
926         gchar offset_sign, zonestr[7];
927         gchar sep1;
928
929         if (str == NULL)
930                 return -1;
931
932         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %6s",
933                         weekday, day, month, year, hh, mm, ss, zone);
934         if (result == 8) return 0;
935
936         /* RFC2822 */
937         result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %6s",
938                         weekday, day, month, year, hh, mm, ss, zone);
939         if (result == 8) return 0;
940
941         result = sscanf(str, "%3s %3s %d %2d:%2d:%2d %d %6s",
942                         weekday, month, day, hh, mm, ss, year, zone);
943         if (result == 8) return 0;
944
945         result = sscanf(str, "%d %9s %d %2d:%2d:%2d %6s",
946                         day, month, year, hh, mm, ss, zone);
947         if (result == 7) return 0;
948
949         *zone = '\0';
950         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
951                         weekday, day, month, year, hh, mm, ss);
952         if (result == 7) return 0;
953
954         result = sscanf(str, "%3s %3s %d %2d:%2d:%2d %d",
955                         weekday, month, day, hh, mm, ss, year);
956         if (result == 7) return 0;
957
958         result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
959                         day, month, year, hh, mm, ss);
960         if (result == 6) return 0;
961
962         *ss = 0;
963         result = sscanf(str, "%10s %d %9s %d %2d:%2d %6s",
964                         weekday, day, month, year, hh, mm, zone);
965         if (result == 7) return 0;
966
967         result = sscanf(str, "%d %9s %d %2d:%2d %5s",
968                         day, month, year, hh, mm, zone);
969         if (result == 6) return 0;
970
971         *zone = '\0';
972         result = sscanf(str, "%10s %d %9s %d %2d:%2d",
973                         weekday, day, month, year, hh, mm);
974         if (result == 6) return 0;
975
976         result = sscanf(str, "%d %9s %d %2d:%2d",
977                         day, month, year, hh, mm);
978         if (result == 5) return 0;
979
980         *weekday = '\0';
981
982         /* RFC3339 subset, with fraction of second */
983         result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d.%d%6s",
984                         year, &month_n, day, &sep1, hh, mm, ss, &secfract, zonestr);
985         if (result == 9
986                         && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
987                 if (month_n >= 1 && month_n <= 12) {
988                         strncpy2(month, monthstr+((month_n-1)*3), 4);
989                         if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
990                                 strcat(zone, "+00:00");
991                         } else if (sscanf(zonestr, "%c%2d:%2d",
992                                                 &offset_sign, &zone1, &zone2) == 3) {
993                                 strcat(zone, zonestr);
994                         }
995                         return 0;
996                 }
997         }
998
999         /* RFC3339 subset, no fraction of second */
1000         result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d%6s",
1001                         year, &month_n, day, &sep1, hh, mm, ss, zonestr);
1002         if (result == 8
1003                         && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
1004                 if (month_n >= 1 && month_n <= 12) {
1005                         strncpy2(month, monthstr+((month_n-1)*3), 4);
1006                         if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
1007                                 strcat(zone, "+00:00");
1008                         } else if (sscanf(zonestr, "%c%2d:%2d",
1009                                                 &offset_sign, &zone1, &zone2) == 3) {
1010                                 strcat(zone, zonestr);
1011                         }
1012                         return 0;
1013                 }
1014         }
1015
1016         *zone = '\0';
1017
1018         /* RFC3339 subset, no fraction of second, and no timezone offset */
1019         /* This particular "subset" is invalid, RFC requires the offset */
1020         result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d",
1021                         year, &month_n, day, hh, mm, ss);
1022         if (result == 6) {
1023                 if (1 <= month_n && month_n <= 12) {
1024                         strncpy2(month, monthstr+((month_n-1)*3), 4);
1025                         return 0;
1026                 }
1027         }
1028
1029         /* ISO8601 format with just date (YYYY-MM-DD) */
1030         result = sscanf(str, "%4d-%2d-%2d",
1031                         year, &month_n, day);
1032         if (result == 3) {
1033                 *hh = *mm = *ss = 0;
1034                 if (1 <= month_n && month_n <= 12) {
1035                         strncpy2(month, monthstr+((month_n-1)*3), 4);
1036                         return 0;
1037                 }
1038         }
1039
1040         return -1;
1041 }
1042
1043 /*
1044  * Hiro, most UNIXen support this function:
1045  * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
1046  */
1047 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
1048 {
1049         gchar weekday[11];
1050         gint day;
1051         gchar month[10];
1052         gint year;
1053         gint hh, mm, ss;
1054         GDateMonth dmonth;
1055         gchar *p;
1056
1057         if (!t)
1058                 return FALSE;
1059         
1060         memset(t, 0, sizeof *t);        
1061
1062         if (procheader_scan_date_string(src, weekday, &day, month, &year,
1063                                         &hh, &mm, &ss, zone) < 0) {
1064                 g_warning("Invalid date: %s", src);
1065                 return FALSE;
1066         }
1067
1068         /* Y2K compliant :) */
1069         if (year < 100) {
1070                 if (year < 70)
1071                         year += 2000;
1072                 else
1073                         year += 1900;
1074         }
1075
1076         month[3] = '\0';
1077         if ((p = strstr(monthstr, month)) != NULL)
1078                 dmonth = (gint)(p - monthstr) / 3 + 1;
1079         else {
1080                 g_warning("Invalid month: %s", month);
1081                 dmonth = G_DATE_BAD_MONTH;
1082         }
1083
1084         t->tm_sec = ss;
1085         t->tm_min = mm;
1086         t->tm_hour = hh;
1087         t->tm_mday = day;
1088         t->tm_mon = dmonth - 1;
1089         t->tm_year = year - 1900;
1090         t->tm_wday = 0;
1091         t->tm_yday = 0;
1092         t->tm_isdst = -1;
1093
1094         mktime(t);
1095
1096         return TRUE;
1097 }
1098
1099 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
1100 {
1101         gchar weekday[11];
1102         gint day;
1103         gchar month[10];
1104         gint year;
1105         gint hh, mm, ss;
1106         gchar zone[7];
1107         GDateMonth dmonth = G_DATE_BAD_MONTH;
1108         gchar *p;
1109         time_t timer;
1110
1111         if (procheader_scan_date_string(src, weekday, &day, month, &year,
1112                                         &hh, &mm, &ss, zone) < 0) {
1113                 if (dest && len > 0)
1114                         strncpy2(dest, src, len);
1115                 return 0;
1116         }
1117
1118         month[3] = '\0';
1119         for (p = monthstr; *p != '\0'; p += 3) {
1120                 if (!g_ascii_strncasecmp(p, month, 3)) {
1121                         dmonth = (gint)(p - monthstr) / 3 + 1;
1122                         break;
1123                 }
1124         }
1125
1126 #ifdef G_OS_WIN32
1127         GTimeZone *tz;
1128         GDateTime *dt, *dt2;
1129
1130         tz = g_time_zone_new(zone); // can't return NULL no need to check for it
1131         dt = g_date_time_new(tz, 1, 1, 1, 0, 0, 0);
1132         g_time_zone_unref(tz);
1133         dt2 = g_date_time_add_full(dt, year-1, dmonth-1, day-1, hh, mm, ss);
1134         g_date_time_unref(dt);
1135
1136         timer = g_date_time_to_unix(dt2);
1137         g_date_time_unref(dt2);
1138
1139 #else
1140         struct tm t;
1141         time_t tz_offset;
1142
1143         /* Y2K compliant :) */
1144         if (year < 1000) {
1145                 if (year < 50)
1146                         year += 2000;
1147                 else
1148                         year += 1900;
1149         }
1150
1151         t.tm_sec = ss;
1152         t.tm_min = mm;
1153         t.tm_hour = hh;
1154         t.tm_mday = day;
1155         t.tm_mon = dmonth - 1;
1156         t.tm_year = year - 1900;
1157         t.tm_wday = 0;
1158         t.tm_yday = 0;
1159         t.tm_isdst = -1;
1160
1161         timer = mktime(&t);
1162         tz_offset = remote_tzoffset_sec(zone);
1163         if (tz_offset != -1)
1164                 timer += tzoffset_sec(&timer) - tz_offset;
1165
1166         if (dest)
1167                 procheader_date_get_localtime(dest, len, timer);
1168 #endif
1169
1170         return timer;
1171 }
1172
1173 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
1174 {
1175         struct tm *lt;
1176         gchar *default_format = "%y/%m/%d(%a) %H:%M";
1177         gchar *str;
1178         const gchar *src_codeset, *dest_codeset;
1179         struct tm buf;
1180
1181         if (timer > 0)
1182                 lt = localtime_r(&timer, &buf);
1183         else {
1184                 time_t dummy = 1;
1185                 lt = localtime_r(&dummy, &buf);
1186         }
1187
1188         if (prefs_common.date_format)
1189                 fast_strftime(dest, len, prefs_common.date_format, lt);
1190         else
1191                 fast_strftime(dest, len, default_format, lt);
1192
1193         if (!g_utf8_validate(dest, -1, NULL)) {
1194                 src_codeset = conv_get_locale_charset_str_no_utf8();
1195                 dest_codeset = CS_UTF_8;
1196                 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
1197                 if (str) {
1198                         strncpy2(dest, str, len);
1199                         g_free(str);
1200                 }
1201         }
1202 }
1203
1204 /* Added by Mel Hadasht on 27 Aug 2001 */
1205 /* Get a header from msginfo */
1206 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar **buf, gchar *header)
1207 {
1208         gchar *file;
1209         FILE *fp;
1210         HeaderEntry hentry[]={ { NULL, NULL, TRUE  },
1211                                                    { NULL, NULL, FALSE } };
1212         gint val;
1213
1214         cm_return_val_if_fail(msginfo != NULL, -1);
1215         cm_return_val_if_fail(buf != NULL, -1);
1216         cm_return_val_if_fail(header != NULL, -1);
1217
1218         hentry[0].name = header;
1219
1220         file = procmsg_get_message_file_path(msginfo);
1221         if ((fp = claws_fopen(file, "rb")) == NULL) {
1222                 FILE_OP_ERROR(file, "claws_fopen");
1223                 g_free(file);
1224                 g_free(*buf);
1225                 *buf = NULL;
1226                 return -1;
1227         }
1228         val = procheader_get_one_field(buf, fp, hentry);
1229
1230         if (claws_fclose(fp) == EOF) {
1231                 FILE_OP_ERROR(file, "claws_fclose");
1232                 claws_unlink(file);
1233                 g_free(file);
1234                 g_free(*buf);
1235                 *buf = NULL;
1236                 return -1;
1237         }
1238
1239         g_free(file);
1240         if (val == -1) {
1241                 /* *buf is already NULL in that case, see procheader_get_one_field() */
1242                 return -1;
1243         }
1244
1245         return 0;
1246 }
1247
1248 HeaderEntry *procheader_entries_from_str(const gchar *str)
1249 {
1250         HeaderEntry *entries = NULL, *he;
1251         int numh = 0, i = 0;
1252         gchar **names = NULL;
1253         const gchar *s = str;
1254
1255         if (s == NULL) {
1256                 return NULL;
1257         }
1258         while (*s != '\0') {
1259                 if (*s == ' ') ++numh;
1260                 ++s;
1261         }
1262         if (numh == 0) {
1263                 return NULL;
1264         }
1265         entries = g_new0(HeaderEntry, numh + 1); /* room for last NULL */
1266         s = str;
1267         ++s; /* skip first space */
1268         names = g_strsplit(s, " ", numh);
1269         he = entries;
1270         while (names[i]) {
1271                 he->name = g_strdup_printf("%s:", names[i]);
1272                 he->body = NULL;
1273                 he->unfold = FALSE;
1274                 ++i, ++he;
1275         }
1276         he->name = NULL;
1277         g_strfreev(names);
1278         return entries;
1279 }
1280
1281 void procheader_entries_free (HeaderEntry *entries)
1282 {
1283         if (entries != NULL) {
1284                 HeaderEntry *he = entries;
1285                 while (he->name != NULL) {
1286                         g_free(he->name);
1287                         if (he->body != NULL)
1288                                 g_free(he->body);
1289                         ++he;                   
1290                 }
1291                 g_free(entries);
1292         }
1293 }
1294
1295 gboolean procheader_header_is_internal(const gchar *hdr_name)
1296 {
1297         const gchar *internal_hdrs[] = {
1298                 "AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:", "CFG:",
1299                 "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:", "R:", "MAID:",
1300                 "SCF:", "RMID:", "FMID:", "NAID:",
1301                 "X-Claws-Account-Id:",
1302                 "X-Claws-Sign:",
1303                 "X-Claws-Encrypt:",
1304                 "X-Claws-Privacy-System:",
1305                 "X-Claws-Auto-Wrapping:",
1306                 "X-Claws-Auto-Indent:",
1307                 "X-Claws-End-Special-Headers:",
1308                 "X-Sylpheed-Account-Id:",
1309                 "X-Sylpheed-Sign:",
1310                 "X-Sylpheed-Encrypt:",
1311                 "X-Sylpheed-Privacy-System:",
1312                 "X-Sylpheed-End-Special-Headers:",
1313                  NULL
1314         };
1315         int i;
1316
1317         for (i = 0; internal_hdrs[i]; i++) {
1318                 if (!strcmp(hdr_name, internal_hdrs[i]))
1319                         return TRUE;
1320         }
1321         return FALSE;
1322 }