* src/procmime.[ch]
[claws.git] / src / procmime.c
index af71a81e392d645311049d261496322854b5c870..59224d3a1ae4f41d471778f3c233784b78f3a696 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2003 Hiroyuki Yamamoto
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #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 "procheader.h"
 #include "base64.h"
+#include "quoted-printable.h"
 #include "uuencode.h"
 #include "unmime.h"
+#include "html.h"
+#include "enriched.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);
-static GList *procmime_get_mime_type_list      (void);
-
 
 MimeInfo *procmime_mimeinfo_new(void)
 {
        MimeInfo *mimeinfo;
 
        mimeinfo = g_new0(MimeInfo, 1);
-       mimeinfo->mime_type     = MIME_UNKNOWN;
+       mimeinfo->type          = MIMETYPE_UNKNOWN;
        mimeinfo->encoding_type = ENC_UNKNOWN;
 
+       mimeinfo->parameters = g_hash_table_new(g_str_hash, g_str_equal);
+       mimeinfo->node       = g_node_new(mimeinfo);
+       
        return mimeinfo;
 }
 
-void procmime_mimeinfo_free(MimeInfo *mimeinfo)
+static gboolean procmime_mimeinfo_parameters_destroy(gpointer key, gpointer value, gpointer user_data)
 {
-       if (!mimeinfo) return;
+       g_free(key);
+       g_free(value);
+       
+       return TRUE;
+}
+
+static gboolean free_func(GNode *node, gpointer data)
+{
+       MimeInfo *mimeinfo = (MimeInfo *) node->data;
 
        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);
+       if(mimeinfo->tmpfile)
+               unlink(mimeinfo->filename);
        g_free(mimeinfo->filename);
-#if USE_GPGME
-       g_free(mimeinfo->plaintextfile);
-       g_free(mimeinfo->sigstatus);
-       g_free(mimeinfo->sigstatus_full);
-#endif
 
-       procmime_mimeinfo_free(mimeinfo->sub);
+       g_free(mimeinfo->subtype);
+       g_free(mimeinfo->description);
+       g_free(mimeinfo->id);
+
+       g_hash_table_foreach_remove(mimeinfo->parameters, procmime_mimeinfo_parameters_destroy, NULL);
+       g_hash_table_destroy(mimeinfo->parameters);
+
+       if (mimeinfo->privacy)
+               privacy_free_privacydata(mimeinfo->privacy);
 
        g_free(mimeinfo);
+
+       return FALSE;
 }
 
 void procmime_mimeinfo_free_all(MimeInfo *mimeinfo)
 {
-       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
+       GNode *node;
 
-               procmime_mimeinfo_free_all(mimeinfo->sub);
-               procmime_mimeinfo_free_all(mimeinfo->children);
-#if USE_GPGME
-               procmime_mimeinfo_free_all(mimeinfo->plaintext);
-#endif
+       g_return_if_fail(mimeinfo);
 
-               next = mimeinfo->next;
-               g_free(mimeinfo);
-               mimeinfo = next;
-       }
+       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;
@@ -134,14 +133,8 @@ void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
        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)
@@ -160,370 +153,109 @@ void procmime_mimeinfo_replace(MimeInfo *old, MimeInfo *new)
                child = new;
        }
 }
-
-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;
 
-               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->parent;
-                       sub->main = partinfo;
-
-                       if (sub->mime_type == MIME_MULTIPART) {
-                               if (sub->level < 8)
-                                       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)
-                       eom = TRUE;     /* broken MIME message */
-               fpos = ftell(fp);
-
-               partinfo->size = fpos - prev_fpos - strlen(buf);
-
-               if (eom) break;
-       }
-       //g_message ("** at " __PRETTY_FUNCTION__ ":%d:", __LINE__);
-}
-
-void procmime_scan_encoding(MimeInfo *mimeinfo, const gchar *encoding)
+MimeInfo *procmime_mimeinfo_parent(MimeInfo *mimeinfo)
 {
-       gchar *buf;
-
-       Xalloca(buf, strlen(encoding) + 1, return);
-       strcpy(buf, encoding);
-
-       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);
 
