#include "base64.h"
#include "uuencode.h"
#include "unmime.h"
+#include "html.h"
+#include "enriched.h"
#include "codeconv.h"
#include "utils.h"
#include "prefs_common.h"
# include "rfc2015.h"
#endif
+#include "prefs.h"
+
static GHashTable *procmime_get_mime_type_table (void);
MimeInfo *procmime_mimeinfo_new(void)
MimeInfo *parent = old->parent;
MimeInfo *child;
- if (!parent) {
- g_warning("oops: Not top message");
- return;
- }
- if (new->next) {
- g_message("oops: new child should not have a sibling");
- return;
- }
+ g_return_if_fail(parent != NULL);
+ g_return_if_fail(new->next == NULL);
for (child = parent->children; child && child != old;
child = child->next)
}
}
+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;
+
+ 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;
return;
}
+
for (npart = 0;; npart++) {
MimeInfo *partinfo;
gboolean eom = FALSE;
if (eom) break;
}
- /*g_message ("** at " __PRETTY_FUNCTION__ ":%d:", __LINE__);*/
}
void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
gchar *buf;
if (conv_get_current_charset() == C_EUC_JP &&
- strchr(content_type, '\007')) {
+ strchr(content_type, '\033')) {
gint len;
len = strlen(content_type) * 2 + 1;
Xalloca(buf, len, return);
g_free(mimeinfo->content_type);
g_free(mimeinfo->charset);
- g_free(mimeinfo->name);
+ /* g_free(mimeinfo->name); */
mimeinfo->content_type = NULL;
mimeinfo->charset = NULL;
- mimeinfo->name = NULL;
+ /* mimeinfo->name = NULL; */
if ((delim = strchr(buf, ';'))) *delim = '\0';
mimeinfo->content_type = cnttype = g_strdup(g_strstrip(buf));
- if (!strncasecmp(cnttype, "text/html", 9))
- mimeinfo->mime_type = MIME_TEXT_HTML;
- else if (!strncasecmp(cnttype, "text/", 5))
- mimeinfo->mime_type = MIME_TEXT;
- else if (!strncasecmp(cnttype, "message/rfc822", 14))
- mimeinfo->mime_type = MIME_MESSAGE_RFC822;
- else if (!strncasecmp(cnttype, "message/", 8))
- mimeinfo->mime_type = MIME_TEXT;
- else if (!strncasecmp(cnttype, "application/octet-stream", 24))
- mimeinfo->mime_type = MIME_APPLICATION_OCTET_STREAM;
- else if (!strncasecmp(cnttype, "application/", 12))
- mimeinfo->mime_type = MIME_APPLICATION;
- else if (!strncasecmp(cnttype, "multipart/", 10))
- mimeinfo->mime_type = MIME_MULTIPART;
- else if (!strncasecmp(cnttype, "image/", 6))
- mimeinfo->mime_type = MIME_IMAGE;
- else if (!strncasecmp(cnttype, "audio/", 6))
- mimeinfo->mime_type = MIME_AUDIO;
- else if (!strcasecmp(cnttype, "text"))
- mimeinfo->mime_type = MIME_TEXT;
- else
- mimeinfo->mime_type = MIME_UNKNOWN;
+ mimeinfo->mime_type = procmime_scan_mime_type(cnttype);
if (!delim) return;
p = delim + 1;
len = strlen(value) + 1;
Xalloca(tmp, len, return);
conv_unmime_header(tmp, len, value, NULL);
+ g_free(mimeinfo->name);
mimeinfo->name = g_strdup(tmp);
} else if (!strcasecmp(attr, "boundary"))
mimeinfo->boundary = g_strdup(value);
gchar *buf;
if (conv_get_current_charset() == C_EUC_JP &&
- strchr(content_disposition, '\007')) {
+ strchr(content_disposition, '\033')) {
gint len;
len = strlen(content_disposition) * 2 + 1;
Xalloca(buf, len, return);
}
}
+void procmime_scan_content_description(MimeInfo *mimeinfo,
+ const gchar *content_description)
+{
+ gchar *delim, *p, *dispos;
+ 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 = g_strdup(tmp);
+}
+
+void procmime_scan_subject(MimeInfo *mimeinfo,
+ const gchar *subject)
+{
+ gchar *delim, *p, *dispos;
+ gchar *buf;
+
+ gchar *tmp;
+ size_t blen;
+
+ 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);
+}
+
enum
{
H_CONTENT_TRANSFER_ENCODING = 0,
H_CONTENT_TYPE = 1,
- H_CONTENT_DISPOSITION = 2
+ H_CONTENT_DISPOSITION = 2,
+ H_CONTENT_DESCRIPTION = 3,
+ H_SUBJECT = 4
};
MimeInfo *procmime_scan_mime_header(FILE *fp)
{"Content-Type:", NULL, TRUE},
{"Content-Disposition:",
NULL, TRUE},
+ {"Content-description:",
+ NULL, TRUE},
+ {"Subject:",
+ NULL, TRUE},
{NULL, NULL, FALSE}};
gchar buf[BUFFSIZE];
gint hnum;
} 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);
+ }
+
if (!mimeinfo->content_type)
- mimeinfo->content_type = g_strdup("text/plain");
+ mimeinfo->content_type = g_strdup("text/plain");
return mimeinfo;
}
return 0;
}
-FILE *procmime_get_text_part(MsgInfo *msginfo)
+struct ContentRenderer {
+ char * content_type;
+ char * renderer;
+};
+
+static GList * renderer_list = NULL;
+
+static struct ContentRenderer *
+content_renderer_new(char * content_type, char * renderer)
+{
+ struct ContentRenderer * cr;
+
+ cr = g_new(struct ContentRenderer, 1);
+ if (cr == NULL)
+ return NULL;
+
+ cr->content_type = g_strdup(content_type);
+ cr->renderer = g_strdup(renderer);
+
+ return cr;
+}
+
+static void content_renderer_free(struct ContentRenderer * cr)
+{
+ g_free(cr->content_type);
+ g_free(cr->renderer);
+ g_free(cr);
+}
+
+void renderer_read_config(void)
{
- FILE *infp, *tmpfp, *outfp;
- MimeInfo *mimeinfo, *partinfo = NULL;
- gchar *src_codeset;
- gboolean conv_fail = FALSE;
gchar buf[BUFFSIZE];
+ FILE * f;
+ gchar * rcpath;
- g_return_val_if_fail(msginfo != NULL, NULL);
+ g_list_foreach(renderer_list, (GFunc) content_renderer_free, NULL);
+ renderer_list = NULL;
- mimeinfo = procmime_scan_message(msginfo);
- if (!mimeinfo) return NULL;
+ rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
+ f = fopen(rcpath, "r");
+ g_free(rcpath);
+
+ if (f == NULL)
+ return;
- if ((infp = procmsg_open_message(msginfo)) == NULL) {
- procmime_mimeinfo_free_all(mimeinfo);
- return NULL;
+ while (fgets(buf, BUFFSIZE, f)) {
+ char * p;
+ struct ContentRenderer * cr;
+
+ strretchomp(buf);
+ p = strchr(buf, ' ');
+ if (p == NULL)
+ continue;
+ * p = 0;
+
+ cr = content_renderer_new(buf, p + 1);
+ if (cr == NULL)
+ continue;
+
+ renderer_list = g_list_append(renderer_list, cr);
}
- if (mimeinfo->mime_type == MIME_MULTIPART) {
- partinfo = mimeinfo->children;
- if (partinfo && partinfo->mime_type == MIME_TEXT) {
- if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
- perror("fseek");
- partinfo = NULL;
- }
- } else
- partinfo = NULL;
- } else if (mimeinfo->mime_type == MIME_TEXT) {
- partinfo = mimeinfo;
- if (fseek(infp, partinfo->fpos, SEEK_SET) < 0) {
- perror("fseek");
- partinfo = NULL;
- }
+ fclose(f);
+}
+
+void renderer_write_config(void)
+{
+ int i;
+ gchar * rcpath;
+ PrefFile *pfile;
+ GList * cur;
+
+ rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, RENDERER_RC, NULL);
+
+ if ((pfile = prefs_write_open(rcpath)) == NULL) {
+ g_warning(_("failed to write configuration to file\n"));
+ g_free(rcpath);
+ return;
}
- if (!partinfo) {
- procmime_mimeinfo_free_all(mimeinfo);
+ g_free(rcpath);
+
+ for(cur = renderer_list ; cur != NULL ; cur = cur->next) {
+ struct ContentRenderer * renderer;
+ renderer = cur->data;
+ fprintf(pfile->fp, "%s %s\n", renderer->content_type,
+ renderer->renderer);
+ }
+
+ if (prefs_write_close(pfile) < 0) {
+ g_warning(_("failed to write configuration to file\n"));
+ return;
+ }
+}
+
+FILE *procmime_get_text_content(MimeInfo *mimeinfo, FILE *infp)
+{
+ FILE *tmpfp, *outfp;
+ gchar *src_codeset;
+ gboolean conv_fail = FALSE;
+ gchar buf[BUFFSIZE];
+ gchar *str;
+ int i;
+ struct ContentRenderer * renderer;
+ GList * cur;
+
+ 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");
return NULL;
}
while (fgets(buf, sizeof(buf), infp) != NULL)
if (buf[0] == '\r' || buf[0] == '\n') break;
- tmpfp = procmime_decode_content(NULL, infp, partinfo);
- if (!tmpfp) {
- procmime_mimeinfo_free_all(mimeinfo);
+ tmpfp = procmime_decode_content(NULL, infp, mimeinfo);
+ if (!tmpfp)
return NULL;
- }
if ((outfp = my_tmpfile()) == NULL) {
perror("tmpfile");
fclose(tmpfp);
- procmime_mimeinfo_free_all(mimeinfo);
return NULL;
}
src_codeset = prefs_common.force_charset
- ? prefs_common.force_charset : partinfo->charset;
+ ? prefs_common.force_charset : mimeinfo->charset;
- while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
- gchar *str;
+ renderer = NULL;
+
+ 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) {
+ renderer = cr;
+ break;
+ }
+ }
+
+ if (renderer != NULL) {
+ FILE * p;
+ int oldout;
+
+ oldout = dup(1);
+
+ dup2(fileno(outfp), 1);
+
+ p = popen(renderer->renderer, "w");
+ if (p != NULL) {
+ size_t count;
+
+ while ((count =
+ fread(buf, sizeof(char), sizeof(buf),
+ tmpfp)) > 0)
+ fwrite(buf, sizeof(char), count, p);
+ pclose(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) {
+ HTMLParser *parser;
+ CodeConverter *conv;
- str = conv_codeset_strdup(buf, src_codeset, NULL);
- if (str) {
+ conv = conv_code_converter_new(src_codeset);
+ parser = html_parser_new(tmpfp, conv);
+ while ((str = html_parse(parser)) != NULL) {
+ fputs(str, outfp);
+ }
+ html_parser_destroy(parser);
+ conv_code_converter_destroy(conv);
+ } else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED) {
+ ERTFParser *parser;
+ CodeConverter *conv;
+
+ conv = conv_code_converter_new(src_codeset);
+ parser = ertf_parser_new(tmpfp, conv);
+ while ((str = ertf_parse(parser)) != NULL) {
fputs(str, outfp);
- g_free(str);
- } else {
- conv_fail = TRUE;
- fputs(buf, outfp);
}
+ ertf_parser_destroy(parser);
+ conv_code_converter_destroy(conv);
}
- if (conv_fail) g_warning(_("Code conversion failed.\n"));
+
+ if (conv_fail)
+ g_warning(_("procmime_get_text_content(): Code conversion failed.\n"));
fclose(tmpfp);
- procmime_mimeinfo_free_all(mimeinfo);
rewind(outfp);
return outfp;
}
+/* search the first text part of (multipart) MIME message,
+ decode, convert it and output to outfp. */
+FILE *procmime_get_first_text_content(MsgInfo *msginfo)
+{
+ FILE *infp, *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)
+ 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);
+
+ 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)
+{
+
+ FILE *infp, *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(str != NULL, FALSE);
+
+ if ((infp = fopen(filename, "r")) == NULL) {
+ FILE_OP_ERROR(filename, "fopen");
+ return FALSE;
+ }
+
+ outfp = procmime_get_text_content(mimeinfo, infp);
+ fclose(infp);
+
+ if (!outfp)
+ return FALSE;
+
+ if (case_sens)
+ StrFindFunc = strstr;
+ else
+ StrFindFunc = strcasestr;
+
+ while (fgets(buf, sizeof(buf), outfp) != NULL) {
+ if (StrFindFunc(buf, str) != NULL) {
+ fclose(outfp);
+ return TRUE;
+ }
+ }
+
+ fclose(outfp);
+
+ return FALSE;
+}
+
+gboolean procmime_find_string(MsgInfo *msginfo, const gchar *str,
+ gboolean case_sens)
+{
+ MimeInfo *mimeinfo;
+ MimeInfo *partinfo;
+ gchar *filename;
+ gboolean found = FALSE;
+
+ g_return_val_if_fail(msginfo != NULL, FALSE);
+ g_return_val_if_fail(str != NULL, FALSE);
+
+ filename = procmsg_get_message_file(msginfo);
+ if (!filename) return FALSE;
+ mimeinfo = procmime_scan_message(msginfo);
+
+ 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 (procmime_find_string_part
+ (partinfo, filename, str, case_sens) == TRUE) {
+ found = TRUE;
+ break;
+ }
+ }
+ }
+
+ procmime_mimeinfo_free_all(mimeinfo);
+ g_free(filename);
+
+ return found;
+}
+
gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
{
static guint32 id = 0;
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)
{
static GHashTable *mime_type_table = NULL;
MimeType *mime_type;
- const gchar *ext, *p;
+ const gchar *p;
+ gchar *ext;
if (!mime_type_table) {
mime_type_table = procmime_get_mime_type_table();
filename = g_basename(filename);
p = strrchr(filename, '.');
- if (p)
- ext = p + 1;
- else
- return NULL;
+ 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) {
gchar *str;
for (cur = mime_type_list; cur != NULL; cur = cur->next) {
gint i;
+ gchar *key;
mime_type = (MimeType *)cur->data;
if (!mime_type->extension) continue;
exts = g_strsplit(mime_type->extension, " ", 16);
- for (i = 0; exts[i] != NULL; i++)
- g_hash_table_insert(table, g_strdup(exts[i]),
- mime_type);
+ for (i = 0; exts[i] != NULL; i++) {
+ /* make the key case insensitive */
+ g_strdown(exts[i]);
+ /* use previously dup'd key on overwriting */
+ if (g_hash_table_lookup(table, exts[i]))
+ key = exts[i];
+ else
+ key = g_strdup(exts[i]);
+ g_hash_table_insert(table, key, mime_type);
+ }
g_strfreev(exts);
}
if (mime_type_list)
return mime_type_list;
- if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
- FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
- return NULL;
+ if ((fp = fopen("/etc/mime.types", "r")) == NULL) {
+ if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
+ FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
+ return NULL;
+ }
}
while (fgets(buf, sizeof(buf), fp) != NULL) {