Rework LuCI build system
[project/luci.git] / applications / luci-app-pbx-voicemail / root / etc / pbx-voicemail / pbx-send-voicemail
1 #!/bin/sh
2 #
3 # Copyright 2011 Iordan Iordanov <iiordanov (AT) gmail.com>
4 #
5 #    This file is part of luci-pbx-voicemail.
6 #
7 #    luci-pbx-voicemail is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU General Public License as published by
9 #    the Free Software Foundation, either version 3 of the License, or
10 #    (at your option) any later version.
11 #
12 #    luci-pbx-voicemail is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU General Public License for more details.
16 #
17 #    You should have received a copy of the GNU General Public License
18 #    along with luci-pbx-voicemail.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 #
21 # Thanks to http://www.zedwood.com for providing an excellent example of how to
22 # properly assemble an email message with a base64 encoded attachment.
23 #
24
25 LOGFILE=/tmp/voicemail/last_sent_voicemail.log
26
27 # Redirect standard error and standard output to a log file.
28 rm  -f "$LOGFILE"
29 exec 1>"$LOGFILE"
30 exec 2>&1
31
32 # Appends its second argument to a file named in the first argument.
33 append_to_file ()
34 {
35     echo "$2">>$1;
36 }
37
38 # Grab the attachment name, which should be sent as the first argument, and
39 # exit with a warning if there is no voicemail to send.
40 ATTACHMENT="$1"
41 [ ! -f "$ATTACHMENT" ] && echo "WARNING: Found no voicemail recording to send." && exit
42
43 # Grab the callerID which should have been sent as an argument.
44 CALLERID="$2"
45 [ -z "$CALLERID" ]   && CALLERID="An unknown caller"
46
47 # Determine addresses we would like to send the voicemail to and exit if none are found.
48 TO="`uci -q get pbx-voicemail.global_voicemail.global_email_addresses | tr ' ' ','`"
49 [ -z "$TO" ] && echo "WARNING: Found no addresses to send voicemail to." && exit
50
51 # See whether we should retain a copy of the voicemail.
52 SAVEPATH="`uci -q get pbx-voicemail.global_voicemail.global_save_path`"
53
54 DATE="`date +%Y-%m-%d`"
55 TIME="`date +%H:%M:%S`"
56 FROM="voicemail@pbx"
57 REPLY="do-not-reply@pbx"
58 SUBJECT="Voicemail from $CALLERID, $DATE, $TIME"
59 MSGBODY="$CALLERID has left voicemail for you on $DATE at $TIME."
60 MIMETYPE="audio/wav"
61 TMP1="/tmp/voicemail/tmpemail1.$$";
62 TMP2="/tmp/voicemail/tmpemail2.$$";
63 BOUNDARY="`date +%s | md5sum | awk '{print $1}'`"
64 FILENAME="voicemail-$DATE-$TIME.WAV"
65
66 # Clean up just in case.
67 rm -f $TMP1 $TMP2
68
69 append_to_file $TMP1 "From: $FROM"
70 append_to_file $TMP1 "To: $TO"
71 append_to_file $TMP1 "Reply-To: $REPLY"
72 append_to_file $TMP1 "Subject: $SUBJECT"
73 append_to_file $TMP1 "Content-Type: multipart/mixed; boundary=\""$BOUNDARY"\""
74 append_to_file $TMP1 ""
75 append_to_file $TMP1 "This is a MIME formatted message.  If you see this text it means that your"
76 append_to_file $TMP1 "email software does not support MIME formatted messages."
77 append_to_file $TMP1 ""
78 append_to_file $TMP1 "--$BOUNDARY"
79 append_to_file $TMP1 "Content-Type: text/plain; charset=ISO-8859-1; format=flowed"
80 append_to_file $TMP1 "Content-Transfer-Encoding: 7bit"
81 append_to_file $TMP1 "Content-Disposition: inline"
82 append_to_file $TMP1 ""
83 append_to_file $TMP1 "$MSGBODY"
84 append_to_file $TMP1 ""
85 append_to_file $TMP1 ""
86 append_to_file $TMP1 "--$BOUNDARY"
87 append_to_file $TMP1 "Content-Type: $MIMETYPE; name=\"$FILENAME\""
88 append_to_file $TMP1 "Content-Transfer-Encoding: base64"
89 append_to_file $TMP1 "Content-Disposition: attachment; filename=\"$FILENAME\";"
90 append_to_file $TMP1 ""
91
92 append_to_file $TMP2 ""
93 append_to_file $TMP2 ""
94 append_to_file $TMP2 "--$BOUNDARY--"
95 append_to_file $TMP2 ""
96 append_to_file $TMP2 ""
97
98 # Cat everything together and pass to msmtprc to send out.
99 ( cat $TMP1
100   cat "$ATTACHMENT" | base64
101   cat $TMP2 ) | msmtp -t -C /etc/pbx-msmtprc
102
103 # Clean up email temp files.
104 rm -f $TMP1 $TMP2
105
106 # Either delete or move the attachment based on the SAVEPATH variable.
107 if [ -z "$SAVEPATH" ]
108 then
109     rm -f "$ATTACHMENT"
110 else
111     mkdir -p "$SAVEPATH"
112     mv --backup=t "$ATTACHMENT" "$SAVEPATH/$FILENAME"
113 fi
114