Merge pull request #11353 from kvuorine/fwknop-fixes
[feed/packages.git] / net / adblock / files / adblock.mail
1 #!/bin/sh
2 # send mail script for adblock notifications
3 # Copyright (c) 2015-2020 Dirk Brenken (dev@brenken.org)
4 # This is free software, licensed under the GNU General Public License v3.
5
6 # Please note: you have to manually install and configure the package 'msmtp' before using this script
7
8 LC_ALL=C
9 PATH="/usr/sbin:/usr/bin:/sbin:/bin"
10
11 if [ -r "/lib/functions.sh" ]
12 then
13 . "/lib/functions.sh"
14 adb_debug="$(uci_get adblock global adb_debug "0")"
15 adb_mailsender="$(uci_get adblock global adb_mailsender "no-reply@adblock")"
16 adb_mailreceiver="$(uci_get adblock global adb_mailreceiver)"
17 adb_mailtopic="$(uci_get adblock global adb_mailtopic "adblock notification")"
18 adb_mailprofile="$(uci_get adblock global adb_mailprofile "adb_notify")"
19 fi
20 adb_ver="${1}"
21 adb_mail="$(command -v msmtp)"
22 adb_logger="$(command -v logger)"
23 adb_logread="$(command -v logread)"
24 adb_rc=1
25
26 f_log()
27 {
28 local class="${1}" log_msg="${2}"
29
30 if [ -x "${adb_logger}" ]
31 then
32 "${adb_logger}" -p "${class}" -t "adblock-${adb_ver}[${$}]" "${log_msg}"
33 else
34 printf "%s %s %s\\n" "${class}" "adblock-${adb_ver}[${$}]" "${log_msg}"
35 fi
36 }
37
38 if [ -z "${adb_mailreceiver}" ]
39 then
40 f_log "err" "please set the mail receiver with the 'adb_mailreceiver' option"
41 exit ${adb_rc}
42 fi
43
44 if [ "${adb_debug}" -eq 1 ]
45 then
46 debug="--debug"
47 fi
48
49 adb_mailhead="From: ${adb_mailsender}\\nTo: ${adb_mailreceiver}\\nSubject: ${adb_mailtopic}\\nReply-to: ${adb_mailsender}\\nMime-Version: 1.0\\nContent-Type: text/html\\nContent-Disposition: inline\\n\\n"
50
51 # info preparation
52 #
53 sys_info="$(strings /etc/banner 2>/dev/null; ubus call system board | sed -e 's/\"release\": {//' | sed -e 's/^[ \t]*//' | sed -e 's/[{}\",]//g' | sed -e 's/[ ]/ \t/' | sed '/^$/d' 2>/dev/null)"
54 adb_info="$(/etc/init.d/adblock status 2>/dev/null)"
55 if [ -f "/var/log/messages" ]
56 then
57 log_info="$(awk '/adblock-/{NR=1;max=79;if(length($0)>max+1)while($0){if(NR==1){print substr($0,1,max),"↵"} else {print " ",substr($0,1,max)}{$0=substr($0,max+1);NR=NR+1}}else print}' /var/log/messages)"
58 elif [ -x "${adb_logread}" ]
59 then
60 log_info="$("${adb_logread}" -e "adblock-" | awk '{NR=1;max=79;if(length($0)>max+1)while($0){if(NR==1){print substr($0,1,max),"↵"} else {print " ",substr($0,1,max)}{$0=substr($0,max+1);NR=NR+1}}else print}')"
61 fi
62
63 # mail body
64 #
65 adb_mailtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
66 adb_mailtext="${adb_mailtext}\\n<strong>++\\n++ System Information ++\\n++</strong>\\n${sys_info}"
67 adb_mailtext="${adb_mailtext}\\n\\n<strong>++\\n++ Adblock Information ++\\n++</strong>\\n${adb_info}"
68 adb_mailtext="${adb_mailtext}\\n\\n<strong>++\\n++ Logfile Information ++\\n++</strong>\\n${log_info}"
69 adb_mailtext="${adb_mailtext}</pre></body></html>"
70
71 # send mail
72 #
73 if [ -x "${adb_mail}" ]
74 then
75 printf "%b" "${adb_mailhead}${adb_mailtext}" 2>/dev/null | "${adb_mail}" ${debug} -a "${adb_mailprofile}" "${adb_mailreceiver}" >/dev/null 2>&1
76 adb_rc=${?}
77 f_log "info" "mail sent to '${adb_mailreceiver}' with rc '${adb_rc}'"
78 else
79 f_log "err" "msmtp mail daemon not found"
80 fi
81 exit ${adb_rc}