da6558181b233da8128412ee75463c2928b92b46
[feed/packages.git] / net / travelmate / files / travelmate.mail
1 #!/bin/sh
2 # send mail script for travelmate notifications
3 # Copyright (c) 2020-2021 Dirk Brenken (dev@brenken.org)
4 # This is free software, licensed under the GNU General Public License v3.
5
6 # set (s)hellcheck exceptions
7 # shellcheck disable=1091,3040
8
9 # Please note: you have to setup the package 'msmtp' before using this script
10
11 . "/lib/functions.sh"
12
13 export LC_ALL=C
14 export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
15 set -o pipefail
16
17 trm_debug="$(uci_get travelmate global trm_debug "0")"
18 trm_mailreceiver="$(uci_get travelmate global trm_mailreceiver)"
19 trm_mailprofile="$(uci_get travelmate global trm_mailprofile "trm_notify")"
20 trm_mailsender="$(uci_get travelmate global trm_mailsender "no-reply@travelmate")"
21 trm_rtfile="$(uci_get travelmate global trm_rtfile "/tmp/trm_runtime.json")"
22 trm_mailpgm="$(command -v msmtp)"
23 trm_logger="$(command -v logger)"
24
25 if [ -z "${trm_mailreceiver}" ]; then
26 "${trm_logger}" -p "err" -t "trm-mail [${$}]" "please set the mail receiver with the 'trm_mailreceiver' option" 2>/dev/null
27 exit 1
28 fi
29
30 if [ "${trm_debug}" = "1" ]; then
31 debug="--debug"
32 fi
33
34 # info preparation
35 #
36 sys_info="$(
37 strings /etc/banner 2>/dev/null
38 ubus call system board | awk 'BEGIN{FS="[{}\"]"}{if($2=="kernel"||$2=="hostname"||$2=="system"||$2=="model"||$2=="description")printf " + %-12s: %s\n",$2,$4}'
39 )"
40 trm_info="$(/etc/init.d/travelmate status 2>/dev/null)"
41 sta_info="$(jsonfilter -i "${trm_rtfile}" -q -l1 -e '@.data.station_id')"
42 trm_mailtopic="$(uci_get travelmate global trm_mailtopic "travelmate connection to '${sta_info}'")"
43 trm_mailhead="From: ${trm_mailsender}\nTo: ${trm_mailreceiver}\nSubject: ${trm_mailtopic}\nReply-to: ${trm_mailsender}\nMime-Version: 1.0\nContent-Type: text/html;charset=utf-8\nContent-Disposition: inline\n\n"
44
45 # mail body
46 #
47 trm_mailtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
48 trm_mailtext="${trm_mailtext}\n<strong>++\n++ System Information ++\n++</strong>\n${sys_info}"
49 trm_mailtext="${trm_mailtext}\n\n<strong>++\n++ Travelmate Information ++\n++</strong>\n${trm_info}"
50 trm_mailtext="${trm_mailtext}</pre></body></html>"
51
52 # send mail
53 #
54 printf "%b" "${trm_mailhead}${trm_mailtext}" 2>/dev/null | "${trm_mailpgm}" ${debug} -a "${trm_mailprofile}" "${trm_mailreceiver}" >/dev/null 2>&1
55 "${trm_logger}" -p "info" -t "trm-mail [${$}]" "mail sent to '${trm_mailreceiver}' with rc '${?}'" 2>/dev/null