forked from aardsoft/cups-email
-
Notifications
You must be signed in to change notification settings - Fork 0
/email
Copy path253 lines (225 loc) · 7.79 KB
/email
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/bin/bash
#
# /usr/lib/cups/backend/email - Script to send e-mail confirmations via the CUPS
# print server
#
# This program is a modified version of fax4cups, available at
# http://vigna.dsi.unimi.it/fax4CUPS/
#
# The current home of this script is https://github.com/aardsoft/cups-email
#
# This script should not need any modifications to adjust it to specific
# installations - all configuration happens through printer options and
# mailrc files. Please read the documentation.
#
# If you do need to make modifications please submit them upstream.
#
###############################################################################
# Variables set through installable options. When extending the PPD
# a corresponding variable usually needs to be added here.
config_vars="Mailx
EpsToPdf
MailBody
MailDefaultDomain
MailSubject
MailFrom
MailTo
MailToSource
mailx_MAILRC
mailx_smtp
mailx_replyto
mailx_sender
TempDirectory
PdfFilePattern"
# Variables set through the user when printing. Via lpr a user can set
# arbitrary options. This list limits it to options exposed via the user
# interface (i.e., for everything not in this list the override will get
# ignored)
config_user_vars="MailSubject
MailTo
MailFrom
MailBody"
echo "DEBUG:email backend call arguments $@" 1>&2
# when called without any arguments CUPS expects us to print a backend
# info for use in the new printer page
if [ $# -eq 0 ]; then
echo "virtual email \"Unknown\" \"E-Mail Device\""
exit 0
fi
function getOptionFromPpd(){
_ppd=$1
shift
_option=$1
shift
_default_only=$1
if ! [ -f "$_ppd" ]; then
echo "ERROR:PPD in $_ppd does not exist." 1>&2
return
fi
_default_option=`grep "^*Default$_option:" $_ppd`
if [ $? -ne 0 ]; then
echo "WARNING:No default value for $_option found in $_ppd" 1>&2
return
fi
_default_option_value=${_default_option##*: }
echo "DEBUG:Default option found with value '$_default_option_value'" 1>&2
if [ -n "$_default_only" ]; then
_ret=$_default_option_value
elif [[ "$_default_option_value" =~ ^Custom\. ]]; then
echo "DEBUG:Custom value found" 1>&2
_ret=${_default_option_value/Custom./}
else
echo "DEBUG:Default value found" 1>&2
_default_value=`grep "^*$_option $_default_option_value/" $_ppd`
_ret=${_default_value##*:}
fi
echo ${_ret//\"}
}
# This reads the queue default options from the queue-specific PPD, and then
# performs initial error handling - no point in continuing if we can see the
# environment is broken.
if [ -z "$PPD" ]; then
echo "ERROR:No PPD provided in request, unable to read options." 1>&2
else
for _var in $config_vars; do
_ret=$(getOptionFromPpd $PPD $_var)
export $_var="$_ret"
done
echo "DEBUG:Environment after PPD parsing: `env`" 1>&2
fi
if [ ! -x "$Mailx" ]; then
echo "ERROR:mailx in '$Mailx' is not usable" 1>&2
exit 1
fi
if [ ! -x "$EpsToPdf" ]; then
echo "ERROR:epstopdf in '$EpsToPdf' is not usable" 1>&2
exit 1
fi
# Check some of the job specific options - everything listed in the
# config_user_vars variable may override global options. We also
# look for billing information.
echo "DEBUG:Options - $5" 1>&2
#TODO: this breaks with spaces in options
for _option in $5; do
echo "DEBUG:Current option: '$_option'" 1>&2
IFS== read _opt_name _opt_value <<< $_option
if [ "$_opt_name" == "job-billing" ]; then
export BillingInfo="$_opt_value"
elif [[ "$config_user_vars" == *$_opt_name* ]]; then
# Some UI dialogs include all user overridable settings, even if
# they are at defaults. They also add the (technically correct)
# Custom. prefix to user specified fields.
# As a result we need to compare options to default options before
# adding overrides, and strip the Custom.
_ret=$(getOptionFromPpd $PPD $_opt_name 1)
if [ "$_ret" == "$_opt_value" ]; then
echo "DEBUG:$_opt_name seems to have default value $_ret" 1>&2
else
echo "DEBUG:Exporting $_opt_name=$_opt_value" 1>&2
export $_opt_name="${_opt_value#Custom.}"
fi
fi
done
case "$MailToSource" in
Auto)
# 1. MailTo set? Take it.
# 2. User name contains @? Take it.
# 3. Job name contains @? Take it.
# 4. Billing info exists and contains @? Take it.
# 5. Take the user name, as it has the highest change to resolve
# to a valid address.
if [ -n "$MailTo" ]; then
echo "DEBUG:Selecting to address from MailTo" 1>&2
break
elif [[ "$2" == *@* ]]; then
echo "DEBUG:Selecting to address from user name" 1>&2
MailTo=$2
break
elif [[ "$3" == *@* ]]; then
echo "DEBUG:Selecting to address from job name" 1>&2
MailTo=$3
break
elif [ -n "$BillingInfo" ] && [[ "$BillingInfo" == *@* ]]; then
echo "DEBUG:Selecting to address from billing info" 1>&2
MailTo=$BillingInfo
break
else
echo "DEBUG:Selecting to address from user name (fallback)" 1>&2
MailTo=$2
break
fi
;;
JobName)
MailTo=$3
;;
UserName)
MailTo=$2
;;
BillingInfo)
if [ -n "$BillingInfo" ]; then
MailTo=$BillingInfo
else
echo "ERROR:Billing info requested as address source, but not present" 1>&2
fi
;;
*)
echo "ERROR:Invalid mail source configuration '$MailToSource'" 1>&2
exit 1
;;
esac
# depending on sendmail setup continuing without a domain might fail, but
# as we don't know the server we just continue either way. This might result
# in a mailx error halting the queue later on, though.
if ! [[ "$MailTo" == *@* ]] && [ -n "$MailDefaultDomain" ]; then
MailTo="$MailTo@$MailDefaultDomain"
fi
if [ -z "$MailFrom" ]; then
echo "ERROR:MailFrom is not defined" 1>&2
exit 1
fi
# cut the mailx_ prefix from all mailx specific vars, and export them for
# mailx
for _var in `set|grep ^mailx_`; do
export ${_var/mailx_/}
done
# if mailrc is just a file name assume it's supposed to be in the cups
# serverroot (generally /etc/cups)
if ! [[ "$MAILRC" == /* ]]; then
export MAILRC="$CUPS_SERVERROOT/$MAILRC"
fi
echo "DEBUG:Environment after exporting mailx variables: `env`" 1>&2
echo 1>&2 # Apparently the first character emitted is somehow "eaten" by the reader
if [ $# -eq 6 ]; then
MAILFILE=$6
else
MAILFILE=$(mktemp </dev/null $TempDirectory/$PdfFilePattern) ||
(echo "ERROR:Failed to create temporary file" 1>&2; exit 1)
cat >$MAILFILE
fi
MAILPDF="$MAILFILE.pdf"
_epstopdf_status=$($EpsToPdf $MAILFILE -o $MAILPDF 2>&1)
_epstopdf_rc=$?
if [ $_epstopdf_rc -ne 0 ]; then
echo "ERROR:Converting to PDF failed: $_epstopdf_status" 1>&2
exit $_epstopdf_rc
fi
# without this mailx returns without error even if sending has failed
export sendwait=1
_mailx_version=$($Mailx -V)
if echo $_mailx_version|grep -q "GNU Mailutils"; then
echo "DEBUG:echo '$MailBody' | $Mailx -n -r '$MailFrom' -s '$MailSubject' -a $MAILPDF $MailTo" 1>&2
_mailx_status=$(echo "$MailBody" | $Mailx -n -r "$MailFrom" -s "$MailSubject" -a $MAILPDF $MailTo 2>&1)
_mailx_rc=$?
else
echo "DEBUG:echo '$MailBody' | $Mailx -n -r '$MailFrom' -s '$MailSubject' -A $MAILPDF $MailTo" 1>&2
_mailx_status=$(echo "$MailBody" | $Mailx -n -r "$MailFrom" -s "$MailSubject" -A $MAILPDF $MailTo 2>&1)
_mailx_rc=$?
fi
rm -f $MAILPDF
if [ $# -lt 6 ];then
rm -f $MAILFILE
fi
if [ $_mailx_rc -ne 0 ]; then
echo "ERROR:Mail sending failed: $_mailx_status" 1>&2
fi
exit $_mailx_rc