RSSyl: Stop earlier when an invalid feed is encountered.
[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         GStatBuf s;
407         FILE *fp;
408         MsgInfo *msginfo;
409
410         if (g_stat(file, &s) < 0) {
411                 FILE_OP_ERROR(file, "stat");
412                 return NULL;
413         }
414         if (!S_ISREG(s.st_mode))
415                 return NULL;
416
417         if ((fp = g_fopen(file, "rb")) == NULL) {
418                 FILE_OP_ERROR(file, "fopen");
419                 return NULL;
420         }
421
422         msginfo = procheader_parse_stream(fp, flags, full, decrypted);
423         fclose(fp);
424
425         if (msginfo) {
426                 msginfo->size = s.st_size;
427                 msginfo->mtime = s.st_mtime;
428         }
429
430         return msginfo;
431 }
432
433 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
434                               gboolean decrypted)
435 {
436         return parse_stream(&str, TRUE, flags, full, decrypted);
437 }
438
439 enum
440 {
441         H_DATE = 0,
442         H_FROM,
443         H_TO,
444         H_CC,
445         H_NEWSGROUPS,
446         H_SUBJECT,
447         H_MSG_ID,
448         H_REFERENCES,
449         H_IN_REPLY_TO,
450         H_CONTENT_TYPE,
451         H_SEEN,
452         H_STATUS,
453         H_FROM_SPACE,
454         H_SC_PLANNED_DOWNLOAD,
455         H_SC_MESSAGE_SIZE,
456         H_FACE,
457         H_X_FACE,
458         H_DISPOSITION_NOTIFICATION_TO,
459         H_RETURN_RECEIPT_TO,
460         H_SC_PARTIALLY_RETRIEVED,
461         H_SC_ACCOUNT_SERVER,
462         H_SC_ACCOUNT_LOGIN,
463         H_LIST_POST,
464         H_LIST_SUBSCRIBE,
465         H_LIST_UNSUBSCRIBE,
466         H_LIST_HELP,
467         H_LIST_ARCHIVE,
468         H_LIST_OWNER,
469         H_RESENT_FROM,
470 };
471
472 static HeaderEntry hentry_full[] = {
473                                    {"Date:",            NULL, FALSE},
474                                    {"From:",            NULL, TRUE},
475                                    {"To:",              NULL, TRUE},
476                                    {"Cc:",              NULL, TRUE},
477                                    {"Newsgroups:",      NULL, TRUE},
478                                    {"Subject:",         NULL, TRUE},
479                                    {"Message-ID:",      NULL, FALSE},
480                                    {"References:",      NULL, FALSE},
481                                    {"In-Reply-To:",     NULL, FALSE},
482                                    {"Content-Type:",    NULL, FALSE},
483                                    {"Seen:",            NULL, FALSE},
484                                    {"Status:",          NULL, FALSE},
485                                    {"From ",            NULL, FALSE},
486                                    {"SC-Marked-For-Download:", NULL, FALSE},
487                                    {"SC-Message-Size:", NULL, FALSE},
488                                    {"Face:",            NULL, FALSE},
489                                    {"X-Face:",          NULL, FALSE},
490                                    {"Disposition-Notification-To:", NULL, FALSE},
491                                    {"Return-Receipt-To:", NULL, FALSE},
492                                    {"SC-Partially-Retrieved:", NULL, FALSE},
493                                    {"SC-Account-Server:", NULL, FALSE},
494                                    {"SC-Account-Login:",NULL, FALSE},
495                                    {"List-Post:",       NULL, TRUE},
496                                    {"List-Subscribe:",  NULL, TRUE},
497                                    {"List-Unsubscribe:",NULL, TRUE},
498                                    {"List-Help:",       NULL, TRUE},
499                                    {"List-Archive:",    NULL, TRUE},
500                                    {"List-Owner:",      NULL, TRUE},
501                                    {"Resent-From:",     NULL, TRUE},
502                                    {NULL,               NULL, FALSE}};
503
504 static HeaderEntry hentry_short[] = {
505                                     {"Date:",           NULL, FALSE},
506                                     {"From:",           NULL, TRUE},
507                                     {"To:",             NULL, TRUE},
508                                     {"Cc:",             NULL, TRUE},
509                                     {"Newsgroups:",     NULL, TRUE},
510                                     {"Subject:",        NULL, TRUE},
511                                     {"Message-ID:",     NULL, FALSE},
512                                     {"References:",     NULL, FALSE},
513                                     {"In-Reply-To:",    NULL, FALSE},
514                                     {"Content-Type:",   NULL, FALSE},
515                                     {"Seen:",           NULL, FALSE},
516                                     {"Status:",         NULL, FALSE},
517                                     {"From ",           NULL, FALSE},
518                                     {"SC-Marked-For-Download:", NULL, FALSE},
519                                     {"SC-Message-Size:",NULL, FALSE},
520                                     {NULL,              NULL, FALSE}};
521
522 static HeaderEntry* procheader_get_headernames(gboolean full)
523 {
524         return full ? hentry_full : hentry_short;
525 }
526
527 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
528                                  gboolean decrypted)
529 {
530         return parse_stream(fp, FALSE, flags, full, decrypted);
531 }
532
533 static gboolean avatar_from_some_face(gpointer source, gpointer userdata)
534 {
535         AvatarCaptureData *acd = (AvatarCaptureData *)source;
536         
537         if (*(acd->content) == '\0') /* won't be null, but may be empty */
538                 return FALSE;
539
540         if (!strcmp(acd->header, hentry_full[H_FACE].name)) {
541                 debug_print("avatar_from_some_face: found 'Face' header\n");
542                 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_FACE, acd->content);
543         }
544 #if HAVE_LIBCOMPFACE
545         else if (!strcmp(acd->header, hentry_full[H_X_FACE].name)) {
546                 debug_print("avatar_from_some_face: found 'X-Face' header\n");
547                 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_XFACE, acd->content);
548         }
549 #endif
550         return FALSE;
551 }
552
553 static guint avatar_hook_id = 0;
554
555 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
556                              gboolean full, gboolean decrypted)
557 {
558         MsgInfo *msginfo;
559         gchar *buf = NULL;
560         gchar *p, *tmp;
561         gchar *hp;
562         HeaderEntry *hentry;
563         gint hnum;
564         void *orig_data = data;
565
566         get_one_field_func get_one_field =
567                 isstring ? (get_one_field_func)string_get_one_field
568                          : (get_one_field_func)procheader_get_one_field;
569
570         hentry = procheader_get_headernames(full);
571
572         if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
573                 while (get_one_field(&buf, data, NULL) != -1) {
574                         if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
575                                 strlen("X-Claws-End-Special-Headers:"))) ||
576                             (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
577                                 strlen("X-Sylpheed-End-Special-Headers:"))))
578                                 break;
579                         /* from other mailers */
580                         if (!strncmp(buf, "Date: ", 6)
581                         ||  !strncmp(buf, "To: ", 4)
582                         ||  !strncmp(buf, "From: ", 6)
583                         ||  !strncmp(buf, "Subject: ", 9)) {
584                                 if (isstring)
585                                         data = orig_data;
586                                 else 
587                                         rewind((FILE *)data);
588                                 g_free(buf);
589                                 buf = NULL;
590                                 break;
591                         }
592                         g_free(buf);
593                         buf = NULL;
594                 }
595         }
596
597         msginfo = procmsg_msginfo_new();
598         
599         if (flags.tmp_flags || flags.perm_flags) 
600                 msginfo->flags = flags;
601         else 
602                 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
603         
604         msginfo->inreplyto = NULL;
605
606         if (avatar_hook_id == 0 && (prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
607                 avatar_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_from_some_face, NULL);
608         } else if (avatar_hook_id != 0 && !(prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
609                 hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_hook_id);
610                 avatar_hook_id = 0;
611         }
612
613         while ((hnum = get_one_field(&buf, data, hentry)) != -1) {
614                 hp = buf + strlen(hentry[hnum].name);
615                 while (*hp == ' ' || *hp == '\t') hp++;
616
617                 switch (hnum) {
618                 case H_DATE:
619                         if (msginfo->date) break;
620                         msginfo->date_t =
621                                 procheader_date_parse(NULL, hp, 0);
622                         if (g_utf8_validate(hp, -1, NULL)) {
623                                 msginfo->date = g_strdup(hp);
624                         } else {
625                                 gchar *utf = conv_codeset_strdup(
626                                         hp, 
627                                         conv_get_locale_charset_str_no_utf8(),
628                                         CS_INTERNAL);
629                                 if (utf == NULL || 
630                                     !g_utf8_validate(utf, -1, NULL)) {
631                                         g_free(utf);
632                                         utf = g_malloc(strlen(buf)*2+1);
633                                         conv_localetodisp(utf, 
634                                                 strlen(hp)*2+1, hp);
635                                 }
636                                 msginfo->date = utf;
637                         }
638                         break;
639                 case H_FROM:
640                         if (msginfo->from) break;
641                         msginfo->from = conv_unmime_header(hp, NULL, TRUE);
642                         msginfo->fromname = procheader_get_fromname(msginfo->from);
643                         remove_return(msginfo->from);
644                         remove_return(msginfo->fromname);
645                         break;
646                 case H_TO:
647                         tmp = conv_unmime_header(hp, NULL, TRUE);
648                         remove_return(tmp);
649                         if (msginfo->to) {
650                                 p = msginfo->to;
651                                 msginfo->to =
652                                         g_strconcat(p, ", ", tmp, NULL);
653                                 g_free(p);
654                         } else
655                                 msginfo->to = g_strdup(tmp);
656                         g_free(tmp);                                
657                         break;
658                 case H_CC:
659                         tmp = conv_unmime_header(hp, NULL, TRUE);
660                         remove_return(tmp);
661                         if (msginfo->cc) {
662                                 p = msginfo->cc;
663                                 msginfo->cc =
664                                         g_strconcat(p, ", ", tmp, NULL);
665                                 g_free(p);
666                         } else
667                                 msginfo->cc = g_strdup(tmp);
668                         g_free(tmp);                                
669                         break;
670                 case H_NEWSGROUPS:
671                         if (msginfo->newsgroups) {
672                                 p = msginfo->newsgroups;
673                                 msginfo->newsgroups =
674                                         g_strconcat(p, ",", hp, NULL);
675                                 g_free(p);
676                         } else
677                                 msginfo->newsgroups = g_strdup(hp);
678                         break;
679                 case H_SUBJECT:
680                         if (msginfo->subject) break;
681                         msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
682                         unfold_line(msginfo->subject);
683                        break;
684                 case H_MSG_ID:
685                         if (msginfo->msgid) break;
686
687                         extract_parenthesis(hp, '<', '>');
688                         remove_space(hp);
689                         msginfo->msgid = g_strdup(hp);
690                         break;
691                 case H_REFERENCES:
692                         msginfo->references =
693                                 references_list_prepend(msginfo->references,
694                                                         hp);
695                         break;
696                 case H_IN_REPLY_TO:
697                         if (msginfo->inreplyto) break;
698
699                         eliminate_parenthesis(hp, '(', ')');
700                         if ((p = strrchr(hp, '<')) != NULL &&
701                             strchr(p + 1, '>') != NULL) {
702                                 extract_parenthesis(p, '<', '>');
703                                 remove_space(p);
704                                 if (*p != '\0')
705                                         msginfo->inreplyto = g_strdup(p);
706                         }
707                         break;
708                 case H_CONTENT_TYPE:
709                         if (!g_ascii_strncasecmp(hp, "multipart/", 10))
710                                 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
711                         break;
712                 case H_DISPOSITION_NOTIFICATION_TO:
713                         if (!msginfo->extradata)
714                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
715                         if (msginfo->extradata->dispositionnotificationto) break;
716                         msginfo->extradata->dispositionnotificationto = g_strdup(hp);
717                         break;
718                 case H_RETURN_RECEIPT_TO:
719                         if (!msginfo->extradata)
720                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
721                         if (msginfo->extradata->returnreceiptto) break;
722                         msginfo->extradata->returnreceiptto = g_strdup(hp);
723                         break;
724 /* partial download infos */                    
725                 case H_SC_PARTIALLY_RETRIEVED:
726                         if (!msginfo->extradata)
727                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
728                         if (msginfo->extradata->partial_recv) break;
729                         msginfo->extradata->partial_recv = g_strdup(hp);
730                         break;
731                 case H_SC_ACCOUNT_SERVER:
732                         if (!msginfo->extradata)
733                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
734                         if (msginfo->extradata->account_server) break;
735                         msginfo->extradata->account_server = g_strdup(hp);
736                         break;
737                 case H_SC_ACCOUNT_LOGIN:
738                         if (!msginfo->extradata)
739                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
740                         if (msginfo->extradata->account_login) break;
741                         msginfo->extradata->account_login = g_strdup(hp);
742                         break;
743                 case H_SC_MESSAGE_SIZE:
744                         if (msginfo->total_size) break;
745                         msginfo->total_size = atoi(hp);
746                         break;
747                 case H_SC_PLANNED_DOWNLOAD:
748                         msginfo->planned_download = atoi(hp);
749                         break;
750 /* end partial download infos */
751                 case H_FROM_SPACE:
752                         if (msginfo->fromspace) break;
753                         msginfo->fromspace = g_strdup(hp);
754                         remove_return(msginfo->fromspace);
755                         break;
756 /* list infos */
757                 case H_LIST_POST:
758                         if (!msginfo->extradata)
759                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
760                         if (msginfo->extradata->list_post) break;
761                         msginfo->extradata->list_post = g_strdup(hp);
762                         break;
763                 case H_LIST_SUBSCRIBE:
764                         if (!msginfo->extradata)
765                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
766                         if (msginfo->extradata->list_subscribe) break;
767                         msginfo->extradata->list_subscribe = g_strdup(hp);
768                         break;
769                 case H_LIST_UNSUBSCRIBE:
770                         if (!msginfo->extradata)
771                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
772                         if (msginfo->extradata->list_unsubscribe) break;
773                         msginfo->extradata->list_unsubscribe = g_strdup(hp);
774                         break;
775                 case H_LIST_HELP:
776                         if (!msginfo->extradata)
777                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
778                         if (msginfo->extradata->list_help) break;
779                         msginfo->extradata->list_help = g_strdup(hp);
780                         break;
781                 case H_LIST_ARCHIVE:
782                         if (!msginfo->extradata)
783                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
784                         if (msginfo->extradata->list_archive) break;
785                         msginfo->extradata->list_archive = g_strdup(hp);
786                         break;
787                 case H_LIST_OWNER:
788                         if (!msginfo->extradata)
789                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
790                         if (msginfo->extradata->list_owner) break;
791                         msginfo->extradata->list_owner = g_strdup(hp);
792                         break;
793                 case H_RESENT_FROM:
794                         if (!msginfo->extradata)
795                                 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
796                         if (msginfo->extradata->resent_from) break;
797                         msginfo->extradata->resent_from = g_strdup(hp);
798                         break;
799 /* end list infos */
800                 default:
801                         break;
802                 }
803                 /* to avoid performance penalty hooklist is invoked only for
804                    headers known to be able to generate avatars */
805                 if (hnum == H_FROM || hnum == H_X_FACE || hnum == H_FACE) {
806                         AvatarCaptureData *acd = g_new0(AvatarCaptureData, 1);
807                         /* no extra memory is wasted, hooks are expected to
808                            take care of copying members when needed */
809                         acd->msginfo = msginfo;
810                         acd->header  = hentry_full[hnum].name;
811                         acd->content = hp;
812                         hooks_invoke(AVATAR_HEADER_UPDATE_HOOKLIST, (gpointer)acd);
813                         g_free(acd);
814                 }
815                 g_free(buf);
816                 buf = NULL;
817         }
818
819         if (!msginfo->inreplyto && msginfo->references)
820                 msginfo->inreplyto =
821                         g_strdup((gchar *)msginfo->references->data);
822
823         return msginfo;
824 }
825
826 gchar *procheader_get_fromname(const gchar *str)
827 {
828         gchar *tmp, *name;
829
830         Xstrdup_a(tmp, str, return NULL);
831
832         if (*tmp == '\"') {
833                 extract_quote(tmp, '\"');
834                 g_strstrip(tmp);
835         } else if (strchr(tmp, '<')) {
836                 eliminate_parenthesis(tmp, '<', '>');
837                 g_strstrip(tmp);
838                 if (*tmp == '\0') {
839                         strcpy(tmp, str);
840                         extract_parenthesis(tmp, '<', '>');
841                         g_strstrip(tmp);
842                 }
843         } else if (strchr(tmp, '(')) {
844                 extract_parenthesis(tmp, '(', ')');
845                 g_strstrip(tmp);
846         }
847
848         if (*tmp == '\0')
849                 name = g_strdup(str);
850         else
851                 name = g_strdup(tmp);
852
853         return name;
854 }
855
856 static gint procheader_scan_date_string(const gchar *str,
857                                         gchar *weekday, gint *day,
858                                         gchar *month, gint *year,
859                                         gint *hh, gint *mm, gint *ss,
860                                         gchar *zone)
861 {
862         gint result;
863         gint month_n;
864         gint secfract;
865         gint zone1 = 0, zone2 = 0;
866         gchar offset_sign, zonestr[7];
867         gchar sep1;
868
869         if (str == NULL)
870                 return -1;
871
872         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %6s",
873                         weekday, day, month, year, hh, mm, ss, zone);
874         if (result == 8) return 0;
875
876         /* RFC2822 */
877         result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %6s",
878                         weekday, day, month, year, hh, mm, ss, zone);
879         if (result == 8) return 0;
880
881         result = sscanf(str, "%d %9s %d %2d:%2d:%2d %6s",
882                         day, month, year, hh, mm, ss, zone);
883         if (result == 7) return 0;
884
885         *zone = '\0';
886         result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
887                         weekday, day, month, year, hh, mm, ss);
888         if (result == 7) return 0;
889
890         result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
891                         day, month, year, hh, mm, ss);
892         if (result == 6) return 0;
893
894         *ss = 0;
895         result = sscanf(str, "%10s %d %9s %d %2d:%2d %6s",
896                         weekday, day, month, year, hh, mm, zone);
897         if (result == 7) return 0;
898
899         result = sscanf(str, "%d %9s %d %2d:%2d %5s",
900                         day, month, year, hh, mm, zone);
901         if (result == 6) return 0;
902
903         *zone = '\0';
904         result = sscanf(str, "%10s %d %9s %d %2d:%2d",
905                         weekday, day, month, year, hh, mm);
906         if (result == 6) return 0;
907
908         result = sscanf(str, "%d %9s %d %2d:%2d",
909                         day, month, year, hh, mm);
910         if (result == 5) return 0;
911
912         *weekday = '\0';
913
914         /* RFC3339 subset, with fraction of second */
915         result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d.%d%6s",
916                         year, &month_n, day, &sep1, hh, mm, ss, &secfract, zonestr);
917         if (result == 9
918                         && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
919                 if (month_n >= 1 && month_n <= 12) {
920                         strncpy2(month, monthstr+((month_n-1)*3), 4);
921                         if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
922                                 strcat(zone, "+00:00");
923                         } else if (sscanf(zonestr, "%c%2d:%2d",
924                                                 &offset_sign, &zone1, &zone2) == 3) {
925                                 strcat(zone, zonestr);
926                         }
927                         return 0;
928                 }
929         }
930
931         /* RFC3339 subset, no fraction of second */
932         result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d%6s",
933                         year, &month_n, day, &sep1, hh, mm, ss, zonestr);
934         if (result == 8
935                         && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
936                 if (month_n >= 1 && month_n <= 12) {
937                         strncpy2(month, monthstr+((month_n-1)*3), 4);
938                         if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
939                                 strcat(zone, "+00:00");
940                         } else if (sscanf(zonestr, "%c%2d:%2d",
941                                                 &offset_sign, &zone1, &zone2) == 3) {
942                                 strcat(zone, zonestr);
943                         }
944                         return 0;
945                 }
946         }
947
948         *zone = '\0';
949
950         /* RFC3339 subset, no fraction of second, and no timezone offset */
951         /* This particular "subset" is invalid, RFC requires the offset */
952         result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d",
953                         year, &month_n, day, hh, mm, ss);
954         if (result == 6) {
955                 if (1 <= month_n && month_n <= 12) {
956                         strncpy2(month, monthstr+((month_n-1)*3), 4);
957                         return 0;
958                 }
959         }
960
961         /* ISO8601 format with just date (YYYY-MM-DD) */
962         result = sscanf(str, "%4d-%2d-%2d",
963                         year, &month_n, day);
964         if (result == 3) {
965                 *hh = *mm = *ss = 0;
966                 if (1 <= month_n && month_n <= 12) {
967                         strncpy2(month, monthstr+((month_n-1)*3), 4);
968                         return 0;
969                 }
970         }
971
972         return -1;
973 }
974
975 /*
976  * Hiro, most UNIXen support this function:
977  * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
978  */
979 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
980 {
981         gchar weekday[11];
982         gint day;
983         gchar month[10];
984         gint year;
985         gint hh, mm, ss;
986         GDateMonth dmonth;
987         gchar *p;
988
989         if (!t)
990                 return FALSE;
991         
992         memset(t, 0, sizeof *t);        
993
994         if (procheader_scan_date_string(src, weekday, &day, month, &year,
995                                         &hh, &mm, &ss, zone) < 0) {
996                 g_warning("Invalid date: %s", src);
997                 return FALSE;
998         }
999
1000         /* Y2K compliant :) */
1001         if (year < 100) {
1002                 if (year < 70)
1003                         year += 2000;
1004                 else
1005                         year += 1900;
1006         }
1007
1008         month[3] = '\0';
1009         if ((p = strstr(monthstr, month)) != NULL)
1010                 dmonth = (gint)(p - monthstr) / 3 + 1;
1011         else {
1012                 g_warning("Invalid month: %s", month);
1013                 dmonth = G_DATE_BAD_MONTH;
1014         }
1015
1016         t->tm_sec = ss;
1017         t->tm_min = mm;
1018         t->tm_hour = hh;
1019         t->tm_mday = day;
1020         t->tm_mon = dmonth - 1;
1021         t->tm_year = year - 1900;
1022         t->tm_wday = 0;
1023         t->tm_yday = 0;
1024         t->tm_isdst = -1;
1025
1026         mktime(t);
1027
1028         return TRUE;
1029 }
1030
1031 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
1032 {
1033         gchar weekday[11];
1034         gint day;
1035         gchar month[10];
1036         gint year;
1037         gint hh, mm, ss;
1038         gchar zone[7];
1039         GDateMonth dmonth = G_DATE_BAD_MONTH;
1040         gchar *p;
1041         time_t timer;
1042
1043         if (procheader_scan_date_string(src, weekday, &day, month, &year,
1044                                         &hh, &mm, &ss, zone) < 0) {
1045                 if (dest && len > 0)
1046                         strncpy2(dest, src, len);
1047                 return 0;
1048         }
1049
1050         month[3] = '\0';
1051         for (p = monthstr; *p != '\0'; p += 3) {
1052                 if (!g_ascii_strncasecmp(p, month, 3)) {
1053                         dmonth = (gint)(p - monthstr) / 3 + 1;
1054                         break;
1055                 }
1056         }
1057
1058 #ifdef G_OS_WIN32
1059         GTimeZone *tz;
1060         GDateTime *dt, *dt2;
1061
1062         tz = g_time_zone_new(zone); // can't return NULL no need to check for it
1063         dt = g_date_time_new(tz, 1, 1, 1, 0, 0, 0);
1064         g_time_zone_unref(tz);
1065         dt2 = g_date_time_add_full(dt, year-1, dmonth-1, day-1, hh, mm, ss);
1066         g_date_time_unref(dt);
1067
1068         timer = g_date_time_to_unix(dt2);
1069         g_date_time_unref(dt2);
1070
1071 #else
1072         struct tm t;
1073         time_t tz_offset;
1074
1075         /* Y2K compliant :) */
1076         if (year < 1000) {
1077                 if (year < 50)
1078                         year += 2000;
1079                 else
1080                         year += 1900;
1081         }
1082
1083         t.tm_sec = ss;
1084         t.tm_min = mm;
1085         t.tm_hour = hh;
1086         t.tm_mday = day;
1087         t.tm_mon = dmonth - 1;
1088         t.tm_year = year - 1900;
1089         t.tm_wday = 0;
1090         t.tm_yday = 0;
1091         t.tm_isdst = -1;
1092
1093         timer = mktime(&t);
1094         tz_offset = remote_tzoffset_sec(zone);
1095         if (tz_offset != -1)
1096                 timer += tzoffset_sec(&timer) - tz_offset;
1097
1098         if (dest)
1099                 procheader_date_get_localtime(dest, len, timer);
1100 #endif
1101
1102         return timer;
1103 }
1104
1105 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
1106 {
1107         struct tm *lt;
1108         gchar *default_format = "%y/%m/%d(%a) %H:%M";
1109         gchar *str;
1110         const gchar *src_codeset, *dest_codeset;
1111         struct tm buf;
1112
1113         if (timer > 0)
1114                 lt = localtime_r(&timer, &buf);
1115         else {
1116                 time_t dummy = 1;
1117                 lt = localtime_r(&dummy, &buf);
1118         }
1119
1120         if (prefs_common.date_format)
1121                 fast_strftime(dest, len, prefs_common.date_format, lt);
1122         else
1123                 fast_strftime(dest, len, default_format, lt);
1124
1125         if (!g_utf8_validate(dest, -1, NULL)) {
1126                 src_codeset = conv_get_locale_charset_str_no_utf8();
1127                 dest_codeset = CS_UTF_8;
1128                 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
1129                 if (str) {
1130                         strncpy2(dest, str, len);
1131                         g_free(str);
1132                 }
1133         }
1134 }
1135
1136 /* Added by Mel Hadasht on 27 Aug 2001 */
1137 /* Get a header from msginfo */
1138 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar **buf, gchar *header)
1139 {
1140         gchar *file;
1141         FILE *fp;
1142         HeaderEntry hentry[]={ { NULL, NULL, TRUE  },
1143                                                    { NULL, NULL, FALSE } };
1144         gint val;
1145
1146         cm_return_val_if_fail(msginfo != NULL, -1);
1147         cm_return_val_if_fail(buf != NULL, -1);
1148         cm_return_val_if_fail(header != NULL, -1);
1149
1150         hentry[0].name = header;
1151
1152         file = procmsg_get_message_file_path(msginfo);
1153         if ((fp = g_fopen(file, "rb")) == NULL) {
1154                 FILE_OP_ERROR(file, "fopen");
1155                 g_free(file);
1156                 g_free(*buf);
1157                 *buf = NULL;
1158                 return -1;
1159         }
1160         val = procheader_get_one_field(buf, fp, hentry);
1161
1162         if (fclose(fp) == EOF) {
1163                 FILE_OP_ERROR(file, "fclose");
1164                 claws_unlink(file);
1165                 g_free(file);
1166                 g_free(*buf);
1167                 *buf = NULL;
1168                 return -1;
1169         }
1170
1171         g_free(file);
1172         if (val == -1) {
1173                 /* *buf is already NULL in that case, see procheader_get_one_field() */
1174                 return -1;
1175         }
1176
1177         return 0;
1178 }
1179
1180 HeaderEntry *procheader_entries_from_str(const gchar *str)
1181 {
1182         HeaderEntry *entries = NULL, *he;
1183         int numh = 0, i = 0;
1184         gchar **names = NULL;
1185         const gchar *s = str;
1186
1187         if (s == NULL) {
1188                 return NULL;
1189         }
1190         while (*s != '\0') {
1191                 if (*s == ' ') ++numh;
1192                 ++s;
1193         }
1194         if (numh == 0) {
1195                 return NULL;
1196         }
1197         entries = g_new0(HeaderEntry, numh + 1); /* room for last NULL */
1198         s = str;
1199         ++s; /* skip first space */
1200         names = g_strsplit(s, " ", numh);
1201         he = entries;
1202         while (names[i]) {
1203                 he->name = g_strdup_printf("%s:", names[i]);
1204                 he->body = NULL;
1205                 he->unfold = FALSE;
1206                 ++i, ++he;
1207         }
1208         he->name = NULL;
1209         g_strfreev(names);
1210         return entries;
1211 }
1212
1213 void procheader_entries_free (HeaderEntry *entries)
1214 {
1215         if (entries != NULL) {
1216                 HeaderEntry *he = entries;
1217                 while (he->name != NULL) {
1218                         g_free(he->name);
1219                         if (he->body != NULL)
1220                                 g_free(he->body);
1221                         ++he;                   
1222                 }
1223                 g_free(entries);
1224         }
1225 }
1226
1227 gboolean procheader_header_is_internal(const gchar *hdr_name)
1228 {
1229         const gchar *internal_hdrs[] = {
1230                 "AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:", "CFG:",
1231                 "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:", "R:", "MAID:",
1232                 "SCF:", "RMID:", "FMID:", "NAID:",
1233                 "X-Claws-Account-Id:",
1234                 "X-Claws-Sign:",
1235                 "X-Claws-Encrypt:",
1236                 "X-Claws-Privacy-System:",
1237                 "X-Claws-Auto-Wrapping:",
1238                 "X-Claws-Auto-Indent:",
1239                 "X-Claws-End-Special-Headers:",
1240                 "X-Sylpheed-Account-Id:",
1241                 "X-Sylpheed-Sign:",
1242                 "X-Sylpheed-Encrypt:",
1243                 "X-Sylpheed-Privacy-System:",
1244                 "X-Sylpheed-End-Special-Headers:",
1245                  NULL
1246         };
1247         int i;
1248
1249         for (i = 0; internal_hdrs[i]; i++) {
1250                 if (!strcmp(hdr_name, internal_hdrs[i]))
1251                         return TRUE;
1252         }
1253         return FALSE;
1254 }