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.

71 lines
2.6 KiB

  1. #!/bin/sh
  2. # send mail script for travelmate notifications
  3. # Copyright (c) 2020 Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # set (s)hellcheck exceptions
  6. # shellcheck disable=1091,2016,2039,2059,2086,2143,2181,2188
  7. # Please note: you have to setup the package 'msmtp' before using this script
  8. export LC_ALL=C
  9. export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  10. set -o pipefail
  11. if [ "$(uci_get 2>/dev/null; printf "%u" "${?}")" = "127" ]
  12. then
  13. . "/lib/functions.sh"
  14. fi
  15. trm_debug="$(uci_get travelmate global trm_debug "0")"
  16. trm_mailreceiver="$(uci_get travelmate global trm_mailreceiver)"
  17. trm_mailprofile="$(uci_get travelmate global trm_mailprofile "trm_notify")"
  18. trm_mailsender="$(uci_get travelmate global trm_mailsender "no-reply@travelmate")"
  19. trm_rtfile="$(uci_get travelmate global trm_rtfile "/tmp/trm_runtime.json")"
  20. trm_mailpgm="$(command -v msmtp)"
  21. trm_logger="$(command -v logger)"
  22. f_log()
  23. {
  24. local class="${1}" log_msg="${2}"
  25. if [ -x "${trm_logger}" ]
  26. then
  27. "${trm_logger}" -p "${class}" -t "trm-mail [${$}]" "${log_msg}"
  28. else
  29. printf "%s %s %s\\n" "${class}" "trm-mail [${$}]" "${log_msg}"
  30. fi
  31. }
  32. if [ -z "${trm_mailreceiver}" ]
  33. then
  34. f_log "err" "please set the mail receiver with the 'trm_mailreceiver' option"
  35. exit 1
  36. fi
  37. if [ "${trm_debug}" = "1" ]
  38. then
  39. debug="--debug"
  40. fi
  41. # info preparation
  42. #
  43. 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)"
  44. trm_info="$(/etc/init.d/travelmate status 2>/dev/null)"
  45. sta_info="$(jsonfilter -i "${trm_rtfile}" -l1 -e '@.data.station_id')"
  46. trm_mailtopic="$(uci_get travelmate global trm_mailtopic "travelmate connection to '${sta_info}'")"
  47. trm_mailhead="From: ${trm_mailsender}\\nTo: ${trm_mailreceiver}\\nSubject: ${trm_mailtopic}\\nReply-to: ${trm_mailsender}\\nMime-Version: 1.0\\nContent-Type: text/html; charset=UTF-8\\nContent-Disposition: inline\\n\\n"
  48. # mail body
  49. #
  50. trm_mailtext="<html><body><pre style='display:block;font-family:monospace;font-size:1rem;padding:20;background-color:#f3eee5;white-space:pre'>"
  51. trm_mailtext="${trm_mailtext}\\n<strong>++\\n++ System Information ++\\n++</strong>\\n${sys_info}"
  52. trm_mailtext="${trm_mailtext}\\n\\n<strong>++\\n++ Travelmate Information ++\\n++</strong>\\n${trm_info}"
  53. trm_mailtext="${trm_mailtext}</pre></body></html>"
  54. # send mail
  55. #
  56. printf "%b" "${trm_mailhead}${trm_mailtext}" 2>/dev/null | "${trm_mailpgm}" ${debug} -a "${trm_mailprofile}" "${trm_mailreceiver}" >/dev/null 2>&1
  57. mail_rc="${?}"
  58. f_log "info" "mail sent to '${trm_mailreceiver}' with rc '${mail_rc}'"
  59. exit ${mail_rc}