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 GError *error = NULL;
418 f = g_file_new_for_path(file);
419 fi = g_file_query_info(f, "standard::size,standard::type,time::modified",
420 G_FILE_QUERY_INFO_NONE, NULL, &error);
422 g_warning(error->message);
427 if (g_stat(file, &s) < 0) {
428 FILE_OP_ERROR(file, "stat");
434 if (g_file_info_get_file_type(fi) != G_FILE_TYPE_REGULAR) {
440 if (!S_ISREG(s.st_mode))
444 if ((fp = g_fopen(file, "rb")) == NULL) {
445 FILE_OP_ERROR(file, "fopen");
449 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
454 msginfo->size = g_file_info_get_size(fi);
455 g_file_info_get_modification_time(fi, &tv);
456 msginfo->mtime = tv.tv_sec;
458 msginfo->size = s.st_size;
459 msginfo->mtime = s.st_mtime;
471 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
474 return parse_stream(&str, TRUE, flags, full, decrypted);
492 H_SC_PLANNED_DOWNLOAD,
496 H_DISPOSITION_NOTIFICATION_TO,
498 H_SC_PARTIALLY_RETRIEVED,
510 static HeaderEntry hentry_full[] = {
511 {"Date:", NULL, FALSE},
512 {"From:", NULL, TRUE},
515 {"Newsgroups:", NULL, TRUE},
516 {"Subject:", NULL, TRUE},
517 {"Message-ID:", NULL, FALSE},
518 {"References:", NULL, FALSE},
519 {"In-Reply-To:", NULL, FALSE},
520 {"Content-Type:", NULL, FALSE},
521 {"Seen:", NULL, FALSE},
522 {"Status:", NULL, FALSE},
523 {"From ", NULL, FALSE},
524 {"SC-Marked-For-Download:", NULL, FALSE},
525 {"SC-Message-Size:", NULL, FALSE},
526 {"Face:", NULL, FALSE},
527 {"X-Face:", NULL, FALSE},
528 {"Disposition-Notification-To:", NULL, FALSE},
529 {"Return-Receipt-To:", NULL, FALSE},
530 {"SC-Partially-Retrieved:", NULL, FALSE},
531 {"SC-Account-Server:", NULL, FALSE},
532 {"SC-Account-Login:",NULL, FALSE},
533 {"List-Post:", NULL, TRUE},
534 {"List-Subscribe:", NULL, TRUE},
535 {"List-Unsubscribe:",NULL, TRUE},
536 {"List-Help:", NULL, TRUE},
537 {"List-Archive:", NULL, TRUE},
538 {"List-Owner:", NULL, TRUE},
539 {"Resent-From:", NULL, TRUE},
540 {NULL, NULL, FALSE}};
542 static HeaderEntry hentry_short[] = {
543 {"Date:", NULL, FALSE},
544 {"From:", NULL, TRUE},
547 {"Newsgroups:", NULL, TRUE},
548 {"Subject:", NULL, TRUE},
549 {"Message-ID:", NULL, FALSE},
550 {"References:", NULL, FALSE},
551 {"In-Reply-To:", NULL, FALSE},
552 {"Content-Type:", NULL, FALSE},
553 {"Seen:", NULL, FALSE},
554 {"Status:", NULL, FALSE},
555 {"From ", NULL, FALSE},
556 {"SC-Marked-For-Download:", NULL, FALSE},
557 {"SC-Message-Size:",NULL, FALSE},
558 {NULL, NULL, FALSE}};
560 static HeaderEntry* procheader_get_headernames(gboolean full)
562 return full ? hentry_full : hentry_short;
565 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
568 return parse_stream(fp, FALSE, flags, full, decrypted);
571 static gboolean avatar_from_some_face(gpointer source, gpointer userdata)
573 AvatarCaptureData *acd = (AvatarCaptureData *)source;
575 if (*(acd->content) == '\0') /* won't be null, but may be empty */
578 if (!strcmp(acd->header, hentry_full[H_FACE].name)) {
579 debug_print("avatar_from_some_face: found 'Face' header\n");
580 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_FACE, acd->content);
583 else if (!strcmp(acd->header, hentry_full[H_X_FACE].name)) {
584 debug_print("avatar_from_some_face: found 'X-Face' header\n");
585 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_XFACE, acd->content);
591 static guint avatar_hook_id = 0;
593 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
594 gboolean full, gboolean decrypted)
602 void *orig_data = data;
604 get_one_field_func get_one_field =
605 isstring ? (get_one_field_func)string_get_one_field
606 : (get_one_field_func)procheader_get_one_field;
608 hentry = procheader_get_headernames(full);
610 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
611 while (get_one_field(&buf, data, NULL) != -1) {
612 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
613 strlen("X-Claws-End-Special-Headers:"))) ||
614 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
615 strlen("X-Sylpheed-End-Special-Headers:")))) {
620 /* from other mailers */
621 if (!strncmp(buf, "Date: ", 6)
622 || !strncmp(buf, "To: ", 4)
623 || !strncmp(buf, "From: ", 6)
624 || !strncmp(buf, "Subject: ", 9)) {
628 rewind((FILE *)data);
638 msginfo = procmsg_msginfo_new();
640 if (flags.tmp_flags || flags.perm_flags)
641 msginfo->flags = flags;
643 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
645 msginfo->inreplyto = NULL;
647 if (avatar_hook_id == 0 && (prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
648 avatar_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_from_some_face, NULL);
649 } else if (avatar_hook_id != 0 && !(prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
650 hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_hook_id);
654 while ((hnum = get_one_field(&buf, data, hentry)) != -1) {
655 hp = buf + strlen(hentry[hnum].name);
656 while (*hp == ' ' || *hp == '\t') hp++;
660 if (msginfo->date) break;
662 procheader_date_parse(NULL, hp, 0);
663 if (g_utf8_validate(hp, -1, NULL)) {
664 msginfo->date = g_strdup(hp);
666 gchar *utf = conv_codeset_strdup(
668 conv_get_locale_charset_str_no_utf8(),
671 !g_utf8_validate(utf, -1, NULL)) {
673 utf = g_malloc(strlen(buf)*2+1);
674 conv_localetodisp(utf,
681 if (msginfo->from) break;
682 msginfo->from = conv_unmime_header(hp, NULL, TRUE);
683 msginfo->fromname = procheader_get_fromname(msginfo->from);
684 remove_return(msginfo->from);
685 remove_return(msginfo->fromname);
688 tmp = conv_unmime_header(hp, NULL, TRUE);
693 g_strconcat(p, ", ", tmp, NULL);
696 msginfo->to = g_strdup(tmp);
700 tmp = conv_unmime_header(hp, NULL, TRUE);
705 g_strconcat(p, ", ", tmp, NULL);
708 msginfo->cc = g_strdup(tmp);
712 if (msginfo->newsgroups) {
713 p = msginfo->newsgroups;
714 msginfo->newsgroups =
715 g_strconcat(p, ",", hp, NULL);
718 msginfo->newsgroups = g_strdup(hp);
721 if (msginfo->subject) break;
722 msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
723 unfold_line(msginfo->subject);
726 if (msginfo->msgid) break;
728 extract_parenthesis(hp, '<', '>');
730 msginfo->msgid = g_strdup(hp);
733 msginfo->references =
734 references_list_prepend(msginfo->references,
738 if (msginfo->inreplyto) break;
740 eliminate_parenthesis(hp, '(', ')');
741 if ((p = strrchr(hp, '<')) != NULL &&
742 strchr(p + 1, '>') != NULL) {
743 extract_parenthesis(p, '<', '>');
746 msginfo->inreplyto = g_strdup(p);
750 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
751 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
753 case H_DISPOSITION_NOTIFICATION_TO:
754 if (!msginfo->extradata)
755 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
756 if (msginfo->extradata->dispositionnotificationto) break;
757 msginfo->extradata->dispositionnotificationto = g_strdup(hp);
759 case H_RETURN_RECEIPT_TO:
760 if (!msginfo->extradata)
761 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
762 if (msginfo->extradata->returnreceiptto) break;
763 msginfo->extradata->returnreceiptto = g_strdup(hp);
765 /* partial download infos */
766 case H_SC_PARTIALLY_RETRIEVED:
767 if (!msginfo->extradata)
768 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
769 if (msginfo->extradata->partial_recv) break;
770 msginfo->extradata->partial_recv = g_strdup(hp);
772 case H_SC_ACCOUNT_SERVER:
773 if (!msginfo->extradata)
774 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
775 if (msginfo->extradata->account_server) break;
776 msginfo->extradata->account_server = g_strdup(hp);
778 case H_SC_ACCOUNT_LOGIN:
779 if (!msginfo->extradata)
780 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
781 if (msginfo->extradata->account_login) break;
782 msginfo->extradata->account_login = g_strdup(hp);
784 case H_SC_MESSAGE_SIZE:
785 if (msginfo->total_size) break;
786 msginfo->total_size = atoi(hp);
788 case H_SC_PLANNED_DOWNLOAD:
789 msginfo->planned_download = atoi(hp);
791 /* end partial download infos */
793 if (msginfo->fromspace) break;
794 msginfo->fromspace = g_strdup(hp);
795 remove_return(msginfo->fromspace);
799 if (!msginfo->extradata)
800 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
801 if (msginfo->extradata->list_post) break;
802 msginfo->extradata->list_post = g_strdup(hp);
804 case H_LIST_SUBSCRIBE:
805 if (!msginfo->extradata)
806 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
807 if (msginfo->extradata->list_subscribe) break;
808 msginfo->extradata->list_subscribe = g_strdup(hp);
810 case H_LIST_UNSUBSCRIBE:
811 if (!msginfo->extradata)
812 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
813 if (msginfo->extradata->list_unsubscribe) break;
814 msginfo->extradata->list_unsubscribe = g_strdup(hp);
817 if (!msginfo->extradata)
818 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
819 if (msginfo->extradata->list_help) break;
820 msginfo->extradata->list_help = g_strdup(hp);
823 if (!msginfo->extradata)
824 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
825 if (msginfo->extradata->list_archive) break;
826 msginfo->extradata->list_archive = g_strdup(hp);
829 if (!msginfo->extradata)
830 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
831 if (msginfo->extradata->list_owner) break;
832 msginfo->extradata->list_owner = g_strdup(hp);
835 if (!msginfo->extradata)
836 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
837 if (msginfo->extradata->resent_from) break;
838 msginfo->extradata->resent_from = g_strdup(hp);
844 /* to avoid performance penalty hooklist is invoked only for
845 headers known to be able to generate avatars */
846 if (hnum == H_FROM || hnum == H_X_FACE || hnum == H_FACE) {
847 AvatarCaptureData *acd = g_new0(AvatarCaptureData, 1);
848 /* no extra memory is wasted, hooks are expected to
849 take care of copying members when needed */
850 acd->msginfo = msginfo;
851 acd->header = hentry_full[hnum].name;
853 hooks_invoke(AVATAR_HEADER_UPDATE_HOOKLIST, (gpointer)acd);
860 if (!msginfo->inreplyto && msginfo->references)
862 g_strdup((gchar *)msginfo->references->data);
867 gchar *procheader_get_fromname(const gchar *str)
871 Xstrdup_a(tmp, str, return NULL);
874 extract_quote(tmp, '\"');
876 } else if (strchr(tmp, '<')) {
877 eliminate_parenthesis(tmp, '<', '>');
881 extract_parenthesis(tmp, '<', '>');
884 } else if (strchr(tmp, '(')) {
885 extract_parenthesis(tmp, '(', ')');
890 name = g_strdup(str);
892 name = g_strdup(tmp);
897 static gint procheader_scan_date_string(const gchar *str,
898 gchar *weekday, gint *day,
899 gchar *month, gint *year,
900 gint *hh, gint *mm, gint *ss,
906 gint zone1 = 0, zone2 = 0;
907 gchar offset_sign, zonestr[7];
913 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %6s",
914 weekday, day, month, year, hh, mm, ss, zone);
915 if (result == 8) return 0;
918 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %6s",
919 weekday, day, month, year, hh, mm, ss, zone);
920 if (result == 8) return 0;
922 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %6s",
923 day, month, year, hh, mm, ss, zone);
924 if (result == 7) return 0;
927 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
928 weekday, day, month, year, hh, mm, ss);
929 if (result == 7) return 0;
931 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
932 day, month, year, hh, mm, ss);
933 if (result == 6) return 0;
936 result = sscanf(str, "%10s %d %9s %d %2d:%2d %6s",
937 weekday, day, month, year, hh, mm, zone);
938 if (result == 7) return 0;
940 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
941 day, month, year, hh, mm, zone);
942 if (result == 6) return 0;
945 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
946 weekday, day, month, year, hh, mm);
947 if (result == 6) return 0;
949 result = sscanf(str, "%d %9s %d %2d:%2d",
950 day, month, year, hh, mm);
951 if (result == 5) return 0;
955 /* RFC3339 subset, with fraction of second */
956 result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d.%d%6s",
957 year, &month_n, day, &sep1, hh, mm, ss, &secfract, zonestr);
959 && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
960 if (month_n >= 1 && month_n <= 12) {
961 strncpy2(month, monthstr+((month_n-1)*3), 4);
962 if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
963 strcat(zone, "+00:00");
964 } else if (sscanf(zonestr, "%c%2d:%2d",
965 &offset_sign, &zone1, &zone2) == 3) {
966 strcat(zone, zonestr);
972 /* RFC3339 subset, no fraction of second */
973 result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d%6s",
974 year, &month_n, day, &sep1, hh, mm, ss, zonestr);
976 && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
977 if (month_n >= 1 && month_n <= 12) {
978 strncpy2(month, monthstr+((month_n-1)*3), 4);
979 if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
980 strcat(zone, "+00:00");
981 } else if (sscanf(zonestr, "%c%2d:%2d",
982 &offset_sign, &zone1, &zone2) == 3) {
983 strcat(zone, zonestr);
991 /* RFC3339 subset, no fraction of second, and no timezone offset */
992 /* This particular "subset" is invalid, RFC requires the offset */
993 result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d",
994 year, &month_n, day, hh, mm, ss);
996 if (1 <= month_n && month_n <= 12) {
997 strncpy2(month, monthstr+((month_n-1)*3), 4);
1002 /* ISO8601 format with just date (YYYY-MM-DD) */
1003 result = sscanf(str, "%4d-%2d-%2d",
1004 year, &month_n, day);
1006 *hh = *mm = *ss = 0;
1007 if (1 <= month_n && month_n <= 12) {
1008 strncpy2(month, monthstr+((month_n-1)*3), 4);
1017 * Hiro, most UNIXen support this function:
1018 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
1020 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
1033 memset(t, 0, sizeof *t);
1035 if (procheader_scan_date_string(src, weekday, &day, month, &year,
1036 &hh, &mm, &ss, zone) < 0) {
1037 g_warning("Invalid date: %s", src);
1041 /* Y2K compliant :) */
1050 if ((p = strstr(monthstr, month)) != NULL)
1051 dmonth = (gint)(p - monthstr) / 3 + 1;
1053 g_warning("Invalid month: %s", month);
1054 dmonth = G_DATE_BAD_MONTH;
1061 t->tm_mon = dmonth - 1;
1062 t->tm_year = year - 1900;
1072 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
1080 GDateMonth dmonth = G_DATE_BAD_MONTH;
1084 if (procheader_scan_date_string(src, weekday, &day, month, &year,
1085 &hh, &mm, &ss, zone) < 0) {
1086 if (dest && len > 0)
1087 strncpy2(dest, src, len);
1092 for (p = monthstr; *p != '\0'; p += 3) {
1093 if (!g_ascii_strncasecmp(p, month, 3)) {
1094 dmonth = (gint)(p - monthstr) / 3 + 1;
1101 GDateTime *dt, *dt2;
1103 tz = g_time_zone_new(zone); // can't return NULL no need to check for it
1104 dt = g_date_time_new(tz, 1, 1, 1, 0, 0, 0);
1105 g_time_zone_unref(tz);
1106 dt2 = g_date_time_add_full(dt, year-1, dmonth-1, day-1, hh, mm, ss);
1107 g_date_time_unref(dt);
1109 timer = g_date_time_to_unix(dt2);
1110 g_date_time_unref(dt2);
1116 /* Y2K compliant :) */
1128 t.tm_mon = dmonth - 1;
1129 t.tm_year = year - 1900;
1135 tz_offset = remote_tzoffset_sec(zone);
1136 if (tz_offset != -1)
1137 timer += tzoffset_sec(&timer) - tz_offset;
1140 procheader_date_get_localtime(dest, len, timer);
1146 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
1149 gchar *default_format = "%y/%m/%d(%a) %H:%M";
1151 const gchar *src_codeset, *dest_codeset;
1155 lt = localtime_r(&timer, &buf);
1158 lt = localtime_r(&dummy, &buf);
1161 if (prefs_common.date_format)
1162 fast_strftime(dest, len, prefs_common.date_format, lt);
1164 fast_strftime(dest, len, default_format, lt);
1166 if (!g_utf8_validate(dest, -1, NULL)) {
1167 src_codeset = conv_get_locale_charset_str_no_utf8();
1168 dest_codeset = CS_UTF_8;
1169 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
1171 strncpy2(dest, str, len);
1177 /* Added by Mel Hadasht on 27 Aug 2001 */
1178 /* Get a header from msginfo */
1179 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar **buf, gchar *header)
1183 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
1184 { NULL, NULL, FALSE } };
1187 cm_return_val_if_fail(msginfo != NULL, -1);
1188 cm_return_val_if_fail(buf != NULL, -1);
1189 cm_return_val_if_fail(header != NULL, -1);
1191 hentry[0].name = header;
1193 file = procmsg_get_message_file_path(msginfo);
1194 if ((fp = g_fopen(file, "rb")) == NULL) {
1195 FILE_OP_ERROR(file, "fopen");
1201 val = procheader_get_one_field(buf, fp, hentry);
1203 if (fclose(fp) == EOF) {
1204 FILE_OP_ERROR(file, "fclose");
1214 /* *buf is already NULL in that case, see procheader_get_one_field() */
1221 HeaderEntry *procheader_entries_from_str(const gchar *str)
1223 HeaderEntry *entries = NULL, *he;
1224 int numh = 0, i = 0;
1225 gchar **names = NULL;
1226 const gchar *s = str;
1231 while (*s != '\0') {
1232 if (*s == ' ') ++numh;
1238 entries = g_new0(HeaderEntry, numh + 1); /* room for last NULL */
1240 ++s; /* skip first space */
1241 names = g_strsplit(s, " ", numh);
1244 he->name = g_strdup_printf("%s:", names[i]);
1254 void procheader_entries_free (HeaderEntry *entries)
1256 if (entries != NULL) {
1257 HeaderEntry *he = entries;
1258 while (he->name != NULL) {
1260 if (he->body != NULL)
1268 gboolean procheader_header_is_internal(const gchar *hdr_name)
1270 const gchar *internal_hdrs[] = {
1271 "AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:", "CFG:",
1272 "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:", "R:", "MAID:",
1273 "SCF:", "RMID:", "FMID:", "NAID:",
1274 "X-Claws-Account-Id:",
1277 "X-Claws-Privacy-System:",
1278 "X-Claws-Auto-Wrapping:",
1279 "X-Claws-Auto-Indent:",
1280 "X-Claws-End-Special-Headers:",
1281 "X-Sylpheed-Account-Id:",
1283 "X-Sylpheed-Encrypt:",
1284 "X-Sylpheed-Privacy-System:",
1285 "X-Sylpheed-End-Special-Headers:",
1290 for (i = 0; internal_hdrs[i]; i++) {
1291 if (!strcmp(hdr_name, internal_hdrs[i]))