Added NTFY token support for protected topics

This commit is contained in:
Stefano Marinelli 2023-10-22 09:04:01 +02:00
parent 295d244517
commit 45aad6775d
3 changed files with 24 additions and 7 deletions

View file

@ -1,6 +1,6 @@
""" """
NotiMail NotiMail
Version: 0.9 - Alpha Version: 0.10 - Alpha
Author: Stefano Marinelli <stefano@dragas.it> Author: Stefano Marinelli <stefano@dragas.it>
License: BSD 3-Clause License License: BSD 3-Clause License
@ -151,8 +151,9 @@ class NotificationProvider:
raise NotImplementedError("Subclasses must implement this method") raise NotImplementedError("Subclasses must implement this method")
class NTFYNotificationProvider(NotificationProvider): class NTFYNotificationProvider(NotificationProvider):
def __init__(self, ntfy_urls): def __init__(self, ntfy_data):
self.ntfy_urls = ntfy_urls # Expecting a list of URLs #self.ntfy_urls = ntfy_urls # Expecting a list of URLs
self.ntfy_data = ntfy_data # Expecting a list of (URL, Token) tuples
def send_notification(self, mail_from, mail_subject): def send_notification(self, mail_from, mail_subject):
mail_subject = mail_subject if mail_subject is not None else "No Subject" mail_subject = mail_subject if mail_subject is not None else "No Subject"
@ -162,12 +163,16 @@ class NTFYNotificationProvider(NotificationProvider):
encoded_from = mail_from.encode('utf-8') encoded_from = mail_from.encode('utf-8')
encoded_subject = mail_subject.encode('utf-8') encoded_subject = mail_subject.encode('utf-8')
for ntfy_url in self.ntfy_urls: for ntfy_url, token in self.ntfy_data:
headers = {"Title": encoded_subject}
if token:
headers["Authorization"] = f"Bearer {token}"
try: try:
response = requests.post( response = requests.post(
ntfy_url, ntfy_url,
data=encoded_from, data=encoded_from,
headers={"Title": encoded_subject} headers=headers
) )
if response.status_code == 200: if response.status_code == 200:
print(f"Notification sent successfully to {ntfy_url}!") print(f"Notification sent successfully to {ntfy_url}!")
@ -182,6 +187,7 @@ class NTFYNotificationProvider(NotificationProvider):
time.sleep(5) # Ensure a delay between notifications time.sleep(5) # Ensure a delay between notifications
class PushoverNotificationProvider(NotificationProvider): class PushoverNotificationProvider(NotificationProvider):
def __init__(self, api_token, user_key): def __init__(self, api_token, user_key):
self.api_token = api_token self.api_token = api_token
@ -382,8 +388,14 @@ def multi_account_main():
providers = [] providers = []
if 'NTFY' in config: if 'NTFY' in config:
ntfy_urls = [config['NTFY'][url_key] for url_key in config['NTFY']] ntfy_data = []
providers.append(NTFYNotificationProvider(ntfy_urls)) for key in config['NTFY']:
if key.startswith("url"):
url = config['NTFY'][key]
token_key = "token" + key[3:]
token = config['NTFY'].get(token_key, None) # Retrieve the token if it exists, else default to None
ntfy_data.append((url, token))
providers.append(NTFYNotificationProvider(ntfy_data))
if 'PUSHOVER' in config: if 'PUSHOVER' in config:
api_token = config['PUSHOVER']['ApiToken'] api_token = config['PUSHOVER']['ApiToken']

View file

@ -136,6 +136,9 @@ With that, you should have NotiMail up and running on your system! Enjoy a more
### Changelog: ### Changelog:
- **Version 0.10.0 - Alpha, not yet released:**
- Authentication Tokens for NTFY Notifications: Enhanced the NTFYNotificationProvider to support optional authentication tokens. If a token is provided in the config.ini file for a specific NTFY URL, the notification request will include an "Authorization" header for authentication.
- **Version 0.9:** - **Version 0.9:**
- Introduced support for monitoring multiple email accounts. Configure multiple accounts in the `config.ini` using the format `[EMAIL:account1]`, `[EMAIL:account2]`, and so on. - Introduced support for monitoring multiple email accounts. Configure multiple accounts in the `config.ini` using the format `[EMAIL:account1]`, `[EMAIL:account2]`, and so on.
- Maintained compatibility with the old single `[EMAIL]` configuration for a smooth upgrade path. - Maintained compatibility with the old single `[EMAIL]` configuration for a smooth upgrade path.

View file

@ -11,7 +11,9 @@ Host = mail.server.com
#If your provider is NTFY, uncomment the following lines and configure #If your provider is NTFY, uncomment the following lines and configure
#[NTFY] #[NTFY]
#Url1 = https://ntfy.sh/TOPIC1 #Url1 = https://ntfy.sh/TOPIC1
#Token1 = Optional token to send notifications to protected topics
#Url2 = https://ntfy.sh/TOPIC2 #Url2 = https://ntfy.sh/TOPIC2
#Token2 = Optional token to send notifications to protected topics
#If your provider is PUSHOVER, uncomment the following lines and configure #If your provider is PUSHOVER, uncomment the following lines and configure
#[PUSHOVER] #[PUSHOVER]