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