#!/bin/sh # (27.08.07) Fixed problem detecting html from munpack info # (01.10.07) Didn't remove tmp files on error condition. # (09.10.07) Replaced 'dc' subshell with shell arithmetic # Given an html email (either as body or attached as multi-part) it will unpack # the html and all the attachments. It rewrites the Content-ID resource # locators with the unpacked filenames so you can see attached images and such. # # Also accepts 'safe' argument. When received it kills all external URLs (for # images, javascript, etc). I use this when I want to see the html but don't # trust the source. # # All temp files are cleaned up. # # To use from mutt include this keybinding in the approparite config file. # Replace keys with whatever you like. # ## view an html email with firefox #macro index "/tmp/tmpbox\n~/bin/mutt-view-html.sh\n" "View HTML in browser" #macro pager "/tmp/tmpbox\n~/bin/mutt-view-html.sh\n" "View HTML in browser" # ## safe version (no external images loaded) #macro index "/tmp/tmpbox\n~/bin/mutt-view-html.sh safe\n" "View (safe) HTML in browser" #macro pager "/tmp/tmpbox\n~/bin/mutt-view-html.sh safe\n" "View (safe) HTML in browser" tmpdir=/tmp/mutthtmltmpdir mbox=/tmp/tmpbox mkdir $tmpdir cd $tmpdir file_info=`munpack -q -t -C $tmpdir $mbox` if [ "$file_info" = "Did not find anything to unpack from $mbox" ]; then cat $mbox | formail -x content-type: | grep -q text/html if [ $? -eq 0 ]; then file_info="part1 (text/html)" # sed here strips out mail headers cat $mbox | sed -e '1,/^$/ d' > part1 fi fi echo $file_info | grep -iq text/html if [ $? -ne 0 ]; then echo "No html found!" rmdir $tmpdir rm $mbox sleep 2 exit 1 fi file_html=`echo $file_info | sed 's/.*\(part[0-9]\+\) (text\/html).*/\1/g'` files=`echo $file_info | sed 's/\([^()]\+\) ([^()]\+)/\1/g'` # need list of datafiles in case attachments are unnamed data_files=`echo "$file_info" | grep -v "\(text/.*\)" | \ sed 's/\([^()]\+\) ([^()]\+)/\1/g'` html_file=$file_html.html mv $file_html $html_file files=`echo $files | sed "s/$file_html/$html_file/"` #/usr/bin/tidy -mq -wrap 0 $html_file 2> /dev/null if [ -n "$html_file" ]; then grep -qi src=\"cid: $html_file if [ $? -eq 0 ]; then filenames=`grep -i content-disposition $mbox | \ sed -e 's/.*filename="\([^"]\+\)"/\1/'` if [ -z $filesnames ]; then # unnamed file attachments :P filenames="$data_files" fi cids=`grep -i content-id $mbox | \ sed -e 's/Content-ID: <\([^<>]\+\)>/\1/'` count=1 for fn in $filenames; do cid=`echo $cids | awk -v count=$count '{ print $count }'` #count=`dc -e "1 $count + p"` count=$(( 1 + $count )) sed -i "s/cid:$cid/$fn/g" $html_file done fi if [ "$1" = "safe" ]; then grep -qi src=\"http $html_file if [ $? -eq 0 ]; then # misses javascript places images, but noscript should # take care of those sed -i "s/src\s*=\s*\"*https*:\/\/[^\">]*\"*/src=\"#\"/g" $html_file sed -i "s/background\s*=\s*\"*https*:\/\/[^\">]*\"*/background=\"#\"/g" $html_file fi fi else rm $files $mbox rmdir $tmpdir exit 1 fi (x-www-browser $tmpdir/$html_file; sleep 3; rm $files $mbox; rmdir $tmpdir) & #(x-www-browser $tmpdir/$html_file; sleep 3; rm $files; rmdir $tmpdir) & ## vim: ft=sh