#!/bin/sh
#
# Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ===
#
# emailrelay-process.sh
#
# An example "--filter" script for the E-MailRelay SMTP server which does
# rot-13 masking.
#

awk="awk"
tmp="/tmp/`basename $0`.$$.tmp"
log="/tmp/`basename $0`.out"
trap "rm -f \"${tmp}\" >/dev/null 2>&1 ; exit" 0 1 2 3 13 15

###
# ProcessContent()
# Processes the content part of an RFC822 message. This
# implementation does rot13 masking.
#
ProcessContent()
{
	"${awk}" '
	BEGIN {
		map_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		map_lower = tolower(map_upper)
		in_header = 1
	}

	function rot( n , c , map )
	{
		return index(map,c) ? substr(map,((index(map,c)+n-1)%length(map))+1,1) : c
	}

	function rot_c( n , c )
	{
		return rot(n,rot(n,c,map_upper),map_lower)
	}

	function rot_s( n , string )
	{
		rot_s_result = ""
		for( i = 1 ; i <= length(string) ; i++ )
			rot_s_result = rot_s_result rot_c(n,substr(string,i,1))
		return rot_s_result
	}

	{
		is_blank = match($0,"^[[:space:]]*$")
		if( in_header && is_blank )
			in_header = 0

		if( in_header )
			print
		else
			print rot_s(13,$0)
	}
	'
}

###
# Wrap()
# Processes an RCF822 message so that the original content
# appears as an attachment.
#
Wrap()
{
	"${awk}" -v boundary="-----`basename $0`.$$" -v message="$@" '
	BEGIN {
		in_header = 1
		n = 1
	}
	{
		is_blank = match($0,"^[[:space:]]*$")
		if( in_header && is_blank )
		{
			printf( "Content-Type: multipart/mixed; boundary=\"%s\"\r\n" , boundary )
			printf( "\r\n" )
			printf( "\r\n" )
			printf( "--%s\r\n" , boundary )
			printf( "Content-Type: text/plain; charset=us-ascii\r\n" )
			printf( "\r\n" )
			printf( "%s\r\n" , message )
			printf( "\r\n" )
			printf( "--%s\r\n" , boundary )
			printf( "Content-Type: message/rfc822\r\n" )
			printf( "Content-Transfer-Encoding: 8bit\r\n" )
			printf( "Content-Description: encrypted message\r\n" )
			printf( "\r\n" )
			for( i = 1 ; i < n ; i++ )
				print header[i]
		}

		if( in_header && is_blank )
			in_header = 0

		if( in_header )
		{
			header[n++] = $0

			is_mime_content = match($0,"^Content-")
			is_continuation = match($0,"^[[:space:]][[:space:]]*[^[:space:]]")
			suppress = is_mime_content || (was_mime_content && is_continuation)
			was_mime_content = suppress

			if( ! suppress )
				print
		}
		else
		{
			print
		}
	}
	END {
		printf( "--%s--\r\n" , boundary )
		printf( "\r\n" )
	}
	'
}

Main()
{
	cat "${1}" | ProcessContent | Wrap "The original message has been encrypted..." > "${tmp}"
	cp "${tmp}" "${1}"
}

debug="0"
if test "${debug}" -eq 1
then
	Main "$@" > "${log}" 2>&1
else
	Main "$@"
fi

