Gnus development mailing list
 help / color / mirror / Atom feed
From: Ivan Kanis <ivan.kanis@googlemail.com>
To: Katsumi Yamaoka <yamaoka@jpl.org>
Cc: ding@gnus.org
Subject: Re: saving inline images
Date: Fri, 21 Dec 2012 09:11:07 +0100	[thread overview]
Message-ID: <87vcbvx1t0.fsf@googlemail.com> (raw)
In-Reply-To: <b4mzk188dlr.fsf@jpl.org> (Katsumi Yamaoka's message of "Fri, 21 Dec 2012 09:15:28 +0900")

[-- Attachment #1: Type: text/plain, Size: 1866 bytes --]

Hello Katsumiさん、

Katsumi Yamaoka <yamaoka@jpl.org> wrote:

> However, if an image is attached to the mail (i.e., it is a MIME
> part of the mail, that is called a cid image), the `i' command
> (currently) doesn't support it.

That's exactly what I am talking about.

I came up with a solution to my need. I am posting here in case someone
else needs it. I don't like it because it involves Python and the
function is hard coded with the nnml backend.

(defun ivan-gnus-save-attachment ()
  "Save all atachments include cid inline attachments."
    (interactive)
  (let ((output-buffer (get-buffer-create "output"))
        (file-name (nnml-article-to-file (gnus-summary-article-number))))
    (switch-to-buffer output-buffer)
    (erase-buffer)
    (insert "The following files were extracted:\n\n")
    (call-process
     "extract-inline-attachment.py" nil output-buffer t file-name)))

I have attached the python script. I would rather do it all in elisp but
whenever I look at gnus code my brain melts ;)

> In that case, you can use the `C-d' command on a summary buffer to
> dissect the mail into some parts. And you can easily find an image
> among them and save it to a file in an ordinary way.

I didn't know that command. Nice! However it gets too long with multiple
images.

C-d C-n C-n C-n C-n <return> K b C-o C-n o ~/tmp/foo.png RET

And I have to do the K b sequence for each image...

> Maybe the `i' command (shr-browse-image) needs to be improved so
> as to support cid images.  Also the `o' command (shr-save-contents)
> had better support saving both external and cid images as well.

That would be nice.

> My only worry is that it may require renaming shr.el to chr.el

Why?

> In natural Japanese, we'd say: 「...」を聴きながら…  ;-)

Thank you for correcting my Japanese!


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: extract-inline-attachment.py --]
[-- Type: text/x-python, Size: 2047 bytes --]

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import email
import mimetypes
import os
import os.path
import sys

def usage ():
    print
    print "Usage:"
    print
    print sys.argv[0] + " file"
    print
    print "Extract inline attachment of mail and put them in ~/tmp"
    print
    print "Argument file is message to be parsed."
    sys.exit(1)

if len(sys.argv) != 2:
    usage() 

fp = open (sys.argv[1])
msg = email.message_from_file(fp)
fp.close()

counter = 1
for part in msg.walk():
    # multipart/* are just containers
    if part.get_content_maintype() == 'multipart':
        continue
    # Applications should really sanitize the given filename so that an
    # email message can't be used to overwrite important files
    filename = part.get_filename()
    if not filename:
        ext = mimetypes.guess_extension(part.get_content_type())
        if not ext:
            # Use a generic bag-of-bits extension
            ext = '.bin'
        filename = 'part-%03d%s' % (counter, ext)
    counter += 1
    filename = os.path.expanduser ("~/tmp/" + filename)
    print filename
    fp = open(filename, 'wb')
    fp.write(part.get_payload(decode=True))
    fp.close()

# Copyright (C) 2012 Ivan Kanis
# Author: Ivan Kanis
#
# 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
# the Free Software Foundation ; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY ; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program ; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# vi:et:sw=4:ts=4:
# Local Variables:
# compile-command: "python foo.py"
# End:
#
# vi:et:sw=4:ts=4:

  reply	other threads:[~2012-12-21  8:11 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-20  9:31 Ivan Kanis
2012-12-21  0:15 ` Katsumi Yamaoka
2012-12-21  8:11   ` Ivan Kanis [this message]
2012-12-21 14:33     ` Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87vcbvx1t0.fsf@googlemail.com \
    --to=ivan.kanis@googlemail.com \
    --cc=ding@gnus.org \
    --cc=yamaoka@jpl.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).