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.

81 lines
3.1 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. LC_ALL=C
  7. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  8. if [ -r "/lib/functions.sh" ]
  9. then
  10. . "/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. fi
  17. adb_ver="${1}"
  18. adb_mail="$(command -v msmtp)"
  19. adb_logger="$(command -v logger)"
  20. adb_logread="$(command -v logread)"
  21. adb_rc=1
  22. f_log()
  23. {
  24. local class="${1}" log_msg="${2}"
  25. if [ -x "${adb_logger}" ]
  26. then
  27. "${adb_logger}" -p "${class}" -t "adblock-${adb_ver}[${$}]" "${log_msg}"
  28. else
  29. printf "%s %s %s\\n" "${class}" "adblock-${adb_ver}[${$}]" "${log_msg}"
  30. fi
  31. }
  32. if [ -z "${adb_mailreceiver}" ]
  33. then
  34. f_log "err" "please set the mail receiver with the 'adb_mailreceiver' option"
  35. exit ${adb_rc}
  36. fi
  37. if [ "${adb_debug}" -eq 1 ]
  38. then
  39. debug="--debug"
  40. fi
  41. 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"
  42. # info preparation
  43. #
  44. 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)"
  45. adb_info="$(/etc/init.d/adblock status 2>/dev/null)"
  46. if [ -f "/var/log/messages" ]
  47. then
  48. 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)"
  49. elif [ -x "${adb_logread}" ]
  50. then
  51. 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}')"
  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. adb_mailtext="${adb_mailtext}\\n\\n<strong>++\\n++ Logfile Information ++\\n++</strong>\\n${log_info}"
  59. adb_mailtext="${adb_mailtext}</pre></body></html>"
  60. # send mail
  61. #
  62. if [ -x "${adb_mail}" ]
  63. then
  64. printf "%b" "${adb_mailhead}${adb_mailtext}" 2>/dev/null | "${adb_mail}" ${debug} -a "${adb_mailprofile}" "${adb_mailreceiver}" >/dev/null 2>&1
  65. adb_rc=${?}
  66. f_log "info" "mail sent to '${adb_mailreceiver}' with rc '${adb_rc}'"
  67. else
  68. f_log "err" "msmtp mail daemon not found"
  69. fi
  70. exit ${adb_rc}