+       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;
-
-       Xalloca(buf, strlen(content_type) + 1, return);
-       strcpy(buf, content_type);
-
-       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));
-
-       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;
-
-       if (!delim) return;
-       p = delim + 1;
-
-       for (;;) {
-               gchar *eq;
-               gchar *attr, *value;
-
-               if ((delim = strchr(p, ';'))) *delim = '\0';
-
-               if (!(eq = strchr(p, '='))) break;
+       g_return_val_if_fail(mimeinfo != NULL, NULL);
+       g_return_val_if_fail(mimeinfo->node != NULL, NULL);
 
-               *eq = '\0';
-               attr = p;
-               g_strstrip(attr);
-               value = eq + 1;
-               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 == '"')
-                       extract_quote(value, '"');
-               else {
-                       eliminate_parenthesis(value, '(', ')');
-                       g_strstrip(value);
-               }
-
-               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);
-                               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;
-
-       Xalloca(buf, strlen(content_disposition) + 1, return);
-       strcpy(buf, content_disposition);
-
-       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;
+       gchar *filename;
+       MimeInfo *mimeinfo;
 
-                               len = strlen(value) + 1;
-                               Xalloca(tmp, len, return);
-                               conv_unmime_header(tmp, len, value, NULL);
-                               g_free(mimeinfo->filename);
-                               mimeinfo->filename = g_strdup(tmp);
-                               break;
-                       }
-               }
+       filename = procmsg_get_message_file(msginfo);
+       if(!filename)
+               return NULL;
+       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 (!delim) break;
-               p = delim + 1;
-       }
+       return mimeinfo;
 }
 
 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)
+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},
-                                      {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));
-               }
-       }
-
-       if (!mimeinfo->content_type)
-               mimeinfo->content_type = g_strdup("text/plain");
-
-       return mimeinfo;
+       g_return_val_if_fail(mimeinfo != NULL, NULL);
+       g_return_val_if_fail(name != NULL, NULL);
+       
+       return g_hash_table_lookup(mimeinfo->parameters, name);
 }
 
-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;
-       gboolean tmp_file = FALSE;
+       gint readend;
+       gchar *tmpfilename;
+       gchar *mimetmpdir;
+       FILE *outfp, *infp;
+       struct stat statbuf;
 
-       g_return_val_if_fail(infp != NULL, NULL);
-       g_return_val_if_fail(mimeinfo != NULL, NULL);
+       g_return_val_if_fail(mimeinfo != NULL, FALSE);
 
-       if (!outfp) {
-               outfp = my_tmpfile();
-               if (!outfp) {
-                       perror("tmpfile");
-                       return NULL;
-               }
-               tmp_file = TRUE;
-       }
+       if(mimeinfo->encoding_type == ENC_BINARY)
+               return TRUE;
 
