/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2003 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2004 Hiroyuki Yamamoto & The Sylpheed-Claws Team
*
* 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
#include "defs.h"
+#include <stdio.h>
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <ctype.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
#include "intl.h"
#include "procmime.h"
#include "codeconv.h"
#include "utils.h"
#include "prefs_common.h"
-
-#if USE_GPGME
-# include "rfc2015.h"
-#endif
-
#include "prefs_gtk.h"
static GHashTable *procmime_get_mime_type_table (void);
MimeInfo *mimeinfo;
mimeinfo = g_new0(MimeInfo, 1);
- mimeinfo->mime_type = MIME_UNKNOWN;
- mimeinfo->encoding_type = ENC_UNKNOWN;
+ mimeinfo->content = MIMECONTENT_EMPTY;
+ mimeinfo->data.filename = NULL;
+
+ mimeinfo->type = MIMETYPE_UNKNOWN;
+ mimeinfo->encoding_type = ENC_UNKNOWN;
+ mimeinfo->typeparameters = g_hash_table_new(g_str_hash, g_str_equal);
+ mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
+ mimeinfo->dispositionparameters
+ = g_hash_table_new(g_str_hash, g_str_equal);
+
+ mimeinfo->node = g_node_new(mimeinfo);
+
return mimeinfo;
}
-void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
+static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
{
- while (mimeinfo != NULL) {
- MimeInfo *next;
-
- g_free(mimeinfo->encoding);
- g_free(mimeinfo->content_type);
- g_free(mimeinfo->charset);
- g_free(mimeinfo->name);
- g_free(mimeinfo->boundary);
- g_free(mimeinfo->content_disposition);
- g_free(mimeinfo->filename);
-#if USE_GPGME
- g_free(mimeinfo->plaintextfile);
- g_free(mimeinfo->sigstatus);
- g_free(mimeinfo->sigstatus_full);
-#endif
+ g_free(key);
+ g_free(value);
+
+ return TRUE;
+}
- procmime_mimeinfo_free_all(mimeinfo->sub);
- procmime_mimeinfo_free_all(mimeinfo->children);
-#if USE_GPGME
- procmime_mimeinfo_free_all(mimeinfo->plaintext);
-#endif
+static gchar *forced_charset = NULL;
+
+void procmime_force_charset(const gchar *str)
+{
+ g_free(forced_charset);
+ forced_charset = NULL;
+ if (str)
+ forced_charset = g_strdup(str);
+}
- next = mimeinfo->next;
- g_free(mimeinfo);
- mimeinfo = next;
+static EncodingType forced_encoding = 0;
+
+void procmime_force_encoding(EncodingType encoding)
+{
+ forced_encoding = encoding;
+}
+
+static gboolean free_func(GNode *node, gpointer data)
+{
+ MimeInfo *mimeinfo = (MimeInfo *) node->data;
+
+ switch (mimeinfo->content) {
+ case MIMECONTENT_FILE:
+ if (mimeinfo->tmp)
+ unlink(mimeinfo->data.filename);
+ g_free(mimeinfo->data.filename);
+ break;
+
+ case MIMECONTENT_MEM:
+ if (mimeinfo->tmp)
+ g_free(mimeinfo->data.mem);
+ default:
+ break;
}
+
+ g_free(mimeinfo->subtype);
+ g_free(mimeinfo->description);
+ g_free(mimeinfo->id);
+
+ g_hash_table_foreach_remove(mimeinfo->typeparameters,
+ procmime_mimeinfo_parameters_destroy, NULL);
+ g_hash_table_destroy(mimeinfo->typeparameters);
+ g_hash_table_foreach_remove(mimeinfo->dispositionparameters,
+ procmime_mimeinfo_parameters_destroy, NULL);
+ g_hash_table_destroy(mimeinfo->dispositionparameters);
+
+ if (mimeinfo->privacy)
+ privacy_free_privacydata(mimeinfo->privacy);
+
+ g_free(mimeinfo);
+
+ return FALSE;
+}
+
+void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
+{
+ GNode *node;
+
+ g_return_if_fail(mimeinfo);
+
+ node = mimeinfo->node;
+ g_node_traverse(node, G_IN_ORDER, G_TRAVERSE_ALL, -1, free_func, NULL);
+
+ g_node_destroy(node);
}
+#if 0 /* UNUSED */
MimeInfo *procmime_mimeinfo_insert(MimeInfo *parent, MimeInfo *mimeinfo)
{
MimeInfo *child = parent->children;
child = new;
}
}
-
-MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
-{
- if (!mimeinfo) return NULL;
-
- if (mimeinfo->children)
- return mimeinfo->children;
- if (mimeinfo->sub)
- return mimeinfo->sub;
- if (mimeinfo->next)
- return mimeinfo->next;
-
- if (mimeinfo->main) {
- mimeinfo = mimeinfo->main;
- if (mimeinfo->next)
- return mimeinfo->next;
- }
-
- for (mimeinfo = mimeinfo->parent; mimeinfo != NULL;
- mimeinfo = mimeinfo->parent) {
- if (mimeinfo->next)
- return mimeinfo->next;
- if (mimeinfo->main) {
- mimeinfo = mimeinfo->main;
- if (mimeinfo->next)
- return mimeinfo->next;
- }
- }
-
- return NULL;
-}
-
-MimeInfo *procmime_scan_message(MsgInfo *msginfo)
-{
- FILE *fp;
- MimeInfo *mimeinfo;
-
- g_return_val_if_fail(msginfo != NULL, NULL);
-
- if ((fp = procmsg_open_message(msginfo)) == NULL) return NULL;
- mimeinfo = procmime_scan_mime_header(fp);
-
- if (mimeinfo) {
- if (mimeinfo->mime_type != MIME_MULTIPART) {
- if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
- perror("fseek");
- }
- if (mimeinfo->mime_type != MIME_TEXT)
- procmime_scan_multipart_message(mimeinfo, fp);
- }
-
-#if USE_GPGME
- if (prefs_common.auto_check_signatures)
- rfc2015_check_signature(mimeinfo, fp);
#endif
- fclose(fp);
-
- return mimeinfo;
-}
-
-void procmime_scan_multipart_message(MimeInfo *mimeinfo, FILE *fp)
-{
- gchar *p;
- gchar *boundary;
- gint boundary_len = 0;
- gchar buf[BUFFSIZE];
- glong fpos, prev_fpos;
- gint npart;
-
- g_return_if_fail(mimeinfo != NULL);
- g_return_if_fail(mimeinfo->mime_type != MIME_TEXT);
-
- if (mimeinfo->mime_type == MIME_MULTIPART) {
- g_return_if_fail(mimeinfo->boundary != NULL);
- g_return_if_fail(mimeinfo->sub == NULL);
- }
- g_return_if_fail(fp != NULL);
-
- boundary = mimeinfo->boundary;
-
- if (boundary) {
- boundary_len = strlen(boundary);
-
- /* look for first boundary */
- while ((p = fgets(buf, sizeof(buf), fp)) != NULL)
- if (IS_BOUNDARY(buf, boundary, boundary_len)) break;
- if (!p) return;
- }
-
- if ((fpos = ftell(fp)) < 0) {
- perror("ftell");
- return;
- }
-
- for (npart = 0;; npart++) {
- MimeInfo *partinfo;
- gboolean eom = FALSE;
-
- prev_fpos = fpos;
- debug_print("prev_fpos: %ld\n", fpos);
-
- partinfo = procmime_scan_mime_header(fp);
- if (!partinfo) break;
- procmime_mimeinfo_insert(mimeinfo, partinfo);
-
- if (partinfo->mime_type == MIME_MULTIPART) {
- if (partinfo->level < 8)
- procmime_scan_multipart_message(partinfo, fp);
- } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) {
- MimeInfo *sub;
-
- partinfo->sub = sub = procmime_scan_mime_header(fp);
- if (!sub) break;
-
- sub->level = partinfo->level + 1;
- sub->parent = partinfo;
- sub->main = partinfo;
-
- if (sub->level < 8) {
- if (sub->mime_type == MIME_MULTIPART) {
- procmime_scan_multipart_message
- (sub, fp);
- } else if (sub->mime_type == MIME_MESSAGE_RFC822) {
- fseek(fp, sub->fpos, SEEK_SET);
- procmime_scan_multipart_message
- (sub, fp);
- }
- }
- }
-
- /* look for next boundary */
- buf[0] = '\0';
- while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
- if (IS_BOUNDARY(buf, boundary, boundary_len)) {
- if (buf[2 + boundary_len] == '-' &&
- buf[2 + boundary_len + 1] == '-')
- eom = TRUE;
- break;
- }
- }
- if (p == NULL) {
- /* broken MIME, or single part MIME message */
- buf[0] = '\0';
- eom = TRUE;
- }
- fpos = ftell(fp);
- debug_print("fpos: %ld\n", fpos);
-
- partinfo->size = fpos - prev_fpos - strlen(buf);
- debug_print("partinfo->size: %d\n", partinfo->size);
- if (partinfo->sub && !partinfo->sub->sub &&
- !partinfo->sub->children) {
- partinfo->sub->size = fpos - partinfo->sub->fpos - strlen(buf);
- debug_print("partinfo->sub->size: %d\n",
- partinfo->sub->size);
- }
- debug_print("boundary: %s\n", buf);
- if (eom) break;
- }
-}
-
-void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
+MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
{
- gchar *buf;
-
- Xstrdup_a(buf, encoding, return);
-
- g_free(mimeinfo->encoding);
-
- mimeinfo->encoding = g_strdup(g_strstrip(buf));
- if (!strcasecmp(buf, "7bit"))
- mimeinfo->encoding_type = ENC_7BIT;
- else if (!strcasecmp(buf, "8bit"))
- mimeinfo->encoding_type = ENC_8BIT;
- else if (!strcasecmp(buf, "quoted-printable"))
- mimeinfo->encoding_type = ENC_QUOTED_PRINTABLE;
- else if (!strcasecmp(buf, "base64"))
- mimeinfo->encoding_type = ENC_BASE64;
- else if (!strcasecmp(buf, "x-uuencode"))
- mimeinfo->encoding_type = ENC_X_UUENCODE;
- else
- mimeinfo->encoding_type = ENC_UNKNOWN;
+ g_return_val_if_fail(mimeinfo != NULL, NULL);
+ g_return_val_if_fail(mimeinfo->node != NULL, NULL);
+ if (mimeinfo->node->parent == NULL)
+ return NULL;
+ return (MimeInfo *) mimeinfo->node->parent->data;
}
-void procmime_scan_content_type(MimeInfo *mimeinfo, const gchar *content_type)
+MimeInfo *procmime_mimeinfo_next(MimeInfo *mimeinfo)
{
- gchar *delim, *p, *cnttype;
- gchar *buf;
+ g_return_val_if_fail(mimeinfo != NULL, NULL);
+ g_return_val_if_fail(mimeinfo->node != NULL, NULL);
- if (conv_get_current_charset() == C_EUC_JP &&
- strchr(content_type, '\033')) {
- gint len;
- len = strlen(content_type) * 2 + 1;
- Xalloca(buf, len, return);
- conv_jistoeuc(buf, len, content_type);
- } else
- Xstrdup_a(buf, content_type, return);
-
- g_free(mimeinfo->content_type);
- g_free(mimeinfo->charset);
- /* g_free(mimeinfo->name); */
- mimeinfo->content_type = NULL;
- mimeinfo->charset = NULL;
- /* mimeinfo->name = NULL; */
-
- if ((delim = strchr(buf, ';'))) *delim = '\0';
- mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
-
- mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
-
- if (!delim) return;
- p = delim + 1;
-
- for (;;) {
- gchar *eq;
- gchar *attr, *value;
-
- if ((delim = strchr(p, ';'))) *delim = '\0';
-
- if (!(eq = strchr(p, '='))) break;
-
- *eq = '\0';
- attr = p;
- g_strstrip(attr);
- value = eq + 1;
- g_strstrip(value);
-
- if (*value == '"')
- extract_quote(value, '"');
- else {
- eliminate_parenthesis(value, '(', ')');
- g_strstrip(value);
- }
+ if (mimeinfo->node->children)
+ return (MimeInfo *) mimeinfo->node->children->data;
+ if (mimeinfo->node->next)
+ return (MimeInfo *) mimeinfo->node->next->data;
- if (*value) {
- if (!strcasecmp(attr, "charset"))
- mimeinfo->charset = g_strdup(value);
- else if (!strcasecmp(attr, "name")) {
- gchar *tmp;
- size_t len;
-
- len = strlen(value) + 1;
- Xalloca(tmp, len, return);
- conv_unmime_header(tmp, len, value, NULL);
- g_free(mimeinfo->name);
- /*pgp signatures should NOT have a name */
- if (mimeinfo->content_type
- && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
- mimeinfo->name = g_strdup(tmp);
- } else if (!strcasecmp(attr, "boundary"))
- mimeinfo->boundary = g_strdup(value);
- }
+ if (mimeinfo->node->parent == NULL)
+ return NULL;
- if (!delim) break;
- p = delim + 1;
+ while (mimeinfo->node->parent != NULL) {
+ mimeinfo = (MimeInfo *) mimeinfo->node->parent->data;
+ if (mimeinfo->node->next)
+ return (MimeInfo *) mimeinfo->node->next->data;
}
- if (mimeinfo->mime_type == MIME_MULTIPART && !mimeinfo->boundary)
- mimeinfo->mime_type = MIME_TEXT;
+ return NULL;
}
-void procmime_scan_content_disposition(MimeInfo *mimeinfo,
- const gchar *content_disposition)
+MimeInfo *procmime_scan_message(MsgInfo *msginfo)
{
- gchar *delim, *p, *dispos;
- gchar *buf;
-
- if (conv_get_current_charset() == C_EUC_JP &&
- strchr(content_disposition, '\033')) {
- gint len;
- len = strlen(content_disposition) * 2 + 1;
- Xalloca(buf, len, return);
- conv_jistoeuc(buf, len, content_disposition);
- } else
- Xstrdup_a(buf, content_disposition, return);
-
- if ((delim = strchr(buf, ';'))) *delim = '\0';
- mimeinfo->content_disposition = dispos = g_strdup(g_strstrip(buf));
-
- if (!delim) return;
- p = delim + 1;
-
- for (;;) {
- gchar *eq;
- gchar *attr, *value;
-
- if ((delim = strchr(p, ';'))) *delim = '\0';
-
- if (!(eq = strchr(p, '='))) break;
-
- *eq = '\0';
- attr = p;
- g_strstrip(attr);
- value = eq + 1;
- g_strstrip(value);
-
- if (*value == '"')
- extract_quote(value, '"');
- else {
- eliminate_parenthesis(value, '(', ')');
- g_strstrip(value);
- }
-
- if (*value) {
- if (!strcasecmp(attr, "filename")) {
- gchar *tmp;
- size_t len;
-
- len = strlen(value) + 1;
- Xalloca(tmp, len, return);
- conv_unmime_header(tmp, len, value, NULL);
- g_free(mimeinfo->filename);
- /*pgp signatures should NOT have a name */
- if (mimeinfo->content_type
- && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
- mimeinfo->filename = g_strdup(tmp);
- break;
- }
- }
+ gchar *filename;
+ MimeInfo *mimeinfo;
- if (!delim) break;
- p = delim + 1;
+ filename = procmsg_get_message_file_path(msginfo);
+ if (!filename || !is_file_exist(filename)) {
+ g_free(filename);
+ filename = procmsg_get_message_file(msginfo);
}
-}
-
-void procmime_scan_content_description(MimeInfo *mimeinfo,
- const gchar *content_description)
-{
- gchar *buf;
-
- gchar *tmp;
- size_t blen;
-
- if (conv_get_current_charset() == C_EUC_JP &&
- strchr(content_description, '\033')) {
- gint len;
- len = strlen(content_description) * 2 + 1;
- Xalloca(buf, len, return);
- conv_jistoeuc(buf, len, content_description);
- } else
- Xstrdup_a(buf, content_description, return);
-
- blen = strlen(buf) + 1;
- Xalloca(tmp, blen, return);
- conv_unmime_header(tmp, blen, buf, NULL);
- g_free(mimeinfo->name);
- mimeinfo->name = NULL;
- /*pgp signatures should NOT have a name */
- if (mimeinfo->content_type
- && strcasecmp(mimeinfo->content_type, "application/pgp-signature"))
- mimeinfo->name = g_strdup(tmp);
-}
-
-void procmime_scan_subject(MimeInfo *mimeinfo,
- const gchar *subject)
-{
- gchar *buf;
+ if (!filename || !is_file_exist(filename))
+ return NULL;
- gchar *tmp;
- size_t blen;
+ if (msginfo->folder->stype != F_QUEUE &&
+ msginfo->folder->stype != F_DRAFT)
+ mimeinfo = procmime_scan_file(filename);
+ else
+ mimeinfo = procmime_scan_queue_file(filename);
+ g_free(filename);
- if (conv_get_current_charset() == C_EUC_JP &&
- strchr(subject, '\033')) {
- gint len;
- len = strlen(subject) * 2 + 1;
- Xalloca(buf, len, return);
- conv_jistoeuc(buf, len, subject);
- } else
- Xstrdup_a(buf, subject, return);
-
- blen = strlen(buf) + 1;
- Xalloca(tmp, blen, return);
- conv_unmime_header(tmp, blen, buf, NULL);
- g_free(mimeinfo->name);
- mimeinfo->name = g_strdup(tmp);
+ return mimeinfo;
}
enum
H_SUBJECT = 4
};
-MimeInfo *procmime_scan_mime_header(FILE *fp)
+const gchar *procmime_mimeinfo_get_parameter(MimeInfo *mimeinfo, const gchar *name)
{
- static HeaderEntry hentry[] = {{"Content-Transfer-Encoding:",
- NULL, FALSE},
- {"Content-Type:", NULL, TRUE},
- {"Content-Disposition:",
- NULL, TRUE},
- {"Content-description:",
- NULL, TRUE},
- {"Subject:",
- NULL, TRUE},
- {NULL, NULL, FALSE}};
- gchar buf[BUFFSIZE];
- gint hnum;
- HeaderEntry *hp;
- MimeInfo *mimeinfo;
-
- g_return_val_if_fail(fp != NULL, NULL);
-
- mimeinfo = procmime_mimeinfo_new();
- mimeinfo->mime_type = MIME_TEXT;
- mimeinfo->encoding_type = ENC_7BIT;
- mimeinfo->fpos = ftell(fp);
-
- while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, hentry))
- != -1) {
- hp = hentry + hnum;
-
- if (H_CONTENT_TRANSFER_ENCODING == hnum) {
- procmime_scan_encoding
- (mimeinfo, buf + strlen(hp->name));
- } else if (H_CONTENT_TYPE == hnum) {
- procmime_scan_content_type
- (mimeinfo, buf + strlen(hp->name));
- } else if (H_CONTENT_DISPOSITION == hnum) {
- procmime_scan_content_disposition
- (mimeinfo, buf + strlen(hp->name));
- } else if (H_CONTENT_DESCRIPTION == hnum) {
- procmime_scan_content_description
- (mimeinfo, buf + strlen(hp->name));
- } else if (H_SUBJECT == hnum) {
- procmime_scan_subject
- (mimeinfo, buf + strlen(hp->name));
- }
- }
-
- if (mimeinfo->mime_type == MIME_APPLICATION_OCTET_STREAM &&
- mimeinfo->name) {
- const gchar *type;
- type = procmime_get_mime_type(mimeinfo->name);
- if (type)
- mimeinfo->mime_type = procmime_scan_mime_type(type);
- }
+ const gchar *value;
- if (!mimeinfo->content_type)
- mimeinfo->content_type = g_strdup("text/plain");
+ g_return_val_if_fail(mimeinfo != NULL, NULL);
+ g_return_val_if_fail(name != NULL, NULL);
- return mimeinfo;
+ value = g_hash_table_lookup(mimeinfo->dispositionparameters, name);
+ if (value == NULL)
+ value = g_hash_table_lookup(mimeinfo->typeparameters, name);
+
+ return value;
}
-FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
+gboolean procmime_decode_content(MimeInfo *mimeinfo)
{
gchar buf[BUFFSIZE];
- gchar *boundary = NULL;
- gint boundary_len = 0;
+ gint readend;
+ gchar *tmpfilename;
+ gchar *mimetmpdir;
+ FILE *outfp, *infp;
+ struct stat statbuf;
gboolean tmp_file = FALSE;
- g_return_val_if_fail(infp != NULL, NULL);
- g_return_val_if_fail(mimeinfo != NULL, NULL);
+ EncodingType encoding = forced_encoding
+ ? forced_encoding
+ : mimeinfo->encoding_type;
+
+ g_return_val_if_fail(mimeinfo != NULL, FALSE);
- if (!outfp) {
- outfp = my_tmpfile();
- if (!outfp) {
- perror("tmpfile");
- return NULL;
- }
- tmp_file = TRUE;
+ if (encoding == ENC_UNKNOWN ||
+ encoding == ENC_BINARY)
+ return TRUE;
+
+ infp = fopen(mimeinfo->data.filename, "rb");
+ if (!infp) {
+ perror("fopen");
+ return FALSE;
}
+ fseek(infp, mimeinfo->offset, SEEK_SET);
- if (mimeinfo->parent && mimeinfo->parent->boundary) {
- boundary = mimeinfo->parent->boundary;
- boundary_len = strlen(boundary);
+ mimetmpdir = get_mime_tmp_dir();
+ outfp = get_tmpfile_in_dir(mimetmpdir, &tmpfilename);
+ if (!outfp) {
+ perror("tmpfile");
+ return FALSE;
}
+ tmp_file = TRUE;
+ readend = mimeinfo->offset + mimeinfo->length;
- if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
- while (fgets(buf, sizeof(buf), infp) != NULL &&
- (!boundary ||
- !IS_BOUNDARY(buf, boundary, boundary_len))) {
+ 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);
}
- } else if (mimeinfo->encoding_type == ENC_BASE64) {
+ } else if (encoding == ENC_BASE64) {
gchar outbuf[BUFFSIZE];
gint len;
Base64Decoder *decoder;
+ 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 (fgets(buf, sizeof(buf), infp) != NULL &&
- (!boundary ||
- !IS_BOUNDARY(buf, boundary, boundary_len))) {
+ 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;
}
- fwrite(outbuf, sizeof(gchar), len, outfp);
+ fwrite(outbuf, sizeof(gchar), len, tmpfp);
}
base64_decoder_free(decoder);
- } else if (mimeinfo->encoding_type == ENC_X_UUENCODE) {
+
+ 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 flag = FALSE;
- while (fgets(buf, sizeof(buf), infp) != NULL &&
- (!boundary ||
- !IS_BOUNDARY(buf, boundary, boundary_len))) {
- if(!flag && strncmp(buf,"begin ", 6)) continue;
+ while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
+ if (!flag && strncmp(buf,"begin ", 6)) continue;
if (flag) {
len = fromuutobits(outbuf, buf);
flag = TRUE;
}
} else {
- while (fgets(buf, sizeof(buf), infp) != NULL &&
- (!boundary ||
- !IS_BOUNDARY(buf, boundary, boundary_len))) {
+ while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
fputs(buf, outfp);
}
}
- if (tmp_file) rewind(outfp);
- return outfp;
+ fclose(outfp);
+ fclose(infp);
+
+ stat(tmpfilename, &statbuf);
+ if (mimeinfo->tmp && (mimeinfo->data.filename != NULL))
+ unlink(mimeinfo->data.filename);
+ if (mimeinfo->data.filename != NULL)
+ g_free(mimeinfo->data.filename);
+ mimeinfo->data.filename = tmpfilename;
+ mimeinfo->tmp = TRUE;
+ mimeinfo->offset = 0;
+ mimeinfo->length = statbuf.st_size;
+ mimeinfo->encoding_type = ENC_BINARY;
+
+ return TRUE;
+}
+
+#define B64_LINE_SIZE 57
+#define B64_BUFFSIZE 77
+
+gboolean procmime_encode_content(MimeInfo *mimeinfo, EncodingType encoding)
+{
+ FILE *infp, *outfp;
+ gint len;
+ gchar *tmpfilename;
+ gchar *mimetmpdir;
+ struct stat statbuf;
+
+ if (mimeinfo->encoding_type != ENC_UNKNOWN &&
+ mimeinfo->encoding_type != ENC_BINARY &&
+ mimeinfo->encoding_type != ENC_7BIT &&
+ mimeinfo->encoding_type != ENC_8BIT)
+ if(!procmime_decode_content(mimeinfo))
+ return FALSE;
+
+ mimetmpdir = get_mime_tmp_dir();
+ outfp = get_tmpfile_in_dir(mimetmpdir, &tmpfilename);
+ if (!outfp) {
+ perror("tmpfile");
+ return FALSE;
+ }
+
+ 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;
+ }
+ } 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);
+ tmp_fp = infp = str_open_as_stream(out);
+ g_free(out);
+ if (infp == NULL)
+ return FALSE;
+ }
+ }
+
+ while ((len = fread(inbuf, sizeof(gchar),
+ 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(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);
+
+ 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];
+
+ while (fgets(buf, sizeof(buf), infp) != NULL) {
+ strcrchomp(buf);
+ fputs(buf, outfp);
+ }
+ }
+
+ 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);
+ mimeinfo->content = MIMECONTENT_FILE;
+ mimeinfo->data.filename = tmpfilename;
+ mimeinfo->tmp = TRUE;
+ mimeinfo->offset = 0;
+ mimeinfo->length = statbuf.st_size;
+ mimeinfo->encoding_type = encoding;
+
+ return TRUE;
}
-gint procmime_get_part(const gchar *outfile, const gchar *infile,
- MimeInfo *mimeinfo)
+gint procmime_get_part(const gchar *outfile, MimeInfo *mimeinfo)
{
FILE *infp, *outfp;
gchar buf[BUFFSIZE];
+ gint restlength, readlength;
g_return_val_if_fail(outfile != NULL, -1);
- g_return_val_if_fail(infile != NULL, -1);
g_return_val_if_fail(mimeinfo != NULL, -1);
- if ((infp = fopen(infile, "rb")) == NULL) {
- FILE_OP_ERROR(infile, "fopen");
+ if (mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
+ return -1;
+
+ if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
return -1;
}
- if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
- FILE_OP_ERROR(infile, "fseek");
+ if (fseek(infp, mimeinfo->offset, SEEK_SET) < 0) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "fseek");
fclose(infp);
return -1;
}
return -1;
}
- while (fgets(buf, sizeof(buf), infp) != NULL)
- if (buf[0] == '\r' || buf[0] == '\n') break;
+ restlength = mimeinfo->length;
- procmime_decode_content(outfp, infp, mimeinfo);
+ while ((restlength > 0) && ((readlength = fread(buf, 1, restlength > BUFFSIZE ? BUFFSIZE : restlength, infp)) > 0)) {
+ fwrite(buf, 1, readlength, outfp);
+ restlength -= readlength;
+ }
fclose(infp);
if (fclose(outfp) == EOF) {
g_free(rcpath);
- for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
+ for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
struct ContentRenderer * renderer;
renderer = cur->data;
fprintf(pfile->fp, "%s %s\n", renderer->content_type,
}
}
-FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
+FILE *procmime_get_text_content(MimeInfo *mimeinfo)
{
FILE *tmpfp, *outfp;
- gchar *src_codeset;
+ const gchar *src_codeset;
gboolean conv_fail = FALSE;
gchar buf[BUFFSIZE];
gchar *str;
struct ContentRenderer * renderer;
GList * cur;
-
+ gchar *tmpfile, *content_type;
+
g_return_val_if_fail(mimeinfo != NULL, NULL);
- g_return_val_if_fail(infp != NULL, NULL);
- g_return_val_if_fail(mimeinfo->mime_type == MIME_TEXT ||
- mimeinfo->mime_type == MIME_TEXT_HTML ||
- mimeinfo->mime_type == MIME_TEXT_ENRICHED, NULL);
- if (fseek(infp, mimeinfo->fpos, SEEK_SET) < 0) {
- perror("fseek");
+ if (!procmime_decode_content(mimeinfo))
return NULL;
- }
- while (fgets(buf, sizeof(buf), infp) != NULL)
- if (buf[0] == '\r' || buf[0] == '\n') break;
+ tmpfile = procmime_get_tmp_file_name(mimeinfo);
+ if (tmpfile == NULL)
+ return NULL;
+
+ if (procmime_get_part(tmpfile, mimeinfo) < 0) {
+ g_free(tmpfile);
+ return NULL;
+ }
- tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
- if (!tmpfp)
+ tmpfp = fopen(tmpfile, "rb");
+ if (tmpfp == NULL) {
+ g_free(tmpfile);
return NULL;
+ }
if ((outfp = my_tmpfile()) == NULL) {
perror("tmpfile");
fclose(tmpfp);
+ g_free(tmpfile);
return NULL;
}
- src_codeset = prefs_common.force_charset
- ? prefs_common.force_charset : mimeinfo->charset;
+ src_codeset = forced_charset
+ ? forced_charset :
+ procmime_mimeinfo_get_parameter(mimeinfo, "charset");
renderer = NULL;
- for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
+ content_type = procmime_get_content_type_str(mimeinfo->type,
+ mimeinfo->subtype);
+ for (cur = renderer_list ; cur != NULL ; cur = cur->next) {
struct ContentRenderer * cr;
+
cr = cur->data;
- if (g_strcasecmp(cr->content_type,
- mimeinfo->content_type) == 0) {
+ if (g_strcasecmp(cr->content_type, content_type) == 0) {
renderer = cr;
break;
}
}
+ g_free(content_type);
if (renderer != NULL) {
FILE * p;
}
dup2(oldout, 1);
- } else if (mimeinfo->mime_type == MIME_TEXT) {
- while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
- str = conv_codeset_strdup(buf, src_codeset, NULL);
- if (str) {
- fputs(str, outfp);
- g_free(str);
- } else {
- conv_fail = TRUE;
- fputs(buf, outfp);
- }
- }
- } else if (mimeinfo->mime_type == MIME_TEXT_HTML) {
+ } else if (mimeinfo->type == MIMETYPE_TEXT && !g_strcasecmp(mimeinfo->subtype, "html")) {
HTMLParser *parser;
CodeConverter *conv;
}
html_parser_destroy(parser);
conv_code_converter_destroy(conv);
- } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
+ } else if (mimeinfo->type == MIMETYPE_TEXT && !g_strcasecmp(mimeinfo->subtype, "enriched")) {
ERTFParser *parser;
CodeConverter *conv;
}
ertf_parser_destroy(parser);
conv_code_converter_destroy(conv);
+ } else if (mimeinfo->type == MIMETYPE_TEXT) {
+ while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
+ str = conv_codeset_strdup(buf, src_codeset, NULL);
+ if (str) {
+ fputs(str, outfp);
+ g_free(str);
+ } else {
+ conv_fail = TRUE;
+ fputs(buf, outfp);
+ }
+ }
}
if (conv_fail)
fclose(tmpfp);
rewind(outfp);
+ unlink(tmpfile);
+ g_free(tmpfile);
return outfp;
}
decode, convert it and output to outfp. */
FILE *procmime_get_first_text_content(MsgInfo *msginfo)
{
- FILE *infp, *outfp = NULL;
+ FILE *outfp = NULL;
MimeInfo *mimeinfo, *partinfo;
g_return_val_if_fail(msginfo != NULL, NULL);
mimeinfo = procmime_scan_message(msginfo);
if (!mimeinfo) return NULL;
- if ((infp = procmsg_open_message(msginfo)) == NULL) {
- procmime_mimeinfo_free_all(mimeinfo);
- return NULL;
- }
-
partinfo = mimeinfo;
- while (partinfo && partinfo->mime_type != MIME_TEXT)
+ while (partinfo && partinfo->type != MIMETYPE_TEXT)
partinfo = procmime_mimeinfo_next(partinfo);
- if (!partinfo) {
- partinfo = mimeinfo;
- while (partinfo && partinfo->mime_type != MIME_TEXT_HTML &&
- partinfo->mime_type != MIME_TEXT_ENRICHED)
- partinfo = procmime_mimeinfo_next(partinfo);
- }
-
if (partinfo)
- outfp = procmime_get_text_content(partinfo, infp);
+ outfp = procmime_get_text_content(partinfo);
- fclose(infp);
procmime_mimeinfo_free_all(mimeinfo);
return outfp;
}
gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
- const gchar *str, gboolean case_sens)
+ const gchar *str, StrFindFunc find_func)
{
-
- FILE *infp, *outfp;
+ 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->mime_type == MIME_TEXT ||
- mimeinfo->mime_type == MIME_TEXT_HTML ||
- mimeinfo->mime_type == MIME_TEXT_ENRICHED, 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);
- if ((infp = fopen(filename, "rb")) == NULL) {
- FILE_OP_ERROR(filename, "fopen");
- return FALSE;
- }
-
- outfp = procmime_get_text_content(mimeinfo, infp);
- fclose(infp);
+ 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;
for (partinfo = mimeinfo; partinfo != NULL;
partinfo = procmime_mimeinfo_next(partinfo)) {
- if (partinfo->mime_type == MIME_TEXT ||
- partinfo->mime_type == MIME_TEXT_HTML ||
- partinfo->mime_type == MIME_TEXT_ENRICHED) {
+ 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;
}
g_snprintf(f_prefix, sizeof(f_prefix), "%08x.", id++);
- if (MIME_TEXT_HTML == mimeinfo->mime_type)
+ if ((mimeinfo->type == MIMETYPE_TEXT) && !g_strcasecmp(mimeinfo->subtype, "html"))
base = "mimetmp.html";
else {
- base = mimeinfo->filename ? mimeinfo->filename
- : mimeinfo->name ? mimeinfo->name : "mimetmp";
- base = g_basename(base);
+ const gchar *basetmp;
+
+ basetmp = procmime_mimeinfo_get_parameter(mimeinfo, "filename");
+ if (basetmp == NULL)
+ 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);
- subst_for_filename(base);
+ subst_for_shellsafe_filename(base);
}
filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
return filename;
}
-ContentType procmime_scan_mime_type(const gchar *mime_type)
-{
- ContentType type;
-
- if (!strncasecmp(mime_type, "text/html", 9))
- type = MIME_TEXT_HTML;
- else if (!strncasecmp(mime_type, "text/enriched", 13))
- type = MIME_TEXT_ENRICHED;
- else if (!strncasecmp(mime_type, "text/", 5))
- type = MIME_TEXT;
- else if (!strncasecmp(mime_type, "message/rfc822", 14))
- type = MIME_MESSAGE_RFC822;
- else if (!strncasecmp(mime_type, "message/", 8))
- type = MIME_TEXT;
- else if (!strncasecmp(mime_type, "application/octet-stream", 24))
- type = MIME_APPLICATION_OCTET_STREAM;
- else if (!strncasecmp(mime_type, "application/", 12))
- type = MIME_APPLICATION;
- else if (!strncasecmp(mime_type, "multipart/", 10))
- type = MIME_MULTIPART;
- else if (!strncasecmp(mime_type, "image/", 6))
- type = MIME_IMAGE;
- else if (!strncasecmp(mime_type, "audio/", 6))
- type = MIME_AUDIO;
- else if (!strcasecmp(mime_type, "text"))
- type = MIME_TEXT;
- else
- type = MIME_UNKNOWN;
-
- return type;
-}
-
static GList *mime_type_list = NULL;
gchar *procmime_get_mime_type(const gchar *filename)
const char *str1 = gptr1;
const char *str2 = gptr2;
- return !strcasecmp(str1, str2);
+ return !g_strcasecmp(str1, str2);
}
static GHashTable *procmime_get_mime_type_table(void)
GList *list = NULL;
FILE *fp;
gchar buf[BUFFSIZE];
- gchar *p, *delim;
+ guchar *p;
+ gchar *delim;
MimeType *mime_type;
if (mime_type_list)
{
if (!charset)
return ENC_8BIT;
- else if (!strncasecmp(charset, "ISO-2022-", 9) ||
- !strcasecmp(charset, "US-ASCII"))
+ else if (!g_strncasecmp(charset, "ISO-2022-", 9) ||
+ !g_strcasecmp(charset, "US-ASCII"))
return ENC_7BIT;
- else if (!strcasecmp(charset, "ISO-8859-5") ||
- !strncasecmp(charset, "KOI8-", 5) ||
- !strcasecmp(charset, "Windows-1251"))
- return ENC_BASE64;
- else if (!strncasecmp(charset, "ISO-8859-", 9))
+ else if (!g_strcasecmp(charset, "ISO-8859-5") ||
+ !g_strncasecmp(charset, "KOI8-", 5) ||
+ !g_strcasecmp(charset, "Windows-1251"))
+ return ENC_8BIT;
+ else if (!g_strncasecmp(charset, "ISO-8859-", 9))
return ENC_QUOTED_PRINTABLE;
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 NULL;
}
+
+/* --- NEW MIME STUFF --- */
+struct TypeTable
+{
+ gchar *str;
+ MimeMediaType type;
+};
+
+static struct TypeTable mime_type_table[] = {
+ {"text", MIMETYPE_TEXT},
+ {"image", MIMETYPE_IMAGE},
+ {"audio", MIMETYPE_AUDIO},
+ {"video", MIMETYPE_VIDEO},
+ {"application", MIMETYPE_APPLICATION},
+ {"message", MIMETYPE_MESSAGE},
+ {"multipart", MIMETYPE_MULTIPART},
+ {NULL, 0},
+};
+
+const gchar *procmime_get_media_type_str(MimeMediaType type)
+{
+ struct TypeTable *type_table;
+
+ for (type_table = mime_type_table; type_table->str != NULL; type_table++) {
+ if (type_table->type == type)
+ return type_table->str;
+ }
+ return NULL;
+}
+
+MimeMediaType procmime_get_media_type(const gchar *str)
+{
+ struct TypeTable *typetablearray;
+
+ for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++)
+ if (g_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0)
+ return typetablearray->type;
+
+ return MIMETYPE_UNKNOWN;
+}
+
+/*!
+ *\brief Safe wrapper for content type string.
+ *
+ *\return const gchar * Pointer to content type string.
+ */
+gchar *procmime_get_content_type_str(MimeMediaType type,
+ const char *subtype)
+{
+ const gchar *type_str = NULL;
+
+ if (subtype == NULL || !(type_str = procmime_get_media_type_str(type)))
+ return g_strdup("unknown");
+ return g_strdup_printf("%s/%s", type_str, subtype);
+}
+
+void procmime_parse_mimepart(MimeInfo *parent,
+ gchar *content_type,
+ gchar *content_encoding,
+ gchar *content_description,
+ gchar *content_id,
+ gchar *content_disposition,
+ const gchar *filename,
+ guint offset,
+ guint length);
+
+void procmime_parse_message_rfc822(MimeInfo *mimeinfo)
+{
+ HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
+ {"Content-Transfer-Encoding:",
+ NULL, FALSE},
+ {"Content-Description:",
+ NULL, TRUE},
+ {"Content-ID:",
+ NULL, TRUE},
+ {"Content-Disposition:",
+ NULL, TRUE},
+ {"MIME-Version:",
+ NULL, TRUE},
+ {NULL, NULL, FALSE}};
+ guint content_start, i;
+ FILE *fp;
+ gchar buf[BUFFSIZE];
+ gint mime_major, mime_minor;
+
+ procmime_decode_content(mimeinfo);
+
+ fp = fopen(mimeinfo->data.filename, "rb");
+ if (fp == NULL) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+ return;
+ }
+ fseek(fp, mimeinfo->offset, SEEK_SET);
+ procheader_get_header_fields(fp, hentry);
+ if (hentry[0].body != NULL)
+ conv_unmime_header(buf, sizeof(buf), hentry[0].body,
+ NULL);
+ if (hentry[2].body != NULL)
+ conv_unmime_header(buf, sizeof(buf), hentry[2].body,
+ NULL);
+ if (hentry[4].body != NULL)
+ conv_unmime_header(buf, sizeof(buf), hentry[4].body,
+ NULL);
+ 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->content = MIMECONTENT_FILE;
+ 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);
+ }
+ for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
+ g_free(hentry[i].body);
+ hentry[i].body = NULL;
+ }
+}
+
+void procmime_parse_multipart(MimeInfo *mimeinfo)
+{
+ HeaderEntry hentry[] = {{"Content-Type:", NULL, TRUE},
+ {"Content-Transfer-Encoding:",
+ NULL, FALSE},
+ {"Content-Description:",
+ NULL, TRUE},
+ {"Content-ID:",
+ NULL, TRUE},
+ {"Content-Disposition:",
+ NULL, TRUE},
+ {NULL, NULL, FALSE}};
+ gchar *p;
+ gchar *boundary;
+ gint boundary_len = 0, lastoffset = -1, i;
+ gchar buf[BUFFSIZE];
+ FILE *fp;
+
+ boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
+ if (!boundary)
+ return;
+ boundary_len = strlen(boundary);
+
+ procmime_decode_content(mimeinfo);
+
+ fp = fopen(mimeinfo->data.filename, "rb");
+ if (fp == NULL) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+ return;
+ }
+ fseek(fp, mimeinfo->offset, SEEK_SET);
+ while ((p = fgets(buf, sizeof(buf), fp)) != NULL) {
+ if (ftell(fp) > (mimeinfo->offset + mimeinfo->length))
+ break;
+
+ if (IS_BOUNDARY(buf, boundary, boundary_len)) {
+ if (lastoffset != -1) {
+ procmime_parse_mimepart(mimeinfo,
+ hentry[0].body, hentry[1].body,
+ hentry[2].body, hentry[3].body,
+ hentry[4].body,
+ mimeinfo->data.filename, lastoffset,
+ (ftell(fp) - strlen(buf)) - lastoffset - 1);
+ }
+
+ if (buf[2 + boundary_len] == '-' &&
+ buf[2 + boundary_len + 1] == '-')
+ break;
+
+ for (i = 0; i < (sizeof hentry / sizeof hentry[0]) ; i++) {
+ g_free(hentry[i].body);
+ hentry[i].body = NULL;
+ }
+ procheader_get_header_fields(fp, hentry);
+ if (hentry[0].body != NULL)
+ conv_unmime_header(buf, sizeof(buf), hentry[0].body,
+ NULL);
+ if (hentry[2].body != NULL)
+ conv_unmime_header(buf, sizeof(buf), hentry[2].body,
+ NULL);
+ if (hentry[4].body != NULL)
+ conv_unmime_header(buf, sizeof(buf), hentry[4].body,
+ NULL);
+ lastoffset = ftell(fp);
+ }
+ }
+ for (i = 0; i < (sizeof hentry / sizeof hentry[0]); i++) {
+ g_free(hentry[i].body);
+ hentry[i].body = NULL;
+ }
+ fclose(fp);
+}
+
+static void parse_parameters(const gchar *parameters, GHashTable *table)
+{
+ gchar *params, *param, *next;
+ GSList *convlist = NULL, *concatlist = NULL, *cur;
+
+ params = g_strdup(parameters);
+ param = params;
+ next = params;
+ for (; next != NULL; param = next) {
+ gchar *attribute, *value, *tmp;
+ gint len;
+ gboolean convert = FALSE;
+
+ next = strchr_with_skip_quote(param, '"', ';');
+ if (next != NULL) {
+ next[0] = '\0';
+ next++;
+ }
+
+ g_strstrip(param);
+
+ attribute = param;
+ value = strchr(attribute, '=');
+ if (value == NULL)
+ continue;
+
+ value[0] = '\0';
+ value++;
+
+ g_strdown(attribute);
+
+ len = strlen(attribute);
+ if (attribute[len - 1] == '*') {
+ gchar *srcpos, *dstpos, *endpos;
+
+ convert = TRUE;
+ attribute[len - 1] = '\0';
+
+ srcpos = value;
+ dstpos = value;
+ endpos = value + strlen(value);
+ while (srcpos < endpos) {
+ if (*srcpos != '%')
+ *dstpos = *srcpos;
+ else {
+ guchar dstvalue;
+
+ if (!get_hex_value(&dstvalue, srcpos[1], srcpos[2]))
+ *dstpos = '?';
+ else
+ *dstpos = dstvalue;
+ srcpos += 2;
+ }
+ srcpos++;
+ dstpos++;
+ }
+ *dstpos = '\0';
+ } else {
+ if (value[0] == '"')
+ extract_quote(value, '"');
+ else if ((tmp = strchr(value, ' ')) != NULL)
+ *tmp = '\0';
+ }
+
+ if (strrchr(attribute, '*') != NULL) {
+ gchar *tmpattr;
+
+ tmpattr = g_strdup(attribute);
+ tmp = strrchr(tmpattr, '*');
+ tmp[0] = '\0';
+
+ if ((tmp[1] == '0') && (tmp[2] == '\0') &&
+ (g_slist_find_custom(concatlist, attribute, g_str_equal) == NULL))
+ concatlist = g_slist_prepend(concatlist, g_strdup(tmpattr));
+
+ if (convert && (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL))
+ convlist = g_slist_prepend(convlist, g_strdup(tmpattr));
+
+ g_free(tmpattr);
+ } else if (convert) {
+ if (g_slist_find_custom(convlist, attribute, g_str_equal) == NULL)
+ convlist = g_slist_prepend(convlist, g_strdup(attribute));
+ }
+
+ if (g_hash_table_lookup(table, attribute) == NULL)
+ g_hash_table_insert(table, g_strdup(attribute), g_strdup(value));
+ }
+
+ for (cur = concatlist; cur != NULL; cur = g_slist_next(cur)) {
+ gchar *attribute, *attrwnum, *partvalue;
+ gint n = 0;
+ GString *value;
+
+ attribute = (gchar *) cur->data;
+ value = g_string_sized_new(64);
+
+ attrwnum = g_strdup_printf("%s*%d", attribute, n);
+ while ((partvalue = g_hash_table_lookup(table, attrwnum)) != NULL) {
+ g_string_append(value, partvalue);
+
+ g_free(attrwnum);
+ n++;
+ attrwnum = g_strdup_printf("%s*%d", attribute, n);
+ }
+ g_free(attrwnum);
+
+ g_hash_table_insert(table, g_strdup(attribute), g_strdup(value->str));
+ g_string_free(value, TRUE);
+ }
+ slist_free_strings(concatlist);
+ g_slist_free(concatlist);
+
+ for (cur = convlist; cur != NULL; cur = g_slist_next(cur)) {
+ gchar *attribute, *key, *value;
+ gchar *charset, *lang, *oldvalue, *newvalue;
+
+ attribute = (gchar *) cur->data;
+ if (!g_hash_table_lookup_extended(table, attribute, (gpointer *) &key, (gpointer *) &value))
+ continue;
+
+ charset = value;
+ lang = strchr(charset, '\'');
+ if (lang == NULL)
+ continue;
+ lang[0] = '\0';
+ lang++;
+ oldvalue = strchr(lang, '\'');
+ if (oldvalue == NULL)
+ continue;
+ oldvalue[0] = '\0';
+ oldvalue++;
+
+ newvalue = conv_codeset_strdup(oldvalue, charset, conv_get_current_charset_str());
+
+ g_hash_table_remove(table, attribute);
+ g_free(key);
+ g_free(value);
+
+ g_hash_table_insert(table, g_strdup(attribute), newvalue);
+ }
+ slist_free_strings(convlist);
+ g_slist_free(convlist);
+
+ g_free(params);
+}
+
+static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
+{
+ g_return_if_fail(content_type != NULL);
+ g_return_if_fail(mimeinfo != NULL);
+
+ /* RFC 2045, page 13 says that the mime subtype is MANDATORY;
+ * if it's not available we use the default Content-Type */
+ if ((content_type[0] == '\0') || (strchr(content_type, '/') == NULL)) {
+ mimeinfo->type = MIMETYPE_TEXT;
+ mimeinfo->subtype = g_strdup("plain");
+ if (g_hash_table_lookup(mimeinfo->typeparameters,
+ "charset") == NULL)
+ g_hash_table_insert(mimeinfo->typeparameters,
+ g_strdup("charset"),
+ g_strdup("us-ascii"));
+ } else {
+ gchar *type, *subtype, *params;
+
+ type = g_strdup(content_type);
+ subtype = strchr(type, '/') + 1;
+ *(subtype - 1) = '\0';
+ if ((params = strchr(subtype, ';')) != NULL) {
+ params[0] = '\0';
+ params++;
+ }
+
+ mimeinfo->type = procmime_get_media_type(type);
+ mimeinfo->subtype = g_strdup(subtype);
+
+ /* Get mimeinfo->typeparameters */
+ if (params != NULL)
+ parse_parameters(params, mimeinfo->typeparameters);
+
+ g_free(type);
+ }
+}
+
+static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
+{
+ gchar *tmp, *params;
+
+ g_return_if_fail(content_disposition != NULL);
+ g_return_if_fail(mimeinfo != NULL);
+
+ tmp = g_strdup(content_disposition);
+ if ((params = strchr(tmp, ';')) != NULL) {
+ params[0] = '\0';
+ params++;
+ }
+ g_strstrip(tmp);
+
+ if (!g_strcasecmp(tmp, "inline"))
+ mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
+ else if (!g_strcasecmp(tmp, "attachment"))
+ mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
+ else
+ mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
+
+ if (params != NULL)
+ parse_parameters(params, mimeinfo->dispositionparameters);
+
+ g_free(tmp);
+}
+
+
+static void procmime_parse_content_encoding(const gchar *content_encoding, MimeInfo *mimeinfo)
+{
+ struct EncodingTable *enc_table;
+
+ for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
+ if (g_strcasecmp(enc_table->str, content_encoding) == 0) {
+ mimeinfo->encoding_type = enc_table->enc_type;
+ return;
+ }
+ }
+ mimeinfo->encoding_type = ENC_UNKNOWN;
+ return;
+}
+
+void procmime_parse_mimepart(MimeInfo *parent,
+ gchar *content_type,
+ gchar *content_encoding,
+ gchar *content_description,
+ gchar *content_id,
+ gchar *content_disposition,
+ const gchar *filename,
+ guint offset,
+ guint length)
+{
+ MimeInfo *mimeinfo;
+
+ /* Create MimeInfo */
+ mimeinfo = procmime_mimeinfo_new();
+ mimeinfo->content = MIMECONTENT_FILE;
+ if (parent != NULL)
+ g_node_append(parent->node, mimeinfo->node);
+ mimeinfo->data.filename = g_strdup(filename);
+ mimeinfo->offset = offset;
+ mimeinfo->length = length;
+
+ if (content_type != NULL) {
+ procmime_parse_content_type(content_type, mimeinfo);
+ } else {
+ mimeinfo->type = MIMETYPE_TEXT;
+ mimeinfo->subtype = g_strdup("plain");
+ if (g_hash_table_lookup(mimeinfo->typeparameters,
+ "charset") == NULL)
+ g_hash_table_insert(mimeinfo->typeparameters, g_strdup("charset"), g_strdup("us-ascii"));
+ }
+
+ if (content_encoding != NULL) {
+ procmime_parse_content_encoding(content_encoding, mimeinfo);
+ } else {
+ mimeinfo->encoding_type = ENC_UNKNOWN;
+ }
+
+ if (content_description != NULL)
+ mimeinfo->description = g_strdup(content_description);
+ else
+ mimeinfo->description = NULL;
+
+ if (content_id != NULL)
+ mimeinfo->id = g_strdup(content_id);
+ else
+ mimeinfo->id = NULL;
+
+ if (content_disposition != NULL)
+ procmime_parse_content_disposition(content_disposition, mimeinfo);
+ else
+ mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
+
+ /* Call parser for mime type */
+ switch (mimeinfo->type) {
+ case MIMETYPE_MESSAGE:
+ if (g_strcasecmp(mimeinfo->subtype, "rfc822") == 0) {
+ procmime_parse_message_rfc822(mimeinfo);
+ }
+ break;
+
+ case MIMETYPE_MULTIPART:
+ procmime_parse_multipart(mimeinfo);
+ break;
+
+ default:
+ break;
+ }
+}
+
+static gchar *typenames[] = {
+ "text",
+ "image",
+ "audio",
+ "video",
+ "application",
+ "message",
+ "multipart",
+ "unknown",
+};
+
+static gboolean output_func(GNode *node, gpointer data)
+{
+ guint i, depth;
+ MimeInfo *mimeinfo = (MimeInfo *) node->data;
+
+ depth = g_node_depth(node);
+ for (i = 0; i < depth; i++)
+ printf(" ");
+ printf("%s/%s (offset:%d length:%d encoding: %d)\n", typenames[mimeinfo->type], mimeinfo->subtype, mimeinfo->offset, mimeinfo->length, mimeinfo->encoding_type);
+
+ return FALSE;
+}
+
+static void output_mime_structure(MimeInfo *mimeinfo, int indent)
+{
+ g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, output_func, NULL);
+}
+
+MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
+{
+ MimeInfo *mimeinfo;
+ struct stat buf;
+
+ stat(filename, &buf);
+
+ mimeinfo = procmime_mimeinfo_new();
+ mimeinfo->content = MIMECONTENT_FILE;
+ mimeinfo->encoding_type = ENC_UNKNOWN;
+ mimeinfo->type = MIMETYPE_MESSAGE;
+ mimeinfo->subtype = g_strdup("rfc822");
+ mimeinfo->data.filename = g_strdup(filename);
+ mimeinfo->offset = offset;
+ mimeinfo->length = buf.st_size - offset;
+
+ procmime_parse_message_rfc822(mimeinfo);
+ if (debug_get_mode())
+ output_mime_structure(mimeinfo, 0);
+
+ return mimeinfo;
+}
+
+MimeInfo *procmime_scan_file(const gchar *filename)
+{
+ MimeInfo *mimeinfo;
+
+ g_return_val_if_fail(filename != NULL, NULL);
+
+ mimeinfo = procmime_scan_file_with_offset(filename, 0);
+
+ return mimeinfo;
+}
+
+MimeInfo *procmime_scan_queue_file(const gchar *filename)
+{
+ FILE *fp;
+ MimeInfo *mimeinfo;
+ gchar buf[BUFFSIZE];
+ gint offset = 0;
+
+ g_return_val_if_fail(filename != NULL, NULL);
+
+ /* Open file */
+ if ((fp = fopen(filename, "rb")) == NULL)
+ return NULL;
+ /* Skip queue header */
+ while (fgets(buf, sizeof(buf), fp) != NULL)
+ if (buf[0] == '\r' || buf[0] == '\n') break;
+ offset = ftell(fp);
+ fclose(fp);
+
+ mimeinfo = procmime_scan_file_with_offset(filename, offset);
+
+ return mimeinfo;
+}
+
+typedef enum {
+ ENC_AS_TOKEN,
+ ENC_AS_QUOTED_STRING,
+ 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;
+ ParametersData *pdata = (ParametersData *)user_data;
+ GString *buf = g_string_new("");
+
+ EncodeAs encas = ENC_AS_TOKEN;
+
+ for (valpos = val; *valpos != 0; valpos++) {
+ if (!IS_ASCII(*valpos)) {
+ encas = ENC_AS_EXTENDED;
+ break;
+ }
+
+ /* CTLs */
+ if (((*valpos >= 0) && (*valpos < 037)) || (*valpos == 0177)) {
+ encas = ENC_AS_QUOTED_STRING;
+ continue;
+ }
+
+ /* tspecials + SPACE */
+ switch (*valpos) {
+ case ' ':
+ case '(':
+ case ')':
+ case '<':
+ case '>':
+ case '@':
+ case ',':
+ case ';':
+ case ':':
+ case '\\':
+ case '"':
+ case '/':
+ case '[':
+ case ']':
+ case '?':
+ case '=':
+ encas = ENC_AS_QUOTED_STRING;
+ continue;
+ }
+ }
+
+ switch (encas) {
+ case ENC_AS_TOKEN:
+ g_string_sprintfa(buf, "%s=%s", param, val);
+ break;
+
+ case ENC_AS_QUOTED_STRING:
+ g_string_sprintfa(buf, "%s=\"%s\"", param, val);
+ break;
+
+ case ENC_AS_EXTENDED:
+ g_string_sprintfa(buf, "%s*=%s''", param,
+ conv_get_current_charset_str());
+ for (valpos = val; *valpos != '\0'; valpos++) {
+ if (IS_ASCII(*valpos) && isalnum(*valpos)) {
+ g_string_sprintfa(buf, "%c", *valpos);
+ } else {
+ gchar hexstr[3] = "XX";
+ get_hex_str(hexstr, *valpos);
+ g_string_sprintfa(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) {
+ 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, pdata);
+ g_free(pdata);
+
+ fprintf(fp, "\n");
+
+ if (mimeinfo->encoding_type != ENC_UNKNOWN)
+ fprintf(fp, "Content-Transfer-Encoding: %s\n", procmime_get_encoding_str(mimeinfo->encoding_type));
+
+ if (mimeinfo->description != NULL)
+ fprintf(fp, "Content-Description: %s\n", mimeinfo->description);
+
+ if (mimeinfo->id != NULL)
+ fprintf(fp, "Content-ID: %s\n", mimeinfo->id);
+
+ if (mimeinfo->disposition != DISPOSITIONTYPE_UNKNOWN) {
+ ParametersData *pdata = g_new0(ParametersData, 1);
+ gchar *buf = NULL;
+ if (mimeinfo->disposition == DISPOSITIONTYPE_INLINE)
+ buf = g_strdup("Content-Disposition: inline");
+ else if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT)
+ buf = g_strdup("Content-Disposition: attachment");
+ else
+ buf = g_strdup("Content-Disposition: unknown");
+
+ fprintf(fp, "%s", buf);
+ pdata->len = strlen(buf);
+ g_free(buf);
+
+ pdata->fp = fp;
+ g_hash_table_foreach(mimeinfo->dispositionparameters, write_parameters, pdata);
+ g_free(pdata);
+ fprintf(fp, "\n");
+ }
+
+ fprintf(fp, "\n");
+}
+
+gint procmime_write_message_rfc822(MimeInfo *mimeinfo, FILE *fp)
+{
+ FILE *infp;
+ GNode *childnode;
+ MimeInfo *child;
+ gchar buf[BUFFSIZE];
+ gboolean skip = FALSE;;
+
+ debug_print("procmime_write_message_rfc822\n");
+
+ /* write header */
+ switch (mimeinfo->content) {
+ case MIMECONTENT_FILE:
+ if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+ return -1;
+ }
+ fseek(infp, mimeinfo->offset, SEEK_SET);
+ while (fgets(buf, sizeof(buf), infp) == buf) {
+ if (buf[0] == '\n' && buf[1] == '\0')
+ break;
+ if (skip && (buf[0] == ' ' || buf[0] == '\t'))
+ continue;
+ if (g_strncasecmp(buf, "Mime-Version:", 13) == 0 ||
+ g_strncasecmp(buf, "Content-Type:", 13) == 0 ||
+ g_strncasecmp(buf, "Content-Transfer-Encoding:", 26) == 0 ||
+ g_strncasecmp(buf, "Content-Description:", 20) == 0 ||
+ g_strncasecmp(buf, "Content-ID:", 11) == 0 ||
+ g_strncasecmp(buf, "Content-Disposition:", 20) == 0) {
+ skip = TRUE;
+ continue;
+ }
+ fwrite(buf, sizeof(gchar), strlen(buf), fp);
+ skip = FALSE;
+ }
+ fclose(infp);
+ break;
+
+ case MIMECONTENT_MEM:
+ fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
+ break;
+
+ default:
+ break;
+ }
+
+ childnode = mimeinfo->node->children;
+ if (childnode == NULL)
+ return -1;
+
+ child = (MimeInfo *) childnode->data;
+ fprintf(fp, "Mime-Version: 1.0\n");
+ procmime_write_mime_header(child, fp);
+ return procmime_write_mimeinfo(child, fp);
+}
+
+gint procmime_write_multipart(MimeInfo *mimeinfo, FILE *fp)
+{
+ FILE *infp;
+ GNode *childnode;
+ gchar *boundary, *str, *str2;
+ gchar buf[BUFFSIZE];
+ gboolean firstboundary;
+
+ debug_print("procmime_write_multipart\n");
+
+ boundary = g_hash_table_lookup(mimeinfo->typeparameters, "boundary");
+
+ switch (mimeinfo->content) {
+ case MIMECONTENT_FILE:
+ if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+ return -1;
+ }
+ fseek(infp, mimeinfo->offset, SEEK_SET);
+ while (fgets(buf, sizeof(buf), infp) == buf) {
+ if (IS_BOUNDARY(buf, boundary, strlen(boundary)))
+ break;
+ fwrite(buf, sizeof(gchar), strlen(buf), fp);
+ }
+ fclose(infp);
+ break;
+
+ case MIMECONTENT_MEM:
+ str = g_strdup(mimeinfo->data.mem);
+ if (((str2 = strstr(str, boundary)) != NULL) && ((str2 - str) >= 2) &&
+ (*(str2 - 1) == '-') && (*(str2 - 2) == '-'))
+ *(str2 - 2) = '\0';
+ fwrite(str, strlen(str), sizeof(gchar), fp);
+ g_free(str);
+ break;
+
+ default:
+ break;
+ }
+
+ childnode = mimeinfo->node->children;
+ firstboundary = TRUE;
+ while (childnode != NULL) {
+ MimeInfo *child = childnode->data;
+
+ if (firstboundary)
+ firstboundary = FALSE;
+ else
+ fprintf(fp, "\n");
+ fprintf(fp, "--%s\n", boundary);
+
+ procmime_write_mime_header(child, fp);
+ if (procmime_write_mimeinfo(child, fp) < 0)
+ return -1;
+
+ childnode = g_node_next_sibling(childnode);
+ }
+ fprintf(fp, "\n--%s--\n", boundary);
+
+ return 0;
+}
+
+gint procmime_write_mimeinfo(MimeInfo *mimeinfo, FILE *fp)
+{
+ FILE *infp;
+
+ debug_print("procmime_write_mimeinfo\n");
+
+ if (G_NODE_IS_LEAF(mimeinfo->node)) {
+ switch (mimeinfo->content) {
+ case MIMECONTENT_FILE:
+ if ((infp = fopen(mimeinfo->data.filename, "rb")) == NULL) {
+ FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+ return -1;
+ }
+ copy_file_part_to_fp(infp, mimeinfo->offset, mimeinfo->length, fp);
+ fclose(infp);
+ return 0;
+
+ case MIMECONTENT_MEM:
+ fwrite(mimeinfo->data.mem, strlen(mimeinfo->data.mem), sizeof(gchar), fp);
+ return 0;
+
+ default:
+ return 0;
+ }
+ } else {
+ /* Call writer for mime type */
+ switch (mimeinfo->type) {
+ case MIMETYPE_MESSAGE:
+ if (g_strcasecmp(mimeinfo->subtype, "rfc822") == 0)
+ return procmime_write_message_rfc822(mimeinfo, fp);
+ break;
+
+ case MIMETYPE_MULTIPART:
+ return procmime_write_multipart(mimeinfo, fp);
+
+ default:
+ break;
+ }
+
+ return -1;
+ }
+
+ return 0;
+}