mhtview source

Home
#!/usr/bin/env bash
#
#	mhtview 0.4 -	View HTML emails with inline images using any
#			MHT-compatible browser. Just pipe the whole email
#			message into mhtview.
#
#	Copyright (C) MMX-MMXVII  Dario Niedermann <dario@darioniedermann.it>
#
#	This program is free software: you can redistribute it and/or modify
#	it under the terms of  the GNU General Public License  version 3  as
#	published by the Free Software Foundation at:
#	
#		<http://www.gnu.org/licenses/>
#
#	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.
#
#	$Id: mhtview 106 2017-08-22 19:49:59Z ndr $
#______________________________________________________________________________

err()
{
	echo "${0##*/}: $1. Aborting..."
	exit 1
}

findTmpDir()				# Find out which temp directory to use:
{
	unset tmpDir
	for tmpDir in	"$TMPDIR" "$TMP" "$TEMP" "$TEMPDIR"	\
			"$HOME/tmp" {,/var}/tmp "$HOME" 0; do
		[ -d "$tmpDir" -a -w "$tmpDir" ] && break
	done
	[ "$tmpDir" = 0 ] && err 'Could not find a temporary directory'
}

mkTmpFileName()				# $1 = template for file name making
{					# returns $tmpFileName
	tmplt="$tmpDir/${0##*/}-$1"
	nomktmp=0; which mktemp 2>&- 1>&-
	if [ $? -eq 0 ]; then				# mktemp is installed
		tmpFileName=`mktemp "$tmplt"`
		nomktmp=$? # >0 if mktemp didn't work
	else						# mktemp not installed
		nomktmp=1
	fi
	# mktemp not installed or incompatible? Homegrown surrogate:
	if [ $nomktmp -gt 0 ]; then
		unset tmpFileName
		# make file names until one doesn't exist:
		while [ -z "$tmpFileName" -o -e "$tmpFileName" ]; do
			tmpFileName="${tmplt/XXXXXX/$$`date +%s`}"
		done
		touch "$tmpFileName"
	fi
	[ $? -gt 0 ] && err 'Could not create temp file'
}

browser="${MHTBROWSER:-$BROWSER}"
[ -z "$browser" ] && err 'set MHTBROWSER or BROWSER environment variables'

# else...
browserName="${browser##*/}"

findTmpDir
mkTmpFileName 'XXXXXX'; mhtFileName="$tmpFileName.mht"
cat >"$mhtFileName"	# STDIN (i.e.: the email msg) goes into this file

if [ "$browserName" = opera ]; then
	sessnDir=~/.opera/sessions	# where Opera stores user sessions
	ps ax |grep 'oper[a]' 1>&- 2>&-	# check if Opera is open
	operaIsNotRunning=$?

	# before starting Opera, temporarily put away its stored sessions:
	if [ $operaIsNotRunning -a -e "$sessnDir" ]; then
		mkTmpFileName operaSessions.XXXXXX
		rm -f "$tmpFileName"; movedSessionsDir="$tmpFileName"
		mkdir -m700 "$movedSessionsDir"
		mv "$sessnDir"/* "$movedSessionsDir"/
	fi

	opera --nomail --nosession "$mhtFileName"

	# after Opera quits, and if it wasn't open, move back session files:
	[ $operaIsNotRunning -a -e "$sessnDir" ] \
		&& mv "$movedSessionsDir"/* "$sessnDir"/
else
	"$browser" "$mhtFileName"
fi
exit 0
“MHTview” is Copyright © Dario Niedermann — Released with no warranty under the terms of the GPLv3 license. Written and tested on Linux using GNU tools.
HomeNo Javascript iconValid HTML 4.01 StrictValid CSS!