Shallow Thoughts : tags : mime

Akkana's Musings on Open Source Computing and Technology, Science, and Nature.

Tue, 04 Apr 2023

A Fake File Manager

After learning how to prevent RawTherapee from intercepting requests for a file manager, I'm happy not to have unwanted RawTherapee windows randomly popping up whenever some program decides it wants to show me a directory. For instance, in Firefox's Download Manager, there's a little folder icon you can click on -- but it doesn't do anything useful if you don't have a file manager installed.

I suppose I could install a file manager; thunar is relatively lightweight. But it seems silly to have to install a whole GUI program I'll never otherwise use just to find out where files were stored. Once I know where to look, a terminal, with shell autocomplete, works fine for navigating my directories, and is much faster and less RSI-inducing than a mouse-based file manager.

Which raises the question: can I make the system do something useful on directory requests, and just show me where the file was stored, or give me a terminal already chdired to the right place? Sort of a fake file manager?

It turned out to be fairly easy.

Read more ...

Tags: , ,
[ 11:24 Apr 04, 2023    More linux | permalink to this entry | ]

Mon, 27 Mar 2023

Zoom Pops Up a Window for ... RawTherapee??

I've been annoyed for some time by the way that Zoom, when it finishes processing a recording after a meeting, pops up a ... Raw Therapee window??

RawTherapee is a program for handling RAW image files, the kind that many digital cameras can generate but that most image apps can't read. It's a fine program. But it's not a file manager, nor is it a video player. It makes absolutely no sense to pop it up to handle a video file. And it's very slow to start up, so I would leave a Zoom meeting, and then half a minute later this weird window would pop up for no apparent reason.

I've seen a few other programs, like wine, pop up these RawTherapee windows. I've been trying for many months to figure out why this happens, and I've finally found the answer, and a fix.

Read more ...

Tags: , ,
[ 16:26 Mar 27, 2023    More linux | permalink to this entry | ]

Sat, 22 May 2021

Mutt mailer: Show Messages With Attachments

A discussion on the Mutt-Users list a while back involved someone trying to configure mutt to show on the index screen which messages had attachments.

I had no idea that was possible! But it's something I've wanted for ages. Normally, mutt shows a list of attachments after the end of the email message. That was useful back in the day when people trimmed their email messages; but now, when most people append the contents of an entire message thread going back several weeks, scrolling down to the end of an email message is barely even possible.

What I'd really like is to see in the message view whether the message has attachments -- up at the top of the message, along with the headers. But showing it in the folder index would be a great start.

What Constitutes an Attachment?

First you have to define what attachments you care about. Most normal email messages have attachments just for the text.

Read more ...

Tags: , , ,
[ 10:46 May 22, 2021    More linux | permalink to this entry | ]

Mon, 07 Oct 2013

Viewing HTML mail messages from Mutt (or other command-line mailers)

Update: the script described in this article has been folded into another script called viewmailattachments.py.

Command-line mailers like mutt have one disadvantage: viewing HTML mail with embedded images. Without images, HTML mail is no problem -- run it through lynx, links or w3m. But if you want to see images in place, how do you do it?

Mutt can send a message to a browser like firefox ... but only the textual part of the message. The images don't show up.

