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.

84 lines
3.2 KiB

  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. # Please note: you have to manually install and configure the package 'msmtp' before using this script
  6. # set (s)hellcheck exceptions
  7. # shellcheck disable=1091,2010,2016,2034,2039,2059,2086,2091,2129,2143,2154,2181,2183,2188
  8. LC_ALL=C
  9. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  10. if [ -r "/lib/functions.sh" ]
  11. then
  12. . "/lib/functions.sh"
  13. adb_debug="$(uci_get adblock global adb_debug "0")"
  14. adb_mailsender="$(uci_get adblock global adb_mailsender "no-reply@adblock")"
  15. adb_mailreceiver="$(uci_get adblock global adb_mailreceiver)"
  16. adb_mailtopic="$(uci_get adblock global adb_mailtopic "adblock notification")"
  17. adb_mailprofile="$(uci_get adblock global adb_mailprofile "adb_notify")"
  18. fi
  19. adb_ver="${1}"
  20. adb_mail="$(command -v msmtp)"
  21. adb_logger="$(command -v logger)"
  22. adb_logread="$(command -v logread)"
  23. adb_rc=1
  24. f_log()
  25. {
  26. local class="${1}" log_msg="${2}"
  27. if [ -x "${adb_logger}" ]
  28. then
  29. "${adb_logger}" -p "${class}" -t "adblock-${adb_ver}[${$}]" "${log_msg}"
  30. else
  31. printf "%s %s %s\\n" "${class}" "adblock-${adb_ver}[${$}]" "${log_msg}"
  32. fi
  33. }
  34. if [ -z "${adb_mailreceiver}" ]
  35. then
  36. f_log "err" "please set the mail receiver with the 'adb_mailreceiver' option"
  37. exit ${adb_rc}
  38. fi
  39. if [ "${adb_debug}" -eq 1 ]
  40. then
  41. debug="--debug"
  42. fi
  43. 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"
  44. # info preparation
  45. #
  46. 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)"
  47. adb_info="$(/etc/init.d/adblock status 2>/dev/null)"
  48. if [ -f "/var/log/messages" ]
  49. then
  50. 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)"
  51. elif [ -x "${adb_logread}" ]
  52. then
  53. 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}')"
  54. fi
  55. # mail body
  56. #
  57. adb_mailtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  58. adb_mailtext="${adb_mailtext}\\n<strong>++\\n++ System Information ++\\n++</strong>\\n${sys_info}"
  59. adb_mailtext="${adb_mailtext}\\n\\n<strong>++\\n++ Adblock Information ++\\n++</strong>\\n${adb_info}"
  60. adb_mailtext="${adb_mailtext}\\n\\n<strong>++\\n++ Logfile Information ++\\n++</strong>\\n${log_info}"
  61. adb_mailtext="${adb_mailtext}</pre></body></html>"
  62. # send mail
  63. #
  64. if [ -x "${adb_mail}" ]
  65. then
  66. printf "%b" "${adb_mailhead}${adb_mailtext}" 2>/dev/null | "${adb_mail}" ${debug} -a "${adb_mailprofile}" "${adb_mailreceiver}" >/dev/null 2>&1
  67. adb_rc=${?}
  68. f_log "info" "mail sent to '${adb_mailreceiver}' with rc '${adb_rc}'"
  69. else
  70. f_log "err" "msmtp mail daemon not found"
  71. fi
  72. exit ${adb_rc}