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.

70 lines
2.9 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_rc=1
  22. if [ "${adb_debug}" -eq 1 ]
  23. then
  24. debug="--debug"
  25. fi
  26. # mail header & receiver check
  27. #
  28. if [ -z "${adb_mreceiver}" ]
  29. then
  30. logger -p "err" -t "adblock-${adb_ver}[${$}]" "please set the mail receiver with the 'adb_mreceiver' option"
  31. exit ${adb_rc}
  32. fi
  33. 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"
  34. # info preparation
  35. #
  36. 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)"
  37. adb_info="$(/etc/init.d/adblock status 2>/dev/null)"
  38. if [ -f "/var/log/messages" ]
  39. then
  40. 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)"
  41. else
  42. log_info="$(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}')"
  43. fi
  44. # mail body
  45. #
  46. adb_mtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  47. adb_mtext="${adb_mtext}\\n<strong>++\\n++ System Information ++\\n++</strong>\\n${sys_info}"
  48. adb_mtext="${adb_mtext}\\n\\n<strong>++\\n++ Adblock Information ++\\n++</strong>\\n${adb_info}"
  49. adb_mtext="${adb_mtext}\\n\\n<strong>++\\n++ Logfile Information ++\\n++</strong>\\n${log_info}"
  50. adb_mtext="${adb_mtext}</pre></body></html>"
  51. # send mail
  52. #
  53. if [ -x "${adb_mail}" ]
  54. then
  55. printf "%b" "${adb_mhead}${adb_mtext}" 2>/dev/null | "${adb_mail}" ${debug} -a "${adb_mprofile}" "${adb_mreceiver}" >/dev/null 2>&1
  56. adb_rc=${?}
  57. logger -p "info" -t "adblock-${adb_ver}[${$}]" "mail sent to '${adb_mreceiver}' with rc '${adb_rc}'"
  58. else
  59. logger -p "err" -t "adblock-${adb_ver}[${$}]" "msmtp mail daemon not found"
  60. fi
  61. exit ${adb_rc}