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); TODO */
143 debug_print("generic_get_one_field: primary allocation error\n");
147 if (hentry != NULL) {
148 /* skip non-required headers */
149 /* and get hentry header line */
152 if (getline(buf, len, data) == NULL) {
153 debug_print("generic_get_one_field: getline\n");
158 if (buf[0] == '\r' || buf[0] == '\n') {
159 debug_print("generic_get_one_field: empty line\n");
164 } while (buf[0] == ' ' || buf[0] == '\t');
166 for (hp = hentry, hnum = 0; hp->name != NULL;
168 if (!g_ascii_strncasecmp(hp->name, buf,
172 } while (hp->name == NULL);
174 /* read first line */
175 if (getline(buf, len, data) == NULL) {
176 debug_print("generic_get_one_field: getline\n");
181 if (buf[0] == '\r' || buf[0] == '\n') {
182 debug_print("generic_get_one_field: empty line\n");
188 /* reduce initial buffer to its useful part */
190 buf = g_realloc(buf, len);
192 debug_print("generic_get_one_field: reallocation error\n");
199 nexthead = peekchar(data);
200 /* ([*WSP CRLF] 1*WSP) */
201 if (nexthead == ' ' || nexthead == '\t') {
206 gboolean skiptab = (nexthead == '\t');
207 /* trim previous trailing \n if requesting one header or
208 * unfolding was requested */
209 if ((!hentry && unfold) || (hp && hp->unfold))
212 buflen = strlen(buf);
215 tmpbuf = g_malloc(BUFFSIZE);
216 if (tmpbuf == NULL) {
217 debug_print("generic_get_one_field: secondary allocation error\n");
222 if (getline(tmpbuf, BUFFSIZE, data) == NULL) {
226 tmplen = strlen(tmpbuf)+1;
228 /* extend initial buffer and concatenate next line */
230 buf = g_realloc(buf, len);
232 debug_print("generic_get_one_field: reallocation error\n");
237 memcpy(buf+buflen, tmpbuf, tmplen);
239 if (skiptab) { /* replace tab with space */
240 *(buf + buflen) = ' ';
243 /* remove trailing new line */
254 gint procheader_get_one_field_asis(gchar **buf, FILE *fp)
256 return generic_get_one_field(buf, fp, NULL,
257 (getlinefunc)fgets_crlf,
258 (peekcharfunc)file_peekchar,
262 GPtrArray *procheader_get_header_array_asis(FILE *fp)
268 cm_return_val_if_fail(fp != NULL, NULL);
270 headers = g_ptr_array_new();
272 while (procheader_get_one_field_asis(&buf, fp) != -1 && buf != NULL) {
273 if ((header = procheader_parse_header(buf)) != NULL)
274 g_ptr_array_add(headers, header);
282 void procheader_header_array_destroy(GPtrArray *harray)
287 cm_return_if_fail(harray != NULL);
289 for (i = 0; i < harray->len; i++) {
290 header = g_ptr_array_index(harray, i);
291 procheader_header_free(header);
294 g_ptr_array_free(harray, TRUE);
297 void procheader_header_free(Header *header)
301 g_free(header->name);
302 g_free(header->body);
307 tests whether two headers' names are equal
308 remove the trailing ':' or ' ' before comparing
311 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
318 if (hdr1[len1 - 1] == ':')
320 if (hdr2[len2 - 1] == ':')
325 return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
329 parse headers, for example :
330 From: dinh@enseirb.fr becomes :
331 header->name = "From:"
332 header->body = "dinh@enseirb.fr"
334 static gboolean header_is_addr_field(const gchar *hdr)
336 static char *addr_headers[] = {
343 "Followup-and-Reply-To:",
344 "Disposition-Notification-To:",
345 "Return-Receipt-To:",
352 for (i = 0; addr_headers[i] != NULL; i++)
353 if (!strcasecmp(hdr, addr_headers[i]))
359 Header * procheader_parse_header(gchar * buf)
363 gboolean addr_field = FALSE;
365 cm_return_val_if_fail(buf != NULL, NULL);
367 if ((*buf == ':') || (*buf == ' '))
370 for (p = buf; *p ; p++) {
371 if ((*p == ':') || (*p == ' ')) {
372 header = g_new(Header, 1);
373 header->name = g_strndup(buf, p - buf + 1);
374 addr_field = header_is_addr_field(header->name);
376 while (*p == ' ' || *p == '\t') p++;
377 header->body = conv_unmime_header(p, NULL, addr_field);
384 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
391 if (hentry == NULL) return;
393 while ((hnum = procheader_get_one_field(&buf, fp, hentry)) != -1 && buf != NULL) {
396 p = buf + strlen(hp->name);
397 while (*p == ' ' || *p == '\t') p++;
399 if (hp->body == NULL)
400 hp->body = g_strdup(p);
401 else if (procheader_headername_equal(hp->name, "To") ||
402 procheader_headername_equal(hp->name, "Cc")) {
403 gchar *tp = hp->body;
404 hp->body = g_strconcat(tp, ", ", p, NULL);
412 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
413 gboolean full, gboolean decrypted)
419 if (g_stat(file, &s) < 0) {
420 FILE_OP_ERROR(file, "stat");
423 if (!S_ISREG(s.st_mode))
426 if ((fp = g_fopen(file, "rb")) == NULL) {
427 FILE_OP_ERROR(file, "fopen");
431 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
435 msginfo->size = s.st_size;
436 msginfo->mtime = s.st_mtime;
442 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
445 return parse_stream(&str, TRUE, flags, full, decrypted);
463 H_SC_PLANNED_DOWNLOAD,
467 H_DISPOSITION_NOTIFICATION_TO,
469 H_SC_PARTIALLY_RETRIEVED,
481 static HeaderEntry hentry_full[] = {
482 {"Date:", NULL, FALSE},
483 {"From:", NULL, TRUE},
486 {"Newsgroups:", NULL, TRUE},
487 {"Subject:", NULL, TRUE},
488 {"Message-ID:", NULL, FALSE},
489 {"References:", NULL, FALSE},
490 {"In-Reply-To:", NULL, FALSE},
491 {"Content-Type:", NULL, FALSE},
492 {"Seen:", NULL, FALSE},
493 {"Status:", NULL, FALSE},
494 {"From ", NULL, FALSE},
495 {"SC-Marked-For-Download:", NULL, FALSE},
496 {"SC-Message-Size:", NULL, FALSE},
497 {"Face:", NULL, FALSE},
498 {"X-Face:", NULL, FALSE},
499 {"Disposition-Notification-To:", NULL, FALSE},
500 {"Return-Receipt-To:", NULL, FALSE},
501 {"SC-Partially-Retrieved:", NULL, FALSE},
502 {"SC-Account-Server:", NULL, FALSE},
503 {"SC-Account-Login:",NULL, FALSE},
504 {"List-Post:", NULL, TRUE},
505 {"List-Subscribe:", NULL, TRUE},
506 {"List-Unsubscribe:",NULL, TRUE},
507 {"List-Help:", NULL, TRUE},
508 {"List-Archive:", NULL, TRUE},
509 {"List-Owner:", NULL, TRUE},
510 {"Resent-From:", NULL, TRUE},
511 {NULL, NULL, FALSE}};
513 static HeaderEntry hentry_short[] = {
514 {"Date:", NULL, FALSE},
515 {"From:", NULL, TRUE},
518 {"Newsgroups:", NULL, TRUE},
519 {"Subject:", NULL, TRUE},
520 {"Message-ID:", NULL, FALSE},
521 {"References:", NULL, FALSE},
522 {"In-Reply-To:", NULL, FALSE},
523 {"Content-Type:", NULL, FALSE},
524 {"Seen:", NULL, FALSE},
525 {"Status:", NULL, FALSE},
526 {"From ", NULL, FALSE},
527 {"SC-Marked-For-Download:", NULL, FALSE},
528 {"SC-Message-Size:",NULL, FALSE},
529 {NULL, NULL, FALSE}};
531 static HeaderEntry* procheader_get_headernames(gboolean full)
533 return full ? hentry_full : hentry_short;
536 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
539 return parse_stream(fp, FALSE, flags, full, decrypted);
542 static gboolean avatar_from_some_face(gpointer source, gpointer userdata)
544 AvatarCaptureData *acd = (AvatarCaptureData *)source;
546 if (*(acd->content) == '\0') /* won't be null, but may be empty */
549 if (!strcmp(acd->header, hentry_full[H_FACE].name)) {
550 debug_print("avatar_from_some_face: found 'Face' header\n");
551 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_FACE, acd->content);
554 else if (!strcmp(acd->header, hentry_full[H_X_FACE].name)) {
555 debug_print("avatar_from_some_face: found 'X-Face' header\n");
556 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_XFACE, acd->content);
562 static guint avatar_hook_id = 0;
564 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
565 gboolean full, gboolean decrypted)
573 void *orig_data = data;
575 get_one_field_func get_one_field =
576 isstring ? (get_one_field_func)string_get_one_field
577 : (get_one_field_func)procheader_get_one_field;
579 hentry = procheader_get_headernames(full);
581 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
582 while (get_one_field(&buf, data, NULL) != -1 && buf != NULL) {
583 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
584 strlen("X-Claws-End-Special-Headers:"))) ||
585 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
586 strlen("X-Sylpheed-End-Special-Headers:"))))
588 /* from other mailers */
589 if (!strncmp(buf, "Date: ", 6)
590 || !strncmp(buf, "To: ", 4)
591 || !strncmp(buf, "From: ", 6)
592 || !strncmp(buf, "Subject: ", 9)) {
596 rewind((FILE *)data);
606 msginfo = procmsg_msginfo_new();
608 if (flags.tmp_flags || flags.perm_flags)
609 msginfo->flags = flags;
611 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
613 msginfo->inreplyto = NULL;
615 if (avatar_hook_id == 0 && (prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
616 avatar_hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_from_some_face, NULL);
617 } else if (avatar_hook_id != 0 && !(prefs_common.enable_avatars & AVATARS_ENABLE_CAPTURE)) {
618 hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_hook_id);
622 while ((hnum = get_one_field(&buf, data, hentry)) != -1 && buf != NULL) {
623 hp = buf + strlen(hentry[hnum].name);
624 while (*hp == ' ' || *hp == '\t') hp++;
628 if (msginfo->date) break;
630 procheader_date_parse(NULL, hp, 0);
631 if (g_utf8_validate(hp, -1, NULL)) {
632 msginfo->date = g_strdup(hp);
634 gchar *utf = conv_codeset_strdup(
636 conv_get_locale_charset_str_no_utf8(),
639 !g_utf8_validate(utf, -1, NULL)) {
641 utf = g_malloc(strlen(buf)*2+1);
642 conv_localetodisp(utf,
649 if (msginfo->from) break;
650 msginfo->from = conv_unmime_header(hp, NULL, TRUE);
651 msginfo->fromname = procheader_get_fromname(msginfo->from);
652 remove_return(msginfo->from);
653 remove_return(msginfo->fromname);
656 tmp = conv_unmime_header(hp, NULL, TRUE);
661 g_strconcat(p, ", ", tmp, NULL);
664 msginfo->to = g_strdup(tmp);
668 tmp = conv_unmime_header(hp, NULL, TRUE);
673 g_strconcat(p, ", ", tmp, NULL);
676 msginfo->cc = g_strdup(tmp);
680 if (msginfo->newsgroups) {
681 p = msginfo->newsgroups;
682 msginfo->newsgroups =
683 g_strconcat(p, ",", hp, NULL);
686 msginfo->newsgroups = g_strdup(hp);
689 if (msginfo->subject) break;
690 msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
691 unfold_line(msginfo->subject);
694 if (msginfo->msgid) break;
696 extract_parenthesis(hp, '<', '>');
698 msginfo->msgid = g_strdup(hp);
701 msginfo->references =
702 references_list_prepend(msginfo->references,
706 if (msginfo->inreplyto) break;
708 eliminate_parenthesis(hp, '(', ')');
709 if ((p = strrchr(hp, '<')) != NULL &&
710 strchr(p + 1, '>') != NULL) {
711 extract_parenthesis(p, '<', '>');
714 msginfo->inreplyto = g_strdup(p);
718 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
719 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
721 case H_DISPOSITION_NOTIFICATION_TO:
722 if (!msginfo->extradata)
723 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
724 if (msginfo->extradata->dispositionnotificationto) break;
725 msginfo->extradata->dispositionnotificationto = g_strdup(hp);
727 case H_RETURN_RECEIPT_TO:
728 if (!msginfo->extradata)
729 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
730 if (msginfo->extradata->returnreceiptto) break;
731 msginfo->extradata->returnreceiptto = g_strdup(hp);
733 /* partial download infos */
734 case H_SC_PARTIALLY_RETRIEVED:
735 if (!msginfo->extradata)
736 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
737 if (msginfo->extradata->partial_recv) break;
738 msginfo->extradata->partial_recv = g_strdup(hp);
740 case H_SC_ACCOUNT_SERVER:
741 if (!msginfo->extradata)
742 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
743 if (msginfo->extradata->account_server) break;
744 msginfo->extradata->account_server = g_strdup(hp);
746 case H_SC_ACCOUNT_LOGIN:
747 if (!msginfo->extradata)
748 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
749 if (msginfo->extradata->account_login) break;
750 msginfo->extradata->account_login = g_strdup(hp);
752 case H_SC_MESSAGE_SIZE:
753 if (msginfo->total_size) break;
754 msginfo->total_size = atoi(hp);
756 case H_SC_PLANNED_DOWNLOAD:
757 msginfo->planned_download = atoi(hp);
759 /* end partial download infos */
761 if (msginfo->fromspace) break;
762 msginfo->fromspace = g_strdup(hp);
763 remove_return(msginfo->fromspace);
767 if (!msginfo->extradata)
768 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
769 if (msginfo->extradata->list_post) break;
770 msginfo->extradata->list_post = g_strdup(hp);
772 case H_LIST_SUBSCRIBE:
773 if (!msginfo->extradata)
774 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
775 if (msginfo->extradata->list_subscribe) break;
776 msginfo->extradata->list_subscribe = g_strdup(hp);
778 case H_LIST_UNSUBSCRIBE:
779 if (!msginfo->extradata)
780 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
781 if (msginfo->extradata->list_unsubscribe) break;
782 msginfo->extradata->list_unsubscribe = g_strdup(hp);
785 if (!msginfo->extradata)
786 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
787 if (msginfo->extradata->list_help) break;
788 msginfo->extradata->list_help = g_strdup(hp);
791 if (!msginfo->extradata)
792 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
793 if (msginfo->extradata->list_archive) break;
794 msginfo->extradata->list_archive = g_strdup(hp);
797 if (!msginfo->extradata)
798 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
799 if (msginfo->extradata->list_owner) break;
800 msginfo->extradata->list_owner = g_strdup(hp);
803 if (!msginfo->extradata)
804 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
805 if (msginfo->extradata->resent_from) break;
806 msginfo->extradata->resent_from = g_strdup(hp);
812 /* to avoid performance penalty hooklist is invoked only for
813 headers known to be able to generate avatars */
814 if (hnum == H_FROM || hnum == H_X_FACE || hnum == H_FACE) {
815 AvatarCaptureData *acd = g_new0(AvatarCaptureData, 1);
816 /* no extra memory is wasted, hooks are expected to
817 take care of copying members when needed */
818 acd->msginfo = msginfo;
819 acd->header = hentry_full[hnum].name;
821 hooks_invoke(AVATAR_HEADER_UPDATE_HOOKLIST, (gpointer)acd);
828 if (!msginfo->inreplyto && msginfo->references)
830 g_strdup((gchar *)msginfo->references->data);
835 gchar *procheader_get_fromname(const gchar *str)
839 Xstrdup_a(tmp, str, return NULL);
842 extract_quote(tmp, '\"');
844 } else if (strchr(tmp, '<')) {
845 eliminate_parenthesis(tmp, '<', '>');
849 extract_parenthesis(tmp, '<', '>');
852 } else if (strchr(tmp, '(')) {
853 extract_parenthesis(tmp, '(', ')');
858 name = g_strdup(str);
860 name = g_strdup(tmp);
865 static gint procheader_scan_date_string(const gchar *str,
866 gchar *weekday, gint *day,
867 gchar *month, gint *year,
868 gint *hh, gint *mm, gint *ss,
874 gint zone1 = 0, zone2 = 0;
875 gchar offset_sign, zonestr[7];
881 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %6s",
882 weekday, day, month, year, hh, mm, ss, zone);
883 if (result == 8) return 0;
886 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %6s",
887 weekday, day, month, year, hh, mm, ss, zone);
888 if (result == 8) return 0;
890 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %6s",
891 day, month, year, hh, mm, ss, zone);
892 if (result == 7) return 0;
895 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
896 weekday, day, month, year, hh, mm, ss);
897 if (result == 7) return 0;
899 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
900 day, month, year, hh, mm, ss);
901 if (result == 6) return 0;
904 result = sscanf(str, "%10s %d %9s %d %2d:%2d %6s",
905 weekday, day, month, year, hh, mm, zone);
906 if (result == 7) return 0;
908 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
909 day, month, year, hh, mm, zone);
910 if (result == 6) return 0;
913 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
914 weekday, day, month, year, hh, mm);
915 if (result == 6) return 0;
917 result = sscanf(str, "%d %9s %d %2d:%2d",
918 day, month, year, hh, mm);
919 if (result == 5) return 0;
923 /* RFC3339 subset, with fraction of second */
924 result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d.%d%6s",
925 year, &month_n, day, &sep1, hh, mm, ss, &secfract, zonestr);
927 && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
928 if (month_n >= 1 && month_n <= 12) {
929 strncpy2(month, monthstr+((month_n-1)*3), 4);
930 if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
931 strcat(zone, "+00:00");
932 } else if (sscanf(zonestr, "%c%2d:%2d",
933 &offset_sign, &zone1, &zone2) == 3) {
934 strcat(zone, zonestr);
940 /* RFC3339 subset, no fraction of second */
941 result = sscanf(str, "%4d-%2d-%2d%c%2d:%2d:%2d%6s",
942 year, &month_n, day, &sep1, hh, mm, ss, zonestr);
944 && (sep1 == 'T' || sep1 == 't' || sep1 == ' ')) {
945 if (month_n >= 1 && month_n <= 12) {
946 strncpy2(month, monthstr+((month_n-1)*3), 4);
947 if (zonestr[0] == 'z' || zonestr[0] == 'Z') {
948 strcat(zone, "+00:00");
949 } else if (sscanf(zonestr, "%c%2d:%2d",
950 &offset_sign, &zone1, &zone2) == 3) {
951 strcat(zone, zonestr);
960 /* This particular "subset" is invalid, RFC requires the time offset */
961 result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d",
962 year, &month_n, day, hh, mm, ss);
964 if (1 <= month_n && month_n <= 12) {
965 strncpy2(month, monthstr+((month_n-1)*3), 4);
974 * Hiro, most UNIXen support this function:
975 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
977 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
990 memset(t, 0, sizeof *t);
992 if (procheader_scan_date_string(src, weekday, &day, month, &year,
993 &hh, &mm, &ss, zone) < 0) {
994 g_warning("Invalid date: %s", src);
998 /* Y2K compliant :) */
1007 if ((p = strstr(monthstr, month)) != NULL)
1008 dmonth = (gint)(p - monthstr) / 3 + 1;
1010 g_warning("Invalid month: %s", month);
1011 dmonth = G_DATE_BAD_MONTH;
1018 t->tm_mon = dmonth - 1;
1019 t->tm_year = year - 1900;
1029 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
1037 GDateMonth dmonth = G_DATE_BAD_MONTH;
1041 if (procheader_scan_date_string(src, weekday, &day, month, &year,
1042 &hh, &mm, &ss, zone) < 0) {
1043 if (dest && len > 0)
1044 strncpy2(dest, src, len);
1049 for (p = monthstr; *p != '\0'; p += 3) {
1050 if (!g_ascii_strncasecmp(p, month, 3)) {
1051 dmonth = (gint)(p - monthstr) / 3 + 1;
1060 tz = g_time_zone_new(zone);
1061 dt = g_date_time_new(tz, year, dmonth, day, hh, mm, ss);
1063 timer = g_date_time_to_unix(dt);
1065 g_date_time_unref(dt);
1066 g_time_zone_unref(tz);
1072 /* Y2K compliant :) */
1084 t.tm_mon = dmonth - 1;
1085 t.tm_year = year - 1900;
1091 tz_offset = remote_tzoffset_sec(zone);
1092 if (tz_offset != -1)
1093 timer += tzoffset_sec(&timer) - tz_offset;
1096 procheader_date_get_localtime(dest, len, timer);
1102 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
1105 gchar *default_format = "%y/%m/%d(%a) %H:%M";
1107 const gchar *src_codeset, *dest_codeset;
1111 lt = localtime_r(&timer, &buf);
1114 lt = localtime_r(&dummy, &buf);
1117 if (prefs_common.date_format)
1118 fast_strftime(dest, len, prefs_common.date_format, lt);
1120 fast_strftime(dest, len, default_format, lt);
1122 if (!g_utf8_validate(dest, -1, NULL)) {
1123 src_codeset = conv_get_locale_charset_str_no_utf8();
1124 dest_codeset = CS_UTF_8;
1125 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
1127 strncpy2(dest, str, len);
1133 /* Added by Mel Hadasht on 27 Aug 2001 */
1134 /* Get a header from msginfo */
1135 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar **buf, gchar *header)
1139 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
1140 { NULL, NULL, FALSE } };
1143 cm_return_val_if_fail(msginfo != NULL, -1);
1144 cm_return_val_if_fail(buf != NULL, -1);
1145 cm_return_val_if_fail(header != NULL, -1);
1147 hentry[0].name = header;
1149 file = procmsg_get_message_file_path(msginfo);
1150 if ((fp = g_fopen(file, "rb")) == NULL) {
1151 FILE_OP_ERROR(file, "fopen");
1157 val = procheader_get_one_field(buf, fp, hentry);
1159 if (fclose(fp) == EOF) {
1160 FILE_OP_ERROR(file, "fclose");
1170 /* *buf is already NULL in that case, see procheader_get_one_field() */
1177 HeaderEntry *procheader_entries_from_str(const gchar *str)
1179 HeaderEntry *entries = NULL, *he;
1180 int numh = 0, i = 0;
1181 gchar **names = NULL;
1182 const gchar *s = str;
1187 while (*s != '\0') {
1188 if (*s == ' ') ++numh;
1194 entries = g_new0(HeaderEntry, numh + 1); /* room for last NULL */
1196 ++s; /* skip first space */
1197 names = g_strsplit(s, " ", numh);
1200 he->name = g_strdup_printf("%s:", names[i]);
1210 void procheader_entries_free (HeaderEntry *entries)
1212 if (entries != NULL) {
1213 HeaderEntry *he = entries;
1214 while (he->name != NULL) {
1216 if (he->body != NULL)
1224 gboolean procheader_header_is_internal(const gchar *hdr_name)
1226 const gchar *internal_hdrs[] = {
1227 "AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:", "CFG:",
1228 "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:", "R:", "MAID:",
1229 "SCF:", "RMID:", "FMID:", "NAID:",
1230 "X-Claws-Account-Id:",
1233 "X-Claws-Privacy-System:",
1234 "X-Claws-Auto-Wrapping:",
1235 "X-Claws-Auto-Indent:",
1236 "X-Claws-End-Special-Headers:",
1237 "X-Sylpheed-Account-Id:",
1239 "X-Sylpheed-Encrypt:",
1240 "X-Sylpheed-Privacy-System:",
1241 "X-Sylpheed-End-Special-Headers:",
1246 for (i = 0; internal_hdrs[i]; i++) {
1247 if (!strcmp(hdr_name, internal_hdrs[i]))