You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
3.0 KiB

  1. #!/bin/sh
  2. # send mail script for adblock notifications
  3. # Copyright (c) 2015-2022 Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # Please note: you have to manually install and configure the package 'msmtp' before using this script
  6. # set (s)hellcheck exceptions
  7. # shellcheck disable=all
  8. LC_ALL=C
  9. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  10. [ -r "/lib/functions.sh" ] && . "/lib/functions.sh"
  11. adb_debug="$(uci_get adblock global adb_debug "0")"
  12. adb_mailsender="$(uci_get adblock global adb_mailsender "no-reply@adblock")"
  13. adb_mailreceiver="$(uci_get adblock global adb_mailreceiver)"
  14. adb_mailtopic="$(uci_get adblock global adb_mailtopic "adblock notification")"
  15. adb_mailprofile="$(uci_get adblock global adb_mailprofile "adb_notify")"
  16. adb_ver="${1}"
  17. adb_mail="$(command -v msmtp)"
  18. adb_logger="$(command -v logger)"
  19. adb_logread="$(command -v logread)"
  20. adb_rc="1"
  21. f_log() {
  22. local class="${1}" log_msg="${2}"
  23. if [ -x "${adb_logger}" ]; then
  24. "${adb_logger}" -p "${class}" -t "adblock-${adb_ver}[${$}]" "${log_msg}"
  25. else
  26. printf "%s %s %s\n" "${class}" "adblock-${adb_ver}[${$}]" "${log_msg}"
  27. fi
  28. }
  29. if [ -z "${adb_mailreceiver}" ]; then
  30. f_log "err" "please set the mail receiver with the 'adb_mailreceiver' option"
  31. exit ${adb_rc}
  32. fi
  33. [ "${adb_debug}" = "1" ] && debug="--debug"
  34. adb_mailhead="From: ${adb_mailsender}\nTo: ${adb_mailreceiver}\nSubject: ${adb_mailtopic}\nReply-to: ${adb_mailsender}\nMime-Version: 1.0\nContent-Type: text/html;charset=utf-8\nContent-Disposition: inline\n\n"
  35. # info preparation
  36. #
  37. sys_info="$(
  38. strings /etc/banner 2>/dev/null
  39. 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
  40. )"
  41. adb_info="$(/etc/init.d/adblock status 2>/dev/null)"
  42. rep_info="${2}"
  43. if [ -x "${adb_logread}" ]; then
  44. log_info="$("${adb_logread}" -l 100 -e "adblock-" | awk '{NR=1;max=120;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}')"
  45. fi
  46. # mail body
  47. #
  48. adb_mailtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  49. adb_mailtext="${adb_mailtext}\n<strong>++\n++ System Information ++\n++</strong>\n${sys_info}"
  50. adb_mailtext="${adb_mailtext}\n\n<strong>++\n++ Adblock Information ++\n++</strong>\n${adb_info}"
  51. if [ -n "${rep_info}" ]; then
  52. adb_mailtext="${adb_mailtext}\n\n<strong>++\n++ Report Information ++\n++</strong>\n${rep_info}"
  53. fi
  54. adb_mailtext="${adb_mailtext}\n\n<strong>++\n++ Logfile Information ++\n++</strong>\n${log_info}"
  55. adb_mailtext="${adb_mailtext}</pre></body></html>"
  56. # send mail
  57. #
  58. if [ -x "${adb_mail}" ]; then
  59. printf "%b" "${adb_mailhead}${adb_mailtext}" 2>/dev/null | "${adb_mail}" ${debug} -a "${adb_mailprofile}" "${adb_mailreceiver}" >/dev/null 2>&1
  60. adb_rc=${?}
  61. f_log "info" "mail sent to '${adb_mailreceiver}' with rc '${adb_rc}'"
  62. else
  63. f_log "err" "msmtp mail daemon not found"
  64. fi
  65. exit ${adb_rc}