Support sub-groups in pkgusr.el
[pkgusr] / bin / mail
1 #!/bin/zsh
2
3 ## Copyright (C) 2007 - 2010 Steve Youngs
4
5 ## Time-stamp: <Friday Oct 15, 2010 22:17:02 steve>
6
7 ## Redistribution and use in source and binary forms, with or without
8 ## modification, are permitted provided that the following conditions
9 ## are met:
10 ##
11 ## 1. Redistributions of source code must retain the above copyright
12 ##    notice, this list of conditions and the following disclaimer.
13 ## 2. Redistributions in binary form must reproduce the above copyright
14 ##    notice, this list of conditions and the following disclaimer in the
15 ##    documentation and/or other materials provided with the distribution.
16 ## 3. Neither the name of the University nor the names of its contributors
17 ##    may be used to endorse or promote products derived from this software
18 ##    without specific prior written permission.
19
20 ## THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 ## IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 ## WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 ## DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 ## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 ## BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 ## WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29 ## OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30 ## IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32
33 ## Commentary:
34 #
35 #  This is a simple wrapper around sendmail that serves as a command
36 #  line mailer, along the lines of mailx or nail.
37
38 ## Code:
39 ourname=${0##*/}
40
41 # Version info
42 VERSION=0.3
43 COPYRIGHT="Copyright (C) 2007 - 2010 Steve Youngs <steve@steveyoungs.com>"
44 version_str="${ourname}: ${VERSION}
45 ${COPYRIGHT}"
46
47 _version () { echo $version_str }
48
49 # Help/Usage
50 usage ()
51 {
52     cat<<EOF
53                                   $ourname
54
55     ${VERSION}
56
57 Synopsis:
58 --------
59
60   $ourname
61         [ -F FROM | --from=FROM ] [ -s SUBJECT | --subject=SUBJECT ]
62         [ -c CC[,...CCn] | --cc=CC[,...CCn] ] [ -f FILE | --file=FILE ]
63         RECIPIENT[,...RECIPIENTn]
64   $ourname
65         [ -v | --version ] [ -h | --help | --usage ]
66
67 Description:
68 -----------
69
70   $ourname is a simple command line mailer.  It is not really meant as
71   a replacement for something better like mailx or nail, but it should
72   suffice until you install a more feature rich mailer.  The author
73   uses SXEmacs/Gnus for day to day email, and this script for any
74   command line mailing.
75
76 Options:
77 -------
78
79   -F FROM
80   --from=FROM  
81         Specify an alternate From address.  If omitted, try the
82         environment variable EMAIL, fall back to:
83           $USER@$(hostname -f).
84
85   -s SUBJECT
86   --subject=SUBJECT
87         Specify a Subject header.  If omitted, "No Subject given" will
88         be used.
89
90   -c CC[,...CCn]
91   --cc=CC[,...CCn]
92         Optional addresses to include on a Cc header.
93
94   -f FILE
95   --file=FILE
96         A plain text file containing the body of the email message.
97         If this option is omitted, stdin is used.  This option should
98         always be the last on the command line.
99
100   -v
101   --version
102         Display version.
103
104   -h
105   --help
106   --usage
107         Display this help.
108
109 Environment Variables:
110 ---------------------
111
112   EMAIL -- To set a From address.  Maybe necessary if your publically
113            known email address is different from your system default.
114
115 ${COPYRIGHT}
116
117 EOF
118     return 0
119 }
120
121 # Process the mail's headers
122 process_mail ()
123 {
124     local MTA=/usr/sbin/sendmail
125
126     [[ -n ${FROM} ]] ||
127         { FROM=${EMAIL} && [[ -n ${FROM} ]] || FROM=${USER}@$(hostname -f) }
128
129     [[ -n ${SUBJECT} ]] || SUBJECT="No Subject given"
130
131     (
132         echo "To: ${RCPT}"
133         [[ -n ${CC} ]] && echo "Cc: ${CC}"
134         echo "From: <${FROM}>"
135         echo "Date: $(date --rfc-2822)"
136         echo "Subject: ${SUBJECT}"
137         echo "User-Agent: bastard mailer, ${VERSION}"
138         echo
139         cat ${FILE}
140     ) | ${MTA} "${RCPT}"
141 }
142
143
144 # Parse the command line
145 args=vh-:F:s:c:f:
146 rv=0
147
148 while getopts $args opts; do
149     case $opts in
150         (-)
151             case $OPTARG in
152                 (from?*)     FROM=${OPTARG/from=/} ;;
153                 (subject?*)  SUBJECT=${OPTARG/subject=/} ;;
154                 (cc?*)       CC=${OPTARG/cc=/} ;;
155                 (file?*)     FILE=${OPTARG/file=/} ;;
156                 (version)    _version; exit 0 ;;
157                 (help|usage) usage; exit 0 ;;
158                 (*)
159                     print Unrecognised option: --$OPTARG >&2
160                     print See $ourname --help >&2
161                     rv=1
162                     ;;
163             esac
164             ;;
165         (F) FROM=${OPTARG} ;;
166         (s) SUBJECT=${OPTARG} ;;
167         (c) CC=${OPTARG} ;;
168         (f) FILE=${OPTARG} ;;
169         (v) _version; exit 0 ;;
170         (h) usage; exit 0 ;;
171         (*)
172             print Unrecognised option -$OPTARG >&2
173             print see $ourname --help >&2
174             rv=1
175             ;;
176     esac
177 done
178 shift $(( $OPTIND - 1 ))
179
180 # What's left on the command line _should_ be the recipients
181 RCPT=$argv
182
183 if [[ $rv -eq 0 ]]; then
184     process_mail
185     exit $?
186 else
187     exit $rv
188 fi