-       if (mimeinfo->parent && mimeinfo->parent->boundary) {
-               boundary = mimeinfo->parent->boundary;
-               boundary_len = strlen(boundary);
+       infp = fopen(mimeinfo->filename, "rb");
+       if(!infp) {
+               perror("fopen");
+               return FALSE;
        }
+       fseek(infp, mimeinfo->offset, SEEK_SET);
 
-       if (mimeinfo->encoding_type == ENC_QUOTED_PRINTABLE) {
-               gboolean softline = FALSE;
+       mimetmpdir = get_mime_tmp_dir();
+       outfp = get_tmpfile_in_dir(mimetmpdir, &tmpfilename);
+       if (!outfp) {
+               perror("tmpfile");
+               return FALSE;
+       }
 
-               while (fgets(buf, sizeof(buf), infp) != NULL &&
-                      (!boundary ||
-                       !IS_BOUNDARY(buf, boundary, boundary_len))) {
-                       guchar *p = buf;
+       readend = mimeinfo->offset + mimeinfo->length;
 
-                       softline = DoOneQPLine(&p, FALSE, softline);
-                       fwrite(buf, p - (guchar *)buf, 1, outfp);
+       if (mimeinfo->encoding_type == 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) {
                gchar outbuf[BUFFSIZE];
@@ -531,9 +263,7 @@ FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
                Base64Decoder *decoder;
 
                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");
@@ -547,9 +277,7 @@ FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
                gint len;
                gboolean flag = FALSE;
 
-               while (fgets(buf, sizeof(buf), infp) != NULL &&
-                      (!boundary ||
-                       !IS_BOUNDARY(buf, boundary, boundary_len))) {
+               while ((ftell(infp) < readend) && (fgets(buf, sizeof(buf), infp) != NULL)) {
                        if(!flag && strncmp(buf,"begin ", 6)) continue;
 
                        if (flag) {
@@ -564,46 +292,60 @@ FILE *procmime_decode_content(FILE *outfp, FILE *infp, MimeInfo *mimeinfo)
                                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->tmpfile)
+               unlink(mimeinfo->filename);
+       g_free(mimeinfo->filename);
+       mimeinfo->filename = tmpfilename;
+       mimeinfo->tmpfile = TRUE;
+       mimeinfo->offset = 0;
+       mimeinfo->length = statbuf.st_size;
+       mimeinfo->encoding_type = ENC_BINARY;
+
+       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, "r")) == NULL) {
-               FILE_OP_ERROR(infile, "fopen");
+       if(mimeinfo->encoding_type != ENC_BINARY && !procmime_decode_content(mimeinfo))
+               return -1;
+
+       if ((infp = fopen(mimeinfo->filename, "rb")) == NULL) {
+               FILE_OP_ERROR(mimeinfo->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->filename, "fseek");
                fclose(infp);
                return -1;
        }
-       if ((outfp = fopen(outfile, "w")) == NULL) {
+       if ((outfp = fopen(outfile, "wb")) == NULL) {
                FILE_OP_ERROR(outfile, "fopen");
                fclose(infp);
                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) {
@@ -615,86 +357,312 @@ gint procmime_get_part(const gchar *outfile, const gchar *infile,
        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, "rb");
+       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)
+{
+       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);
-               return NULL;
+       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);
        }
 
-       while (fgets(buf, sizeof(buf), infp) != NULL)
-               if (buf[0] == '\r' || buf[0] == '\n') break;
+       if (prefs_file_close(pfile) < 0) {
+               g_warning("failed to write configuration to file\n");
+               return;
+       }
+}
 
-       tmpfp = procmime_decode_content(NULL, infp, partinfo);
-       if (!tmpfp) {
-               procmime_mimeinfo_free_all(mimeinfo);
+FILE *procmime_get_text_content(MimeInfo *mimeinfo)
+{
+       FILE *tmpfp, *outfp;
+       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);
+
+       if (!procmime_decode_content(mimeinfo))
+               return NULL;
+
+       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 = fopen(tmpfile, "rb");
+       if (tmpfp == NULL) {
+               g_free(tmpfile);
                return NULL;
        }
 
        if ((outfp = my_tmpfile()) == NULL) {
                perror("tmpfile");
                fclose(tmpfp);
-               procmime_mimeinfo_free_all(mimeinfo);
+               g_free(tmpfile);
                return NULL;
        }
 
        src_codeset = prefs_common.force_charset
-               ? prefs_common.force_charset : partinfo->charset;
+               ? prefs_common.force_charset : 
+               procmime_mimeinfo_get_parameter(mimeinfo, "charset");
 
-       while (fgets(buf, sizeof(buf), tmpfp) != NULL) {
-               gchar *str;
+       renderer = NULL;
 
-               str = conv_codeset_strdup(buf, src_codeset, NULL);
-               if (str) {
+       content_type = g_strdup_printf("%s/%s", procmime_get_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, content_type) == 0) {
+                       renderer = cr;
+                       break;
+               }
+       }
+       g_free(content_type);
+
+       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->type == MIMETYPE_TEXT && !g_strcasecmp(mimeinfo->subtype, "html")) {
+               HTMLParser *parser;
+               CodeConverter *conv;
+
+               conv = conv_code_converter_new(src_codeset);
+               parser = html_parser_new(tmpfp, conv);
+               while ((str = html_parse(parser)) != NULL) {
                        fputs(str, outfp);
-                       g_free(str);
-               } else {
-                       conv_fail = TRUE;
-                       fputs(buf, outfp);
+               }
+               html_parser_destroy(parser);
+               conv_code_converter_destroy(conv);
+       } else if (mimeinfo->type == MIMETYPE_TEXT && !g_strcasecmp(mimeinfo->subtype, "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);
+               }
+               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) 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);
+       unlink(tmpfile);
+       g_free(tmpfile);
 
        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 *outfp = NULL;
+       MimeInfo *mimeinfo, *partinfo;
+
+       g_return_val_if_fail(msginfo != NULL, NULL);
+
+       mimeinfo = procmime_scan_message(msginfo);
+       if (!mimeinfo) return NULL;
+
+       partinfo = mimeinfo;
+       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;
+}
+
+gboolean procmime_find_string_part(MimeInfo *mimeinfo, const gchar *filename,
+                                  const gchar *str, gboolean case_sens)
+{
+       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);
+
+       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) {
+                       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->type == MIMETYPE_TEXT) {
+                       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;
@@ -706,13 +674,14 @@ gchar *procmime_get_tmp_file_name(MimeInfo *mimeinfo)
 
        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 = mimeinfo->name ? mimeinfo->name : "mimetmp";
                base = g_basename(base);
                if (*base == '\0') base = "mimetmp";
+               Xstrdup_a(base, base, return NULL);
+               subst_for_filename(base);
        }
 
        filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
