#include <stdio.h>
#include <glib.h>
+#include <glib/gi18n.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <sys/stat.h>
#include <unistd.h>
-#include "intl.h"
#include "procmime.h"
#include "procheader.h"
#include "base64.h"
#include "codeconv.h"
#include "utils.h"
#include "prefs_common.h"
-
#include "prefs_gtk.h"
static GHashTable *procmime_get_mime_type_table (void);
gchar *filename;
MimeInfo *mimeinfo;
- filename = procmsg_get_message_file(msginfo);
- if (!filename)
+ filename = procmsg_get_message_file_path(msginfo);
+ if (!filename || !is_file_exist(filename)) {
+ g_free(filename);
+ filename = procmsg_get_message_file(msginfo);
+ }
+ if (!filename || !is_file_exist(filename))
return NULL;
+
if (msginfo->folder->stype != F_QUEUE &&
msginfo->folder->stype != F_DRAFT)
mimeinfo = procmime_scan_file(filename);
gchar *tmpfilename;
FILE *outfp, *infp;
struct stat statbuf;
+ gboolean tmp_file = FALSE;
EncodingType encoding = forced_encoding
? forced_encoding
perror("tmpfile");
return FALSE;
}
-
+ tmp_file = TRUE;
readend = mimeinfo->offset + mimeinfo->length;
if (encoding == ENC_QUOTED_PRINTABLE) {
while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
gint len;
len = qp_decode_line(buf);
- fwrite(buf, len, 1, outfp);
+ fwrite(buf, 1, len, outfp);
}
} else if (encoding == ENC_BASE64) {
gchar outbuf[BUFFSIZE];
gint len;
Base64Decoder *decoder;
+ gboolean got_error = FALSE;
+ gboolean uncanonicalize = FALSE;
+ FILE *tmpfp = outfp;
+
+ if (mimeinfo->type == MIMETYPE_TEXT ||
+ mimeinfo->type == MIMETYPE_MESSAGE) {
+ uncanonicalize = TRUE;
+ tmpfp = my_tmpfile();
+ if (!tmpfp) {
+ perror("tmpfile");
+ if (tmp_file) fclose(outfp);
+ return FALSE;
+ }
+ }
decoder = base64_decoder_new();
while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
len = base64_decoder_decode(decoder, buf, outbuf);
- if (len < 0) {
- g_warning("Bad BASE64 content\n");
- break;
+ if (len < 0 && !got_error) {
+ g_warning("Bad BASE64 content.\n");
+ fwrite(_("[Error decoding BASE64]\n"),
+ sizeof(gchar),
+ strlen(_("[Error decoding BASE64]\n")),
+ tmpfp);
+ got_error = TRUE;
+ continue;
+ } else if (len >= 0) {
+ /* print out the error message only once
+ * per block */
+ fwrite(outbuf, sizeof(gchar), len, tmpfp);
+ got_error = FALSE;
}
- fwrite(outbuf, sizeof(gchar), len, outfp);
}
base64_decoder_free(decoder);
+
+ if (uncanonicalize) {
+ rewind(tmpfp);
+ while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
+ strcrchomp(buf);
+ fputs(buf, outfp);
+ }
+ fclose(tmpfp);
+ }
} else if (encoding == ENC_X_UUENCODE) {
gchar outbuf[BUFFSIZE];
gint len;
gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
{
- FILE *infp, *outfp;
+ FILE *infp = NULL, *outfp;
gint len;
- gchar *tmpfilename, *tmpout;
+ gchar *tmpfilename;
struct stat statbuf;
if (mimeinfo->encoding_type != ENC_UNKNOWN &&
return FALSE;
}
- if (mimeinfo->content == MIMECONTENT_MEM) {
- infp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmpout);
- if (infp)
- fclose(infp);
- else
+ if (mimeinfo->content == MIMECONTENT_FILE) {
+ if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ g_warning("Can't open file %s\n", mimeinfo->data.filename);
return FALSE;
-
- str_write_to_file(mimeinfo->data.mem, tmpout);
- g_free(mimeinfo->data.mem);
- mimeinfo->tmp = TRUE;
- mimeinfo->data.filename = tmpout;
- }
-
- if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
- g_warning("Can't open file %s\n", mimeinfo->data.filename);
- if (mimeinfo->content == MIMECONTENT_MEM) {
- unlink(mimeinfo->data.filename);
- g_free(mimeinfo->data.filename);
- mimeinfo->data.filename = NULL;
}
- return FALSE;
+ } else if (mimeinfo->content == MIMECONTENT_MEM) {
+ infp = str_open_as_stream(mimeinfo->data.mem);
+ if (infp == NULL)
+ return FALSE;
}
if (encoding == ENC_BASE64) {
gchar inbuf[B64_LINE_SIZE], outbuf[B64_BUFFSIZE];
+ FILE *tmp_fp = infp;
+ gchar *tmp_file = NULL;
+
+ if (mimeinfo->type == MIMETYPE_TEXT ||
+ mimeinfo->type == MIMETYPE_MESSAGE) {
+ if (mimeinfo->content == MIMECONTENT_FILE) {
+ tmp_file = get_tmp_file();
+ if (canonicalize_file(mimeinfo->data.filename, tmp_file) < 0) {
+ g_free(tmp_file);
+ fclose(infp);
+ return FALSE;
+ }
+ if ((tmp_fp = fopen(tmp_file, "rb")) == NULL) {
+ FILE_OP_ERROR(tmp_file, "fopen");
+ unlink(tmp_file);
+ g_free(tmp_file);
+ fclose(infp);
+ return FALSE;
+ }
+ } else {
+ gchar *out = canonicalize_str(mimeinfo->data.mem);
+ fclose(infp);
+ infp = str_open_as_stream(out);
+ tmp_fp = infp;
+ g_free(out);
+ if (infp == NULL)
+ return FALSE;
+ }
+ }
while ((len = fread(inbuf, sizeof(gchar),
- B64_LINE_SIZE, infp))
+ B64_LINE_SIZE, tmp_fp))
== B64_LINE_SIZE) {
base64_encode(outbuf, inbuf, B64_LINE_SIZE);
fputs(outbuf, outfp);
fputc('\n', outfp);
}
- if (len > 0 && feof(infp)) {
+ if (len > 0 && feof(tmp_fp)) {
base64_encode(outbuf, inbuf, len);
fputs(outbuf, outfp);
fputc('\n', outfp);
}
+
+ if (tmp_file) {
+ fclose(tmp_fp);
+ unlink(tmp_file);
+ g_free(tmp_file);
+ }
} else if (encoding == ENC_QUOTED_PRINTABLE) {
gchar inbuf[BUFFSIZE], outbuf[BUFFSIZE * 4];
while (fgets(inbuf, sizeof(inbuf), infp) != NULL) {
qp_encode_line(outbuf, inbuf);
- fputs(outbuf, outfp);
+
+ if (!strncmp("From ", outbuf, sizeof("From ")-1)) {
+ gchar *tmpbuf = outbuf;
+
+ tmpbuf += sizeof("From ")-1;
+
+ fputs("=46rom ", outfp);
+ fputs(tmpbuf, outfp);
+ } else
+ fputs(outbuf, outfp);
}
} else {
gchar buf[BUFFSIZE];
fclose(outfp);
fclose(infp);
+ if (mimeinfo->content == MIMECONTENT_FILE) {
+ if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
+ unlink(mimeinfo->data.filename);
+ g_free(mimeinfo->data.filename);
+ } else if (mimeinfo->content == MIMECONTENT_MEM) {
+ if (mimeinfo->tmp && (mimeinfo->data.mem != NULL))
+ g_free(mimeinfo->data.mem);
+ }
+
stat(tmpfilename, &statbuf);
- if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
- unlink(mimeinfo->data.filename);
- g_free(mimeinfo->data.filename);
+ mimeinfo->content = MIMECONTENT_FILE;
mimeinfo->data.filename = tmpfilename;
mimeinfo->tmp = TRUE;
mimeinfo->offset = 0;
mimeinfo->length = statbuf.st_size;
mimeinfo->encoding_type = encoding;
- mimeinfo->content = MIMECONTENT_FILE;
return TRUE;
}
}
dup2(oldout, 1);
-#warning FIXME_GTK2 HTML/RTF not yet utf8
/* CodeConverter seems to have no effect here */
} else if (mimeinfo->type == MIMETYPE_TEXT && !g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
HTMLParser *parser;
if (!mimeinfo) return NULL;
partinfo = mimeinfo;
- while (partinfo && partinfo->type != MIMETYPE_TEXT)
+ while (partinfo && partinfo->type != MIMETYPE_TEXT) {
partinfo = procmime_mimeinfo_next(partinfo);
+ }
+ if (partinfo)
+ outfp = procmime_get_text_content(partinfo);
+
+ procmime_mimeinfo_free_all(mimeinfo);
+
+ return outfp;
+}
+
+
+static gboolean find_encrypted_func(GNode *node, gpointer data)
+{
+ MimeInfo *mimeinfo = (MimeInfo *) node->data;
+ MimeInfo **encinfo = (MimeInfo **) data;
+
+ if (privacy_mimeinfo_is_encrypted(mimeinfo)) {
+ *encinfo = mimeinfo;
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+static MimeInfo *find_encrypted_part(MimeInfo *rootinfo)
+{
+ MimeInfo *encinfo = NULL;
+
+ g_node_traverse(rootinfo->node, G_IN_ORDER, G_TRAVERSE_ALL, -1,
+ find_encrypted_func, &encinfo);
+
+ return encinfo;
+}
+
+/* search the first encrypted text part of (multipart) MIME message,
+ decode, convert it and output to outfp. */
+FILE *procmime_get_first_encrypted_text_content(MsgInfo *msginfo)
+{
+ FILE *outfp = NULL;
+ MimeInfo *mimeinfo, *partinfo, *encinfo;
+
+ g_return_val_if_fail(msginfo != NULL, NULL);
+
+ mimeinfo = procmime_scan_message(msginfo);
+ if (!mimeinfo) {
+ return NULL;
+ }
+
+ partinfo = mimeinfo;
+ if ((encinfo = find_encrypted_part(partinfo)) != NULL) {
+ debug_print("decrypting message part\n");
+ if (privacy_mimeinfo_decrypt(encinfo) < 0)
+ return NULL;
+ }
+ partinfo = mimeinfo;
+ while (partinfo && partinfo->type != MIMETYPE_TEXT) {
+ partinfo = procmime_mimeinfo_next(partinfo);
+ }
if (partinfo)
outfp = procmime_get_text_content(partinfo);
return outfp;
}
+gboolean procmime_msginfo_is_encrypted(MsgInfo *msginfo)
+{
+ MimeInfo *mimeinfo, *partinfo;
+ gboolean result = FALSE;
+
+ g_return_val_if_fail(msginfo != NULL, FALSE);
+
+ mimeinfo = procmime_scan_message(msginfo);
+ if (!mimeinfo) {
+ return FALSE;
+ }
+
+ partinfo = mimeinfo;
+ result = (find_encrypted_part(partinfo) != NULL);
+ procmime_mimeinfo_free_all(mimeinfo);
+
+ return result;
+}
+
gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
- const gchar *str, gboolean case_sens)
+ const gchar *str, StrFindFunc find_func)
{
FILE *outfp;
gchar buf[BUFFSIZE];
- gchar *(* StrFindFunc) (const gchar *haystack, const gchar *needle);
g_return_val_if_fail(mimeinfo != NULL, FALSE);
g_return_val_if_fail(mimeinfo->type == MIMETYPE_TEXT, FALSE);
g_return_val_if_fail(str != NULL, FALSE);
+ g_return_val_if_fail(find_func != NULL, FALSE);
outfp = procmime_get_text_content(mimeinfo);
if (!outfp)
return FALSE;
- if (case_sens)
- StrFindFunc = strstr;
- else
- StrFindFunc = strcasestr;
-
while (fgets(buf, sizeof(buf), outfp) != NULL) {
- if (StrFindFunc(buf, str) != NULL) {
+ strretchomp(buf);
+ if (find_func(buf, str)) {
fclose(outfp);
return TRUE;
}
}
gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
- gboolean case_sens)
+ StrFindFunc find_func)
{
MimeInfo *mimeinfo;
MimeInfo *partinfo;
g_return_val_if_fail(msginfo != NULL, FALSE);
g_return_val_if_fail(str != NULL, FALSE);
+ g_return_val_if_fail(find_func != NULL, FALSE);
filename = procmsg_get_message_file(msginfo);
if (!filename) return FALSE;
partinfo = procmime_mimeinfo_next(partinfo)) {
if (partinfo->type == MIMETYPE_TEXT) {
if (procmime_find_string_part
- (partinfo, filename, str, case_sens) == TRUE) {
+ (partinfo, filename, str, find_func) == TRUE) {
found = TRUE;
break;
}
gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
{
static guint32 id = 0;
- const gchar *base;
+ gchar *base;
gchar *filename;
gchar f_prefix[10];
g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
- base = "mimetmp.html";
+ base = g_strdup("mimetmp.html");
else {
const gchar *basetmp;
basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
if (basetmp == NULL)
basetmp = "mimetmp";
- base = g_basename(basetmp);
- if (*base == '\0') base = "mimetmp";
- Xstrdup_a(base, base, return NULL);
+ basetmp = g_path_get_basename(basetmp);
+ if (*basetmp == '\0') basetmp = g_strdup("mimetmp");
+ base = conv_filename_from_utf8(basetmp);
subst_for_shellsafe_filename(base);
}
filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
f_prefix, base, NULL);
+ g_free(base);
+
return filename;
}
static GHashTable *mime_type_table = NULL;
MimeType *mime_type;
const gchar *p;
- gchar *ext;
+ gchar *ext = NULL;
+ gchar *base;
if (!mime_type_table) {
mime_type_table = procmime_get_mime_type_table();
if (!mime_type_table) return NULL;
}
- filename = g_basename(filename);
- p = strrchr(filename, '.');
+ base = g_path_get_basename(filename);
+ if ((p = strrchr(base, '.')) != NULL)
+ Xstrdup_a(ext, p + 1, p = NULL );
+ g_free(base);
if (!p) return NULL;
- Xstrdup_a(ext, p + 1, return NULL);
g_strdown(ext);
mime_type = g_hash_table_lookup(mime_type_table, ext);
if (mime_type) {
guchar *p;
gchar *delim;
MimeType *mime_type;
+ gboolean fp_is_glob_file = TRUE;
if (mime_type_list)
return mime_type_list;
-
- if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
- if ((fp = fopen(SYSCONFDIR "/mime.types", "rb")) == NULL) {
- FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
- return NULL;
+
+ if ((fp = fopen("/usr/share/mime/globs", "rb")) == NULL) {
+ fp_is_glob_file = FALSE;
+ if ((fp = fopen("/etc/mime.types", "rb")) == NULL) {
+ if ((fp = fopen(SYSCONFDIR "/mime.types", "rb"))
+ == NULL) {
+ FILE_OP_ERROR(SYSCONFDIR "/mime.types",
+ "fopen");
+ return NULL;
+ }
}
}
g_strstrip(buf);
p = buf;
- while (*p && !isspace(*p)) p++;
+
+ if (fp_is_glob_file) {
+ while (*p && !isspace(*p) && (*p!=':')) p++;
+ } else {
+ while (*p && !isspace(*p)) p++;
+ }
+
if (*p) {
*p = '\0';
p++;
mime_type->type = g_strdup(buf);
mime_type->sub_type = g_strdup(delim + 1);
- while (*p && isspace(*p)) p++;
+ if (fp_is_glob_file) {
+ while (*p && (isspace(*p)||(*p=='*')||(*p=='.'))) p++;
+ } else {
+ while (*p && isspace(*p)) p++;
+ }
+
if (*p)
mime_type->extension = g_strdup(p);
else
return ENC_8BIT;
}
-EncodingType procmime_get_encoding_for_file(const gchar *file)
+EncodingType procmime_get_encoding_for_text_file(const gchar *file)
{
FILE *fp;
- guchar buf[BUFSIZ];
+ guchar buf[BUFFSIZE];
size_t len;
+ size_t octet_chars = 0;
+ size_t total_len = 0;
+ gfloat octet_percentage;
if ((fp = fopen(file, "rb")) == NULL) {
FILE_OP_ERROR(file, "fopen");
return ENC_UNKNOWN;
}
- while ((len = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
+ while ((len = fread(buf, sizeof(guchar), sizeof(buf), fp)) > 0) {
guchar *p;
gint i;
- for (p = buf, i = 0; i < len; p++, i++) {
- if (*p & 0x80) {
- fclose(fp);
- return ENC_BASE64;
- }
+ for (p = buf, i = 0; i < len; ++p, ++i) {
+ if (*p & 0x80)
+ ++octet_chars;
}
+ total_len += len;
}
fclose(fp);
- return ENC_7BIT;
+
+ if (total_len > 0)
+ octet_percentage = (gfloat)octet_chars / (gfloat)total_len;
+ else
+ octet_percentage = 0.0;
+
+ debug_print("procmime_get_encoding_for_text_file(): "
+ "8bit chars: %d / %d (%f%%)\n", octet_chars, total_len,
+ 100.0 * octet_percentage);
+
+ if (octet_percentage > 0.20) {
+ debug_print("using BASE64\n");
+ return ENC_BASE64;
+ } else if (octet_chars > 0) {
+ debug_print("using quoted-printable\n");
+ return ENC_QUOTED_PRINTABLE;
+ } else {
+ debug_print("using 7bit\n");
+ return ENC_7BIT;
+ }
}
struct EncodingTable
return g_strdup_printf("%s/%s", type_str, subtype);
}
-void procmime_parse_mimepart(MimeInfo *parent,
+int procmime_parse_mimepart(MimeInfo *parent,
gchar *content_type,
gchar *content_encoding,
gchar *content_description,
{NULL, NULL, FALSE}};
guint content_start, i;
FILE *fp;
- gint mime_major, mime_minor;
+ gchar *tmp;
procmime_decode_content(mimeinfo);
}
fseek(fp, mimeinfo->offset, SEEK_SET);
procheader_get_header_fields(fp, hentry);
- if (hentry[0].body != NULL)
- conv_unmime_header_overwrite(hentry[0].body);
- if (hentry[2].body != NULL)
- conv_unmime_header_overwrite(hentry[2].body);
- if (hentry[4].body != NULL)
- conv_unmime_header_overwrite(hentry[4].body);
+ if (hentry[0].body != NULL) {
+ tmp = conv_unmime_header(hentry[0].body, NULL);
+ g_free(hentry[0].body);
+ hentry[0].body = tmp;
+ }
+ if (hentry[2].body != NULL) {
+ tmp = conv_unmime_header(hentry[2].body, NULL);
+ g_free(hentry[2].body);
+ hentry[2].body = tmp;
+ }
+ if (hentry[4].body != NULL) {
+ tmp = conv_unmime_header(hentry[4].body, NULL);
+ g_free(hentry[4].body);
+ hentry[4].body = tmp;
+ }
content_start = ftell(fp);
fclose(fp);
-
- if ((hentry[5].body != NULL) &&
- (sscanf(hentry[5].body, "%d.%d", &mime_major, &mime_minor) == 2) &&
- (mime_major == 1) && (mime_minor == 0)) {
- procmime_parse_mimepart(mimeinfo,
- hentry[0].body, hentry[1].body,
- hentry[2].body, hentry[3].body,
- hentry[4].body,
- mimeinfo->data.filename, content_start,
- mimeinfo->length - (content_start - mimeinfo->offset));
- } else {
- MimeInfo *subinfo;
-
- subinfo = procmime_mimeinfo_new();
- subinfo->encoding_type = ENC_UNKNOWN;
- subinfo->type = MIMETYPE_TEXT;
- subinfo->subtype = g_strdup("plain");
- subinfo->data.filename = g_strdup(mimeinfo->data.filename);
- subinfo->offset = content_start;
- subinfo->length = mimeinfo->length - (content_start - mimeinfo->offset);
-
- g_node_append(mimeinfo->node, subinfo->node);
- }
+
+ procmime_parse_mimepart(mimeinfo,
+ hentry[0].body, hentry[1].body,
+ hentry[2].body, hentry[3].body,
+ hentry[4].body,
+ mimeinfo->data.filename, content_start,
+ mimeinfo->length - (content_start - mimeinfo->offset));
+
for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
g_free(hentry[i].body);
hentry[i].body = NULL;
{"Content-Disposition:",
NULL, TRUE},
{NULL, NULL, FALSE}};
- gchar *p;
+ gchar *p, *tmp;
gchar *boundary;
gint boundary_len = 0, lastoffset = -1, i;
gchar buf[BUFFSIZE];
FILE *fp;
+ int result = 0;
boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
if (!boundary)
return;
}
fseek(fp, mimeinfo->offset, SEEK_SET);
- while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
+ while ((p = fgets(buf, sizeof(buf), fp)) != NULL && result == 0) {
if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
break;
if (IS_BOUNDARY(buf, boundary, boundary_len)) {
if (lastoffset != -1) {
- procmime_parse_mimepart(mimeinfo,
+ result = procmime_parse_mimepart(mimeinfo,
hentry[0].body, hentry[1].body,
hentry[2].body, hentry[3].body,
hentry[4].body,
hentry[i].body = NULL;
}
procheader_get_header_fields(fp, hentry);
- if (hentry[0].body != NULL)
- conv_unmime_header_overwrite(hentry[0].body);
- if (hentry[2].body != NULL)
- conv_unmime_header_overwrite(hentry[2].body);
- if (hentry[4].body != NULL)
- conv_unmime_header_overwrite(hentry[4].body);
+ if (hentry[0].body != NULL) {
+ tmp = conv_unmime_header(hentry[0].body, NULL);
+ g_free(hentry[0].body);
+ hentry[0].body = tmp;
+ }
+ if (hentry[2].body != NULL) {
+ tmp = conv_unmime_header(hentry[2].body, NULL);
+ g_free(hentry[2].body);
+ hentry[2].body = tmp;
+ }
+ if (hentry[4].body != NULL) {
+ tmp = conv_unmime_header(hentry[4].body, NULL);
+ g_free(hentry[4].body);
+ hentry[4].body = tmp;
+ }
lastoffset = ftell(fp);
}
}
return;
}
-void procmime_parse_mimepart(MimeInfo *parent,
+int procmime_parse_mimepart(MimeInfo *parent,
gchar *content_type,
gchar *content_encoding,
gchar *content_description,
/* Create MimeInfo */
mimeinfo = procmime_mimeinfo_new();
mimeinfo->content = MIMECONTENT_FILE;
- if (parent != NULL)
+ if (parent != NULL) {
+ if (g_node_depth(parent->node) > 32) {
+ /* 32 is an arbitrary value
+ * this avoids DOSsing ourselves
+ * with enormous messages
+ */
+ procmime_mimeinfo_free_all(mimeinfo);
+ return -1;
+ }
g_node_append(parent->node, mimeinfo->node);
+ }
mimeinfo->data.filename = g_strdup(filename);
mimeinfo->offset = offset;
mimeinfo->length = length;
default:
break;
}
+
+ return 0;
}
static gchar *typenames[] = {
ENC_AS_EXTENDED,
} EncodeAs;
+typedef struct _ParametersData {
+ FILE *fp;
+ guint len;
+} ParametersData;
+
static void write_parameters(gpointer key, gpointer value, gpointer user_data)
{
gchar *param = key;
gchar *val = value, *valpos;
- FILE *fp = user_data;
+ ParametersData *pdata = (ParametersData *)user_data;
+ GString *buf = g_string_new("");
+
EncodeAs encas = ENC_AS_TOKEN;
for (valpos = val; *valpos != 0; valpos++) {
switch (encas) {
case ENC_AS_TOKEN:
- fprintf(fp, "; %s=", param);
- fprintf(fp, "%s", val);
+ g_string_append_printf(buf, "%s=%s", param, val);
break;
case ENC_AS_QUOTED_STRING:
- fprintf(fp, "; %s=", param);
- fprintf(fp, "\"%s\"", val);
+ g_string_append_printf(buf, "%s=\"%s\"", param, val);
break;
case ENC_AS_EXTENDED:
- fprintf(fp, "; %s*=", param);
- fprintf(fp, "%s''", conv_get_current_charset_str());
+ if (!g_utf8_validate(val, -1, NULL))
+ g_string_append_printf(buf, "%s*=%s''", param,
+ conv_get_locale_charset_str());
+ else
+ g_string_append_printf(buf, "%s*=%s''", param,
+ CS_INTERNAL);
for (valpos = val; *valpos != '\0'; valpos++) {
- if (IS_ASCII(*valpos) && isalnum(*valpos))
- fprintf(fp, "%c", *valpos);
- else {
+ if (IS_ASCII(*valpos) && isalnum(*valpos)) {
+ g_string_append_printf(buf, "%c", *valpos);
+ } else {
gchar hexstr[3] = "XX";
get_hex_str(hexstr, *valpos);
- fprintf(fp, "%%%s", hexstr);
+ g_string_append_printf(buf, "%%%s", hexstr);
}
}
break;
}
+
+ if (buf->str && strlen(buf->str)) {
+ if (pdata->len + strlen(buf->str) + 2 > 76) {
+ fprintf(pdata->fp, ";\n %s", buf->str);
+ pdata->len = strlen(buf->str) + 1;
+ } else {
+ fprintf(pdata->fp, "; %s", buf->str);
+ pdata->len += strlen(buf->str) + 2;
+ }
+ }
+ g_string_free(buf, TRUE);
}
void procmime_write_mime_header(MimeInfo *mimeinfo, FILE *fp)
{
struct TypeTable *type_table;
-
+ ParametersData *pdata = g_new0(ParametersData, 1);
debug_print("procmime_write_mime_header\n");
+
+ pdata->fp = fp;
for (type_table = mime_type_table; type_table->str != NULL; type_table++)
if (mimeinfo->type == type_table->type) {
- fprintf(fp, "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
+ gchar *buf = g_strdup_printf(
+ "Content-Type: %s/%s", type_table->str, mimeinfo->subtype);
+ fprintf(fp, "%s", buf);
+ pdata->len = strlen(buf);
+ g_free(buf);
break;
}
- g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, fp);
+ g_hash_table_foreach(mimeinfo->typeparameters, write_parameters, pdata);
+ g_free(pdata);
+
fprintf(fp, "\n");
if (mimeinfo->encoding_type != ENC_UNKNOWN)
fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
- fprintf(fp, "Content-Disposition: ");
+ ParametersData *pdata = g_new0(ParametersData, 1);
+ gchar *buf = NULL;
if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
- fprintf(fp, "inline");
+ buf = g_strdup("Content-Disposition: inline");
else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
- fprintf(fp, "attachment");
+ buf = g_strdup("Content-Disposition: attachment");
else
- fprintf(fp, "unknown");
+ buf = g_strdup("Content-Disposition: unknown");
+
+ fprintf(fp, "%s", buf);
+ pdata->len = strlen(buf);
+ g_free(buf);
- /* FIXME: linebreaks after too many parameters */
- g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, fp);
+ pdata->fp = fp;
+ g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
+ g_free(pdata);
fprintf(fp, "\n");
}
break;
case MIMECONTENT_MEM:
- fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
+ fwrite(mimeinfo->data.mem,
+ sizeof(gchar),
+ strlen(mimeinfo->data.mem),
+ fp);
break;
default:
if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
(*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
*(str2 - 2) = '\0';
- fwrite(str, strlen(str), sizeof(gchar), fp);
+ fwrite(str, sizeof(gchar), strlen(str), fp);
g_free(str);
break;
return 0;
case MIMECONTENT_MEM:
- fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
+ fwrite(mimeinfo->data.mem,
+ sizeof(gchar),
+ strlen(mimeinfo->data.mem),
+ fp);
return 0;
default:
return 0;
}
+
+gchar *procmime_get_part_file_name(MimeInfo *mimeinfo)
+{
+ gchar *base;
+
+ if ((mimeinfo->type == MIMETYPE_TEXT) && !g_ascii_strcasecmp(mimeinfo->subtype, "html"))
+ base = g_strdup("mimetmp.html");
+ else {
+ const gchar *basetmp;
+ gchar *basename;
+
+ basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
+ if (basetmp == NULL)
+ basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "name");
+ if (basetmp == NULL)
+ basetmp = "mimetmp";
+ basename = g_path_get_basename(basetmp);
+ if (*basename == '\0') {
+ g_free(basename);
+ basename = g_strdup("mimetmp");
+ }
+ base = conv_filename_from_utf8(basename);
+ g_free(basename);
+ subst_for_shellsafe_filename(base);
+ }
+
+ return base;
+}
+