2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2014 Hiroyuki Yamamoto and the Claws Mail team
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.
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.
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/>.
22 #include "claws-features.h"
26 #include <glib/gi18n.h>
37 #include "procheader.h"
40 #include "prefs_common.h"
47 static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
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[]);
54 static gint string_get_one_field(gchar **buf, char **str,
55 HeaderEntry hentry[]);
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,
63 peekcharfunc peekchar,
65 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
66 gboolean full, gboolean decrypted);
69 gint procheader_get_one_field(gchar **buf, FILE *fp,
72 return generic_get_one_field(buf, fp, hentry,
73 (getlinefunc)fgets_crlf, (peekcharfunc)file_peekchar,
77 static gint string_get_one_field(gchar **buf, char **str,
80 return generic_get_one_field(buf, str, hentry,
81 (getlinefunc)string_getline,
82 (peekcharfunc)string_peekchar,
86 static char *string_getline(char *buf, size_t len, char **str)
88 gboolean is_cr = FALSE;
89 gboolean last_was_cr = FALSE;
94 for (; **str && len > 1; --len) {
95 is_cr = (**str == '\r');
96 if ((*buf++ = *(*str)++) == '\n') {
112 static int string_peekchar(char **str)
117 static int file_peekchar(FILE *fp)
119 return ungetc(getc(fp), fp);
122 static gint generic_get_one_field(gchar **bufptr, void *data,
124 getlinefunc getline, peekcharfunc peekchar,
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
134 HeaderEntry *hp = NULL;
138 cm_return_val_if_fail(bufptr != NULL, -1);
143 if (hentry != NULL) {
144 /* skip non-required headers */
145 /* and get hentry header line */
148 if (getline(buf, len, data) == NULL) {
149 debug_print("generic_get_one_field: getline\n");
154 if (buf[0] == '\r' || buf[0] == '\n') {
155 debug_print("generic_get_one_field: empty line\n");
160 } while (buf[0] == ' ' || buf[0] == '\t');
162 for (hp = hentry, hnum = 0; hp->name != NULL;
164 if (!g_ascii_strncasecmp(hp->name, buf,
168 } while (hp->name == NULL);
170 /* read first line */
171 if (getline(buf, len, data) == NULL) {
172 debug_print("generic_get_one_field: getline\n");
177 if (buf[0] == '\r' || buf[0] == '\n') {
178 debug_print("generic_get_one_field: empty line\n");
184 /* reduce initial buffer to its useful part */
186 buf = g_realloc(buf, len);
188 debug_print("generic_get_one_field: reallocation error\n");
195 nexthead = peekchar(data);
196 /* ([*WSP CRLF] 1*WSP) */
197 if (nexthead == ' ' || nexthead == '\t') {
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))
208 buflen = strlen(buf);
211 tmpbuf = g_malloc(BUFFSIZE);
213 if (getline(tmpbuf, BUFFSIZE, data) == NULL) {
217 tmplen = strlen(tmpbuf)+1;
219 /* extend initial buffer and concatenate next line */
221 buf = g_realloc(buf, len);
223 debug_print("generic_get_one_field: reallocation error\n");
228 memcpy(buf+buflen, tmpbuf, tmplen);
230 if (skiptab) { /* replace tab with space */
231 *(buf + buflen) = ' ';
234 /* remove trailing new line */
245 gint procheader_get_one_field_asis(gchar **buf, FILE *fp)
247 return generic_get_one_field(buf, fp, NULL,
248 (getlinefunc)fgets_crlf,
249 (peekcharfunc)file_peekchar,
253 GPtrArray *procheader_get_header_array_asis(FILE *fp)
259 cm_return_val_if_fail(fp != NULL, NULL);
261 headers = g_ptr_array_new();
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);
273 void procheader_header_array_destroy(GPtrArray *harray)
278 cm_return_if_fail(harray != NULL);
280 for (i = 0; i < harray->len; i++) {
281 header = g_ptr_array_index(harray, i);
282 procheader_header_free(header);
285 g_ptr_array_free(harray, TRUE);
288 void procheader_header_free(Header *header)
292 g_free(header->name);
293 g_free(header->body);
298 tests whether two headers' names are equal
299 remove the trailing ':' or ' ' before comparing
302 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
309 if (hdr1[len1 - 1] == ':')
311 if (hdr2[len2 - 1] == ':')
316 return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
320 parse headers, for example :
321 From: dinh@enseirb.fr becomes :
322 header->name = "From:"
323 header->body = "dinh@enseirb.fr"
325 static gboolean header_is_addr_field(const gchar *hdr)
327 static char *addr_headers[] = {
334 "Followup-and-Reply-To:",
335 "Disposition-Notification-To:",
336 "Return-Receipt-To:",
343 for (i = 0; addr_headers[i] != NULL; i++)
344 if (!strcasecmp(hdr, addr_headers[i]))
350 Header * procheader_parse_header(gchar * buf)
354 gboolean addr_field = FALSE;
356 cm_return_val_if_fail(buf != NULL, NULL);
358 if ((*buf == ':') || (*buf == ' '))
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);
367 while (*p == ' ' || *p == '\t') p++;
368 header->body = conv_unmime_header(p, NULL, addr_field);
375 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
382 if (hentry == NULL) return;
384 while ((hnum = procheader_get_one_field(&buf, fp, hentry)) != -1) {
387 p = buf + strlen(hp->name);
388 while (*p == ' ' || *p == '\t') p++;
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);
403 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
404 gboolean full, gboolean decrypted)
410 if (g_stat(file, &s) < 0) {
411 FILE_OP_ERROR(file, "stat");
414 if (!S_ISREG(s.st_mode))
417 if ((fp = g_fopen(file, "rb")) == NULL) {
418 FILE_OP_ERROR(file, "fopen");
422 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
426 msginfo->size = s.st_size;
427 msginfo->mtime = s.st_mtime;
433 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
436 return parse_stream(&str, TRUE, flags, full, decrypted);
454 H_SC_PLANNED_DOWNLOAD,
458 H_DISPOSITION_NOTIFICATION_TO,
460 H_SC_PARTIALLY_RETRIEVED,
472 static HeaderEntry hentry_full[] = {
473 {"Date:", NULL, FALSE},
474 {"From:", 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}};
504 static HeaderEntry hentry_short[] = {
505 {"Date:", NULL, FALSE},
506 {"From:", 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}};
522 static HeaderEntry* procheader_get_headernames(gboolean full)
524 return full ? hentry_full : hentry_short;
527 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
530 return parse_stream(fp, FALSE, flags, full, decrypted);
533 static gboolean avatar_from_some_face(gpointer source, gpointer userdata)
535 AvatarCaptureData *acd = (AvatarCaptureData *)source;
537 if (*(acd->content) == '\0') /* won't be null, but may be empty */
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);
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);
553 static guint avatar_hook_id = 0;
555 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
556 gboolean full, gboolean decrypted)
564 void *orig_data = data;
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;
570 hentry = procheader_get_headernames(full);
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:"))))
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)) {
587 rewind((FILE *)data);
597 msginfo = procmsg_msginfo_new();
599 if (flags.tmp_flags || flags.perm_flags)
600 msginfo->flags = flags;
602 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
604 msginfo->inreplyto = NULL;
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);
613 while ((hnum = get_one_field(&buf, data, hentry)) != -1) {
614 hp = buf + strlen(hentry[hnum].name);
615 while (*hp == ' ' || *hp == '\t') hp++;
619 if (msginfo->date) break;
621 procheader_date_parse(NULL, hp, 0);
622 if (g_utf8_validate(hp, -1, NULL)) {
623 msginfo->date = g_strdup(hp);
625 gchar *utf = conv_codeset_strdup(
627 conv_get_locale_charset_str_no_utf8(),
630 !g_utf8_validate(utf, -1, NULL)) {
632 utf = g_malloc(strlen(buf)*2+1);
633 conv_localetodisp(utf,
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);
647 tmp = conv_unmime_header(hp, NULL, TRUE);
652 g_strconcat(p, ", ", tmp, NULL);
655 msginfo->to = g_strdup(tmp);
659 tmp = conv_unmime_header(hp, NULL, TRUE);
664 g_strconcat(p, ", ", tmp, NULL);
667 msginfo->cc = g_strdup(tmp);
671 if (msginfo->newsgroups) {
672 p = msginfo->newsgroups;
673 msginfo->newsgroups =
674 g_strconcat(p, ",", hp, NULL);
677 msginfo->newsgroups = g_strdup(hp);
680 if (msginfo->subject) break;
681 msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
682 unfold_line(msginfo->subject);
685 if (msginfo->msgid) break;
687 extract_parenthesis(hp, '<', '>');
689 msginfo->msgid = g_strdup(hp);
692 msginfo->references =
693 references_list_prepend(msginfo->references,
697 if (msginfo->inreplyto) break;
699 eliminate_parenthesis(hp, '(', ')');
700 if ((p = strrchr(hp, '<')) != NULL &&
701 strchr(p + 1, '>') != NULL) {
702 extract_parenthesis(p, '<', '>');
705 msginfo->inreplyto = g_strdup(p);
709 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
710 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
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);
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);
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);
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);
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);
743 case H_SC_MESSAGE_SIZE:
744 if (msginfo->total_size) break;
745 msginfo->total_size = atoi(hp);
747 case H_SC_PLANNED_DOWNLOAD:
748 msginfo->planned_download = atoi(hp);
750 /* end partial download infos */
752 if (msginfo->fromspace) break;
753 msginfo->fromspace = g_strdup(hp);
754 remove_return(msginfo->fromspace);
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);
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);
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);
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);
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);
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);
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);
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;
812 hooks_invoke(AVATAR_HEADER_UPDATE_HOOKLIST, (gpointer)acd);
819 if (!msginfo->inreplyto && msginfo->references)
821 g_strdup((gchar *)msginfo->references->data);
826 gchar *procheader_get_fromname(const gchar *str)
830 Xstrdup_a(tmp, str, return NULL);
833 extract_quote(tmp, '\"');
835 } else if (strchr(tmp, '<')) {
836 eliminate_parenthesis(tmp, '<', '>');
840 extract_parenthesis(tmp, '<', '>');
843 } else if (strchr(tmp, '(')) {
844 extract_parenthesis(tmp, '(', ')');
849 name = g_strdup(str);
851 name = g_strdup(tmp);
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,
865 gint zone1 = 0, zone2 = 0;
866 gchar offset_sign, zonestr[7];
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;
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;
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;
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;
890 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
891 day, month, year, hh, mm, ss);
892 if (result == 6) return 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;
899 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
900 day, month, year, hh, mm, zone);
901 if (result == 6) return 0;
904 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
905 weekday, day, month, year, hh, mm);
906 if (result == 6) return 0;
908 result = sscanf(str, "%d %9s %d %2d:%2d",
909 day, month, year, hh, mm);
910 if (result == 5) return 0;
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);
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);
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);
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);
951 /* This particular "subset" is invalid, RFC requires the time offset */
952 result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d",
953 year, &month_n, day, hh, mm, ss);
955 if (1 <= month_n && month_n <= 12) {
956 strncpy2(month, monthstr+((month_n-1)*3), 4);
965 * Hiro, most UNIXen support this function:
966 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
968 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
981 memset(t, 0, sizeof *t);
983 if (procheader_scan_date_string(src, weekday, &day, month, &year,
984 &hh, &mm, &ss, zone) < 0) {
985 g_warning("Invalid date: %s", src);
989 /* Y2K compliant :) */
998 if ((p = strstr(monthstr, month)) != NULL)
999 dmonth = (gint)(p - monthstr) / 3 + 1;
1001 g_warning("Invalid month: %s", month);
1002 dmonth = G_DATE_BAD_MONTH;
1009 t->tm_mon = dmonth - 1;
1010 t->tm_year = year - 1900;
1020 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
1028 GDateMonth dmonth = G_DATE_BAD_MONTH;
1032 if (procheader_scan_date_string(src, weekday, &day, month, &year,
1033 &hh, &mm, &ss, zone) < 0) {
1034 if (dest && len > 0)
1035 strncpy2(dest, src, len);
1040 for (p = monthstr; *p != '\0'; p += 3) {
1041 if (!g_ascii_strncasecmp(p, month, 3)) {
1042 dmonth = (gint)(p - monthstr) / 3 + 1;
1051 tz = g_time_zone_new(zone);
1052 dt = g_date_time_new(tz, year, dmonth, day, hh, mm, ss);
1054 timer = g_date_time_to_unix(dt);
1056 g_date_time_unref(dt);
1057 g_time_zone_unref(tz);
1063 /* Y2K compliant :) */
1075 t.tm_mon = dmonth - 1;
1076 t.tm_year = year - 1900;
1082 tz_offset = remote_tzoffset_sec(zone);
1083 if (tz_offset != -1)
1084 timer += tzoffset_sec(&timer) - tz_offset;
1087 procheader_date_get_localtime(dest, len, timer);
1093 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
1096 gchar *default_format = "%y/%m/%d(%a) %H:%M";
1098 const gchar *src_codeset, *dest_codeset;
1102 lt = localtime_r(&timer, &buf);
1105 lt = localtime_r(&dummy, &buf);
1108 if (prefs_common.date_format)
1109 fast_strftime(dest, len, prefs_common.date_format, lt);
1111 fast_strftime(dest, len, default_format, lt);
1113 if (!g_utf8_validate(dest, -1, NULL)) {
1114 src_codeset = conv_get_locale_charset_str_no_utf8();
1115 dest_codeset = CS_UTF_8;
1116 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
1118 strncpy2(dest, str, len);
1124 /* Added by Mel Hadasht on 27 Aug 2001 */
1125 /* Get a header from msginfo */
1126 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar **buf, gchar *header)
1130 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
1131 { NULL, NULL, FALSE } };
1134 cm_return_val_if_fail(msginfo != NULL, -1);
1135 cm_return_val_if_fail(buf != NULL, -1);
1136 cm_return_val_if_fail(header != NULL, -1);
1138 hentry[0].name = header;
1140 file = procmsg_get_message_file_path(msginfo);
1141 if ((fp = g_fopen(file, "rb")) == NULL) {
1142 FILE_OP_ERROR(file, "fopen");
1148 val = procheader_get_one_field(buf, fp, hentry);
1150 if (fclose(fp) == EOF) {
1151 FILE_OP_ERROR(file, "fclose");
1161 /* *buf is already NULL in that case, see procheader_get_one_field() */
1168 HeaderEntry *procheader_entries_from_str(const gchar *str)
1170 HeaderEntry *entries = NULL, *he;
1171 int numh = 0, i = 0;
1172 gchar **names = NULL;
1173 const gchar *s = str;
1178 while (*s != '\0') {
1179 if (*s == ' ') ++numh;
1185 entries = g_new0(HeaderEntry, numh + 1); /* room for last NULL */
1187 ++s; /* skip first space */
1188 names = g_strsplit(s, " ", numh);
1191 he->name = g_strdup_printf("%s:", names[i]);
1201 void procheader_entries_free (HeaderEntry *entries)
1203 if (entries != NULL) {
1204 HeaderEntry *he = entries;
1205 while (he->name != NULL) {
1207 if (he->body != NULL)
1215 gboolean procheader_header_is_internal(const gchar *hdr_name)
1217 const gchar *internal_hdrs[] = {
1218 "AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:", "CFG:",
1219 "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:", "R:", "MAID:",
1220 "SCF:", "RMID:", "FMID:", "NAID:",
1221 "X-Claws-Account-Id:",
1224 "X-Claws-Privacy-System:",
1225 "X-Claws-Auto-Wrapping:",
1226 "X-Claws-Auto-Indent:",
1227 "X-Claws-End-Special-Headers:",
1228 "X-Sylpheed-Account-Id:",
1230 "X-Sylpheed-Encrypt:",
1231 "X-Sylpheed-Privacy-System:",
1232 "X-Sylpheed-End-Special-Headers:",
1237 for (i = 0; internal_hdrs[i]; i++) {
1238 if (!strcmp(hdr_name, internal_hdrs[i]))