@@ -727,7 +696,8 @@ 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();
@@ -736,11 +706,10 @@ gchar *procmime_get_mime_type(const gchar *filename)
 
        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;
@@ -753,6 +722,27 @@ gchar *procmime_get_mime_type(const gchar *filename)
        return NULL;
 }
 
+static guint procmime_str_hash(gconstpointer gptr)
+{
+       guint hash_result = 0;
+       const char *str;
+
+       for (str = gptr; str && *str; str++) {
+               if (isupper(*str)) hash_result += (*str + ' ');
+               else hash_result += *str;
+       }
+
+       return hash_result;
+}
+
+static gint procmime_str_equal(gconstpointer gptr1, gconstpointer gptr2)
+{
+       const char *str1 = gptr1;
+       const char *str2 = gptr2;
+
+       return !strcasecmp(str1, str2);
+}
+
 static GHashTable *procmime_get_mime_type_table(void)
 {
        GHashTable *table = NULL;
@@ -765,26 +755,34 @@ static GHashTable *procmime_get_mime_type_table(void)
                if (!mime_type_list) return NULL;
        }
 
-       table = g_hash_table_new(g_str_hash, g_str_equal);
+       table = g_hash_table_new(procmime_str_hash, procmime_str_equal);
 
        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);
        }
 
        return table;
 }
 
-static GList *procmime_get_mime_type_list(void)
+GList *procmime_get_mime_type_list(void)
 {
        GList *list = NULL;
        FILE *fp;
@@ -792,9 +790,14 @@ static GList *procmime_get_mime_type_list(void)
        gchar *p, *delim;
        MimeType *mime_type;
 
-       if ((fp = fopen(SYSCONFDIR "/mime.types", "r")) == NULL) {
-               FILE_OP_ERROR(SYSCONFDIR "/mime.types", "fopen");
-               return NULL;
+       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;
+               }
        }
 
        while (fgets(buf, sizeof(buf), fp) != NULL) {
@@ -840,21 +843,470 @@ EncodingType procmime_get_encoding_for_charset(const gchar *charset)
        else if (!strncasecmp(charset, "ISO-2022-", 9) ||
                 !strcasecmp(charset, "US-ASCII"))
                return ENC_7BIT;
+       else if (!strcasecmp(charset, "ISO-8859-5") ||
+                !strncasecmp(charset, "KOI8-", 5) ||
+                !strcasecmp(charset, "Windows-1251"))
+               return ENC_8BIT;
+       else if (!strncasecmp(charset, "ISO-8859-", 9))
+               return ENC_QUOTED_PRINTABLE;
        else
                return ENC_8BIT;
-               /* return ENC_BASE64; */
-               /* return ENC_QUOTED_PRINTABLE; */
 }
 
