Skip to content

Commit

Permalink
PLUGINS, NTFY, handleEmpty work⤵
Browse files Browse the repository at this point in the history
  • Loading branch information
jokob-sk committed Oct 18, 2023
1 parent 1a3cf49 commit 5d64433
Show file tree
Hide file tree
Showing 18 changed files with 594 additions and 128 deletions.
5 changes: 0 additions & 5 deletions dockerfiles/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ echo "[INSTALL] Run setup scripts"
"$INSTALL_DIR/pialert/install/install_dependencies.sh"
"$INSTALL_DIR/pialert/install/install_python.sh"

# # executes a new shell session with the user specified in the USER variable.
# if [ -n "$USER" ]; then
# exec su - "${USER}"
# fi

# Change port number if set
if [ -n "${PORT}" ]; then
sed -ie 's/listen 20211/listen '${PORT}'/g' /etc/nginx/sites-available/default
Expand Down
13 changes: 1 addition & 12 deletions front/php/templates/language/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -533,18 +533,7 @@
"WEBHOOK_SIZE_description" : "The maximum size of the webhook payload as number of characters in the passed string. If above limit, it will be truncated and a <code>(text was truncated)</code> message is appended.",
"WEBHOOK_SECRET_name": "HMAC Secret",
"WEBHOOK_SECRET_description": "When set, use this secret to generate the SHA256-HMAC hex digest value of the request body, which will be passed as the <code>X-Webhook-Signature</code> header to the request. You can find more informations <a target=\"_blank\" href=\"https://github.com/jokob-sk/Pi.Alert/blob/main/docs/WEBHOOK_SECRET.md\">here</a>.",
"NTFY_display_name" : "NTFY",
"NTFY_icon" : "<i class=\"fa fa-terminal\"></i>",
"REPORT_NTFY_name" : "Enable NTFY",
"REPORT_NTFY_description" : "Enable sending notifications via <a target=\"_blank\" href=\"https://ntfy.sh/\">NTFY</a>.",
"NTFY_HOST_name" : "NTFY host URL",
"NTFY_HOST_description" : "NTFY host URL starting with <code>http://</code> or <code>https://</code>. You can use the hosted instance on <a target=\"_blank\" href=\"https://ntfy.sh/\">https://ntfy.sh</a> by simply entering <code>https://ntfy.sh</code>.",
"NTFY_TOPIC_name" : "NTFY topic",
"NTFY_TOPIC_description" : "Your secret topic.",
"NTFY_USER_name" : "NTFY user",
"NTFY_USER_description" : "Enter user if you need (host) an instance with enabled authetication.",
"NTFY_PASSWORD_name" : "NTFY password",
"NTFY_PASSWORD_description" : "Enter password if you need (host) an instance with enabled authetication.",

"PUSHSAFER_display_name" : "Pushsafer",
"PUSHSAFER_icon" : "<i class=\"fa fa-bell\"></i>",
"REPORT_PUSHSAFER_name" : "Enable Pushsafer",
Expand Down
2 changes: 1 addition & 1 deletion front/plugins/_publisher_apprise/apprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def main():
watched3 = 'null',
watched4 = 'null',
extra = 'null',
foreignKey = 'null'
foreignKey = notification["GUID"]
)

plugin_objects.write_result_file()
Expand Down
42 changes: 35 additions & 7 deletions front/plugins/_publisher_email/email_smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import os
import pathlib
import sys
import re
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email.utils import parseaddr
import smtplib
import socket
import ssl
Expand Down Expand Up @@ -66,7 +69,7 @@ def main():
watched3 = 'null',
watched4 = 'null',
extra = 'null',
foreignKey = 'null'
foreignKey = notification["GUID"]
)

plugin_objects.write_result_file()
Expand All @@ -89,13 +92,16 @@ def send(pHTML, pText):

mylog('debug', [f'[{pluginName}] SMTP_REPORT_TO: {hide_email(str(get_setting_value("SMTP_REPORT_TO")))} SMTP_USER: {hide_email(str(get_setting_value("SMTP_USER")))}'])


subject, from_email, to_email, message_html, message_text = sanitize_email_content('Pi.Alert Report', get_setting_value("SMTP_REPORT_FROM"), get_setting_value("SMTP_REPORT_TO"), pHTML, pText)

# Compose email
msg = MIMEMultipart('alternative')
msg['Subject'] = 'Pi.Alert Report'
msg['From'] = get_setting_value("SMTP_REPORT_FROM")
msg['To'] = get_setting_value("SMTP_REPORT_TO")
msg.attach (MIMEText (pText, 'plain'))
msg.attach (MIMEText (pHTML, 'html'))
msg['Subject'] = subject
msg['From'] = from_email
msg['To'] = to_email
msg.attach (MIMEText (message_text, 'plain'))
msg.attach (MIMEText (message_html, 'html'))

# Set a timeout for the SMTP connection (in seconds)
smtp_timeout = 30
Expand Down Expand Up @@ -124,8 +130,9 @@ def send(pHTML, pText):
except ssl.SSLError as e:
mylog('none', [' ERROR: Could not establish SSL connection (ssl.SSLError)'])
mylog('none', [' ERROR: Are you sure you need SMTP_FORCE_SSL enabled? Check your SMTP provider docs.'])
mylog('none', [' ERROR: ', str(e)])
mylog('none', [' ERROR: ', str(e)])

# ----------------------------------------------------------------------------------
def send_email(msg):
# Send mail
if get_setting_value('SMTP_FORCE_SSL'):
Expand Down Expand Up @@ -168,5 +175,26 @@ def send_email(msg):
smtp_connection.sendmail (get_setting_value("SMTP_REPORT_FROM"), get_setting_value("SMTP_REPORT_TO"), msg.as_string())
smtp_connection.quit()

# ----------------------------------------------------------------------------------
def sanitize_email_content(subject, from_email, to_email, message_html, message_text):
# Validate and sanitize subject
subject = Header(subject, 'utf-8').encode()

# Validate and sanitize sender's email address
from_name, from_address = parseaddr(from_email)
from_email = Header(from_name, 'utf-8').encode() + ' <' + from_address + '>'

# Validate and sanitize recipient's email address
to_name, to_address = parseaddr(to_email)
to_email = Header(to_name, 'utf-8').encode() + ' <' + to_address + '>'

# Validate and sanitize message content
# Remove potentially problematic characters
message_html = re.sub(r'[^\x00-\x7F]+', ' ', message_html)
message_text = re.sub(r'[^\x00-\x7F]+', ' ', message_text)

return subject, from_email, to_email, message_html, message_text

# ----------------------------------------------------------------------------------
if __name__ == '__main__':
sys.exit(main())
2 changes: 1 addition & 1 deletion front/plugins/_publisher_mqtt/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__(self, deviceId, deviceName, sensorType, sensorName, icon, mac):
watched3 = hash_value,
watched4 = mac,
extra = input_string,
foreignKey = deviceId
foreignKey = notification["GUID"]
)

#-------------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions front/plugins/_publisher_ntfy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Overview

- TBC

### Usage

- Go to settings and fill in relevant details.

Loading

0 comments on commit 5d64433

Please sign in to comment.