scoring & bugfix for filtering
[claws.git] / src / procheader.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2001 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <time.h>
29
30 #include "intl.h"
31 #include "procheader.h"
32 #include "procmsg.h"
33 #include "codeconv.h"
34 #include "prefs_common.h"
35 #include "utils.h"
36
37 #define BUFFSIZE        8192
38
39 /*
40   procheader_get_one_field
41   - reads fp and puts the header and the corresponding content into buf
42     if one of these is one of hentry table.
43   - if hentry is NULL, ignores no headers
44  */
45
46 gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
47                               HeaderEntry hentry[])
48 {
49         gint nexthead;
50         gint hnum = 0;
51         HeaderEntry *hp = NULL;
52
53         if (hentry != NULL) {
54                 /* skip non-required headers */
55                 do {
56                         do {
57                                 if (fgets(buf, len, fp) == NULL)
58                                         return -1;
59                                 if (buf[0] == '\r' || buf[0] == '\n')
60                                         return -1;
61                         } while (buf[0] == ' ' || buf[0] == '\t');
62
63                         for (hp = hentry, hnum = 0; hp->name != NULL;
64                              hp++, hnum++) {
65                                 if (!strncasecmp(hp->name, buf,
66                                                  strlen(hp->name)))
67                                         break;
68                         }
69                 } while (hp->name == NULL);
70         } else {
71                 if (fgets(buf, len, fp) == NULL) return -1;
72                 if (buf[0] == '\r' || buf[0] == '\n') return -1;
73         }
74
75         /* unfold the specified folded line */
76         if (hp && hp->unfold) {
77                 gboolean folded = FALSE;
78                 gchar *bufp = buf + strlen(buf);
79
80                 while (1) {
81                         nexthead = fgetc(fp);
82
83                         /* folded */
84                         if (nexthead == ' ' || nexthead == '\t')
85                                 folded = TRUE;
86                         else if (nexthead == EOF)
87                                 break;
88                         else if (folded == TRUE) {
89                                 /* concatenate next line */
90                                 if ((len - (bufp - buf)) <= 2) break;
91
92                                 /* replace return code on the tail end
93                                    with space */
94                                 *(bufp - 1) = ' ';
95                                 *bufp++ = nexthead;
96                                 *bufp = '\0';
97                                 if (nexthead == '\r' || nexthead == '\n') {
98                                         folded = FALSE;
99                                         continue;
100                                 }
101                                 if (fgets(bufp, len - (bufp - buf), fp)
102                                     == NULL) break;
103                                 bufp += strlen(bufp);
104
105                                 folded = FALSE;
106                         } else {
107                                 ungetc(nexthead, fp);
108                                 break;
109                         }
110                 }
111
112                 /* remove trailing return code */
113                 strretchomp(buf);
114
115                 return hnum;
116         }
117
118         while (1) {
119                 nexthead = fgetc(fp);
120                 if (nexthead == ' ' || nexthead == '\t') {
121                         size_t buflen = strlen(buf);
122
123                         /* concatenate next line */
124                         if ((len - buflen) > 2) {
125                                 gchar *p = buf + buflen;
126
127                                 *p++ = nexthead;
128                                 *p = '\0';
129                                 buflen++;
130                                 if (fgets(p, len - buflen, fp) == NULL)
131                                         break;
132                         } else
133                                 break;
134                 } else {
135                         if (nexthead != EOF)
136                                 ungetc(nexthead, fp);
137                         break;
138                 }
139         }
140
141         /* remove trailing return code */
142         strretchomp(buf);
143
144         return hnum;
145 }
146
147 gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
148 {
149         gboolean folded = FALSE;
150         gint nexthead;
151         gchar *bufp;
152
153         if (fgets(buf, len, fp) == NULL) return NULL;
154         if (buf[0] == '\r' || buf[0] == '\n') return NULL;
155         bufp = buf + strlen(buf);
156
157         while (1) {
158                 nexthead = fgetc(fp);
159
160                 /* folded */
161                 if (nexthead == ' ' || nexthead == '\t')
162                         folded = TRUE;
163                 else if (nexthead == EOF)
164                         break;
165                 else if (folded == TRUE) {
166                         /* concatenate next line */
167                         if ((len - (bufp - buf)) <= 2) break;
168
169                         /* replace return code on the tail end
170                            with space */
171                         *(bufp - 1) = ' ';
172                         *bufp++ = nexthead;
173                         *bufp = '\0';
174                         if (nexthead == '\r' || nexthead == '\n') {
175                                 folded = FALSE;
176                                 continue;
177                         }
178                         if (fgets(bufp, len - (bufp - buf), fp)
179                             == NULL) break;
180                         bufp += strlen(bufp);
181
182                         folded = FALSE;
183                 } else {
184                         ungetc(nexthead, fp);
185                         break;
186                 }
187         }
188
189         /* remove trailing return code */
190         strretchomp(buf);
191
192         return buf;
193 }
194
195 /*
196   tests whether two headers' names are equal
197   remove the trailing ':' or ' ' before comparing
198 */
199
200 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
201 {
202         int len1;
203         int len2;
204
205         len1 = strlen(hdr1);
206         len2 = strlen(hdr2);
207         if ((hdr1[len1 - 1] == ':') || (hdr1[len1 - 1] == ' '))
208                 len1--;
209         if ((hdr2[len2 - 1] == ':') || (hdr2[len2 - 1] == ' '))
210                 len2--;
211         if (len1 != len2)
212                 return 0;
213         return (strncasecmp(hdr1, hdr2, len1) == 0);
214 }
215
216 void procheader_header_free(Header * header)
217 {
218         g_free(header->name);
219         g_free(header->body);
220         g_free(header);
221 }
222
223 /*
224   parse headers, for example :
225   From: dinh@enseirb.fr becomes :
226   header->name = "From:"
227   header->body = "dinh@enseirb.fr"
228  */
229
230 Header * procheader_parse_header(gchar * buf)
231 {
232         gchar tmp[BUFFSIZE];
233         gchar *p = buf;
234         Header * header;
235
236         if ((*buf == ':') || (*buf == ' '))
237                 return NULL;
238
239         for (p = buf; *p ; p++) {
240                 if ((*p == ':') || (*p == ' ')) {
241                         header = g_new(Header, 1);
242                         header->name = g_strndup(buf, p - buf + 1);
243                         p++;
244                         while (*p == ' ' || *p == '\t') p++;
245                         conv_unmime_header(tmp, sizeof(tmp), p, NULL);
246                         header->body = g_strdup(tmp);
247                         return header;
248                 }
249         }
250         return NULL;
251 }
252
253 GSList *procheader_get_header_list(const gchar *file)
254 {
255         FILE *fp;
256         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
257         gchar *p;
258         GSList *hlist = NULL;
259         Header *header;
260
261         if ((fp = fopen(file, "r")) == NULL) {
262                 FILE_OP_ERROR(file, "fopen");
263                 return NULL;
264         }
265
266         while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
267                 header = procheader_parse_header(buf);
268                 if (header != NULL)
269                         hlist = g_slist_append(hlist, header);
270         }
271
272         fclose(fp);
273         return hlist;
274 }
275
276 void procheader_header_list_destroy(GSList *hlist)
277 {
278         Header *header;
279
280         while (hlist != NULL) {
281                 header = hlist->data;
282
283                 procheader_header_free(header);
284                 hlist = g_slist_remove(hlist, header);
285         }
286 }
287
288 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
289 {
290         gchar buf[BUFFSIZE];
291         HeaderEntry *hp;
292         gint hnum;
293         gchar *p;
294
295         if (hentry == NULL) return;
296
297         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
298                != -1) {
299                 hp = hentry + hnum;
300
301                 p = buf + strlen(hp->name);
302                 while (*p == ' ' || *p == '\t') p++;
303
304                 if (hp->body == NULL)
305                         hp->body = g_strdup(p);
306                 else if (!strcasecmp(hp->name, "To:") ||
307                          !strcasecmp(hp->name, "Cc:")) {
308                         gchar *tp = hp->body;
309                         hp->body = g_strconcat(tp, ", ", p, NULL);
310                         g_free(tp);
311                 }
312         }
313 }
314
315 enum
316 {
317         H_DATE          = 0,
318         H_FROM          = 1,
319         H_TO            = 2,
320         H_CC            = 3,
321         H_NEWSGROUPS    = 4,
322         H_SUBJECT       = 5,
323         H_MSG_ID        = 6,
324         H_REFERENCES    = 7,
325         H_IN_REPLY_TO   = 8,
326         H_CONTENT_TYPE  = 9,
327         H_SEEN          = 10,
328         H_X_FACE        = 11,
329         H_DISPOSITION_NOTIFICATION_TO = 12
330 };
331
332 MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
333 {
334         static HeaderEntry hentry_full[] = {{"Date:",           NULL, FALSE},
335                                            {"From:",            NULL, TRUE},
336                                            {"To:",              NULL, TRUE},
337                                            {"Cc:",              NULL, TRUE},
338                                            {"Newsgroups:",      NULL, TRUE},
339                                            {"Subject:",         NULL, TRUE},
340                                            {"Message-Id:",      NULL, FALSE},
341                                            {"References:",      NULL, FALSE},
342                                            {"In-Reply-To:",     NULL, FALSE},
343                                            {"Content-Type:",    NULL, FALSE},
344                                            {"Seen:",            NULL, FALSE},
345                                            {"X-Face:",          NULL, FALSE},
346                                            {"Disposition-Notification-To:",NULL, FALSE},
347                                            {NULL,               NULL, FALSE}};
348
349         static HeaderEntry hentry_short[] = {{"Date:",          NULL, FALSE},
350                                             {"From:",           NULL, TRUE},
351                                             {"To:",             NULL, TRUE},
352                                             {"Cc:",             NULL, TRUE},
353                                             {"Newsgroups:",     NULL, TRUE},
354                                             {"Subject:",        NULL, TRUE},
355                                             {"Message-Id:",     NULL, FALSE},
356                                             {"References:",     NULL, FALSE},
357                                             {"In-Reply-To:",    NULL, FALSE},
358                                             {"Content-Type:",   NULL, FALSE},
359                                             {"Seen:",           NULL, FALSE},
360                                             {NULL,              NULL, FALSE}};
361
362         FILE *fp;
363         MsgInfo *msginfo;
364         gchar buf[BUFFSIZE], tmp[BUFFSIZE];
365         gchar *reference = NULL;
366         gchar *p;
367         gchar *hp;
368         HeaderEntry *hentry;
369         gint hnum;
370
371         hentry = full ? hentry_full : hentry_short;
372
373         if ((fp = fopen(file, "r")) == NULL) {
374                 FILE_OP_ERROR(file, "fopen");
375                 return NULL;
376         }
377         if (MSG_IS_QUEUED(flags)) {
378                 while (fgets(buf, sizeof(buf), fp) != NULL)
379                         if (buf[0] == '\r' || buf[0] == '\n') break;
380         }
381
382         msginfo = g_new0(MsgInfo, 1);
383         msginfo->flags = flags != 0 ? flags : MSG_NEW|MSG_UNREAD;
384         msginfo->inreplyto = NULL;
385
386         while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
387                != -1) {
388                 hp = buf + strlen(hentry[hnum].name);
389                 while (*hp == ' ' || *hp == '\t') hp++;
390
391                 switch (hnum) {
392                 case H_DATE:
393                         if (msginfo->date) break;
394                         msginfo->date_t =
395                                 procheader_date_parse(NULL, hp, 0);
396                         msginfo->date = g_strdup(hp);
397                         break;
398                 case H_FROM:
399                         if (msginfo->from) break;
400                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
401                         msginfo->from = g_strdup(tmp);
402                         msginfo->fromname = procheader_get_fromname(tmp);
403                         break;
404                 case H_TO:
405                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
406                         if (msginfo->to) {
407                                 p = msginfo->to;
408                                 msginfo->to =
409                                         g_strconcat(p, ", ", tmp, NULL);
410                                 g_free(p);
411                         } else
412                                 msginfo->to = g_strdup(tmp);
413                         break;
414                 case H_CC:
415                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
416                         if (msginfo->cc) {
417                                 p = msginfo->cc;
418                                 msginfo->cc =
419                                         g_strconcat(p, ", ", tmp, NULL);
420                                 g_free(p);
421                         } else
422                                 msginfo->cc = g_strdup(tmp);
423                         break;
424                 case H_NEWSGROUPS:
425                         if (msginfo->newsgroups) {
426                                 p = msginfo->newsgroups;
427                                 msginfo->newsgroups =
428                                         g_strconcat(p, ",", hp, NULL);
429                                 g_free(p);
430                         } else
431                                 msginfo->newsgroups = g_strdup(buf + 12);
432                         break;
433                 case H_SUBJECT:
434                         if (msginfo->subject) break;
435                         conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
436                         msginfo->subject = g_strdup(tmp);
437                         break;
438                 case H_MSG_ID:
439                         if (msginfo->msgid) break;
440
441                         extract_parenthesis(hp, '<', '>');
442                         remove_space(hp);
443                         msginfo->msgid = g_strdup(hp);
444                         break;
445                 case H_REFERENCES:
446                         if (!reference) {
447                                 eliminate_parenthesis(hp, '(', ')');
448                                 if ((p = strrchr(hp, '<')) != NULL &&
449                                     strchr(p + 1, '>') != NULL) {
450                                         extract_parenthesis(p, '<', '>');
451                                         remove_space(p);
452                                         if (*p != '\0')
453                                                 reference = g_strdup(p);
454                                 }
455                         }
456                         break;
457                 case H_IN_REPLY_TO:
458                         if (!reference) {
459                                 eliminate_parenthesis(hp, '(', ')');
460                                 extract_parenthesis(hp, '<', '>');
461                                 remove_space(hp);
462                                 if (*hp != '\0')
463                                         reference = g_strdup(hp);
464                         }
465                         break;
466                 case H_CONTENT_TYPE:
467                         if (!strncasecmp(hp, "multipart", 9))
468                                 msginfo->flags |= MSG_MIME;
469                         break;
470                 case H_SEEN:
471                         /* mnews Seen header */
472                         MSG_UNSET_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
473                         break;
474                 case H_X_FACE:
475                         if (msginfo->xface) break;
476                         msginfo->xface = g_strdup(hp);
477                         break;
478                 case H_DISPOSITION_NOTIFICATION_TO:
479                         if (msginfo->dispositionnotificationto) break;
480                         msginfo->dispositionnotificationto = g_strdup(hp);
481                         break;
482                 default:
483                 }
484         }
485         msginfo->inreplyto = reference;
486
487         fclose(fp);
488
489         return msginfo;
490 }
491
492 gchar *procheader_get_fromname(const gchar *str)
493 {
494         gchar *tmp, *name;
495
496         Xalloca(tmp, strlen(str) + 1, return NULL);
497         strcpy(tmp, str);
498
499         if (*tmp == '\"') {
500                 extract_quote(tmp, '\"');
501                 g_strstrip(tmp);
502         } else if (strchr(tmp, '<')) {
503                 eliminate_parenthesis(tmp, '<', '>');
504                 g_strstrip(tmp);
505                 if (*tmp == '\0') {
506                         strcpy(tmp, str);
507                         extract_parenthesis(tmp, '<', '>');
508                         g_strstrip(tmp);
509                 }
510         } else if (strchr(tmp, '(')) {
511                 extract_parenthesis(tmp, '(', ')');
512                 g_strstrip(tmp);
513         }
514
515         if (*tmp == '\0')
516                 name = g_strdup(str);
517         else
518                 name = g_strdup(tmp);
519
520         return name;
521 }
522
523 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
524 {
525         static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
526         gchar weekday[4];
527         gint day;
528         gchar month[4];
529         gint year;
530         gint hh, mm, ss;
531         gchar zone[6];
532         gint result;
533         GDateMonth dmonth;
534         struct tm t;
535         gchar *p;
536         time_t timer;
537
538         /* parsing date field... */
539         result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
540                         weekday, &day, month, &year, &hh, &mm, &ss, zone);
541         if (result != 8) {
542                 result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
543                                 &day, month, &year, &hh, &mm, &ss, zone);
544                 if (result != 7) {
545                         ss = 0;
546                         result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
547                                         weekday, &day, month, &year, &hh, &mm, zone);
548                         if (result != 7) {
549                                 result = sscanf(src, "%d %3s %d %2d:%2d %5s",
550                                                 &day, month, &year, &hh, &mm,
551                                                 zone);
552                                 if (result != 6) {
553                                         g_warning("Invalid date: %s\n", src);
554                                         if (dest && len > 0)
555                                                 strncpy2(dest, src, len);
556                                         return 0;
557                                 }
558                         }
559                 }
560         }
561
562         /* Y2K compliant :) */
563         if (year < 100) {
564                 if (year < 70)
565                         year += 2000;
566                 else
567                         year += 1900;
568         }
569
570         if ((p = strstr(monthstr, month)) != NULL)
571                 dmonth = (gint)(p - monthstr) / 3 + 1;
572         else {
573                 g_warning("Invalid month: %s\n", month);
574                 dmonth = G_DATE_BAD_MONTH;
575         }
576
577         t.tm_sec = ss;
578         t.tm_min = mm;
579         t.tm_hour = hh;
580         t.tm_mday = day;
581         t.tm_mon = dmonth - 1;
582         t.tm_year = year - 1900;
583         t.tm_wday = 0;
584         t.tm_yday = 0;
585         t.tm_isdst = -1;
586
587         timer = mktime(&t);
588         timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
589
590         if (dest)
591                 procheader_date_get_localtime(dest, len, timer);
592
593         return timer;
594 }
595
596 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
597 {
598         struct tm *lt;
599         gchar *default_format = "%y/%m/%d(%a) %H:%M";
600
601         lt = localtime(&timer);
602
603         if (prefs_common.date_format)
604                 strftime(dest, len, prefs_common.date_format, lt);
605         else
606                 strftime(dest, len, default_format, lt);
607 }