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