1#!/bin/sh
2#
3# Copyright (C) 2001-2013 Graeme Walker <graeme_walker@users.sourceforge.net>
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17# ===
18#
19# emailrelay-submit.sh
20#
21# An example script that deposits e-mail messages into sub-directories
22# of the main E-MailRelay spool directory depending on the "To:"
23# address. This could be used with an E-MailRelay POP server running
24# with the "pop-by-name" option so that messages get routed appropriately.
25#
26# usage: emailrelay-submit.sh
27#
28
29store="__SPOOL_DIR__"
30log="/var/log/emailrelay-submit.out"
31awk="awk" # nawk
32
33tmp="/tmp/`basename $0.$$.tmp`"
34trap "rm -f \"${tmp}\" 2>/dev/null ; exit 0" 0
35trap "rm -f \"${tmp}\" 2>/dev/null ; exit 1" 1 2 3 13 15
36
37List()
38{
39	# Maps from the given "To:" address to a spool subdirectory -- edit as required
40	to_="${1}"
41	to_="`echo \"${to_}\" | tr '[A-Z]' '[a-z]'`"
42	case "${to_}" in
43		me@*) echo me_1 me_2 ;;
44		other@*) echo other_1 other_2 ;;
45		*) echo postmaster ;;
46	esac
47}
48
49Create()
50{
51	# Creates a spool subdirectory if it doesnt already exist
52	dir_="${1}"
53	if test ! -f "${dir_}"
54	then
55		echo `basename $0`: creating directory \"${dir_}\"
56		mkdir "${dir_}"
57		chown root:daemon "${dir_}"
58		chmod 775 "${dir_}"
59	fi
60}
61
62Main()
63{
64	# take a copy of the content
65	cat > ${tmp}
66
67	# parse out the "To:" address
68	to="`head -500 \"${tmp}\" | grep '^To:' | ${awk} '{print $2}'`"
69	echo `basename $0`: to \"${to}\"
70
71	# submit the message into the main spool directory
72	content="`cat \"${tmp}\" | \"${sbin}emailrelay-submit\" --verbose --spool-dir \"${store}\" \"${to}\"`"
73	envelope="`echo \"${content}\" | sed 's/content/envelope/'`"
74	if test \! -f "${content}"
75	then
76		echo `basename $0`: emailrelay-submit failed >&2
77		trap "" 0 # leave it in /tmp
78		return
79	fi
80
81	# link & copy into subdirectories
82	copied="0"
83	for name in `List "${to}"` ""
84	do
85		if test "${name}" != ""
86		then
87			Create "${store}/${name}"
88
89			c="${store}/${name}/`basename \"${content}\"`"
90			e="${store}/${name}/`basename \"${envelope}\"`"
91
92			ln "${content}" "${c}" && cp -p "${envelope}" "${e}"
93			if test "$?" -ne 0 ; then return ; fi
94			copied="1"
95		fi
96	done
97
98	# delete from the main directory
99	if test "${copied}" -eq 1
100	then
101		rm -f "${envelope}" && rm -f "${content}"
102	fi
103}
104
105Main "$@" >> "${log}" 2>&1
106
107