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.
6 # Please note: you have to manually install and configure the package 'msmtp' before using this script
9 PATH
="/usr/sbin:/usr/bin:/sbin:/bin"
11 if [ -r "/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
")"
21 adb_mail
="$(command -v msmtp)"
22 adb_logger
="$(command -v logger)"
23 adb_logread
="$(command -v logread)"
28 local class
="${1}" log_msg
="${2}"
30 if [ -x "${adb_logger}" ]
32 "${adb_logger}" -p "${class}" -t "adblock-${adb_ver}[${$}]" "${log_msg}"
34 printf "%s %s %s\\n" "${class}" "adblock-${adb_ver}[${$}]" "${log_msg}"
38 if [ -z "${adb_mailreceiver}" ]
40 f_log
"err" "please set the mail receiver with the 'adb_mailreceiver' option"
44 if [ "${adb_debug}" -eq 1 ]
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"
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" ]
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}" ]
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}')"
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>"
73 if [ -x "${adb_mail}" ]
75 printf "%b" "${adb_mailhead}${adb_mailtext}" 2>/dev/null | "${adb_mail}" ${debug} -a "${adb_mailprofile}" "${adb_mailreceiver}" >/dev
/null
2>&1
77 f_log
"info" "mail sent to '${adb_mailreceiver}' with rc '${adb_rc}'"
79 f_log
"err" "msmtp mail daemon not found"