diff --git a/NotiMail.py b/NotiMail.py index 062397e..dc015dd 100644 --- a/NotiMail.py +++ b/NotiMail.py @@ -1,6 +1,6 @@ """ NotiMail -Version: 0.9 - Alpha +Version: 0.10 - Alpha Author: Stefano Marinelli License: BSD 3-Clause License @@ -151,8 +151,9 @@ class NotificationProvider: raise NotImplementedError("Subclasses must implement this method") class NTFYNotificationProvider(NotificationProvider): - def __init__(self, ntfy_urls): - self.ntfy_urls = ntfy_urls # Expecting a list of URLs + def __init__(self, ntfy_data): + #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): 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_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: response = requests.post( ntfy_url, data=encoded_from, - headers={"Title": encoded_subject} + headers=headers ) if response.status_code == 200: print(f"Notification sent successfully to {ntfy_url}!") @@ -182,6 +187,7 @@ class NTFYNotificationProvider(NotificationProvider): time.sleep(5) # Ensure a delay between notifications + class PushoverNotificationProvider(NotificationProvider): def __init__(self, api_token, user_key): self.api_token = api_token @@ -382,8 +388,14 @@ def multi_account_main(): providers = [] if 'NTFY' in config: - ntfy_urls = [config['NTFY'][url_key] for url_key in config['NTFY']] - providers.append(NTFYNotificationProvider(ntfy_urls)) + ntfy_data = [] + 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: api_token = config['PUSHOVER']['ApiToken'] diff --git a/README.md b/README.md index 9849e2a..c06bc21 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,9 @@ With that, you should have NotiMail up and running on your system! Enjoy a more ### 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:** - 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. diff --git a/config.ini b/config.ini index 201ad4e..54a51f8 100644 --- a/config.ini +++ b/config.ini @@ -11,7 +11,9 @@ Host = mail.server.com #If your provider is NTFY, uncomment the following lines and configure #[NTFY] #Url1 = https://ntfy.sh/TOPIC1 +#Token1 = Optional token to send notifications to protected topics #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 #[PUSHOVER]