/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2003 Hiroyuki Yamamoto
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
for (hp = hentry, hnum = 0; hp->name != NULL;
hp++, hnum++) {
if (!strncasecmp(hp->name, buf,
- strlen(hp->name))) {
+ strlen(hp->name)))
break;
- }
}
} while (hp->name == NULL);
} else {
gboolean folded = FALSE;
gchar *bufp = buf + strlen(buf);
+ for (; bufp > buf &&
+ (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
+ bufp--)
+ *(bufp - 1) = '\0';
+
while (1) {
nexthead = fgetc(fp);
else if (nexthead == EOF)
break;
else if (folded == TRUE) {
- /* concatenate next line */
+ if (nexthead == '\r' || nexthead == '\n') {
+ folded = FALSE;
+ continue;
+ }
+
if ((len - (bufp - buf)) <= 2) break;
/* replace return code on the tail end
with space */
- *(bufp - 1) = ' ';
+ *bufp++ = ' ';
*bufp++ = nexthead;
*bufp = '\0';
- if (nexthead == '\r' || nexthead == '\n') {
- folded = FALSE;
- continue;
- }
+ /* concatenate next line */
if (fgets(bufp, len - (bufp - buf), fp)
== NULL) break;
bufp += strlen(bufp);
+ for (; bufp > buf &&
+ (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
+ bufp--)
+ *(bufp - 1) = '\0';
+
folded = FALSE;
} else {
ungetc(nexthead, fp);
}
}
- /* remove trailing return code */
- strretchomp(buf);
-
return hnum;
}
if (buf[0] == '\r' || buf[0] == '\n') return NULL;
bufp = buf + strlen(buf);
+ for (; bufp > buf &&
+ (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
+ bufp--)
+ *(bufp - 1) = '\0';
+
while (1) {
nexthead = fgetc(fp);
else if (nexthead == EOF)
break;
else if (folded == TRUE) {
- /* concatenate next line */
+ if (nexthead == '\r' || nexthead == '\n') {
+ folded = FALSE;
+ continue;
+ }
+
if ((len - (bufp - buf)) <= 2) break;
/* replace return code on the tail end
with space */
- *(bufp - 1) = ' ';
+ *bufp++ = ' ';
*bufp++ = nexthead;
*bufp = '\0';
- if (nexthead == '\r' || nexthead == '\n') {
- folded = FALSE;
- continue;
- }
+
+ /* concatenate next line */
if (fgets(bufp, len - (bufp - buf), fp)
== NULL) break;
bufp += strlen(bufp);
+ for (; bufp > buf &&
+ (*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
+ bufp--)
+ *(bufp - 1) = '\0';
+
folded = FALSE;
} else {
ungetc(nexthead, fp);
FILE *fp;
GSList *hlist;
- if ((fp = fopen(file, "r")) == NULL) {
+ if ((fp = fopen(file, "rb")) == NULL) {
FILE_OP_ERROR(file, "fopen");
return NULL;
}
GSList *procheader_get_header_list(FILE *fp)
{
- gchar buf[BUFFSIZE], tmp[BUFFSIZE];
- gchar *p;
+ gchar buf[BUFFSIZE];
GSList *hlist = NULL;
Header *header;
g_return_val_if_fail(fp != NULL, NULL);
while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
- if (header = procheader_parse_header(buf))
+ if ((header = procheader_parse_header(buf)) != NULL)
hlist = g_slist_append(hlist, header);
/*
if (*buf == ':') continue;
GPtrArray *procheader_get_header_array(FILE *fp)
{
- gchar buf[BUFFSIZE], tmp[BUFFSIZE];
- gchar *p;
+ gchar buf[BUFFSIZE];
GPtrArray *headers;
Header *header;
headers = g_ptr_array_new();
while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
- if (header = procheader_parse_header(buf))
+ if ((header = procheader_parse_header(buf)) != NULL)
g_ptr_array_add(headers, header);
/*
if (*buf == ':') continue;
GPtrArray *procheader_get_header_array_asis(FILE *fp)
{
- gchar buf[BUFFSIZE], tmp[BUFFSIZE];
- gchar *p;
+ gchar buf[BUFFSIZE];
GPtrArray *headers;
Header *header;
headers = g_ptr_array_new();
while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
- if (header = procheader_parse_header(buf))
+ if ((header = procheader_parse_header(buf)) != NULL)
g_ptr_array_add(headers, header);
/*
if (*buf == ':') continue;
p++;
while (*p == ' ' || *p == '\t') p++;
conv_unmime_header(tmp, sizeof(tmp), p, NULL);
- header->body = g_strdup(tmp);
+ if(tmp == NULL)
+ header->body = g_strdup(p);
+ else
+ header->body = g_strdup(tmp);
return header;
}
}
}
}
+MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
+ gboolean full, gboolean decrypted)
+{
+ FILE *fp;
+ MsgInfo *msginfo;
+
+ if ((fp = fopen(file, "rb")) == NULL) {
+ FILE_OP_ERROR(file, "fopen");
+ return NULL;
+ }
+
+ msginfo = procheader_parse_stream(fp, flags, full, decrypted);
+ fclose(fp);
+ return msginfo;
+}
+
+MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
+ gboolean decrypted)
+{
+ FILE *fp;
+ MsgInfo *msginfo;
+
+ if ((fp = str_open_as_stream(str)) == NULL)
+ return NULL;
+
+ msginfo = procheader_parse_stream(fp, flags, full, decrypted);
+ fclose(fp);
+ return msginfo;
+}
+
enum
{
H_DATE = 0,
H_RETURN_RECEIPT_TO = 16
};
-MsgInfo *procheader_parse(const gchar *file, MsgFlags flags, gboolean full)
+static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
+ {"From:", NULL, TRUE},
+ {"To:", NULL, TRUE},
+ {"Cc:", NULL, TRUE},
+ {"Newsgroups:", NULL, TRUE},
+ {"Subject:", NULL, TRUE},
+ {"Message-Id:", NULL, FALSE},
+ {"References:", NULL, FALSE},
+ {"In-Reply-To:", NULL, FALSE},
+ {"Content-Type:", NULL, FALSE},
+ {"Seen:", NULL, FALSE},
+ {"Status:", NULL, FALSE},
+ {"X-Status:", NULL, FALSE},
+ {"From ", NULL, FALSE},
+ {"X-Face:", NULL, FALSE},
+ {"Disposition-Notification-To:", NULL, FALSE},
+ {"Return-Receipt-To:", NULL, FALSE},
+ {NULL, NULL, FALSE}};
+
+static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
+ {"From:", NULL, TRUE},
+ {"To:", NULL, TRUE},
+ {"Cc:", NULL, TRUE},
+ {"Newsgroups:", NULL, TRUE},
+ {"Subject:", NULL, TRUE},
+ {"Message-Id:", NULL, FALSE},
+ {"References:", NULL, FALSE},
+ {"In-Reply-To:", NULL, FALSE},
+ {"Content-Type:", NULL, FALSE},
+ {"Seen:", NULL, FALSE},
+ {"Status:", NULL, FALSE},
+ {"X-Status:", NULL, FALSE},
+ {"From ", NULL, FALSE},
+ {NULL, NULL, FALSE}};
+
+HeaderEntry* procheader_get_headernames(gboolean full)
{
- FILE *fp;
- MsgInfo *msginfo;
-
- if ((fp = fopen(file, "r")) == NULL) {
- FILE_OP_ERROR(file, "fopen");
- return NULL;
- }
-
- msginfo = procheader_file_parse(fp, flags, full);
-
- fclose(fp);
-
- return msginfo;
+ return full ? hentry_full : hentry_short;
}
-
-/* FIXME: we should not allow headers in messages to change the sylpheed marks
- * so we're currently disabling setting any MsgFlags when detecting X-Seen,
- * Seen, X-Status, Status. See macro ALLOW_HEADER_HINT */
-
-#define ALLOW_HEADER_HINT
-
-MsgInfo *procheader_file_parse(FILE * fp, MsgFlags flags,
- gboolean full)
+MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
+ gboolean decrypted)
{
- static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
- {"From:", NULL, TRUE},
- {"To:", NULL, TRUE},
- {"Cc:", NULL, TRUE},
- {"Newsgroups:", NULL, TRUE},
- {"Subject:", NULL, TRUE},
- {"Message-Id:", NULL, FALSE},
- {"References:", NULL, FALSE},
- {"In-Reply-To:", NULL, FALSE},
- {"Content-Type:", NULL, FALSE},
- {"Seen:", NULL, FALSE},
- {"Status:", NULL, FALSE},
- {"X-Status:", NULL, FALSE},
- {"From ", NULL, FALSE},
- {"X-Face:", NULL, FALSE},
- {"Disposition-Notification-To:", NULL, FALSE},
- {"Return-Receipt-To:", NULL, FALSE},
- {NULL, NULL, FALSE}};
-
- static HeaderEntry hentry_short[] = {{"Date:", NULL, FALSE},
- {"From:", NULL, TRUE},
- {"To:", NULL, TRUE},
- {"Cc:", NULL, TRUE},
- {"Newsgroups:", NULL, TRUE},
- {"Subject:", NULL, TRUE},
- {"Message-Id:", NULL, FALSE},
- {"References:", NULL, FALSE},
- {"In-Reply-To:", NULL, FALSE},
- {"Content-Type:", NULL, FALSE},
- {"Seen:", NULL, FALSE},
- {"Status:", NULL, FALSE},
- {"X-Status:", NULL, FALSE},
- {"From ", NULL, FALSE},
- {NULL, NULL, FALSE}};
-
MsgInfo *msginfo;
gchar buf[BUFFSIZE], tmp[BUFFSIZE];
gchar *reference = NULL;
HeaderEntry *hentry;
gint hnum;
- hentry = full ? hentry_full : hentry_short;
+ hentry = procheader_get_headernames(full);
- if (MSG_IS_QUEUED(flags)) {
+ if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
while (fgets(buf, sizeof(buf), fp) != NULL)
if (buf[0] == '\r' || buf[0] == '\n') break;
}
- msginfo = g_new0(MsgInfo, 1);
+ msginfo = procmsg_msginfo_new();
if (flags.tmp_flags || flags.perm_flags)
msginfo->flags = flags;
else
MSG_SET_PERM_FLAGS(msginfo->flags, MSG_NEW | MSG_UNREAD);
+ if (decrypted)
+ MSG_UNSET_TMP_FLAGS(msginfo->flags, MSG_MIME);
+
msginfo->inreplyto = NULL;
while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
!= -1) {
hp = buf + strlen(hentry[hnum].name);
-
while (*hp == ' ' || *hp == '\t') hp++;
switch (hnum) {
g_strconcat(p, ",", hp, NULL);
g_free(p);
} else
- msginfo->newsgroups = g_strdup(buf + 12);
+ msginfo->newsgroups = g_strdup(hp);
break;
case H_SUBJECT:
if (msginfo->subject) break;
msginfo->msgid = g_strdup(hp);
break;
case H_REFERENCES:
+ case H_IN_REPLY_TO:
if (!reference) {
msginfo->references = g_strdup(hp);
eliminate_parenthesis(hp, '(', ')');
}
}
break;
- case H_IN_REPLY_TO:
- if (!reference) {
- eliminate_parenthesis(hp, '(', ')');
- extract_parenthesis(hp, '<', '>');
- remove_space(hp);
- if (*hp != '\0')
- reference = g_strdup(hp);
- }
- break;
case H_CONTENT_TYPE:
- if (!strncasecmp(hp, "multipart", 9))
+ if (decrypted) {
+ if (!strncasecmp(hp, "multipart", 9)) {
+ if (strncasecmp(hp, "multipart/signed", 16)) {
+ MSG_SET_TMP_FLAGS(msginfo->flags,
+ MSG_MIME);
+ } else {
+ MSG_SET_TMP_FLAGS(msginfo->flags,
+ MSG_SIGNED);
+ }
+ }
+ }
+ else if (!strncasecmp(hp, "multipart/encrypted", 19)) {
+ MSG_SET_TMP_FLAGS(msginfo->flags,
+ MSG_ENCRYPTED);
+ }
+ else if (!strncasecmp(hp, "multipart", 9) &&
+ !strncasecmp(hp, "multipart/signed", 16)) {
+ MSG_SET_TMP_FLAGS(msginfo->flags, MSG_SIGNED);
+ }
+ else if (!strncasecmp(hp, "multipart", 9))
MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MIME);
break;
#ifdef ALLOW_HEADER_HINT
{
gchar *tmp, *name;
- Xalloca(tmp, strlen(str) + 1, return NULL);
- strcpy(tmp, str);
+ Xstrdup_a(tmp, str, return NULL);
if (*tmp == '\"') {
extract_quote(tmp, '\"');
return name;
}
-time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
+static gint procheader_scan_date_string(const gchar *str,
+ gchar *weekday, gint *day,
+ gchar *month, gint *year,
+ gint *hh, gint *mm, gint *ss,
+ gchar *zone)
+{
+ gint result;
+
+ result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d %5s",
+ weekday, day, month, year, hh, mm, ss, zone);
+ if (result == 8) return 0;
+
+ result = sscanf(str, "%3s,%d %9s %d %2d:%2d:%2d %5s",
+ weekday, day, month, year, hh, mm, ss, zone);
+ if (result == 8) return 0;
+
+ result = sscanf(str, "%d %9s %d %2d:%2d:%2d %5s",
+ day, month, year, hh, mm, ss, zone);
+ if (result == 7) return 0;
+
+ *zone = '\0';
+ result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
+ weekday, day, month, year, hh, mm, ss);
+ if (result == 7) return 0;
+
+ *ss = 0;
+ result = sscanf(str, "%10s %d %9s %d %2d:%2d %5s",
+ weekday, day, month, year, hh, mm, zone);
+ if (result == 7) return 0;
+
+ result = sscanf(str, "%d %9s %d %2d:%2d %5s",
+ day, month, year, hh, mm, zone);
+ if (result == 6) return 0;
+
+ return -1;
+}
+
+/*
+ * Hiro, most UNIXen support this function:
+ * http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?getdate
+ */
+gboolean procheader_date_parse_to_tm(const gchar *src, struct tm *t, char *zone)
{
static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
- gchar weekday[4];
+ gchar weekday[11];
gint day;
- gchar month[4];
+ gchar month[10];
gint year;
gint hh, mm, ss;
- gchar zone[6];
- gint result;
GDateMonth dmonth;
- struct tm t;
gchar *p;
- time_t timer;
- /* parsing date field... */
- result = sscanf(src, "%3s, %d %3s %d %2d:%2d:%2d %5s",
- weekday, &day, month, &year, &hh, &mm, &ss, zone);
- if (result != 8) {
- result = sscanf(src, "%d %3s %d %2d:%2d:%2d %5s",
- &day, month, &year, &hh, &mm, &ss, zone);
- if (result != 7) {
- ss = 0;
- result = sscanf(src, "%3s, %d %3s %d %2d:%2d %5s",
- weekday, &day, month, &year, &hh, &mm, zone);
- if (result != 7) {
- result = sscanf(src, "%d %3s %d %2d:%2d %5s",
- &day, month, &year, &hh, &mm,
- zone);
- if (result != 6) {
- g_warning("Invalid date: %s\n", src);
- if (dest && len > 0)
- strncpy2(dest, src, len);
- return 0;
- }
- }
- }
+ if (!t)
+ return FALSE;
+
+ memset(t, 0, sizeof *t);
+
+ if (procheader_scan_date_string(src, weekday, &day, month, &year,
+ &hh, &mm, &ss, zone) < 0) {
+ g_warning("Invalid date: %s\n", src);
+ return FALSE;
}
/* Y2K compliant :) */
year += 1900;
}
+ month[3] = '\0';
if ((p = strstr(monthstr, month)) != NULL)
dmonth = (gint)(p - monthstr) / 3 + 1;
else {
dmonth = G_DATE_BAD_MONTH;
}
+ t->tm_sec = ss;
+ t->tm_min = mm;
+ t->tm_hour = hh;
+ t->tm_mday = day;
+ t->tm_mon = dmonth - 1;
+ t->tm_year = year - 1900;
+ t->tm_wday = 0;
+ t->tm_yday = 0;
+ t->tm_isdst = -1;
+
+ mktime(t);
+
+ return TRUE;
+}
+
+time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
+{
+ static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
+ gchar weekday[11];
+ gint day;
+ gchar month[10];
+ gint year;
+ gint hh, mm, ss;
+ gchar zone[6];
+ GDateMonth dmonth = G_DATE_BAD_MONTH;
+ struct tm t;
+ gchar *p;
+ time_t timer;
+ time_t tz_offset;
+
+ if (procheader_scan_date_string(src, weekday, &day, month, &year,
+ &hh, &mm, &ss, zone) < 0) {
+ g_warning("Invalid date: %s\n", src);
+ if (dest && len > 0)
+ strncpy2(dest, src, len);
+ return 0;
+ }
+
+ /* Y2K compliant :) */
+ if (year < 1000) {
+ if (year < 50)
+ year += 2000;
+ else
+ year += 1900;
+ }
+
+ month[3] = '\0';
+ for (p = monthstr; *p != '\0'; p += 3) {
+ if (!strncasecmp(p, month, 3)) {
+ dmonth = (gint)(p - monthstr) / 3 + 1;
+ break;
+ }
+ }
+ if (*p == '\0')
+ g_warning("Invalid month: %s\n", month);
+
t.tm_sec = ss;
t.tm_min = mm;
t.tm_hour = hh;
t.tm_isdst = -1;
timer = mktime(&t);
- timer += tzoffset_sec(&timer) - remote_tzoffset_sec(zone);
+ tz_offset = remote_tzoffset_sec(zone);
+ if (tz_offset != -1)
+ timer += tzoffset_sec(&timer) - tz_offset;
if (dest)
procheader_date_get_localtime(dest, len, timer);
strftime(dest, len, prefs_common.date_format, lt);
else
strftime(dest, len, default_format, lt);
+
+#warning FIXME_GTK2
+#if 1
+ {
+ gchar *str;
+ const gchar *src_codeset, *dest_codeset;
+
+ src_codeset = conv_get_current_charset_str();
+ dest_codeset = CS_UTF_8;
+ str = conv_codeset_strdup(dest, src_codeset, dest_codeset);
+ if (str) {
+ g_snprintf(dest, len, "%s", str);
+ strncpy2(dest, str, len);
+ g_free(str);
+ }
+ }
+#endif
+}
+
+gint get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
+{
+ gchar *file;
+ FILE *fp;
+ HeaderEntry hentry[]={ { header, NULL, TRUE },
+ { NULL, NULL, FALSE } };
+ gint val;
+
+ g_return_val_if_fail(msginfo != NULL, -1);
+ file = procmsg_get_message_file_path(msginfo);
+ if ((fp = fopen(file, "rb")) == NULL) {
+ FILE_OP_ERROR(file, "fopen");
+ g_free(file);
+ return -1;
+ }
+ val = procheader_get_one_field(buf,len, fp, hentry);
+ if (fclose(fp) == EOF) {
+ FILE_OP_ERROR(file, "fclose");
+ unlink(file);
+ g_free(file);
+ return -1;
+ }
+
+ g_free(file);
+ if (val == -1)
+ return -1;
+
+ return 0;
}