claws-mail/src/procheader.c

1001 lines
23 KiB
C
Raw Normal View History

2001-04-19 14:21:46 +02:00
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
* Copyright (C) 1999-2004 Hiroyuki Yamamoto
2001-04-19 14:21:46 +02:00
*
* 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
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/stat.h>
2001-04-19 14:21:46 +02:00
#include "intl.h"
#include "procheader.h"
#include "procmsg.h"
#include "codeconv.h"
#include "prefs_common.h"
2001-04-27 18:05:55 +02:00
#include "utils.h"
2001-04-19 14:21:46 +02:00
#define BUFFSIZE 8192
2004-01-12 22:28:31 +01:00
typedef char *(*getlinefunc)(char *, int, void *);
typedef int (*peekcharfunc)(void *);
typedef int (*getcharfunc)(void *);
typedef gint (*get_one_field_func)(gchar *, gint, void *, HeaderEntry[]);
static gint string_get_one_field(gchar *buf, gint len, char **str,
HeaderEntry hentry[]);
static char *string_getline(char *buf, int len, char **str);
static int string_peekchar(char **str);
static int file_peekchar(FILE *fp);
static gint generic_get_one_field(gchar *buf, gint len, void *data,
HeaderEntry hentry[],
getlinefunc getline,
peekcharfunc peekchar,
2004-02-21 12:01:01 +01:00
gboolean unfold);
2004-01-12 22:28:31 +01:00
static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
gboolean full, gboolean decrypted);
2001-04-19 14:21:46 +02:00
gint procheader_get_one_field(gchar *buf, gint len, FILE *fp,
HeaderEntry hentry[])
2004-01-12 22:28:31 +01:00
{
return generic_get_one_field(buf, len, fp, hentry,
(getlinefunc)fgets, (peekcharfunc)file_peekchar,
2004-02-21 12:01:01 +01:00
TRUE);
2004-01-12 22:28:31 +01:00
}
static gint string_get_one_field(gchar *buf, gint len, char **str,
HeaderEntry hentry[])
{
return generic_get_one_field(buf, len, str, hentry,
(getlinefunc)string_getline,
(peekcharfunc)string_peekchar,
2004-02-21 12:01:01 +01:00
TRUE);
2004-01-12 22:28:31 +01:00
}
static char *string_getline(char *buf, int len, char **str)
{
if (!**str)
return NULL;
for (; **str && len > 1; --len)
if ((*buf++ = *(*str)++) == '\n')
break;
*buf = '\0';
return buf;
}
static int string_peekchar(char **str)
{
return **str;
}
static int file_peekchar(FILE *fp)
{
return ungetc(getc(fp), fp);
}
static gint generic_get_one_field(gchar *buf, gint len, void *data,
HeaderEntry *hentry,
getlinefunc getline, peekcharfunc peekchar,
2004-02-21 12:01:01 +01:00
gboolean unfold)
2001-04-19 14:21:46 +02:00
{
gint nexthead;
gint hnum = 0;
HeaderEntry *hp = NULL;
if (hentry != NULL) {
/* skip non-required headers */
do {
do {
2004-01-12 22:28:31 +01:00
if (getline(buf, len, data) == NULL)
2001-04-19 14:21:46 +02:00
return -1;
if (buf[0] == '\r' || buf[0] == '\n')
return -1;
} while (buf[0] == ' ' || buf[0] == '\t');
for (hp = hentry, hnum = 0; hp->name != NULL;
hp++, hnum++) {
if (!g_ascii_strncasecmp(hp->name, buf,
2002-07-11 09:48:24 +02:00
strlen(hp->name)))
2001-04-19 14:21:46 +02:00
break;
}
} while (hp->name == NULL);
} else {
2004-01-12 22:28:31 +01:00
if (getline(buf, len, data) == NULL) return -1;
2001-04-19 14:21:46 +02:00
if (buf[0] == '\r' || buf[0] == '\n') return -1;
}
2004-01-12 22:28:31 +01:00
/* unfold line */
2001-04-19 14:21:46 +02:00
while (1) {
2004-01-12 22:28:31 +01:00
nexthead = peekchar(data);
/* ([*WSP CRLF] 1*WSP) */
2001-04-19 14:21:46 +02:00
if (nexthead == ' ' || nexthead == '\t') {
2004-02-21 12:01:01 +01:00
size_t buflen;
/* trim previous trailing \n if requesting one header or
* unfolding was requested */
if ((!hentry && unfold) || (hp && hp->unfold))
strretchomp(buf);
buflen = strlen(buf);
2004-01-12 22:28:31 +01:00
2001-04-19 14:21:46 +02:00
/* concatenate next line */
if ((len - buflen) > 2) {
2004-01-12 22:28:31 +01:00
if (getline(buf + buflen, len - buflen, data) == NULL)
2001-04-19 14:21:46 +02:00
break;
} else
break;
2004-02-21 12:01:01 +01:00
} else {
/* remove trailing new line */
strretchomp(buf);
2001-04-19 14:21:46 +02:00
break;
2004-02-21 12:01:01 +01:00
}
2001-04-19 14:21:46 +02:00
}
return hnum;
}
2004-02-21 12:01:01 +01:00
gint procheader_get_one_field_asis(gchar *buf, gint len, FILE *fp)
{
return generic_get_one_field(buf, len, fp, NULL,
(getlinefunc)fgets,
(peekcharfunc)file_peekchar,
FALSE);
}
2004-01-12 22:28:31 +01:00
#if 0
2001-04-19 14:21:46 +02:00
gchar *procheader_get_unfolded_line(gchar *buf, gint len, FILE *fp)
{
gboolean folded = FALSE;
gint nexthead;
gchar *bufp;
if (fgets(buf, len, fp) == NULL) return NULL;
if (buf[0] == '\r' || buf[0] == '\n') return NULL;
bufp = buf + strlen(buf);
2002-07-11 09:48:24 +02:00
for (; bufp > buf &&
(*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
bufp--)
*(bufp - 1) = '\0';
2001-04-19 14:21:46 +02:00
while (1) {
nexthead = fgetc(fp);
/* folded */
if (nexthead == ' ' || nexthead == '\t')
folded = TRUE;
else if (nexthead == EOF)
break;
else if (folded == TRUE) {
2002-07-11 09:48:24 +02:00
if (nexthead == '\r' || nexthead == '\n') {
folded = FALSE;
continue;
}
2001-04-19 14:21:46 +02:00
if ((len - (bufp - buf)) <= 2) break;
/* replace return code on the tail end
with space */
2002-07-11 09:48:24 +02:00
*bufp++ = ' ';
2001-04-19 14:21:46 +02:00
*bufp++ = nexthead;
*bufp = '\0';
2002-07-11 09:48:24 +02:00
/* concatenate next line */
2001-04-19 14:21:46 +02:00
if (fgets(bufp, len - (bufp - buf), fp)
== NULL) break;
bufp += strlen(bufp);
2002-07-11 09:48:24 +02:00
for (; bufp > buf &&
(*(bufp - 1) == '\n' || *(bufp - 1) == '\r');
bufp--)
*(bufp - 1) = '\0';
2001-04-19 14:21:46 +02:00
folded = FALSE;
} else {
ungetc(nexthead, fp);
break;
}
}
/* remove trailing return code */
strretchomp(buf);
return buf;
}
2004-01-12 22:28:31 +01:00
#endif
2001-04-19 14:21:46 +02:00
2004-01-12 22:28:31 +01:00
#if 0
2001-05-06 22:06:56 +02:00
GSList *procheader_get_header_list_from_file(const gchar *file)
{
FILE *fp;
GSList *hlist;
2002-03-14 11:17:32 +01:00
if ((fp = fopen(file, "rb")) == NULL) {
2001-05-06 22:06:56 +02:00
FILE_OP_ERROR(file, "fopen");
return NULL;
}
hlist = procheader_get_header_list(fp);
fclose(fp);
return hlist;
}
GSList *procheader_get_header_list(FILE *fp)
{
gchar buf[BUFFSIZE];
2001-05-06 22:06:56 +02:00
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)) != NULL)
2001-05-08 16:29:01 +02:00
hlist = g_slist_append(hlist, header);
/*
2001-05-06 22:06:56 +02:00
if (*buf == ':') continue;
for (p = buf; *p && *p != ' '; p++) {
if (*p == ':') {
header = g_new(Header, 1);
header->name = g_strndup(buf, p - buf);
p++;
while (*p == ' ' || *p == '\t') p++;
conv_unmime_header(tmp, sizeof(tmp), p, NULL);
header->body = g_strdup(tmp);
hlist = g_slist_append(hlist, header);
break;
}
}
*/
2001-05-06 22:06:56 +02:00
}
return hlist;
}
2004-01-12 22:28:31 +01:00
#endif
2001-05-06 22:06:56 +02:00
2004-01-12 22:28:31 +01:00
#if 0
2001-05-06 22:06:56 +02:00
GPtrArray *procheader_get_header_array(FILE *fp)
{
gchar buf[BUFFSIZE];
2001-05-06 22:06:56 +02:00
GPtrArray *headers;
Header *header;
g_return_val_if_fail(fp != NULL, NULL);
headers = g_ptr_array_new();
while (procheader_get_unfolded_line(buf, sizeof(buf), fp) != NULL) {
if ((header = procheader_parse_header(buf)) != NULL)
g_ptr_array_add(headers, header);
/*
2001-05-06 22:06:56 +02:00
if (*buf == ':') continue;
for (p = buf; *p && *p != ' '; p++) {
if (*p == ':') {
header = g_new(Header, 1);
header->name = g_strndup(buf, p - buf);
p++;
while (*p == ' ' || *p == '\t') p++;
conv_unmime_header(tmp, sizeof(tmp), p, NULL);
header->body = g_strdup(tmp);
g_ptr_array_add(headers, header);
break;
}
}
2001-05-08 16:29:01 +02:00
*/
2001-05-06 22:06:56 +02:00
}
return headers;
}
2004-01-12 22:28:31 +01:00
#endif
2001-05-06 22:06:56 +02:00
GPtrArray *procheader_get_header_array_asis(FILE *fp)
{
gchar buf[BUFFSIZE];
2001-05-06 22:06:56 +02:00
GPtrArray *headers;
Header *header;
g_return_val_if_fail(fp != NULL, NULL);
headers = g_ptr_array_new();
2004-02-21 12:01:01 +01:00
while (procheader_get_one_field_asis(buf, sizeof(buf), fp) != -1) {
if ((header = procheader_parse_header(buf)) != NULL)
g_ptr_array_add(headers, header);
/*
2001-05-06 22:06:56 +02:00
if (*buf == ':') continue;
for (p = buf; *p && *p != ' '; p++) {
if (*p == ':') {
header = g_new(Header, 1);
header->name = g_strndup(buf, p - buf);
p++;
conv_unmime_header(tmp, sizeof(tmp), p, NULL);
header->body = g_strdup(tmp);
g_ptr_array_add(headers, header);
break;
}
}
*/
2001-05-06 22:06:56 +02:00
}
return headers;
}
2004-01-12 22:28:31 +01:00
#if 0
2001-05-06 22:06:56 +02:00
void procheader_header_list_destroy(GSList *hlist)
{
Header *header;
while (hlist != NULL) {
header = hlist->data;
procheader_header_free(header);
hlist = g_slist_remove(hlist, header);
}
}
2004-01-12 22:28:31 +01:00
#endif
2001-05-06 22:06:56 +02:00
void procheader_header_array_destroy(GPtrArray *harray)
{
gint i;
Header *header;
for (i = 0; i < harray->len; i++) {
header = g_ptr_array_index(harray, i);
procheader_header_free(header);
}
g_ptr_array_free(harray, TRUE);
}
void procheader_header_free(Header *header)
{
if (!header) return;
g_free(header->name);
g_free(header->body);
g_free(header);
}
2001-05-06 06:27:28 +02:00
/*
2001-05-06 06:48:43 +02:00
tests whether two headers' names are equal
remove the trailing ':' or ' ' before comparing
2001-05-06 06:27:28 +02:00
*/
gboolean procheader_headername_equal(char * hdr1, char * hdr2)
{
int len1;
int len2;
len1 = strlen(hdr1);
len2 = strlen(hdr2);
2001-05-23 14:08:40 +02:00
if (hdr1[len1 - 1] == ':')
2001-05-06 06:27:28 +02:00
len1--;
2001-05-23 14:08:40 +02:00
if (hdr2[len2 - 1] == ':')
2001-05-06 06:27:28 +02:00
len2--;
if (len1 != len2)
return 0;
2001-05-23 14:08:40 +02:00
return (g_ascii_strncasecmp(hdr1, hdr2, len1) == 0);
2001-05-06 06:27:28 +02:00
}
/*
parse headers, for example :
From: dinh@enseirb.fr becomes :
header->name = "From:"
header->body = "dinh@enseirb.fr"
*/
Header * procheader_parse_header(gchar * buf)
{
gchar tmp[BUFFSIZE];
gchar *p = buf;
Header * header;
if ((*buf == ':') || (*buf == ' '))
return NULL;
for (p = buf; *p ; p++) {
if ((*p == ':') || (*p == ' ')) {
header = g_new(Header, 1);
header->name = g_strndup(buf, p - buf + 1);
p++;
while (*p == ' ' || *p == '\t') p++;
conv_unmime_header(tmp, sizeof(tmp), p, NULL);
if(tmp == NULL)
header->body = g_strdup(p);
else
header->body = g_strdup(tmp);
2001-05-06 06:27:28 +02:00
return header;
}
}
return NULL;
}
2001-04-19 14:21:46 +02:00
void procheader_get_header_fields(FILE *fp, HeaderEntry hentry[])
{
gchar buf[BUFFSIZE];
HeaderEntry *hp;
gint hnum;
gchar *p;
if (hentry == NULL) return;
while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
!= -1) {
hp = hentry + hnum;
p = buf + strlen(hp->name);
while (*p == ' ' || *p == '\t') p++;
if (hp->body == NULL)
hp->body = g_strdup(p);
else if (procheader_headername_equal(hp->name, "To") ||
procheader_headername_equal(hp->name, "Cc")) {
2001-04-19 14:21:46 +02:00
gchar *tp = hp->body;
hp->body = g_strconcat(tp, ", ", p, NULL);
g_free(tp);
}
}
}
2002-06-19 09:31:13 +02:00
MsgInfo *procheader_parse_file(const gchar *file, MsgFlags flags,
gboolean full, gboolean decrypted)
{
struct stat s;
2002-06-19 09:31:13 +02:00
FILE *fp;
MsgInfo *msginfo;
if (stat(file, &s) < 0) {
FILE_OP_ERROR(file, "stat");
return NULL;
}
if (!S_ISREG(s.st_mode))
return NULL;
2002-06-19 09:31:13 +02:00
if ((fp = fopen(file, "rb")) == NULL) {
FILE_OP_ERROR(file, "fopen");
return NULL;
}
msginfo = procheader_parse_stream(fp, flags, full, decrypted);
fclose(fp);
if (msginfo) {
msginfo->size = s.st_size;
msginfo->mtime = s.st_mtime;
}
2002-06-19 09:31:13 +02:00
return msginfo;
}
MsgInfo *procheader_parse_str(const gchar *str, MsgFlags flags, gboolean full,
gboolean decrypted)
{
2004-01-12 22:28:31 +01:00
return parse_stream(&str, TRUE, flags, full, decrypted);
2002-06-19 09:31:13 +02:00
}
2001-04-19 14:21:46 +02:00
enum
{
H_DATE = 0,
H_FROM = 1,
H_TO = 2,
H_CC = 3,
H_NEWSGROUPS = 4,
H_SUBJECT = 5,
H_MSG_ID = 6,
H_REFERENCES = 7,
H_IN_REPLY_TO = 8,
H_CONTENT_TYPE = 9,
H_SEEN = 10,
2001-05-23 14:08:40 +02:00
H_STATUS = 11,
H_X_STATUS = 12,
2001-05-28 00:40:29 +02:00
H_FROM_SPACE = 13,
H_SC_PLANNED_DOWNLOAD = 14,
H_X_FACE = 15,
H_DISPOSITION_NOTIFICATION_TO = 16,
H_RETURN_RECEIPT_TO = 17,
H_SC_PARTIALLY_RETRIEVED = 18,
H_SC_ACCOUNT_SERVER = 19,
H_SC_ACCOUNT_LOGIN = 20,
H_SC_MESSAGE_SIZE = 21
2001-04-19 14:21:46 +02:00
};
static HeaderEntry hentry_full[] = {{"Date:", NULL, FALSE},
{"From:", NULL, TRUE},
{"To:", NULL, TRUE},
{"Cc:", NULL, TRUE},
{"Newsgroups:", NULL, TRUE},
{"Subject:", NULL, TRUE},
2004-07-16 13:12:46 +02:00
{"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},
{"SC-Marked-For-Download:", NULL, FALSE},
{"X-Face:", NULL, FALSE},
{"Disposition-Notification-To:", NULL, FALSE},
{"Return-Receipt-To:", NULL, FALSE},
{"SC-Partially-Retrieved:", NULL, FALSE},
{"SC-Account-Server:", NULL, FALSE},
{"SC-Account-Login:",NULL, FALSE},
{"SC-Message-Size:", 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},
2004-07-16 13:12:46 +02:00
{"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},
{"SC-Marked-For-Download:", NULL, FALSE},
{NULL, NULL, FALSE}};
HeaderEntry* procheader_get_headernames(gboolean full)
{
return full ? hentry_full : hentry_short;
}
2004-01-12 22:28:31 +01:00
MsgInfo *procheader_parse_stream(FILE *fp, MsgFlags flags, gboolean full,
2002-06-19 09:31:13 +02:00
gboolean decrypted)
2004-01-12 22:28:31 +01:00
{
return parse_stream(fp, FALSE, flags, full, decrypted);
}
static MsgInfo *parse_stream(void *data, gboolean isstring, MsgFlags flags,
gboolean full, gboolean decrypted)
2001-04-19 14:21:46 +02:00
{
MsgInfo *msginfo;
gchar buf[BUFFSIZE], tmp[BUFFSIZE];
gchar *reference = NULL;
gchar *p;
gchar *hp;
HeaderEntry *hentry;
2001-04-19 14:21:46 +02:00
gint hnum;
2004-01-12 22:28:31 +01:00
get_one_field_func get_one_field =
isstring ? (get_one_field_func)string_get_one_field
: (get_one_field_func)procheader_get_one_field;
2001-04-19 14:21:46 +02:00
hentry = procheader_get_headernames(full);
2001-04-19 14:21:46 +02:00
if (MSG_IS_QUEUED(flags) || MSG_IS_DRAFT(flags)) {
2004-01-12 22:28:31 +01:00
while (get_one_field(buf, sizeof(buf), data, NULL) != -1)
; /* loop */
2001-04-19 14:21:46 +02:00
}
2002-06-30 01:33:42 +02:00
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);
2001-04-19 14:21:46 +02:00
msginfo->inreplyto = NULL;
2004-01-12 22:28:31 +01:00
while ((hnum = get_one_field(buf, sizeof(buf), data, hentry))
2001-04-19 14:21:46 +02:00
!= -1) {
hp = buf + strlen(hentry[hnum].name);
while (*hp == ' ' || *hp == '\t') hp++;
switch (hnum) {
case H_DATE:
if (msginfo->date) break;
msginfo->date_t =
procheader_date_parse(NULL, hp, 0);
msginfo->date = g_strdup(hp);
break;
case H_FROM:
if (msginfo->from) break;
conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
msginfo->from = g_strdup(tmp);
msginfo->fromname = procheader_get_fromname(tmp);
break;
case H_TO:
conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
if (msginfo->to) {
p = msginfo->to;
msginfo->to =
g_strconcat(p, ", ", tmp, NULL);
g_free(p);
} else
msginfo->to = g_strdup(tmp);
break;
case H_CC:
conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
if (msginfo->cc) {
p = msginfo->cc;
msginfo->cc =
g_strconcat(p, ", ", tmp, NULL);
g_free(p);
} else
msginfo->cc = g_strdup(tmp);
break;
2001-04-19 14:21:46 +02:00
case H_NEWSGROUPS:
if (msginfo->newsgroups) {
p = msginfo->newsgroups;
msginfo->newsgroups =
g_strconcat(p, ",", hp, NULL);
g_free(p);
} else
msginfo->newsgroups = g_strdup(hp);
2001-04-19 14:21:46 +02:00
break;
case H_SUBJECT:
if (msginfo->subject) break;
conv_unmime_header(tmp, sizeof(tmp), hp, NULL);
msginfo->subject = g_strdup(tmp);
break;
case H_MSG_ID:
if (msginfo->msgid) break;
extract_parenthesis(hp, '<', '>');
remove_space(hp);
msginfo->msgid = g_strdup(hp);
break;
case H_REFERENCES:
2001-10-30 11:25:49 +01:00
case H_IN_REPLY_TO:
2001-04-19 14:21:46 +02:00
if (!reference) {
2001-05-13 04:57:01 +02:00
msginfo->references = g_strdup(hp);
2001-04-19 14:21:46 +02:00
eliminate_parenthesis(hp, '(', ')');
if ((p = strrchr(hp, '<')) != NULL &&
strchr(p + 1, '>') != NULL) {
extract_parenthesis(p, '<', '>');
remove_space(p);
if (*p != '\0')
reference = g_strdup(p);
}
}
break;
case H_CONTENT_TYPE:
if (!g_ascii_strncasecmp(hp, "multipart/", 10))
2004-06-25 10:05:09 +02:00
MSG_SET_TMP_FLAGS(msginfo->flags, MSG_MULTIPART);
2001-04-19 14:21:46 +02:00
break;
2001-07-27 20:04:08 +02:00
#ifdef ALLOW_HEADER_HINT
2001-04-19 14:21:46 +02:00
case H_SEEN:
/* mnews Seen header */
MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW|MSG_UNREAD);
2001-04-19 14:21:46 +02:00
break;
2001-07-27 20:04:08 +02:00
#endif
2001-04-19 14:21:46 +02:00
case H_X_FACE:
if (msginfo->xface) break;
msginfo->xface = g_strdup(hp);
break;
2001-05-02 16:16:51 +02:00
case H_DISPOSITION_NOTIFICATION_TO:
if (msginfo->dispositionnotificationto) break;
msginfo->dispositionnotificationto = g_strdup(hp);
break;
case H_RETURN_RECEIPT_TO:
if (msginfo->returnreceiptto) break;
msginfo->returnreceiptto = g_strdup(hp);
break;
case H_SC_PARTIALLY_RETRIEVED:
if (msginfo->partial_recv) break;
msginfo->partial_recv = g_strdup(hp);
break;
case H_SC_ACCOUNT_SERVER:
if (msginfo->account_server) break;
msginfo->account_server = g_strdup(hp);
break;
case H_SC_ACCOUNT_LOGIN:
if (msginfo->account_login) break;
msginfo->account_login = g_strdup(hp);
break;
case H_SC_MESSAGE_SIZE:
if (msginfo->total_size) break;
msginfo->total_size = atoi(hp);
break;
case H_SC_PLANNED_DOWNLOAD:
msginfo->planned_download = atoi(hp);
break;
2001-07-27 20:04:08 +02:00
#ifdef ALLOW_HEADER_HINT
2001-05-23 14:08:40 +02:00
case H_STATUS:
if (strchr(hp, 'R') != NULL)
MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
2001-05-23 14:08:40 +02:00
if (strchr(hp, 'O') != NULL)
MSG_UNSET_PERM_FLAGS(msginfo->flags, MSG_NEW);
2001-05-23 14:08:40 +02:00
if (strchr(hp, 'U') != NULL)
MSG_SET_PERM_FLAGS(msginfo->flags, MSG_UNREAD);
2001-05-23 14:08:40 +02:00
break;
case H_X_STATUS:
2001-05-28 00:40:29 +02:00
if (strchr(hp, 'D') != NULL)
MSG_SET_PERM_FLAGS(msginfo->flags,
2001-05-28 00:40:29 +02:00
MSG_REALLY_DELETED);
if (strchr(hp, 'F') != NULL)
MSG_SET_PERM_FLAGS(msginfo->flags, MSG_MARKED);
2001-05-28 00:40:29 +02:00
if (strchr(hp, 'd') != NULL)
MSG_SET_PERM_FLAGS(msginfo->flags, MSG_DELETED);
2001-05-28 00:40:29 +02:00
if (strchr(hp, 'r') != NULL)
MSG_SET_PERM_FLAGS(msginfo->flags, MSG_REPLIED);
2001-05-28 00:40:29 +02:00
if (strchr(hp, 'f') != NULL)
MSG_SET_PERM_FLAGS(msginfo->flags, MSG_FORWARDED);
2001-05-23 14:08:40 +02:00
break;
2001-07-27 20:04:08 +02:00
#endif
2001-05-23 14:08:40 +02:00
case H_FROM_SPACE:
if (msginfo->fromspace) break;
msginfo->fromspace = g_strdup(hp);
break;
2001-04-19 14:21:46 +02:00
default:
break;
2001-04-19 14:21:46 +02:00
}
}
msginfo->inreplyto = reference;
return msginfo;
}
gchar *procheader_get_fromname(const gchar *str)
{
gchar *tmp, *name;
2001-11-07 11:29:45 +01:00
Xstrdup_a(tmp, str, return NULL);
2001-04-19 14:21:46 +02:00
if (*tmp == '\"') {
extract_quote(tmp, '\"');
g_strstrip(tmp);
} else if (strchr(tmp, '<')) {
eliminate_parenthesis(tmp, '<', '>');
g_strstrip(tmp);
if (*tmp == '\0') {
strcpy(tmp, str);
extract_parenthesis(tmp, '<', '>');
g_strstrip(tmp);
}
} else if (strchr(tmp, '(')) {
extract_parenthesis(tmp, '(', ')');
g_strstrip(tmp);
}
if (*tmp == '\0')
name = g_strdup(str);
else
name = g_strdup(tmp);
return name;
}
2002-05-08 08:31:47 +02:00
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;
2003-04-23 09:55:01 +02:00
*zone = '\0';
result = sscanf(str, "%10s %d %9s %d %2d:%2d:%2d",
weekday, day, month, year, hh, mm, ss);
if (result == 7) return 0;
2002-05-08 08:31:47 +02:00
*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[11];
gint day;
gchar month[10];
gint year;
gint hh, mm, ss;
GDateMonth dmonth;
gchar *p;
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 :) */
if (year < 100) {
if (year < 70)
year += 2000;
else
year += 1900;
}
month[3] = '\0';
if ((p = strstr(monthstr, month)) != NULL)
dmonth = (gint)(p - monthstr) / 3 + 1;
else {
g_warning("Invalid month: %s\n", month);
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;
}
2001-04-19 14:21:46 +02:00
time_t procheader_date_parse(gchar *dest, const gchar *src, gint len)
{
static gchar monthstr[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
2001-11-21 09:53:27 +01:00
gchar weekday[11];
2001-04-19 14:21:46 +02:00
gint day;
2001-11-21 09:53:27 +01:00
gchar month[10];
2001-04-19 14:21:46 +02:00
gint year;
gint hh, mm, ss;
gchar zone[6];
2003-02-17 08:38:54 +01:00
GDateMonth dmonth = G_DATE_BAD_MONTH;
2001-04-19 14:21:46 +02:00
struct tm t;
gchar *p;
time_t timer;
2003-04-23 09:55:01 +02:00
time_t tz_offset;
2001-04-19 14:21:46 +02:00
2002-05-08 08:31:47 +02:00
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;
2001-04-19 14:21:46 +02:00
}
/* Y2K compliant :) */
2003-04-23 09:55:01 +02:00
if (year < 1000) {
if (year < 50)
2001-04-19 14:21:46 +02:00
year += 2000;
else
year += 1900;
}
2001-11-21 09:53:27 +01:00
month[3] = '\0';
2003-02-17 08:38:54 +01:00
for (p = monthstr; *p != '\0'; p += 3) {
if (!g_ascii_strncasecmp(p, month, 3)) {
2003-02-17 08:38:54 +01:00
dmonth = (gint)(p - monthstr) / 3 + 1;
break;
}
2001-04-19 14:21:46 +02:00
}
2003-02-17 08:38:54 +01:00
if (*p == '\0')
g_warning("Invalid month: %s\n", month);
2001-04-19 14:21:46 +02:00
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;
timer = mktime(&t);
2003-04-23 09:55:01 +02:00
tz_offset = remote_tzoffset_sec(zone);
if (tz_offset != -1)
timer += tzoffset_sec(&timer) - tz_offset;
2001-04-19 14:21:46 +02:00
if (dest)
procheader_date_get_localtime(dest, len, timer);
return timer;
}
void procheader_date_get_localtime(gchar *dest, gint len, const time_t timer)
{
struct tm *lt;
2001-04-27 18:05:55 +02:00
gchar *default_format = "%y/%m/%d(%a) %H:%M";
gchar *str;
const gchar *src_codeset, *dest_codeset;
lt = localtime(&timer);
if (prefs_common.date_format)
strftime(dest, len, prefs_common.date_format, lt);
else
2001-04-27 18:05:55 +02:00
strftime(dest, len, default_format, lt);
2003-10-05 12:10:30 +02:00
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);
2003-10-05 12:10:30 +02:00
}
2001-04-19 14:21:46 +02:00
}
2001-08-30 12:51:36 +02:00
2004-01-12 22:28:31 +01:00
/* Added by Mel Hadasht on 27 Aug 2001 */
/* Get a header from msginfo */
gint procheader_get_header_from_msginfo(MsgInfo *msginfo, gchar *buf, gint len, gchar *header)
2001-08-30 12:51:36 +02:00
{
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) {
2001-08-30 12:51:36 +02:00
FILE_OP_ERROR(file, "fopen");
g_free(file);
return -1;
}
val = procheader_get_one_field(buf,len, fp, hentry);
if (fclose(fp) == EOF) {
2001-08-30 12:51:36 +02:00
FILE_OP_ERROR(file, "fclose");
unlink(file);
g_free(file);
2001-08-30 12:51:36 +02:00
return -1;
}
g_free(file);
if (val == -1)
return -1;
return 0;
2001-08-30 12:51:36 +02:00
}