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.

83 lines
3.1 KiB

  1. #!/bin/sh
  2. # send mail script for adblock notifications
  3. # written by Dirk Brenken (dev@brenken.org)
  4. # Please note: you have to manually install and configure the package 'msmtp' before using this script
  5. # This is free software, licensed under the GNU General Public License v3.
  6. # You should have received a copy of the GNU General Public License
  7. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  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 extra adb_debug "0")"
  14. adb_msender="$(uci_get adblock extra adb_msender "no-reply@adblock")"
  15. adb_mreceiver="$(uci_get adblock extra adb_mreceiver)"
  16. adb_mtopic="$(uci_get adblock extra adb_mtopic "adblock notification")"
  17. adb_mprofile="$(uci_get adblock extra adb_mprofile "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_mreceiver}" ]
  35. then
  36. f_log "err" "please set the mail receiver with the 'adb_mreceiver' option"
  37. exit ${adb_rc}
  38. fi
  39. if [ "${adb_debug}" -eq 1 ]
  40. then
  41. debug="--debug"
  42. fi
  43. adb_mhead="From: ${adb_msender}\\nTo: ${adb_mreceiver}\\nSubject: ${adb_mtopic}\\nReply-to: ${adb_msender}\\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),"&#8629;"} 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),"&#8629;"} else {print " ",substr($0,1,max)}{$0=substr($0,max+1);NR=NR+1}}else print}')"
  54. fi
  55. # mail body
  56. #
  57. adb_mtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  58. adb_mtext="${adb_mtext}\\n<strong>++\\n++ System Information ++\\n++</strong>\\n${sys_info}"
  59. adb_mtext="${adb_mtext}\\n\\n<strong>++\\n++ Adblock Information ++\\n++</strong>\\n${adb_info}"
  60. adb_mtext="${adb_mtext}\\n\\n<strong>++\\n++ Logfile Information ++\\n++</strong>\\n${log_info}"
  61. adb_mtext="${adb_mtext}</pre></body></html>"
  62. # send mail
  63. #
  64. if [ -x "${adb_mail}" ] && [ -n "${adb_mreceiver}" ]
  65. then
  66. printf "%b" "${adb_mhead}${adb_mtext}" 2>/dev/null | "${adb_mail}" ${debug} -a "${adb_mprofile}" "${adb_mreceiver}" >/dev/null 2>&1
  67. adb_rc=${?}
  68. f_log "info" "mail sent to '${adb_mreceiver}' with rc '${adb_rc}'"
  69. else
  70. f_log "err" "msmtp mail daemon not found"
  71. fi
  72. exit ${adb_rc}