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 gboolean procheader_skip_headers(FILE *fp)
88 gchar *buf = g_malloc(BUFFSIZE);
90 if (fgets_crlf(buf, BUFFSIZE - 1, fp) == NULL) {
94 if (buf[0] == '\r' || buf[0] == '\n') {
104 static char *string_getline(char *buf, size_t len, char **str)
106 gboolean is_cr = FALSE;
107 gboolean last_was_cr = FALSE;
112 for (; **str && len > 1; --len) {
113 is_cr = (**str == '\r');
114 if ((*buf++ = *(*str)++) == '\n') {
130 static int string_peekchar(char **str)
135 static int file_peekchar(FILE *fp)
137 return ungetc(getc(fp), fp);
140 static gint generic_get_one_field(gchar **bufptr, void *data,
142 getlinefunc getline, peekcharfunc peekchar,
145 /* returns -1 in case of failure of any kind, whatever it's a parsing error
146 or an allocation error. if returns -1, *bufptr is always NULL, and vice-versa,
147 and if returning 0 (OK), *bufptr is always non-NULL, so callers just have to
148 test the return value
152 HeaderEntry *hp = NULL;
156 cm_return_val_if_fail(bufptr != NULL, -1);
161 if (hentry != NULL) {
162 /* skip non-required headers */
163 /* and get hentry header line */
166 if (getline(buf, len, data) == NULL) {
167 debug_print("generic_get_one_field: getline\n");
172 if (buf[0] == '\r' || buf[0] == '\n') {
173 debug_print("generic_get_one_field: empty line\n");
178 } while (buf[0] == ' ' || buf[0] == '\t');
180 for (hp = hentry, hnum = 0; hp->name != NULL;
182 if (!g_ascii_strncasecmp(hp->name, buf,
186 } while (hp->name == NULL);
188 /* read first line */
189 if (getline(buf, len, data) == NULL) {
190 debug_print("generic_get_one_field: getline\n");
195 if (buf[0] == '\r' || buf[0] == '\n') {
196 debug_print("generic_get_one_field: empty line\n");
202 /* reduce initial buffer to its useful part */
204 buf = g_realloc(buf, len);
206 debug_print("generic_get_one_field: reallocation error\n");
213 nexthead = peekchar(data);
214 /* ([*WSP CRLF] 1*WSP) */
215 if (nexthead == ' ' || nexthead == '\t') {
220 gboolean skiptab = (nexthead == '\t');
221 /* trim previous trailing \n if requesting one header or
222 * unfolding was requested */
223 if ((!hentry && unfold) || (hp && hp->unfold))
226 buflen = strlen(buf);
229 tmpbuf = g_malloc(BUFFSIZE);
231 if (getline(tmpbuf, BUFFSIZE, data) == NULL) {
235 tmplen = strlen(tmpbuf)+1;
237 /* extend initial buffer and concatenate next line */
239 buf = g_realloc(buf, len);
241 debug_print("generic_get_one_field: reallocation error\n");
246 memcpy(buf+buflen, tmpbuf, tmplen);
248 if (skiptab) { /* replace tab with space */
249 *(buf + buflen) = ' ';
252 /* remove trailing new line */
263 gint procheader_get_one_field_asis(gchar **buf, FILE *fp)
265 return generic_get_one_field(buf, fp, NULL,
266 (getlinefunc)fgets_crlf,
267 (peekcharfunc)file_peekchar,
271 GPtrArray *procheader_get_header_array_asis(FILE *fp)
277 cm_return_val_if_fail(fp != NULL, NULL);
279 headers = g_ptr_array_new();
281 while (procheader_get_one_field_asis(&buf, fp) != -1) {
282 if ((header = procheader_parse_header(buf)) != NULL)
283 g_ptr_array_add(headers, header);
291 void procheader_header_array_destroy(GPtrArray *harray)
296 cm_return_if_fail(harray != NULL);
298 for (i = 0; i < harray->len; i++) {
299 header = g_ptr_array_index(harray, i);
300 procheader_header_free(header);
303 g_ptr_array_free(harray, TRUE);
306 void procheader_header_free(Header *header)
310 g_free(header->name);
311 g_free(header->body);
316 tests whether two headers' names are equal
317 remove the trailing ':' or ' ' before comparing
320 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
327 if (hdr1[len1 - 1] == ':')
329 if (hdr2[len2 - 1] == ':')
334 return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
338 parse headers, for example :
339 From: dinh@enseirb.fr becomes :
340 header->name = "From:"
341 header->body = "dinh@enseirb.fr"
343 static gboolean header_is_addr_field(const gchar *hdr)
345 static char *addr_headers[] = {
352 "Followup-and-Reply-To:",
353 "Disposition-Notification-To:",
354 "Return-Receipt-To:",
361 for (i = 0; addr_headers[i] != NULL; i++)
362 if (!strcasecmp(hdr, addr_headers[i]))
368 Header * procheader_parse_header(gchar * buf)
372 gboolean addr_field = FALSE;
374 cm_return_val_if_fail(buf != NULL, NULL);
376 if ((*buf == ':') || (*buf == ' '))
379 for (p = buf; *p ; p++) {
380 if ((*p == ':') || (*p == ' ')) {
381 header = g_new(Header, 1);
382 header->name = g_strndup(buf, p - buf + 1);
383 addr_field = header_is_addr_field(header->name);
385 while (*p == ' ' || *p == '\t') p++;
386 header->body = conv_unmime_header(p, NULL, addr_field);
393 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
400 if (hentry == NULL) return;
402 while ((hnum = procheader_get_one_field(&buf, fp, hentry)) != -1) {
405 p = buf + strlen(hp->name);
406 while (*p == ' ' || *p == '\t') p++;
408 if (hp->body == NULL)
409 hp->body = g_strdup(p);
410 else if (procheader_headername_equal(hp->name, "To") ||
411 procheader_headername_equal(hp->name, "Cc")) {
412 gchar *tp = hp->body;
413 hp->body = g_strconcat(tp, ", ", p, NULL);
421 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
422 gboolean full, gboolean decrypted)
428 GError *error = NULL;
436 f = g_file_new_for_path(file);
437 fi = g_file_query_info(f, "standard::size,standard::type,time::modified",
438 G_FILE_QUERY_INFO_NONE, NULL, &error);
440 g_warning(error->message);
445 if (g_stat(file, &s) < 0) {
446 FILE_OP_ERROR(file, "stat");
452 if (g_file_info_get_file_type(fi) != G_FILE_TYPE_REGULAR) {
458 if (!S_ISREG(s.st_mode))
462 if ((fp = g_fopen(file, "rb")) == NULL) {
463 FILE_OP_ERROR(file, "fopen");
467 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
472 msginfo->size = g_file_info_get_size(fi);
473 g_file_info_get_modification_time(fi, &tv);
474 msginfo->mtime = tv.tv_sec;
476 msginfo->size = s.st_size;
477 msginfo->mtime = s.st_mtime;
489 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
492 return parse_stream(&str, TRUE, flags, full, decrypted);
510 H_SC_PLANNED_DOWNLOAD,
514 H_DISPOSITION_NOTIFICATION_TO,
516 H_SC_PARTIALLY_RETRIEVED,
528 static HeaderEntry hentry_full[] = {
529 {"Date:", NULL, FALSE},
530 {"From:", NULL, TRUE},
533 {"Newsgroups:", NULL, TRUE},
534 {"Subject:", NULL, TRUE},
535 {"Message-ID:", NULL, FALSE},
536 {"References:", NULL, FALSE},
537 {"In-Reply-To:", NULL, FALSE},
538 {"Content-Type:", NULL, FALSE},
539 {"Seen:", NULL, FALSE},
540 {"Status:", NULL, FALSE},
541 {"From ", NULL, FALSE},
542 {"SC-Marked-For-Download:", NULL, FALSE},
543 {"SC-Message-Size:", NULL, FALSE},
544 {"Face:", NULL, FALSE},
545 {"X-Face:", NULL, FALSE},
546 {"Disposition-Notification-To:", NULL, FALSE},
547 {"Return-Receipt-To:", NULL, FALSE},
548 {"SC-Partially-Retrieved:", NULL, FALSE},
549 {"SC-Account-Server:", NULL, FALSE},
550 {"SC-Account-Login:",NULL, FALSE},
551 {"List-Post:", NULL, TRUE},
552 {"List-Subscribe:", NULL, TRUE},
553 {"List-Unsubscribe:",NULL, TRUE},
554 {"List-Help:", NULL, TRUE},
555 {"List-Archive:", NULL, TRUE},
556 {"List-Owner:", NULL, TRUE},
557 {"Resent-From:", NULL, TRUE},
558 {NULL, NULL, FALSE}};
560 static HeaderEntry hentry_short[] = {
561 {"Date:", NULL, FALSE},
562 {"From:", NULL, TRUE},
565 {"Newsgroups:", NULL, TRUE},
566 {"Subject:", NULL, TRUE},
567 {"Message-ID:", NULL, FALSE},
568 {"References:", NULL, FALSE},
569 {"In-Reply-To:", NULL, FALSE},
570 {"Content-Type:", NULL, FALSE},
571 {"Seen:", NULL, FALSE},
572 {"Status:", NULL, FALSE},
573 {"From ", NULL, FALSE},
574 {"SC-Marked-For-Download:", NULL, FALSE},
575 {"SC-Message-Size:",NULL, FALSE},
576 {NULL, NULL, FALSE}};
578 static HeaderEntry* procheader_get_headernames(gboolean full)
580 return full ? hentry_full : hentry_short;
583 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
586 return parse_stream(fp, FALSE, flags, full, decrypted);
589 static gboolean avatar_from_some_face(gpointer source, gpointer userdata)
591 AvatarCaptureData *acd = (AvatarCaptureData *)source;
593 if (*(acd->content) == '\0') /* won't be null, but may be empty */
596 if (!strcmp(acd->header, hentry_full[H_FACE].name)) {
597 debug_print("avatar_from_some_face: found 'Face' header\n");
598 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_FACE, acd->content);
601 else if (!strcmp(acd->header, hentry_full[H_X_FACE].name)) {
602 debug_print("avatar_from_some_face: found 'X-Face' header\n");
603 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_XFACE, acd->content);
609 static gulong avatar_hook_id = HOOK_NONE;
611 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
612 gboolean full, gboolean decrypted)
620 void *orig_data = data;
622 get_one_field_func get_one_field =
623 isstring ? (get_one_field_func)string_get_one_field
624 : (get_one_field_func)procheader_get_one_field;
626 hentry = procheader_get_headernames(full);
628 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
629 while (get_one_field(&buf, data, NULL) != -1) {
630 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
631 strlen("X-Claws-End-Special-Headers:"))) ||
632 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
633 strlen("X-Sylpheed-End-Special-Headers:")))) {
638 /* from other mailers */
639 if (!strncmp(buf, "Date: ", 6)
640 || !strncmp(buf, "To: ", 4)
641 || !strncmp(buf, "From: ", 6)
642 || !strncmp(buf, "Subject: ", 9)) {
646 rewind((FILE *)data);
656 msginfo = procmsg_msginfo_new();
658 if (flags.tmp_flags || flags.perm_flags)
659 msginfo->flags = flags;
661 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
663 msginfo->inreplyto = NULL;
665 if (avatar_hook_id == HOOK_NONE && (prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
666 avatar_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_from_some_face, NULL);
667 } else if (avatar_hook_id != HOOK_NONE && !(prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
668 hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_hook_id);
669 avatar_hook_id = HOOK_NONE;
672 while ((hnum = get_one_field(&buf, data, hentry)) != -1) {
673 hp = buf + strlen(hentry[hnum].name);
674 while (*hp == ' ' || *hp == '\t') hp++;
678 if (msginfo->date) break;
680 procheader_date_parse(NULL, hp, 0);
681 if (g_utf8_validate(hp, -1, NULL)) {
682 msginfo->date = g_strdup(hp);
684 gchar *utf = conv_codeset_strdup(
686 conv_get_locale_charset_str_no_utf8(),
689 !g_utf8_validate(utf, -1, NULL)) {
691 utf = g_malloc(strlen(buf)*2+1);
692 conv_localetodisp(utf,
699 if (msginfo->from) break;
700 msginfo->from = conv_unmime_header(hp, NULL, TRUE);
701 msginfo->fromname = procheader_get_fromname(msginfo->from);
702 remove_return(msginfo->from);
703 remove_return(msginfo->fromname);
706 tmp = conv_unmime_header(hp, NULL, TRUE);
711 g_strconcat(p, ", ", tmp, NULL);
714 msginfo->to = g_strdup(tmp);
718 tmp = conv_unmime_header(hp, NULL, TRUE);
723 g_strconcat(p, ", ", tmp, NULL);
726 msginfo->cc = g_strdup(tmp);
730 if (msginfo->newsgroups) {
731 p = msginfo->newsgroups;
732 msginfo->newsgroups =
733 g_strconcat(p, ",", hp, NULL);
736 msginfo->newsgroups = g_strdup(hp);
739 if (msginfo->subject) break;
740 msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
741 unfold_line(msginfo->subject);
744 if (msginfo->msgid) break;
746 extract_parenthesis(hp, '<', '>');
748 msginfo->msgid = g_strdup(hp);
751 msginfo->references =
752 references_list_prepend(msginfo->references,
756 if (msginfo->inreplyto) break;
758 eliminate_parenthesis(hp, '(', ')');
759 if ((p = strrchr(hp, '<')) != NULL &&
760 strchr(p + 1, '>') != NULL) {
761 extract_parenthesis(p, '<', '>');
764 msginfo->inreplyto = g_strdup(p);
768 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
769 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
771 case H_DISPOSITION_NOTIFICATION_TO:
772 if (!msginfo->extradata)
773 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
774 if (msginfo->extradata->dispositionnotificationto) break;
775 msginfo->extradata->dispositionnotificationto = g_strdup(hp);
777 case H_RETURN_RECEIPT_TO:
778 if (!msginfo->extradata)
779 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
780 if (msginfo->extradata->returnreceiptto) break;
781 msginfo->extradata->returnreceiptto = g_strdup(hp);
783 /* partial download infos */
784 case H_SC_PARTIALLY_RETRIEVED:
785 if (!msginfo->extradata)
786 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
787 if (msginfo->extradata->partial_recv) break;
788 msginfo->extradata->partial_recv = g_strdup(hp);
790 case H_SC_ACCOUNT_SERVER:
791 if (!msginfo->extradata)
792 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
793 if (msginfo->extradata->account_server) break;
794 msginfo->extradata->account_server = g_strdup(hp);
796 case H_SC_ACCOUNT_LOGIN:
797 if (!msginfo->extradata)
798 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
799 if (msginfo->extradata->account_login) break;
800 msginfo->extradata->account_login = g_strdup(hp);
802 case H_SC_MESSAGE_SIZE:
803 if (msginfo->total_size) break;
804 msginfo->total_size = atoi(hp);
806 case H_SC_PLANNED_DOWNLOAD:
807 msginfo->planned_download = atoi(hp);
809 /* end partial download infos */
811 if (msginfo->fromspace) break;
812 msginfo->fromspace = g_strdup(hp);
813 remove_return(msginfo->fromspace);
817 if (!msginfo->extradata)
818 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
819 if (msginfo->extradata->list_post) break;
820 msginfo->extradata->list_post = g_strdup(hp);
822 case H_LIST_SUBSCRIBE:
823 if (!msginfo->extradata)
824 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
825 if (msginfo->extradata->list_subscribe) break;
826 msginfo->extradata->list_subscribe = g_strdup(hp);
828 case H_LIST_UNSUBSCRIBE:
829 if (!msginfo->extradata)
830 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
831 if (msginfo->extradata->list_unsubscribe) break;
832 msginfo->extradata->list_unsubscribe = g_strdup(hp);
835 if (!msginfo->extradata)
836 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
837 if (msginfo->extradata->list_help) break;
838 msginfo->extradata->list_help = g_strdup(hp);
841 if (!msginfo->extradata)
842 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
843 if (msginfo->extradata->list_archive) break;
844 msginfo->extradata->list_archive = g_strdup(hp);
847 if (!msginfo->extradata)
848 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
849 if (msginfo->extradata->list_owner) break;
850 msginfo->extradata->list_owner = g_strdup(hp);
853 if (!msginfo->extradata)
854 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
855 if (msginfo->extradata->resent_from) break;
856 msginfo->extradata->resent_from = g_strdup(hp);
862 /* to avoid performance penalty hooklist is invoked only for
863 headers known to be able to generate avatars */
864 if (hnum == H_FROM || hnum == H_X_FACE || hnum == H_FACE) {
865 AvatarCaptureData *acd = g_new0(AvatarCaptureData, 1);
866 /* no extra memory is wasted, hooks are expected to
867 take care of copying members when needed */
868 acd->msginfo = msginfo;
869 acd->header = hentry_full[hnum].name;
871 hooks_invoke(AVATAR_HEADER_UPDATE_HOOKLIST, (gpointer)acd);
878 if (!msginfo->inreplyto && msginfo->references)
880 g_strdup((gchar *)msginfo->references->data);
885 gchar *procheader_get_fromname(const gchar *str)
889 Xstrdup_a(tmp, str, return NULL);
892 extract_quote(tmp, '\"');
894 } else if (strchr(tmp, '<')) {
895 eliminate_parenthesis(tmp, '<', '>');
899 extract_parenthesis(tmp, '<', '>');
902 } else if (strchr(tmp, '(')) {
903 extract_parenthesis(tmp, '(', ')');
908 name = g_strdup(str);
910 name = g_strdup(tmp);
915 static gint procheader_scan_date_string(const gchar *str,
916 gchar *weekday, gint *day,
917 gchar *month, gint *year,
918 gint *hh, gint *mm, gint *ss,
924 gint zone1 = 0, zone2 = 0;
925 gchar offset_sign, zonestr[7];
931 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %6s",
932 weekday, day, month, year, hh, mm, ss, zone);
933 if (result == 8) return 0;
936 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %6s",
937 weekday, day, month, year, hh, mm, ss, zone);
938 if (result == 8) return 0;
940 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %6s",
941 day, month, year, hh, mm, ss, zone);
942 if (result == 7) return 0;
945 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
946 weekday, day, month, year, hh, mm, ss);
947 if (result == 7) return 0;
949 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
950 day, month, year, hh, mm, ss);
951 if (result == 6) return 0;
954 result = sscanf(str, "%10s %d %9s %d %2d:%2d %6s",
955 weekday, day, month, year, hh, mm, zone);
956 if (result == 7) return 0;
958 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
959 day, month, year, hh, mm, zone);
960 if (result == 6) return 0;
963 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
964 weekday, day, month, year, hh, mm);
965 if (result == 6) return 0;
967 result = sscanf(str, "%d %9s %d %2d:%2d",
968 day, month, year, hh, mm);
969 if (result == 5) return 0;
973 /* RFC3339 subset, with fraction of second */
974 result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d.%d%6s",
975 year, &month_n, day, &sep1, hh, mm, ss, &secfract, zonestr);
977 && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
978 if (month_n >= 1 && month_n <= 12) {
979 strncpy2(month, monthstr+((month_n-1)*3), 4);
980 if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
981 strcat(zone, "+00:00");
982 } else if (sscanf(zonestr, "%c%2d:%2d",
983 &offset_sign, &zone1, &zone2) == 3) {
984 strcat(zone, zonestr);
990 /* RFC3339 subset, no fraction of second */
991 result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d%6s",
992 year, &month_n, day, &sep1, hh, mm, ss, zonestr);
994 && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
995 if (month_n >= 1 && month_n <= 12) {
996 strncpy2(month, monthstr+((month_n-1)*3), 4);
997 if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
998 strcat(zone, "+00:00");
999 } else if (sscanf(zonestr, "%c%2d:%2d",
1000 &offset_sign, &zone1, &zone2) == 3) {
1001 strcat(zone, zonestr);
1009 /* RFC3339 subset, no fraction of second, and no timezone offset */
1010 /* This particular "subset" is invalid, RFC requires the offset */
1011 result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d",
1012 year, &month_n, day, hh, mm, ss);
1014 if (1 <= month_n && month_n <= 12) {
1015 strncpy2(month, monthstr+((month_n-1)*3), 4);
1020 /* ISO8601 format with just date (YYYY-MM-DD) */
1021 result = sscanf(str, "%4d-%2d-%2d",
1022 year, &month_n, day);
1024 *hh = *mm = *ss = 0;
1025 if (1 <= month_n && month_n <= 12) {
1026 strncpy2(month, monthstr+((month_n-1)*3), 4);
1035 * Hiro, most UNIXen support this function:
1036 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
1038 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
1051 memset(t, 0, sizeof *t);
1053 if (procheader_scan_date_string(src, weekday, &day, month, &year,
1054 &hh, &mm, &ss, zone) < 0) {
1055 g_warning("Invalid date: %s", src);
1059 /* Y2K compliant :) */
1068 if ((p = strstr(monthstr, month)) != NULL)
1069 dmonth = (gint)(p - monthstr) / 3 + 1;
1071 g_warning("Invalid month: %s", month);
1072 dmonth = G_DATE_BAD_MONTH;
1079 t->tm_mon = dmonth - 1;
1080 t->tm_year = year - 1900;
1090 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
1098 GDateMonth dmonth = G_DATE_BAD_MONTH;
1102 if (procheader_scan_date_string(src, weekday, &day, month, &year,
1103 &hh, &mm, &ss, zone) < 0) {
1104 if (dest && len > 0)
1105 strncpy2(dest, src, len);
1110 for (p = monthstr; *p != '\0'; p += 3) {
1111 if (!g_ascii_strncasecmp(p, month, 3)) {
1112 dmonth = (gint)(p - monthstr) / 3 + 1;
1119 GDateTime *dt, *dt2;
1121 tz = g_time_zone_new(zone); // can't return NULL no need to check for it
1122 dt = g_date_time_new(tz, 1, 1, 1, 0, 0, 0);
1123 g_time_zone_unref(tz);
1124 dt2 = g_date_time_add_full(dt, year-1, dmonth-1, day-1, hh, mm, ss);
1125 g_date_time_unref(dt);
1127 timer = g_date_time_to_unix(dt2);
1128 g_date_time_unref(dt2);
1134 /* Y2K compliant :) */
1146 t.tm_mon = dmonth - 1;
1147 t.tm_year = year - 1900;
1153 tz_offset = remote_tzoffset_sec(zone);
1154 if (tz_offset != -1)
1155 timer += tzoffset_sec(&timer) - tz_offset;
1158 procheader_date_get_localtime(dest, len, timer);
1164 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
1167 gchar *default_format = "%y/%m/%d(%a) %H:%M";
1169 const gchar *src_codeset, *dest_codeset;
1173 lt = localtime_r(&timer, &buf);
1176 lt = localtime_r(&dummy, &buf);
1179 if (prefs_common.date_format)
1180 fast_strftime(dest, len, prefs_common.date_format, lt);
1182 fast_strftime(dest, len, default_format, lt);
1184 if (!g_utf8_validate(dest, -1, NULL)) {
1185 src_codeset = conv_get_locale_charset_str_no_utf8();
1186 dest_codeset = CS_UTF_8;
1187 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
1189 strncpy2(dest, str, len);
1195 /* Added by Mel Hadasht on 27 Aug 2001 */
1196 /* Get a header from msginfo */
1197 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar **buf, gchar *header)
1201 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
1202 { NULL, NULL, FALSE } };
1205 cm_return_val_if_fail(msginfo != NULL, -1);
1206 cm_return_val_if_fail(buf != NULL, -1);
1207 cm_return_val_if_fail(header != NULL, -1);
1209 hentry[0].name = header;
1211 file = procmsg_get_message_file_path(msginfo);
1212 if ((fp = g_fopen(file, "rb")) == NULL) {
1213 FILE_OP_ERROR(file, "fopen");
1219 val = procheader_get_one_field(buf, fp, hentry);
1221 if (fclose(fp) == EOF) {
1222 FILE_OP_ERROR(file, "fclose");
1232 /* *buf is already NULL in that case, see procheader_get_one_field() */
1239 HeaderEntry *procheader_entries_from_str(const gchar *str)
1241 HeaderEntry *entries = NULL, *he;
1242 int numh = 0, i = 0;
1243 gchar **names = NULL;
1244 const gchar *s = str;
1249 while (*s != '\0') {
1250 if (*s == ' ') ++numh;
1256 entries = g_new0(HeaderEntry, numh + 1); /* room for last NULL */
1258 ++s; /* skip first space */
1259 names = g_strsplit(s, " ", numh);
1262 he->name = g_strdup_printf("%s:", names[i]);
1272 void procheader_entries_free (HeaderEntry *entries)
1274 if (entries != NULL) {
1275 HeaderEntry *he = entries;
1276 while (he->name != NULL) {
1278 if (he->body != NULL)
1286 gboolean procheader_header_is_internal(const gchar *hdr_name)
1288 const gchar *internal_hdrs[] = {
1289 "AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:", "CFG:",
1290 "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:", "R:", "MAID:",
1291 "SCF:", "RMID:", "FMID:", "NAID:",
1292 "X-Claws-Account-Id:",
1295 "X-Claws-Privacy-System:",
1296 "X-Claws-Auto-Wrapping:",
1297 "X-Claws-Auto-Indent:",
1298 "X-Claws-End-Special-Headers:",
1299 "X-Sylpheed-Account-Id:",
1301 "X-Sylpheed-Encrypt:",
1302 "X-Sylpheed-Privacy-System:",
1303 "X-Sylpheed-End-Special-Headers:",
1308 for (i = 0; internal_hdrs[i]; i++) {
1309 if (!strcmp(hdr_name, internal_hdrs[i]))