+EncodingType procmime_get_encoding_for_file(const gchar *file)
+{
+       FILE *fp;
+       guchar buf[BUFSIZ];
+       size_t len;
+
+       if ((fp = fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "fopen");
+               return ENC_UNKNOWN;
+       }
+
+       while ((len = fread(buf, sizeof(gchar), 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;
+                       }
+               }
+       }
+
+       fclose(fp);
+       return ENC_7BIT;
+}
+
+struct EncodingTable 
+{
+       gchar *str;
+       EncodingType enc_type;
+};
+
+struct EncodingTable encoding_table[] = {
+       {"7bit", ENC_7BIT},
+       {"8bit", ENC_8BIT},
+       {"binary", ENC_BINARY},
+       {"quoted-printable", ENC_QUOTED_PRINTABLE},
+       {"base64", ENC_BASE64},
+       {"x-uuencode", ENC_UNKNOWN},
+       {NULL, ENC_UNKNOWN},
+};
+
 const gchar *procmime_get_encoding_str(EncodingType encoding)
 {
-       static const gchar *encoding_str[] = {
-               "7bit", "8bit", "quoted-printable", "base64", "x-uuencode",
-               NULL
-       };
+       struct EncodingTable *enc_table;
+       
+       for (enc_table = encoding_table; enc_table->str != NULL; enc_table++) {
+               if (enc_table->enc_type == encoding)
+                       return enc_table->str;
+       }
+       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_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;
+}
+
+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},
+                               {NULL,             NULL, FALSE}};
+       guint content_start, i;
+       FILE *fp;
+
+       if(mimeinfo->encoding_type != ENC_BINARY && 
+          mimeinfo->encoding_type != ENC_7BIT && 
+          mimeinfo->encoding_type != ENC_8BIT)
+               procmime_decode_content(mimeinfo);
+
+       fp = fopen(mimeinfo->filename, "rb");
+       fseek(fp, mimeinfo->offset, SEEK_SET);
+       procheader_get_header_fields(fp, hentry);
+       content_start = ftell(fp);
+       fclose(fp);
+
+       procmime_parse_mimepart(mimeinfo,
+                               hentry[0].body, hentry[1].body,
+                               hentry[2].body, hentry[3].body, 
+                               hentry[4].body, 
+                               mimeinfo->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;
+       }
+}
+
+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;
 
