2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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 *, size_t, void *, HeaderEntry[]);
54 static gint string_get_one_field(gchar *buf, size_t len, 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 *buf, size_t len, 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, size_t len, FILE *fp,
72 return generic_get_one_field(buf, len, fp, hentry,
73 (getlinefunc)fgets_crlf, (peekcharfunc)file_peekchar,
77 static gint string_get_one_field(gchar *buf, size_t len, char **str,
80 return generic_get_one_field(buf, len, 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 *buf, size_t len, void *data,
124 getlinefunc getline, peekcharfunc peekchar,
129 HeaderEntry *hp = NULL;
131 if (hentry != NULL) {
132 /* skip non-required headers */
135 if (getline(buf, len, data) == NULL)
137 if (buf[0] == '\r' || buf[0] == '\n')
139 } while (buf[0] == ' ' || buf[0] == '\t');
141 for (hp = hentry, hnum = 0; hp->name != NULL;
143 if (!g_ascii_strncasecmp(hp->name, buf,
147 } while (hp->name == NULL);
149 if (getline(buf, len, data) == NULL) return -1;
150 if (buf[0] == '\r' || buf[0] == '\n') return -1;
155 nexthead = peekchar(data);
156 /* ([*WSP CRLF] 1*WSP) */
157 if (nexthead == ' ' || nexthead == '\t') {
159 gboolean skiptab = (nexthead == '\t');
160 /* trim previous trailing \n if requesting one header or
161 * unfolding was requested */
162 if ((!hentry && unfold) || (hp && hp->unfold))
165 buflen = strlen(buf);
167 /* concatenate next line */
168 if ((len - buflen) > 2) {
169 if (getline(buf + buflen, len - buflen, data) == NULL)
171 if (skiptab) { /* replace tab with space */
172 *(buf + buflen) = ' ';
177 /* remove trailing new line */
186 gint procheader_get_one_field_asis(gchar *buf, size_t len, FILE *fp)
188 return generic_get_one_field(buf, len, fp, NULL,
189 (getlinefunc)fgets_crlf,
190 (peekcharfunc)file_peekchar,
194 GPtrArray *procheader_get_header_array_asis(FILE *fp)
200 cm_return_val_if_fail(fp != NULL, NULL);
202 headers = g_ptr_array_new();
204 while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
205 if ((header = procheader_parse_header(buf)) != NULL)
206 g_ptr_array_add(headers, header);
212 void procheader_header_array_destroy(GPtrArray *harray)
217 for (i = 0; i < harray->len; i++) {
218 header = g_ptr_array_index(harray, i);
219 procheader_header_free(header);
222 g_ptr_array_free(harray, TRUE);
225 void procheader_header_free(Header *header)
229 g_free(header->name);
230 g_free(header->body);
235 tests whether two headers' names are equal
236 remove the trailing ':' or ' ' before comparing
239 gboolean procheader_headername_equal(char * hdr1, char * hdr2)
246 if (hdr1[len1 - 1] == ':')
248 if (hdr2[len2 - 1] == ':')
253 return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
257 parse headers, for example :
258 From: dinh@enseirb.fr becomes :
259 header->name = "From:"
260 header->body = "dinh@enseirb.fr"
262 static gboolean header_is_addr_field(const gchar *hdr)
264 static char *addr_headers[] = {
271 "Followup-and-Reply-To:",
272 "Disposition-Notification-To:",
273 "Return-Receipt-To:",
280 for (i = 0; addr_headers[i] != NULL; i++)
281 if (!strcasecmp(hdr, addr_headers[i]))
287 Header * procheader_parse_header(gchar * buf)
291 gboolean addr_field = FALSE;
293 if ((*buf == ':') || (*buf == ' '))
296 for (p = buf; *p ; p++) {
297 if ((*p == ':') || (*p == ' ')) {
298 header = g_new(Header, 1);
299 header->name = g_strndup(buf, p - buf + 1);
300 addr_field = header_is_addr_field(header->name);
302 while (*p == ' ' || *p == '\t') p++;
303 header->body = conv_unmime_header(p, NULL, addr_field);
310 void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
317 if (hentry == NULL) return;
319 while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
323 p = buf + strlen(hp->name);
324 while (*p == ' ' || *p == '\t') p++;
326 if (hp->body == NULL)
327 hp->body = g_strdup(p);
328 else if (procheader_headername_equal(hp->name, "To") ||
329 procheader_headername_equal(hp->name, "Cc")) {
330 gchar *tp = hp->body;
331 hp->body = g_strconcat(tp, ", ", p, NULL);
337 MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
338 gboolean full, gboolean decrypted)
344 if (g_stat(file, &s) < 0) {
345 FILE_OP_ERROR(file, "stat");
348 if (!S_ISREG(s.st_mode))
351 if ((fp = g_fopen(file, "rb")) == NULL) {
352 FILE_OP_ERROR(file, "fopen");
356 msginfo = procheader_parse_stream(fp, flags, full, decrypted);
360 msginfo->size = s.st_size;
361 msginfo->mtime = s.st_mtime;
367 MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
370 return parse_stream(&str, TRUE, flags, full, decrypted);
389 H_SC_PLANNED_DOWNLOAD = 14,
390 H_SC_MESSAGE_SIZE = 15,
393 H_DISPOSITION_NOTIFICATION_TO = 18,
394 H_RETURN_RECEIPT_TO = 19,
395 H_SC_PARTIALLY_RETRIEVED = 20,
396 H_SC_ACCOUNT_SERVER = 21,
397 H_SC_ACCOUNT_LOGIN = 22,
399 H_LIST_SUBSCRIBE = 24,
400 H_LIST_UNSUBSCRIBE = 25,
406 static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
407 {"From:", NULL, TRUE},
410 {"Newsgroups:", NULL, TRUE},
411 {"Subject:", NULL, TRUE},
412 {"Message-ID:", NULL, FALSE},
413 {"References:", NULL, FALSE},
414 {"In-Reply-To:", NULL, FALSE},
415 {"Content-Type:", NULL, FALSE},
416 {"Seen:", NULL, FALSE},
417 {"Status:", NULL, FALSE},
418 {"X-Status:", NULL, FALSE},
419 {"From ", NULL, FALSE},
420 {"SC-Marked-For-Download:", NULL, FALSE},
421 {"SC-Message-Size:", NULL, FALSE},
422 {"Face:", NULL, FALSE},
423 {"X-Face:", NULL, FALSE},
424 {"Disposition-Notification-To:", NULL, FALSE},
425 {"Return-Receipt-To:", NULL, FALSE},
426 {"SC-Partially-Retrieved:", NULL, FALSE},
427 {"SC-Account-Server:", NULL, FALSE},
428 {"SC-Account-Login:",NULL, FALSE},
429 {"List-Post:", NULL, TRUE},
430 {"List-Subscribe:", NULL, TRUE},
431 {"List-Unsubscribe:",NULL, TRUE},
432 {"List-Help:", NULL, TRUE},
433 {"List-Archive:", NULL, TRUE},
434 {"List-Owner:", NULL, TRUE},
435 {NULL, NULL, FALSE}};
437 static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
438 {"From:", NULL, TRUE},
441 {"Newsgroups:", NULL, TRUE},
442 {"Subject:", NULL, TRUE},
443 {"Message-ID:", NULL, FALSE},
444 {"References:", NULL, FALSE},
445 {"In-Reply-To:", NULL, FALSE},
446 {"Content-Type:", NULL, FALSE},
447 {"Seen:", NULL, FALSE},
448 {"Status:", NULL, FALSE},
449 {"X-Status:", NULL, FALSE},
450 {"From ", NULL, FALSE},
451 {"SC-Marked-For-Download:", NULL, FALSE},
452 {"SC-Message-Size:",NULL, FALSE},
453 {NULL, NULL, FALSE}};
455 static HeaderEntry* procheader_get_headernames(gboolean full)
457 return full ? hentry_full : hentry_short;
460 MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
463 return parse_stream(fp, FALSE, flags, full, decrypted);
466 static gboolean avatar_from_some_face(gpointer source, gpointer userdata)
468 AvatarCaptureData *acd = (AvatarCaptureData *)source;
470 if (*(acd->content) == '\0') /* won't be null, but may be empty */
473 if (!strcmp(acd->header, hentry_full[H_FACE].name)) {
474 debug_print("avatar_from_some_face: found 'Face' header\n");
475 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_FACE, acd->content);
478 else if (!strcmp(acd->header, hentry_full[H_X_FACE].name)) {
479 debug_print("avatar_from_some_face: found 'X-Face' header\n");
480 procmsg_msginfo_add_avatar(acd->msginfo, AVATAR_XFACE, acd->content);
486 static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
487 gboolean full, gboolean decrypted)
495 void *orig_data = data;
498 get_one_field_func get_one_field =
499 isstring ? (get_one_field_func)string_get_one_field
500 : (get_one_field_func)procheader_get_one_field;
502 hentry = procheader_get_headernames(full);
504 if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
505 while (get_one_field(buf, sizeof(buf), data, NULL) != -1) {
506 if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
507 strlen("X-Claws-End-Special-Headers:"))) ||
508 (!strncmp(buf, "X-Sylpheed-End-Special-Headers: 1",
509 strlen("X-Sylpheed-End-Special-Headers:"))))
511 /* from other mailers */
512 if (!strncmp(buf, "Date: ", 6)
513 || !strncmp(buf, "To: ", 4)
514 || !strncmp(buf, "From: ", 6)
515 || !strncmp(buf, "Subject: ", 9)) {
519 rewind((FILE *)data);
525 msginfo = procmsg_msginfo_new();
527 if (flags.tmp_flags || flags.perm_flags)
528 msginfo->flags = flags;
530 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
532 msginfo->inreplyto = NULL;
534 if (prefs_common.enable_avatars | AVATARS_ENABLE_CAPTURE) {
535 hook_id = hooks_register_hook(AVATAR_HEADER_UPDATE_HOOKLIST, avatar_from_some_face, NULL);
538 while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
540 hp = buf + strlen(hentry[hnum].name);
541 while (*hp == ' ' || *hp == '\t') hp++;
545 if (msginfo->date) break;
547 procheader_date_parse(NULL, hp, 0);
548 if (g_utf8_validate(hp, -1, NULL)) {
549 msginfo->date = g_strdup(hp);
551 gchar *utf = conv_codeset_strdup(
553 conv_get_locale_charset_str_no_utf8(),
556 !g_utf8_validate(utf, -1, NULL)) {
558 utf = g_malloc(strlen(buf)*2+1);
559 conv_localetodisp(utf,
566 if (msginfo->from) break;
567 msginfo->from = conv_unmime_header(hp, NULL, TRUE);
568 msginfo->fromname = procheader_get_fromname(msginfo->from);
569 remove_return(msginfo->from);
570 remove_return(msginfo->fromname);
573 tmp = conv_unmime_header(hp, NULL, TRUE);
578 g_strconcat(p, ", ", tmp, NULL);
581 msginfo->to = g_strdup(tmp);
585 tmp = conv_unmime_header(hp, NULL, TRUE);
590 g_strconcat(p, ", ", tmp, NULL);
593 msginfo->cc = g_strdup(tmp);
597 if (msginfo->newsgroups) {
598 p = msginfo->newsgroups;
599 msginfo->newsgroups =
600 g_strconcat(p, ",", hp, NULL);
603 msginfo->newsgroups = g_strdup(hp);
606 if (msginfo->subject) break;
607 msginfo->subject = conv_unmime_header(hp, NULL, FALSE);
608 unfold_line(msginfo->subject);
611 if (msginfo->msgid) break;
613 extract_parenthesis(hp, '<', '>');
615 msginfo->msgid = g_strdup(hp);
618 msginfo->references =
619 references_list_prepend(msginfo->references,
623 if (msginfo->inreplyto) break;
625 eliminate_parenthesis(hp, '(', ')');
626 if ((p = strrchr(hp, '<')) != NULL &&
627 strchr(p + 1, '>') != NULL) {
628 extract_parenthesis(p, '<', '>');
631 msginfo->inreplyto = g_strdup(p);
635 if (!g_ascii_strncasecmp(hp, "multipart/", 10))
636 MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
638 #ifdef ALLOW_HEADER_HINT
640 /* mnews Seen header */
641 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
644 case H_DISPOSITION_NOTIFICATION_TO:
645 if (!msginfo->extradata)
646 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
647 if (msginfo->extradata->dispositionnotificationto) break;
648 msginfo->extradata->dispositionnotificationto = g_strdup(hp);
650 case H_RETURN_RECEIPT_TO:
651 if (!msginfo->extradata)
652 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
653 if (msginfo->extradata->returnreceiptto) break;
654 msginfo->extradata->returnreceiptto = g_strdup(hp);
656 /* partial download infos */
657 case H_SC_PARTIALLY_RETRIEVED:
658 if (!msginfo->extradata)
659 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
660 if (msginfo->extradata->partial_recv) break;
661 msginfo->extradata->partial_recv = g_strdup(hp);
663 case H_SC_ACCOUNT_SERVER:
664 if (!msginfo->extradata)
665 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
666 if (msginfo->extradata->account_server) break;
667 msginfo->extradata->account_server = g_strdup(hp);
669 case H_SC_ACCOUNT_LOGIN:
670 if (!msginfo->extradata)
671 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
672 if (msginfo->extradata->account_login) break;
673 msginfo->extradata->account_login = g_strdup(hp);
675 case H_SC_MESSAGE_SIZE:
676 if (msginfo->total_size) break;
677 msginfo->total_size = atoi(hp);
679 case H_SC_PLANNED_DOWNLOAD:
680 msginfo->planned_download = atoi(hp);
682 /* end partial download infos */
683 #ifdef ALLOW_HEADER_HINT
685 if (strchr(hp, 'R') != NULL)
686 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
687 if (strchr(hp, 'O') != NULL)
688 MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
689 if (strchr(hp, 'U') != NULL)
690 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
693 if (strchr(hp, 'D') != NULL)
694 MSG_SET_PERM_FLAGS(msginfo->flags,
696 if (strchr(hp, 'F') != NULL)
697 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
698 if (strchr(hp, 'd') != NULL)
699 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
700 if (strchr(hp, 'r') != NULL)
701 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
702 if (strchr(hp, 'f') != NULL)
703 MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
707 if (msginfo->fromspace) break;
708 msginfo->fromspace = g_strdup(hp);
709 remove_return(msginfo->fromspace);
713 if (!msginfo->extradata)
714 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
715 if (msginfo->extradata->list_post) break;
716 msginfo->extradata->list_post = g_strdup(hp);
718 case H_LIST_SUBSCRIBE:
719 if (!msginfo->extradata)
720 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
721 if (msginfo->extradata->list_subscribe) break;
722 msginfo->extradata->list_subscribe = g_strdup(hp);
724 case H_LIST_UNSUBSCRIBE:
725 if (!msginfo->extradata)
726 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
727 if (msginfo->extradata->list_unsubscribe) break;
728 msginfo->extradata->list_unsubscribe = g_strdup(hp);
731 if (!msginfo->extradata)
732 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
733 if (msginfo->extradata->list_help) break;
734 msginfo->extradata->list_help = g_strdup(hp);
737 if (!msginfo->extradata)
738 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
739 if (msginfo->extradata->list_archive) break;
740 msginfo->extradata->list_archive = g_strdup(hp);
743 if (!msginfo->extradata)
744 msginfo->extradata = g_new0(MsgInfoExtraData, 1);
745 if (msginfo->extradata->list_owner) break;
746 msginfo->extradata->list_owner = g_strdup(hp);
752 /* to avoid performance penalty hooklist is invoked only for
753 headers known to be able to generate avatars */
754 if (hnum == H_FROM || hnum == H_X_FACE || hnum == H_FACE) {
755 AvatarCaptureData *acd = g_new0(AvatarCaptureData, 1);
756 /* no extra memory is wasted, hooks are expected to
757 take care of copying members when needed */
758 acd->msginfo = msginfo;
759 acd->header = hentry_full[hnum].name;
761 hooks_invoke(AVATAR_HEADER_UPDATE_HOOKLIST, (gpointer)acd);
766 if (!msginfo->inreplyto && msginfo->references)
768 g_strdup((gchar *)msginfo->references->data);
771 hooks_unregister_hook(AVATAR_HEADER_UPDATE_HOOKLIST, hook_id);
777 gchar *procheader_get_fromname(const gchar *str)
781 Xstrdup_a(tmp, str, return NULL);
784 extract_quote(tmp, '\"');
786 } else if (strchr(tmp, '<')) {
787 eliminate_parenthesis(tmp, '<', '>');
791 extract_parenthesis(tmp, '<', '>');
794 } else if (strchr(tmp, '(')) {
795 extract_parenthesis(tmp, '(', ')');
800 name = g_strdup(str);
802 name = g_strdup(tmp);
807 static gint procheader_scan_date_string(const gchar *str,
808 gchar *weekday, gint *day,
809 gchar *month, gint *year,
810 gint *hh, gint *mm, gint *ss,
821 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
822 weekday, day, month, year, hh, mm, ss, zone);
823 if (result == 8) return 0;
826 result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
827 weekday, day, month, year, hh, mm, ss, zone);
828 if (result == 8) return 0;
830 result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
831 day, month, year, hh, mm, ss, zone);
832 if (result == 7) return 0;
835 result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
836 weekday, day, month, year, hh, mm, ss);
837 if (result == 7) return 0;
839 result = sscanf(str, "%d %9s %d %2d:%2d:%2d",
840 day, month, year, hh, mm, ss);
841 if (result == 6) return 0;
844 result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
845 weekday, day, month, year, hh, mm, zone);
846 if (result == 7) return 0;
848 result = sscanf(str, "%d %9s %d %2d:%2d %5s",
849 day, month, year, hh, mm, zone);
850 if (result == 6) return 0;
853 result = sscanf(str, "%10s %d %9s %d %2d:%2d",
854 weekday, day, month, year, hh, mm);
855 if (result == 6) return 0;
857 result = sscanf(str, "%d %9s %d %2d:%2d",
858 day, month, year, hh, mm);
859 if (result == 5) return 0;
863 result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d+%2s:%2s",
864 year, &month_n, day, hh, mm, ss, zone1, zone2);
866 if (1 <= month_n && month_n <= 12) {
867 strncpy2(month, monthstr+((month_n-1)*3), 4);
869 strncpy2(zone+1, zone1, 3);
870 strncpy2(zone+3, zone2, 3);
878 result = sscanf(str, "%4d-%2d-%2d %2d:%2d:%2d",
879 year, &month_n, day, hh, mm, ss);
881 if (1 <= month_n && month_n <= 12) {
882 strncpy2(month, monthstr+((month_n-1)*3), 4);
891 * Hiro, most UNIXen support this function:
892 * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
894 gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
907 memset(t, 0, sizeof *t);
909 if (procheader_scan_date_string(src, weekday, &day, month, &year,
910 &hh, &mm, &ss, zone) < 0) {
911 g_warning("Invalid date: %s\n", src);
915 /* Y2K compliant :) */
924 if ((p = strstr(monthstr, month)) != NULL)
925 dmonth = (gint)(p - monthstr) / 3 + 1;
927 g_warning("Invalid month: %s\n", month);
928 dmonth = G_DATE_BAD_MONTH;
935 t->tm_mon = dmonth - 1;
936 t->tm_year = year - 1900;
946 time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
954 GDateMonth dmonth = G_DATE_BAD_MONTH;
960 if (procheader_scan_date_string(src, weekday, &day, month, &year,
961 &hh, &mm, &ss, zone) < 0) {
963 strncpy2(dest, src, len);
967 /* Y2K compliant :) */
976 for (p = monthstr; *p != '\0'; p += 3) {
977 if (!g_ascii_strncasecmp(p, month, 3)) {
978 dmonth = (gint)(p - monthstr) / 3 + 1;
987 t.tm_mon = dmonth - 1;
988 t.tm_year = year - 1900;
994 tz_offset = remote_tzoffset_sec(zone);
996 timer += tzoffset_sec(&timer) - tz_offset;
999 procheader_date_get_localtime(dest, len, timer);
1004 void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
1007 gchar *default_format = "%y/%m/%d(%a) %H:%M";
1009 const gchar *src_codeset, *dest_codeset;
1013 lt = localtime_r(&timer, &buf);
1016 lt = localtime_r(&dummy, &buf);
1019 if (prefs_common.date_format)
1020 fast_strftime(dest, len, prefs_common.date_format, lt);
1022 fast_strftime(dest, len, default_format, lt);
1024 if (!g_utf8_validate(dest, -1, NULL)) {
1025 src_codeset = conv_get_locale_charset_str_no_utf8();
1026 dest_codeset = CS_UTF_8;
1027 str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
1029 strncpy2(dest, str, len);
1035 /* Added by Mel Hadasht on 27 Aug 2001 */
1036 /* Get a header from msginfo */
1037 gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
1041 HeaderEntry hentry[]={ { NULL, NULL, TRUE },
1042 { NULL, NULL, FALSE } };
1045 hentry[0].name = header;
1047 cm_return_val_if_fail(msginfo != NULL, -1);
1048 file = procmsg_get_message_file_path(msginfo);
1049 if ((fp = g_fopen(file, "rb")) == NULL) {
1050 FILE_OP_ERROR(file, "fopen");
1054 val = procheader_get_one_field(buf,len, fp, hentry);
1055 if (fclose(fp) == EOF) {
1056 FILE_OP_ERROR(file, "fclose");
1069 HeaderEntry *procheader_entries_from_str(const gchar *str)
1071 HeaderEntry *entries = NULL, *he;
1072 int numh = 0, i = 0;
1073 gchar **names = NULL;
1074 const gchar *s = str;
1079 while (*s != '\0') {
1080 if (*s == ' ') ++numh;
1086 entries = g_new0(HeaderEntry, numh + 1); /* room for last NULL */
1088 ++s; /* skip first space */
1089 names = g_strsplit(s, " ", numh);
1092 he->name = g_strdup_printf("%s:", names[i]);
1102 void procheader_entries_free (HeaderEntry *entries)
1104 if (entries != NULL) {
1105 HeaderEntry *he = entries;
1106 while (he->name != NULL) {
1108 if (he->body != NULL)