]> diplodocus.org Git - nmh/blob - docs/contrib/localpostproc
context_find.c: Hoist strlen(3) out of search loop.
[nmh] / docs / contrib / localpostproc
1 #!/bin/sh
2 #
3 # localpostproc - A sample postproc which changes the submission email server
4 # based on user-supplied criteria.
5 #
6 # The basic concept is that we change where we submit mail to based on the
7 # message contents. We use scan(1) to get out fields we care about. But
8 # really, you could use ANY criteria, such as environment variables,
9 # recipients, etc etc.
10 #
11
12 #
13 # Find the draft message, always the last argument, and whether we've
14 # been called with -whom; the latter sucks.
15 #
16 # The case statement has to know about switches that take arguments;
17 # add to this list as necessary.
18 #
19
20 whom=0
21
22 find_draftmessage() {
23 while test $# -gt 0; do
24 case "$1" in
25 -al* | -filt* | -wi* | -client | -idanno | -server | \
26 -partno | -saslmech | -user | -por* | -width | \
27 -file* | -mhl* | -mt* | -cr* | -lib* | -auth* | -sendmail)
28 shift
29 ;;
30 -whom)
31 whom=1
32 ;;
33 -*) ;;
34 *)
35 draftmessage="$1"
36 return 0
37 ;;
38 esac
39 shift
40 done
41
42 echo "Cannot find draft message name in argument list"
43 exit 1
44 }
45
46 realpost="$(mhparam libexecdir)/post"
47
48 if [ $# -eq 0 ]; then
49 echo "Usage: [post switches] filename"
50 exit 1
51 fi
52
53 find_draftmessage "$@"
54
55 if [ $whom -eq 1 ]; then
56 exec "$realpost" "$@"
57 fi
58
59 fromhost=$(scan -format '%<{resent-from}%(host{resent-from})%|%(host{from})%>' -file "$draftmessage")
60
61 if [ $? -ne 0 ]; then
62 echo "Unable to run scan on draft file $draftmessage, aborting"
63 exit 1
64 fi
65
66 if [ -z "$fromhost" ]; then
67 echo "Could not determine hostname of From: address"
68 exit 1;
69 fi
70
71 #
72 # Here we use the hostname in the "from" address, but you could use anything
73 #
74
75 case "$fromhost" in
76 *host1.com)
77 postflags="-server smtp.host1.com -sasl -port submission"
78 ;;
79
80 host2.com)
81 postflags="-server smtp.host2.com -sasl -tls -port submission"
82 ;;
83
84 *)
85 echo "Don't know how to send email from $fromhost"
86 exit 1
87 ;;
88 esac
89
90 exec "$realpost" $postflags "$@"