Merge pull request #831 from j-hao/master

Handle attached images when write msg body to html
This commit is contained in:
Dirk-Jan C. Binnema 2016-05-07 12:27:46 +03:00
commit 7585506e37
5 changed files with 49 additions and 6 deletions

View File

@ -322,6 +322,15 @@ mu_msg_part_get_filename (MuMsgPart *mpart, gboolean construct_if_needed)
mpart->index, construct_if_needed);
}
const gchar*
mu_msg_part_get_content_id (MuMsgPart *mpart)
{
g_return_val_if_fail (mpart, NULL);
g_return_val_if_fail (GMIME_IS_OBJECT(mpart->data), NULL);
return g_mime_object_get_content_id((GMimeObject*)mpart->data);
}
static MuMsgPartType
get_disposition (GMimeObject *mobj)

View File

@ -114,6 +114,17 @@ char *mu_msg_part_get_filename (MuMsgPart *mpart, gboolean construct_if_needed)
G_GNUC_WARN_UNUSED_RESULT;
/**
* get appropriate content id for the mime-part
*
* @param mpart a MuMsgPart
*
* @return const content id
*/
const gchar*
mu_msg_part_get_content_id (MuMsgPart *mpart)
G_GNUC_WARN_UNUSED_RESULT;
/**
* get the text in the MuMsgPart (ie. in its GMimePart)
*

View File

@ -354,16 +354,17 @@ static void
each_part (MuMsg *msg, MuMsgPart *part, PartInfo *pinfo)
{
char *name, *tmp, *parttype;
char *tmpfile;
char *tmpfile, *cid;
name = mu_msg_part_get_filename (part, TRUE);
tmpfile = get_temp_file_maybe (msg, part, pinfo->opts);
parttype = get_part_type_string (part->part_type);
cid = mu_str_escape_c_literal(mu_msg_part_get_content_id(part), TRUE);
tmp = g_strdup_printf
("%s(:index %d :name \"%s\" :mime-type \"%s/%s\"%s%s "
":type %s "
":attachment %s :size %i %s %s)",
":attachment %s %s%s :size %i %s %s)",
pinfo->parts ? pinfo->parts: "",
part->index,
name ? mu_str_escape_c_literal(name, FALSE) : "noname",
@ -372,6 +373,7 @@ each_part (MuMsg *msg, MuMsgPart *part, PartInfo *pinfo)
tmpfile ? " :temp" : "", tmpfile ? tmpfile : "",
parttype,
mu_msg_part_maybe_attachment (part) ? "t" : "nil",
cid ? " :cid" : "", cid ? cid : "",
(int)part->size,
sig_verdict (part),
dec_verdict (part));
@ -379,6 +381,7 @@ each_part (MuMsg *msg, MuMsgPart *part, PartInfo *pinfo)
g_free (name);
g_free (tmpfile);
g_free (parttype);
g_free (cid);
g_free (pinfo->parts);
pinfo->parts = tmp;

View File

@ -74,14 +74,29 @@ Works for the message view."
"Write the body (either html or text) to a temporary file;
return the filename."
(let* ((html (mu4e-message-field msg :body-html))
(txt (mu4e-message-field msg :body-txt))
(tmpfile (mu4e-make-temp-file "html")))
(txt (mu4e-message-field msg :body-txt))
(tmpfile (mu4e-make-temp-file "html"))
(attachments (remove-if (lambda (part)
(or (null (plist-get part :attachment))
(null (plist-get part :cid))))
(mu4e-message-field msg :parts))))
(unless (or html txt)
(mu4e-error "No body part for this message"))
(with-temp-buffer
(insert "<head><meta charset=\"UTF-8\"></head>\n")
(insert (or html (concat "<pre>" txt "</pre>")))
(write-file tmpfile)
;; rewrite attachment urls
(mapc (lambda (attachment)
(goto-char (point-min))
(while (re-search-forward (format "src=\"cid:%s\"" (plist-get attachment :cid)) nil t)
(if (plist-get attachment :temp)
(replace-match (format "src=\"%s\"" (plist-get attachment :temp)))
(replace-match (format "src=\"%s%s\"" temporary-file-directory (plist-get attachment :name)))
(mu4e~proc-extract 'save (mu4e-message-field :docid) (plist-get attachment :index) mu4e-decryption-policy temporary-file-directory)
(mu4e-remove-file-later (format "%s%s" temporary-file-directory (plist-get attachment :name))))))
attachments)
(save-buffer)
tmpfile)))
(defun mu4e-action-view-in-browser (msg)

View File

@ -139,6 +139,12 @@ return the result."
(mu4e~get-folder 'mu4e-trash-folder msg))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mu4e-remove-file-later (filename)
"Remove FILENAME in a few seconds."
(run-at-time "10 sec" nil
(lambda () (ignore-errors (delete-file filename)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun mu4e-make-temp-file (ext)
@ -146,8 +152,7 @@ return the result."
self-destruct in a few seconds, enough to open it in another
program."
(let ((tmpfile (make-temp-file "mu4e-" nil (concat "." ext))))
(run-at-time "10 sec" nil
(lambda (fname) (ignore-errors (delete-file fname))) tmpfile)
(mu4e-remove-file-later tmpfile)
tmpfile))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;