[SPAM?] Re: [SPAM?] Can I use Mutt from Bash to extract attachments into an arbitrary directory?

2016-09-14 Thread Cameron Simpson

On 14Sep2016 16:12, Are Troi  wrote:

Last night at a technical talk I lamented the loss around 5 years ago
from Fedora of command-line tools to extract email attachments from a
BASH script and a colleague told me Mutt can do this.


If you mean the MIME tools mpack and munpak, I still use them. (On a Mac, where 
the Macports package is called mpack).


Wouldn't you be better off just fetching and buiulding them?


I installed Mutt, checked out the man pages, went through the
documentation, spent an hour or more searching the archives and it is
not at all readily apparent that Mutt can do this, or how.


Mutt is probably a poor match for the task because although it will decode 
messages etc, all the saving is interactive. In particular, there's no API for 
"iterating" over attachments, let along recursively.



I fired up mutt on an example file and it opens it up just fine, the
problem is having a user drive the user interface defeats the whole
purpose; it must be done programatically. So, I've been looking into
the various libraries that handle MIME - I understand Perl and Python
both have good libraries, though I've been more interested in the Java
one because there are some synergies there - but nevermind all that.


I'd be going for the Python stuff, lacking your context.


My supposition is that if mutt can do it, the only way is to define a
macro, as documented on this page:

https://dev.mutt.org/trac/wiki/MuttFaq/Attachment

(A brief but hopefully helpful digression is that I've spent a good
bit of time in the documentation and other materials and apparently
there's some tiny little assumption made about macros that is too
obvious to the documentation writer to bother documenting but is
unknwon to a newbie about how / where macros are defined. There's
nothing in the man page, nothing on the on-screen help, and the
documentation apparently assumes you already know how they're defined!
The best help I found so far is the "(ab)use "macros" as variables" on
the wiki under "ConfigTricks" - from that I can probably figure it
out...)


Macros get defined in your config file, with all the other mutt config. Mutt 
can be told to use a specific config file instead of the default on the command 
line if you wish.



So.. that example was:
macro attach W /home/gawron/attachments/ macro


I think the lack of iteration kills the macro approach.

I advocate getting the mpack tools and using them, or writing something using 
Python or Java that suits your needs.



...I'm guessing... And I'd launch it like this:
$ cd /dir/where/email/is
$ echo A | mutt -f mailfile.name
...Am I on the right track? What do I expect in the directory? Any
other guidance, please?


I have a macro for mutt bound to:

 mail-open-attachments

but mail-open-attachments is just a shell script that uses the mpack tools:

 https://bitbucket.org/cameron_simpson/css/src/tip/bin/mail-open-attachments

It just makes a scratch directory, unpacks the files, and opened the scratch 
directory in the Finder. It is my quick'n'dirty for grabbing what I've been 
sent.


Cheers,
Cameron Simpson 


[SPAM?] Re: Can I use Mutt from Bash to extract attachments into an arbitrary directory?

2016-09-14 Thread Luis Mochan
On Wed, Sep 14, 2016 at 04:12:48PM -0700, Are Troi wrote:
> Hi All,
> 
> Last night at a technical talk I lamented the loss around 5 years ago
> from Fedora of command-line tools to extract email attachments from a
> BASH script and a colleague told me Mutt can do this.
> ...

Maybe you could use the program 'ripmime' directly from bash. I found
it in the debian repositories. I use it manually through a mutt macro

macro index Ys "| ~/.mutt/saveattachments\n" "Save attachments"

in my muttrc file. The script 'saveattachments' is the following