-       if (encoding >= ENC_7BIT && encoding <= ENC_UNKNOWN)
-               return encoding_str[encoding];
+       boundary = g_hash_table_lookup(mimeinfo->parameters, "boundary");
+       if (!boundary)
+               return;
+       boundary_len = strlen(boundary);
+
+       if (mimeinfo->encoding_type != ENC_BINARY && 
+           mimeinfo->encoding_type != ENC_7BIT && 
+           mimeinfo->encoding_type != ENC_8BIT)
+               procmime_decode_content(mimeinfo);
+
+       fp = fopen(mimeinfo->filename, "rb");
+       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->filename, lastoffset,
+                                                       (ftell(fp) - strlen(buf)) - lastoffset);
+                       }
+                       
+                       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);
+                       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 add_to_mimeinfo_parameters(gchar **parts, MimeInfo *mimeinfo)
+{
+       gchar **strarray;
+
+       for (strarray = parts; *strarray != NULL; strarray++) {
+               gchar **parameters_parts;
+
+               parameters_parts = g_strsplit(*strarray, "=", 1);
+               if ((parameters_parts[0] != NULL) && (parameters_parts[1] != NULL)) {
+                       gchar *firstspace;
+
+                       g_strstrip(parameters_parts[0]);
+                       g_strstrip(parameters_parts[1]);
+                       g_strdown(parameters_parts[0]);
+                       if(parameters_parts[1][0] == '"')
+                               extract_quote(parameters_parts[1], '"');
+                       else if ((firstspace = strchr(parameters_parts[1], ' ')) != NULL)
+                               *firstspace = '\0';
+                       if(g_hash_table_lookup(mimeinfo->parameters,
+                                              parameters_parts[0]) == NULL)
+                               g_hash_table_insert(mimeinfo->parameters,
+                                                   g_strdup(parameters_parts[0]),
+                                                   g_strdup(parameters_parts[1]));
+               }
+               g_strfreev(parameters_parts);
+       }
+}      
+
+static void procmime_parse_content_type(const gchar *content_type, MimeInfo *mimeinfo)
+{
+       gchar **content_type_parts;
+       gchar **strarray;
+       gchar *str;
+       struct TypeTable *typetablearray;
+       
+       g_return_if_fail(content_type != NULL);
+       g_return_if_fail(mimeinfo != NULL);
+       
+       /* Split content type into parts and remove trailing
+          and leading whitespaces from all strings */
+       content_type_parts = g_strsplit(content_type, ";", 0);
+       for (strarray = content_type_parts; *strarray != NULL; strarray++) {
+               g_strstrip(*strarray);
+       }
+
+       /* Get mimeinfo->type and mimeinfo->subtype */
+       mimeinfo->type = MIMETYPE_UNKNOWN;
+       str = content_type_parts[0];
+       if (str == NULL) {
+               g_strfreev(content_type_parts);
+               return;
+       }
+       for (typetablearray = mime_type_table; typetablearray->str != NULL; typetablearray++) {
+               if (g_strncasecmp(str, typetablearray->str, strlen(typetablearray->str)) == 0 &&
+                   str[strlen(typetablearray->str)] == '/') {
+                       mimeinfo->type = typetablearray->type;
+                       mimeinfo->subtype = g_strdup(str + strlen(typetablearray->str) + 1);
+                       break;
+               }
+       }
+
+       /* Get mimeinfo->parmeters */
+       add_to_mimeinfo_parameters(&content_type_parts[1], mimeinfo);
+       g_strfreev(content_type_parts);
+}
+
+static void procmime_parse_content_disposition(const gchar *content_disposition, MimeInfo *mimeinfo)
+{
+       gchar **content_disp_parts;
+       gchar **strarray;
+       gchar *str;
+
+       g_return_if_fail(content_disposition != NULL);
+       g_return_if_fail(mimeinfo != NULL);
+
+       /* Split into parts and remove trailing
+          and leading whitespaces from all strings */
+       content_disp_parts = g_strsplit(content_disposition, ";", 0);
+       for (strarray = content_disp_parts; *strarray != NULL; strarray++) {
+               g_strstrip(*strarray);
+       }
+       /* Get mimeinfo->disposition */
+       str = content_disp_parts[0];
+       if (str == NULL) {
+               g_strfreev(content_disp_parts);
+               return;
+       }
+       if (!g_strcasecmp(str, "inline")) 
+               mimeinfo->disposition = DISPOSITIONTYPE_INLINE;
+       else if (!g_strcasecmp(str, "attachment"))
+               mimeinfo->disposition = DISPOSITIONTYPE_ATTACHMENT;
        else
+               mimeinfo->disposition = DISPOSITIONTYPE_UNKNOWN;
+       
+       add_to_mimeinfo_parameters(&content_disp_parts[1], mimeinfo);
+       g_strfreev(content_disp_parts);
+}
+
+
+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();
+       if (parent != NULL)
+               g_node_append(parent->node, mimeinfo->node);
+       mimeinfo->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");
+               g_hash_table_insert(mimeinfo->parameters, g_strdup("charset"), g_strdup("us-ascii"));
+       }
+
+       if (content_encoding != NULL) {
+               procmime_parse_content_encoding(content_encoding, mimeinfo);
+       } else {
+               mimeinfo->encoding_type = ENC_7BIT;
+       }
+
+       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_INLINE;
+
+       /* 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);
+}
+
+static MimeInfo *procmime_scan_file_with_offset(const gchar *filename, int offset)
+{
+       MimeInfo *mimeinfo;
+       struct stat buf;
+
+       stat(filename, &buf);
+
+       mimeinfo = procmime_mimeinfo_new();
+       mimeinfo->encoding_type = ENC_BINARY;
+       mimeinfo->type = MIMETYPE_MESSAGE;
+       mimeinfo->subtype = g_strdup("rfc822");
+       mimeinfo->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(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(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;
 }