Debian Codegraph @

email2fax-axmail

raw
1 #!/bin/bash -x
2
3 # email2fax-axmail 1.0beta
4 # This script allows you to send faxes through your Hylafax server.
5 # It extracts PDF,TIFF or PS attachments from an email and then passes
6 # it to Hylafax thru "sendfax" command.
7
8 # Hylafax adaptation by:
9 # (c) Brian Rogers - NCC 2007
10 # (c) Raffaello Cassetta 2006
11 # Original work by:
12 # (c) Tomasz Chmielewski < tch @ wpkg . org > 2005-2006
13 # License: GPLv2
14
15 # Variables you might want to change
16
17 FAXMAIL="NCC FAX <[email protected]>" # email from which confirmations are sent
18
19 DEFSENDER="[email protected]" # If email doesn't have "From:" field, default to this for Hylafax error mails
20
21 LOGFILE=/var/log/fax.log # where to log
22
23 TMPDIR=/var/email2fax # dir for temp files; must be writable by the launching user
24
25 MAXAGE=30 # how long to keep temp files, in minutes.
26 # There should be no reason for keeping these files so long because
27 # once sendfax has enqueued the document, this is stored in Hylafax
28 # spool directory and there resides until fax transmission's completion
29 # In a production environment, with tens or hundreds of fax submitted every
30 # minute, you should reduce this to a value as low as possible. Anyway, leave
31 # some grace time to tolerate delays in bash script executions (due to high
32 # CPU load or whatelse)
33
34 WHOAMI=`whoami` # User that launches script - useful for testing/debugging when invoked
35 # by MDAs or other daemons
36
37 SENDFAXOPTS=" " # Default sendfax options
38
39
40 # You probably don't have to change anything below...
41
42 DATADIR=$TMPDIR/`date +%s-%N`
43 EMAILFILE=$DATADIR/emailfile.eml
44 COVERFILE=$DATADIR/coverpage.txt
45 MESSAGEFILE=$DATADIR/message.txt
46
47 DATETIME=`date +"%A, %B %d %Y, at %R:%S"` # date put in email confirmations
48 #DATETIME=`date +"%A, %B %d %Y, at %r"` # date put in email confirmations, different format
49
50 VERSION="email2fax-axmail v1.0" # version
51
52 PATH="/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:"$PATH
53
54 ############################################################
55 # check how we are started, print help if pipe wasn't used #
56 ############################################################
57
58 [ -p /dev/stdin ]
59 [ $? -eq 1 ] && EMPTY=1 && cat <<EOF
60 $VERSION, email to fax gateway for Hylafax
61
62 Usage:
63
64 "piped message" | email2fax-axmail [OPTIONS]
65
66 Options:
67 --nomail don't send any error or confirmation email when a message has been submitted (default: send error or confirmation email)
68 --noconfirm don't send any confirmation email when a message has been submitted, send email only for errors (default: send confirmation or error email)
69 --nodelete don't delete temporary files (default: delete temporary files)
70 --nofax don't send the fax (default: send fax)
71 --debug enable debug logging
72 --notify enable notification by electronic mail when the facsimile has been delivered.
73 by default HylaFAX will notify the submitter only if there is a problem with a job.
74 --notify-all enable notification by electronic mail when the facsimile has been delivered and when it is
75 requeued for retransmission. by default HylaFAX will notify the submitter only if there is a
76 problem with a job.
77 --usebody uses message body as faxcover
78
79 Email can contain either PDF,PS,TIFF attachments or text body, and must have at least one destination fax number as a subject.
80 Multiple destination numbers should be separated by spaces.
81 If no attachment is supplied, message body is sent as fax.
82
83 Examples:
84
85 This one sends a fax from the command line, and enables debug logging:
86
87 cat message.eml | email2fax-axmail --debug
88
89
90 This one enables debug logging, doesn't send a confirmation email,
91 doesn't delete temporary files, and doesn't send a fax (useful for testing etc.):
92
93 cat message.eml | email2fax-axmail --debug --nomail --nodelete --nofax
94
95
96 This entry in .procmailrc located in fax recipient home directory intercepts all emails
97 to fax recipient, and passes them to email2fax-axmail:
98
99 :0fw
100 | /usr/local/bin/email2fax-axmail
101
102
103 The same can be done in .forward file located in the same directory with this semantic:
104
105 "| /usr/local/bin/email2fax-axmail"
106
107 quotes included
108
109
110 By default, email2fax-axmail logs ALL messages to $LOGFILE, and saves temporary files in $TMPDIR.
111 These paths, along with several different variables, can be changed
112 at the beginning of this script ($0).
113
114 See http://wpkg.org/email2fax for updates, bug reports, and answers.
115
116 EOF
117
118 [ "$EMPTY" == "1" ] && exit 0
119
120 cat <<EOF >> $LOGFILE
121 --------------------------------------------------------------
122 $VERSION started on $DATETIME with options:
123
124 $0 $@
125
126 by user: $WHOAMI
127
128 EOF
129
130
131 #################################################
132 # Check the error code and make a proper action #
133 #################################################
134
135 send_msg()
136 {
137
138 [ $1 -eq 1 -o $1 -eq 2 ] && NEWSUBJECT="Fax problem"
139 [ $1 -eq 0 -a $NOFAX -eq 1 ] && NEWSUBJECT="Fax not sent"
140 [ $1 -eq 0 -a $NOFAX -eq 0 ] && NEWSUBJECT="Fax confirmation"
141
142 #FAXNUMBERS=`echo $NEWNUM | sed -e "s/ /, /g" -e "s/@/ /" | awk '{print $2}'`
143 FAXNUMBERS=`cat $COVERFILE |grep Subject:|sed -e "s/Subject: //" -e "s/@/ /" | awk '{print $2}' `
144 cat <<EOF >$MESSAGEFILE
145 From: $FAXMAIL
146 To: $SENDER
147 Subject: $NEWSUBJECT
148
149 Dear sender,
150
151 EOF
152
153 case "$1" in
154
155 0)
156 if [ $NOFAX -eq 0 ] ; then
157
158 if [ $NOCONFIRM -eq 0 ] ; then
159 cat <<EOF >>$MESSAGEFILE
160 The message was accepted for fax delivery.
161
162 Fax number(s): $FAXNUMBERS
163 Date: $DATETIME
164
165 EOF
166 [ $NOMAIL -ne 1 ] && cat $MESSAGEFILE | sendmail -t
167
168 fi
169 else
170
171 cat <<EOF >>$MESSAGEFILE
172 Fax sending is currently disabled, please contact your administrator.
173
174 Fax number(s): $FAXNUMBERS
175 Date: $DATETIME
176
177 EOF
178 [ $NOMAIL -ne 1 ] && cat $MESSAGEFILE | sendmail -t
179
180 fi
181
182 del_temp
183
184 ;;
185
186 1)
187 echo "No e-mail message passed to email2fax-axmail, or message broken." >>$LOGFILE
188 cat <<EOF >>$MESSAGEFILE
189 The message you sent was broken or invalid.
190 Check if it had a proper attachment (must be one PDF or one TIFF attachment
191 or plain text body),
192 and if the fax number was correct, and try again.
193 If the problem persists, contact your administrator.
194
195 Fax number(s): $FAXNUMBERS
196 Date: $DATETIME
197
198 EOF
199
200 [ $NOMAIL -ne 1 ] && cat $MESSAGEFILE | sendmail -t
201 del_temp
202 ;;
203
204 2)
205 cat <<EOF >>$MESSAGEFILE
206 System not configured properly for sending faxes, contact your administrator.
207
208 Date: $DATETIME
209
210 EOF
211
212
213 [ $NOMAIL -ne 1 ] && echo $NOMAIL && cat $MESSAGEFILE | sendmail -t
214 del_temp
215
216 esac
217 }
218
219
220 #######################################
221 # Delete temporary files if requested #
222 #######################################
223
224 del_temp()
225 {
226 if [ $NODELETE -eq 0 ] ; then
227 [ $DEBUG -eq 1 ] && echo "Removing old temporary files" >>$LOGFILE
228 [ $DEBUG -eq 0 ] && find $TMPDIR/* -type d -mmin +$MAXAGE -exec rm -fvr {} \; &>/dev/null
229 [ $DEBUG -eq 1 ] && find $TMPDIR/* -type d -mmin +$MAXAGE -exec rm -fvr {} \; >>$LOGFILE
230 echo >>$LOGFILE
231 else
232 echo "Not removing old temporary files" >>$LOGFILE
233 echo >>$LOGFILE
234 fi
235 }
236
237
238 #####################################################
239 # By default, our internal "error code" should be 0 #
240 #####################################################
241 ERRORCODE=0
242
243
244 #############################################
245 # Make sure temp dir exists and is writable #
246 #############################################
247
248 mkdir -p $DATADIR
249 if [ $? -ne 0 ] ; then
250 echo "$DATADIR not writable, can't continue!" >>$LOGFILE
251 ERRORCODE=2
252 fi
253
254 ###########################################
255 # Check if "munpack" program is installed #
256 ###########################################
257
258 if [ `which munpack &>/dev/null ; echo $?` -ne 0 ] ; then
259 echo "munpack is not installed: can't extract attachments, can't continue!" >>$LOGFILE
260 ERRORCODE=2
261 fi
262
263 ############################################
264 # Read flags and set appropriate behaviour #
265 ############################################
266
267 # --nomail takes precedence over --noconfirm. If --nomail is present then --noconfirm is ignored
268 [ `echo $@ | grep -i -q '\-\-nomail' ; echo $?` -eq 0 ] && NOMAIL=1 || NOMAIL=0
269 if test $NOMAIL = 0; then
270 [ `echo $@ | grep -i -q '\-\-noconfirm' ; echo $?` -eq 0 ] && NOCONFIRM=1 || NOCONFIRM=0
271 else
272 NOCONFIRM=0
273 fi
274
275 [ `echo $@ | grep -i -q '\-\-usebody' ; echo $?` -eq 0 ] && USEBODY=1 || USEBODY=0
276 [ `echo $@ | grep -i -q '\-\-nodelete' ; echo $?` -eq 0 ] && NODELETE=1 || NODELETE=0
277 [ `echo $@ | grep -i -q '\-\-nofax' ; echo $?` -eq 0 ] && NOFAX=1 || NOFAX=0
278 [ `echo $@ | grep -i -q '\-\-debug' ; echo $?` -eq 0 ] && DEBUG=1 || DEBUG=0
279
280 # --notify-all takes precedence over --notify. If --notify-all is present then --notify is ignored
281 [ `echo $@ | grep -i -q '\-\-notify\-all' ; echo $?` -eq 0 ] && NOTIFYALL=1 || NOTIFYALL=0
282 if test $NOTIFYALL = 1; then
283 NOTIFY="-R"
284 else
285 [ `echo $@ | grep -i -q '\-\-notify' ; echo $?` -eq 0 ] && NOTIFY="-D" || NOTIFY=""
286 fi
287
288 UNKNOWN_FLAGS=`echo $@ | sed -e "s/--nomail//gI" -e "s/--nodelete//gI" -e "s/--nofax//gI" -e "s/--debug//gI" -e "s/--usebody//gI" -e "s/--notify-all//gI" -e "s/--notify//gI" -e "s/--noconfirm//gI"`
289 [ -z $UNKNOWN_FLAGS ]
290 [ $? -ne 0 ] && echo "WARNING: unknown flags: $UNKNOWN_FLAGS" >>$LOGFILE
291
292 if [ $DEBUG -eq 1 ] ; then
293 cat <<EOF >>$LOGFILE
294 Variables used:
295
296 FAXMAIL: $FAXMAIL
297 LOGFILE: $LOGFILE
298 VERSION: $VERSION
299 TMPDIR: $TMPDIR
300 DATADIR: $DATADIR
301 EMAILFILE: $EMAILFILE
302 MESSAGEFILE: $MESSAGEFILE
303 DATETIME: $DATETIME
304
305 EOF
306
307 fi
308
309
310 ############################################################
311 # If we can't continue, send a message with an explanation #
312 ############################################################
313
314 [ $ERRORCODE -eq 2 ] && send_msg 2 && exit 2
315
316
317 #########################################
318 # Make a temporary file out of an email #
319 #########################################
320
321 cat | sed 's/$//' > $EMAILFILE
322 cp $EMAILFILE $COVERFILE
323
324 ##############################
325 # COVERPAGE VARIABLES ADDED: #
326 ##############################
327 COVERTO=`cat $COVERFILE |grep Subject:|sed -e "s/Subject: //" -e "s/@/ /" | awk '{print $1}' `
328 COVERCOM=`cat $COVERFILE | grep Subject:|sed -e "s/Subject: //" -e "s/@/ /" | awk '{$1=""; $2=""; sub(" ", " "); print}' `
329
330 ######################################################################
331 # If input file is too short (broken email etc.) send error and quit #
332 ######################################################################
333
334 if [ `du -b $EMAILFILE | awk '{print $1}'` -lt 15 ] ; then
335 echo "No e-mail message passed to email2fax-axmail, or message broken." >>$LOGFILE
336 # send_msg 1
337 # exit 1
338 fi
339
340
341 #############################################################################################
342 # Get the email address to send a confirmation to, and telephone number(s) to send faxes to #
343 # Verify suitable data and take appropriate action #
344 #############################################################################################
345
346 SUBJLINE=`grep -m 1 "Subject:" $EMAILFILE`
347 # TOLINE=`grep -m 1 "To:" $EMAILFILE.1`
348 SENDER=`grep -m 1 "From:" $EMAILFILE | sed -e "s/From://"`
349 NUMBERS=`echo $SUBJLINE | awk '{print NF}'`
350 SUBJECT=`echo $SUBJLINE | sed -e "s/Subject://" `
351
352 # If no fax numbers in the subject, send an explanation and quit
353 [ $NUMBERS -lt 2 ] && send_msg 1 && exit 1
354
355 # If no sender in file, don't send a confirmation email
356 if [ -z "$SENDER" ] ; then
357 NOMAIL=1
358 SENDER=$DEFSENDER
359 fi
360
361 #########################################################################
362 # Extract everything from mail, check files and take appropriate action #
363 #########################################################################
364
365 # extract everything from mail
366 MOUT=`munpack -t -C "$DATADIR" "$EMAILFILE"`
367
368 # I AGREE: the following "for" cycle is terrible, please make it better
369 # ^^^^^^^^
370 SEQ="ODD"
371 for P in $MOUT
372 do
373 if [ "$SEQ" = "ODD" ] ; then
374 SEQ="EVEN"
375 FORVAR=$P
376 else
377 FORVAR=$FORVAR" "$P
378 if [ `echo "$FORVAR"|grep -c "text/plain"` -ne 0 ] ; then
379 TXT=`echo $FORVAR | cut -f1 -d " "`" "$TXT
380 # TXT=`cat $DATADIR/$EMAILFILE|formail -k -X X-Origin: -X X-Mailer >$DATADIR/part1`
381 $FORVAR=$P
382 elif [ `echo "$FORVAR"|grep -c -e "pdf" -e "tif" -e "postscript"` -ne 0 ] ; then
383 ATTACH=`echo $FORVAR | cut -f1 -d " "`" "$ATTACH
384 else
385 mkdir $DATADIR/convdir
386 cat $DATADIR/emailfile.eml|formail -k -X X-Origin: -X X-Mailer >$DATADIR/part1
387 touch $DATADIR/convdir/foo
388 /usr/bin/text2ps -B $DATADIR/part1 > $DATADIR/convdir/part1.ps
389 # fi
390 fi
391 SEQ="ODD"
392 fi
393 done
394
395 # If no txt nor pdf nor tif nor ps parts are present, send error and exit
396 # If only text is present, then it's only a mail
397 if [ -z "$ATTACH" -a -z "$TXT" || -f "$CONVDIR/part1.ps" ] ; then
398 echo "No pdf nor tif nor ps nor text body present." >>$LOGFILE
399 send_msg 1
400 exit 1
401 elif [ -z "$ATTACH" ] ; then
402 ISMAIL=1
403 else
404 ISMAIL=0
405 fi
406
407
408 #############################################################################
409 # Read destination numbers from subject, verify and take appropriate action #
410 #############################################################################
411
412 # Convert fax numbers from the subject field to separate numbers
413 SUBJLINE=`grep -m 1 "Subject:" $EMAILFILE | sed -e "s/Subject://" -e "s/[-() ]/ /g" -e "s/,/ /g"`
414
415 NEWNUM=""
416
417 for FAXNUM in $SUBJLINE
418
419 do
420 FAXNUM=`cat $COVERFILE | grep -m 1 "Subject:" | sed -e "s/Subject: //" -e "s/@/ /" | awk '{print $2}' `
421 [ ! -z `echo "$FAXNUM" | sed -e "s/[0-9]//g"` ] && echo "Wrong fax number: $FAXNUM" >>$LOGFILE && FAXNUM=
422
423 [ -n "$FAXNUM" ] && NEWNUM="$NEWNUM $FAXNUM"
424
425 done
426
427 # If no numbers have been munged, send an error message and exit
428 if test "$NEWNUM" = "" ; then
429
430 echo "No destination numbers present in Subject field." >>$LOGFILE
431 # send_msg 1
432 # exit 1
433
434 fi
435
436 # Log who sends fax where
437 cat <<EOF >>$LOGFILE
438 Fax from$SENDER to fax number(s): $FAXNUM
439 Attachments: $ATTACH
440 EOF
441
442
443 ############################
444 # Send document to sendfax #
445 ############################
446
447 if [ $NOFAX -eq 0 ] ; then
448
449 # If the user wants to send the body or it's only a text message, then create text parts list.
450 # We use faxmail to convert plain text to ps format - just to avoid strange characters that could confuse sendfax.
451 # There should be a dedicated app for doing this, but faxmail is available here and now (default with Hylafax)- nonetheless, it works :P
452 if [ $USEBODY -eq 1 -o $ISMAIL -eq 1 ] ; then
453 CONVDIR="$DATADIR/convdir" # to avoid name collisions we create a dir for converted text
454 mkdir $CONVDIR
455 for ITEM in $TXT
456 do
457 cat $DATADIR/emailfile.eml | formail -k -X X-Origin: -X X-mailer: > part1
458 TXT2PS="$CONVDIR/$ITEM.ps"
459 /usr/bin/text2ps -B $DATADIR/part1 > $CONVDIR/part1.ps
460 TEXTPART=""$TXT2PS" "$TEXTPART""
461 done
462 else
463 TEXTPART=""
464 fi
465
466 # If we have to send PDF or TIF attachments then create list
467 if [ $ISMAIL -eq 0 ] ; then
468 for ITEM in $ATTACH
469 do
470 ATTACHMENT=""$DATADIR/$ITEM" "$ATTACHMENT""
471 done
472 else
473 ATTACHMENT=""
474 fi
475
476 # create destination array for "sendfax"
477 NUM=`cat $COVERFILE | grep -m 1 "Subject:" | sed -e "s/Subject: //" -e "s/@/ /" | awk '{print $2}' `
478 # DEST=""
479 # for NUM in $NEWNUM
480 # do
481 # DEST="-d $COVERTO@$NUM "$DEST
482 DEST="-d $COVERTO@$NUM"
483 # done
484 # TEXTPART="$CONVDIR/part1.ps"
485
486 ## if [ -z "$TEXTPART" ]
487 sendfax -r "Amateur Communication" -x "axMail-Fax" -c "$COVERCOM" -f "$SENDER" $DEST $CONVDIR/part1.ps $TEXTPART $ATTACHMENT >> $LOGFILE 2>&1 &
488 ## else
489 ## sendfax -r "Email2Fax" -x "E-Fax at NCC" -c "$COVERCOM" -f "$SENDER" $DEST $TEXTPART $ATTACHMENT >> $LOGFILE 2>&1 &
490 ## fi
491 else
492
493 echo "Not sending fax because --nofax was used" >>$LOGFILE
494
495 fi
496
497 echo >>$LOGFILE
498
499 send_msg 0