#!/bin/bash
# put filenames into arguments
# empty directory unless it is already empty
set - ~/attachments/*   
[ "$*" != "$HOME/attachments/*" ] && rm ~/attachments/*
ripmime -i - -d ~/attachments

I always use the same directory to save the attachments manually, but
I guess the lines above may be modified to let mutt save the
attachments automatically in the desired directory.

-- 

  o
W. Luis Mochán,  | tel:(52)(777)329-1734 /<(*)
Instituto de Ciencias Físicas, UNAM  | fax:(52)(777)317-5388 `>/   /\
Apdo. Postal 48-3, 62251 |   (*)/\/  \
Cuernavaca, Morelos, México  | moc...@fis.unam.mx   /\_/\__/
GPG: 791EB9EB, C949 3F81 6D9B 1191 9A16  C2DF 5F0A C52B 791E B9EB




[SPAM?] Can I use Mutt from Bash to extract attachments into an arbitrary directory?

2016-09-14 Thread Are Troi
Hi All,

Last night at a technical talk I lamented the loss around 5 years ago
from Fedora of command-line tools to extract email attachments from a
BASH script and a colleague told me Mutt can do this.

I installed Mutt, checked out the man pages, went through the
documentation, spent an hour or more searching the archives and it is
not at all readily apparent that Mutt can do this, or how.

Just to be clear what my goals are: I'm trying to automate processing
of inbound email for a particular job. I have the mail server
(postfix) using the .forward ability to pipe to a script, which has
the advantage of running as the owner of the script. The script dumps
the mail into a particular directory - no other contents. It's just
the one file. From there, I need to extract all the MIME parts /
attachments, in order. Encoded parts need to be decoded - lots of
images are expected, perhaps some HTML, etc.

I fired up mutt on an example file and it opens it up just fine, the
problem is having a user drive the user interface defeats the whole
purpose; it must be done programatically. So, I've been looking into
the various libraries that handle MIME - I understand Perl and Python
both have good libraries, though I've been more interested in the Java
one because there are some synergies there - but nevermind all that.

My supposition is that if mutt can do it, the only way is to define a
macro, as documented on this page:

https://dev.mutt.org/trac/wiki/MuttFaq/Attachment

(A brief but hopefully helpful digression is that I've spent a good
bit of time in the documentation and other materials and apparently
there's some tiny little assumption made about macros that is too
obvious to the documentation writer to bother documenting but is
unknwon to a newbie about how / where macros are defined. There's
nothing in the man page, nothing on the on-screen help, and the
documentation apparently assumes you already know how they're defined!
The best help I found so far is the "(ab)use "macros" as variables" on
the wiki under "ConfigTricks" - from that I can probably figure it
out...)

So.. that example was:

macro attach W /home/gawron/attachments/ macro
attach E /home/gawron/attachments/

It's not clear why there are two bindings here, but anyway, this
wouldn't work because a concurrent mail deilivery would have a race
condition and screw up the processing but the script could cd into the
directory first...

So, maybe the macro would be:

macro attach A .

...I'm guessing... And I'd launch it like this:

$ cd /dir/where/email/is
$ echo A | mutt -f mailfile.name

...Am I on the right track? What do I expect in the directory? Any
other guidance, please?

Regards,
Troi


[SPAM?] Re: Can I use Mutt from Bash to extract attachments into an arbitrary directory?

2016-09-14 Thread Cameron Simpson

On 14Sep2016 18:35, David Champion  wrote:

I'd be going for the Python stuff, lacking your context.

See attached.
You can pipe a message into this program (within mutt or elsewhere):
| mutt-savefiles /tmp/foo

It will create a directory under /tmp/foo named for the message's
message-id, and store each attachment inside. Filenames are taken
from the MIME or generated sequentially if there is no filename.


Just an aside, now often do you encounter "/" in a Message-ID? It is legal, and 
has long discouraged me from the otherwise obvious and inuitive 
name-a-file-after-the-message-id.



#!/usr/bin/env python

[...]

That is very nice, even cleaner than I imagined it might be.

Cheers,
Cameron Simpson 


[SPAM?] Re: [SPAM?] Re: [SPAM?] Can I use Mutt from Bash to extract attachments into an arbitrary directory?

2016-09-14 Thread David Champion
* On 14 Sep 2016, Cameron Simpson wrote: 
> 
> Mutt is probably a poor match for the task because although it will decode
> messages etc, all the saving is interactive. In particular, there's no API
> for "iterating" over attachments, let along recursively.

Agree. It's entirely doable, but not worth the trouble and the
maintenance when there are other fine options.

> I'd be going for the Python stuff, lacking your context.

See attached.

You can pipe a message into this program (within mutt or elsewhere):

| mutt-savefiles /tmp/foo

It will create a directory under /tmp/foo named for the message's
message-id, and store each attachment inside. Filenames are taken
from the MIME or generated sequentially if there is no filename.

-- 
David Champion • d...@bikeshed.us
#!/usr/bin/env python
#
# TODO: merge into sympafile
#

import os
import sys
import email
import mimetypes

m = email.message_from_file(sys.stdin)

# mimetypes very unfortunately maps text/plain to .ksh, so
# we'll favor this internal list in type lookups.
localtypes = {
'text/plain': '.txt',
}

if 'message-id' in m:
msgid = m['message-id'].strip('<>')
else:
msgid = str(time.time()).replace('.', '_')

if len(sys.argv) > 1:
dirname = sys.argv[1]
else:
dirname = '.'

dirname = os.path.join(dirname, msgid)
try:
os.makedirs(dirname)
except:
pass

n = 0
for p in m.walk():
n += 1
mtype = p.get_content_type()
if mtype.startswith('multipart/'):
# container
continue

ext = localtypes.get(mtype.lower()) or \
  mimetypes.guess_extension(mtype) or \
  '.bin'
filename = p.get_filename() or ('%02d%s' % (n, ext))
filename = os.path.join(dirname, filename)
data = p.get_payload()
print filename, len(data)
fp = open(filename, 'w')
fp.write(data)
fp.close()


signature.asc
Description: PGP signature