bash | mail | decode subjects with multiple encoded parts

function multiDecode() {
    CTXT="$@"
    while true; do
	echo "$CTXT" | grep '=?' > /dev/null
	if [ $? -ne 0 ]; then
	    echo -n "$CTXT"
	    return 0
	else
	    PAT="$(echo $CTXT | perl -ne 's|.*\=\?(.+)\?\=.*|$1|;print')"
	    RPAT="=?${PAT}?="

	    A_CONTENT=(${PAT//\?/ })
	    if [ "x${A_CONTENT[1]}" == "xQ" -o "x${A_CONTENT[1]}" == "xq" ]; then
		CRES="$(echo "${A_CONTENT[2]}" | qprint -d | sed 's|\&|\+|g' | iconv -t ${A_CONTENT[0]})"
	    else
		CRES="$(echo "${A_CONTENT[2]}" | base64 -i -d | sed 's|\&|\+|g' | iconv -t ${A_CONTENT[0]} )"
	    fi
	    XRES="$(echo "$CTXT" | sed "s|$RPAT|$CRES|")"
	    CTXT="$XRES"
	fi
    done
}

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.