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.

86 lines
3.1 KiB

  1. #!/bin/sh
  2. # send mail script for adblock notifications
  3. # Copyright (c) 2015-2021 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}" = "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;charset=utf-8\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. rep_info="${2}"
  49. if [ -x "${adb_logread}" ]
  50. then
  51. 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}')"
  52. fi
  53. # mail body
  54. #
  55. adb_mailtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  56. adb_mailtext="${adb_mailtext}\n<strong>++\n++ System Information ++\n++</strong>\n${sys_info}"
  57. adb_mailtext="${adb_mailtext}\n\n<strong>++\n++ Adblock Information ++\n++</strong>\n${adb_info}"
  58. if [ -n "${rep_info}" ]
  59. then
  60. adb_mailtext="${adb_mailtext}\n\n<strong>++\n++ Report Information ++\n++</strong>\n${rep_info}"
  61. fi
  62. adb_mailtext="${adb_mailtext}\n\n<strong>++\n++ Logfile Information ++\n++</strong>\n${log_info}"
  63. adb_mailtext="${adb_mailtext}</pre></body></html>"
  64. # send mail
  65. #
  66. if [ -x "${adb_mail}" ]
  67. then
  68. printf "%b" "${adb_mailhead}${adb_mailtext}" 2>/dev/null | "${adb_mail}" ${debug} -a "${adb_mailprofile}" "${adb_mailreceiver}" >/dev/null 2>&1
  69. adb_rc=${?}
  70. f_log "info" "mail sent to '${adb_mailreceiver}' with rc '${adb_rc}'"
  71. else
  72. f_log "err" "msmtp mail daemon not found"
  73. fi
  74. exit ${adb_rc}