That's because mail messages include images, not as separate files, but as attachments within the same file, encoded it a format known as MIME (Multipurpose Internet Mail Extensions). An image link in the HTML, instead of looking like <img src="picture.jpg">., will instead look something like <img src="cid:0635428E-AE25-4FA0-93AC-6B8379300161">. (Apple's Mail.app) or <img src="cid:1.3631871432@web82503.mail.mud.yahoo.com">. (Yahoo's webmail).

CID stands for Content ID, and refers to the ID of the image as it is encoded in MIME inside the image. GUI mail programs, of course, know how to decode this and show the image. Mutt doesn't.

A web search finds a handful of shell scripts that use the munpack program (part of the mpack package on Debian systems) to split off the files; then they use various combinations of sed and awk to try to view those files. Except that none of the scripts I found actually work for messages sent from modern mailers -- they don't decode the CID links properly.

I wasted several hours fiddling with various shell scripts, trying to adjust sed and awk commands to figure out the problem, when I had the usual epiphany that always eventually arises from shell script fiddling: "Wouldn't this be a lot easier in Python?"

Python's email package

Python has a package called email that knows how to list and unpack MIME attachments. Starting from the example near the bottom of that page, it was easy to split off the various attachments and save them in a temp directory. The key is

import email

fp = open(msgfile)
msg = email.message_from_file(fp)
fp.close()

for part in msg.walk():

That left the problem of how to match CIDs with filenames, and rewrite the links in the HTML message accordingly.

The documentation on the email package is a bit unclear, unfortunately. For instance, they don't give any hints what object you'll get when iterating over a message with walk, and if you try it, they're just type 'instance'. So what operations can you expect are legal on them? If you run help(part) in the Python console on one of the parts you get from walk, it's generally class Message, so you can use the Message API, with functions like get_content_type(), get_filename(). and get_payload().

More useful, it has dictionary keys() for the attributes it knows about each attachment. part.keys() gets you a list like

['Content-Type', 
 'Content-Transfer-Encoding',
 'Content-ID',
 'Content-Disposition' ]

So by making a list relating part.get_filename() (with a made-up filename if it doesn't have one already) to part['Content-ID'], I'd have enough information to rewrite those links.

Case-insensitive dictionary matching

But wait! Not so simple. That list is from a Yahoo mail message, but if you try keys() on a part sent by Apple mail, instead if will be 'Content-Id'. Note the lower-case d, Id, instead of the ID that Yahoo used.

Unfortunately, Python doesn't have a way of looking up items in a dictionary with the key being case-sensitive. So I used a loop:

    for k in part.keys():
        if k.lower() == 'content-id':
            print "Content ID is", part[k]

Most mailers seem to put angle brackets around the content id, so that would print things like "Content ID is <14.3631871432@web82503.mail.mud.yahoo.com>". Those angle brackets have to be removed, since the CID links in the HTML file don't have them.

for k in part.keys():
    if k.lower() == 'content-id':
        if part[k].startswith('<') and part[k].endswith('>'):
            part[k] = part[k][1:-1]

But that didn't work -- the angle brackets were still there, even though if I printed part[k][1:-1] it printed without angle brackets. What was up?

Unmutable parts inside email.Message

It turned out that the parts inside an email Message (and maybe the Message itself) are unmutable -- you can't change them. Python doesn't throw an exception; it just doesn't change anything. So I had to make a local copy:

for k in part.keys():
    if k.lower() == 'content-id':
        content_id = part[k]
        if content_id.startswith('<') and content_id.endswith('>'):
            content_id = content_id[1:-1]
and then save content_id, not part[k], in my list of filenames and CIDs.

Then the rest is easy. Assuming I've built up a list called subfiles containing dictionaries with 'filename' and 'Content-Id', I can do the substitution in the HTML source:

    htmlsrc = html_part.get_payload(decode=True)
    for sf in subfiles:
        htmlsrc = re.sub('cid: ?' + sf['Content-Id'],
                         'file://' + sf['filename'],
                         htmlsrc, flags=re.IGNORECASE)

Then all I have to do is hook it up to a key in my .muttrc:

# macro  index  <F10>  "<copy-message>/tmp/mutttmpbox\n<enter><shell-escape>~/bin/viewhtmlmail.py\n" "View HTML in browser"
# macro  pager  <F10>  "<copy-message>/tmp/mutttmpbox\n<enter><shell-escape>~/bin/viewhtmlmail.py\n" "View HTML in browser"

Works nicely! Here's the complete script: viewhtmlmail.

Tags: , , , , ,
[ 11:49 Oct 07, 2013    More tech/email | permalink to this entry | ]

Sat, 23 Aug 2008

Forwarding a message with attachments in Mutt

I've wanted to know forever how to forward a message with all or some of its attachments from mutt.

You can set the variable mime_forward so that when you forward a message, it includes the entire message, headers, attachments and all, as a single attachment. You can't edit this or change it in any way. If you want to trim the original message, or omit one of the attachments, you're out of luck.

I've found two ways to do it.

First: type v to get to the attachments screen. Type t repeatedly to tag all the attachments, including the initial small text/plain attachment (that's the original message body). When they're all tagged, type ;f (forward all tagged attachments). After you fill in the To: prompt, you'll be able to edit the message body, and when you leave the editor, you'll have the attachment list there to edit as you see fit.

If that doesn't work (I haven't tried it on HTML messages), there's a slightly more elaborate procedure: use
Esc-e   resend-message   use the current message as a template for a new one.
This calls up an editor on the current message, including headers. Change the From to your name, the To to your intended recipient, and edit the message body to your heart's content. When you're done, you're sent to the Compose screen, where you can adjust the attachment list and send the message.

Forwarding is pretty clearly not what Esc-E was intended for ... but it does the job and might be a handy trick to know.

Tags: , , ,
[ 23:14 Aug 23, 2008    More linux | permalink to this entry | ]