Introduced a new column email_account to the processed_emails table to differentiate emails based on the account they belong to.

Added logic in the DatabaseHandler class to automatically check the existing schema and update it to include the email_account column if it doesn't exist.
Changelog moved to a new file named CHANGELOG.md
This commit is contained in:
Stefano Marinelli 2023-10-24 11:48:13 +02:00
parent a5884402ff
commit eef362f907
3 changed files with 86 additions and 59 deletions

53
CHANGELOG.md Normal file
View file

@ -0,0 +1,53 @@
### Version 0.12.1
#### Changes:
- **Moved the Changelog** to its own file
- **Introduced a new column**, email_account, to the processed_emails table. This differentiates emails based on the account they belong to, avoiding potential UID conflicts between different mailboxes.
- **Added logic** in the DatabaseHandler class to automatically check the existing schema and update it to include the email_account column if it doesn't exist.
### Version 0.12
#### New Features:
- **Multiple Folder Monitoring**: From this version onwards, NotiMail supports monitoring multiple folders for each configured email account. This allows users to, for example, monitor both the 'inbox' and 'junk' folders simultaneously. Each folder is treated as a separate connection, ensuring robust and comprehensive monitoring.
- **Configuration Validation**: Introduced command-line functions to validate the config.ini file. This ensures that your settings are correctly configured before you start the NotiMail service.
- **Notification Provider Testing**: You can now test the notification providers by sending a test notification to verify their correct setup and functioning.
- **IMAP Folder Listing**: Added a feature to list all IMAP folders of the configured mailboxes, providing better insights and facilitating precise folder monitoring.
#### Changes:
- **Configuration (`config.ini`)**: Introduced the ability to specify multiple folders for each email account using the `Folders` configuration key. If not specified, it defaults to the 'inbox' folder.
- **Logging Enhancements**: All logging and print statements now include the folder name, providing a clearer understanding of ongoing operations per folder.
- **Codebase**: Enhanced error handling, and improved logging to provide clearer insights into the application's operations.
### Version 0.11
#### New Features:
- Command-Line Flexibility: Now you can customize your config file path using -c or --config flags. The default remains 'config.ini'.
- Man Page: Introduced a man page for NotiMail, making it easier for users to understand usage and options.
- Dynamic Database Location: Configure your database path in the settings, allowing for more flexible deployments.
#### Changes:
- **Codebase:**
- Introduced `argparse` to enable command-line arguments. You can now specify the path to a configuration file using the `-c` or `--config` option.
- Moved the configuration reading to the top of the script to make it globally accessible.
- Updated the logging setup to use the log file location from the config file (`LogFileLocation`). If not set, it defaults to `notimail.log`.
- Modified the `DatabaseHandler` class to use the database location from the config file (`DataBaseLocation`). If not set, it defaults to `processed_emails.db`.
- Improved Logging: Logs now include thread names, providing clearer insights into operations.
- Stability Upgrades: Enhanced handling of 'BYE' responses from servers. Plus, in case of connection loss, the tool will now auto-retry to maintain smooth operations.
- Socket Timeout: Adjusted the default socket timeout setting to further optimize responsiveness.
- **Configuration (`config.ini`):**
- Introduced a new `[GENERAL]` section. You can specify `LogFileLocation` and `DataBaseLocation` within this section to set the desired paths for the log file and database, respectively.
### Version 0.10
- 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.

View file

@ -1,7 +1,7 @@
#!/usr/bin/env python3
"""
NotiMail
Version: 0.12
Version: 0.12.1
Author: Stefano Marinelli <stefano@dragas.it>
License: BSD 3-Clause License
@ -102,24 +102,40 @@ class DatabaseHandler:
self.connection = sqlite3.connect(db_name)
self.cursor = self.connection.cursor()
self.create_table()
self.update_schema_if_needed()
def create_table(self):
self.cursor.execute('''
CREATE TABLE IF NOT EXISTS processed_emails (
uid TEXT PRIMARY KEY,
email_account TEXT,
uid TEXT,
notified INTEGER,
processed_date TEXT
processed_date TEXT,
PRIMARY KEY(email_account, uid)
)''')
self.connection.commit()
def add_email(self, uid, notified):
date_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.cursor.execute("INSERT INTO processed_emails (uid, notified, processed_date) VALUES (?, ?, ?)",
(uid, notified, date_str))
def update_schema_if_needed(self):
# Check if the email_account column exists
self.cursor.execute("PRAGMA table_info(processed_emails)")
columns = [column[1] for column in self.cursor.fetchall()]
if 'email_account' not in columns:
# Add the email_account column and set its default value to 'unknown'
self.cursor.execute("ALTER TABLE processed_emails ADD COLUMN email_account TEXT DEFAULT 'unknown'")
# Update the primary key to be a composite key of email_account and uid
self.cursor.execute("CREATE UNIQUE INDEX idx_email_account_uid ON processed_emails(email_account, uid)")
self.connection.commit()
def is_email_notified(self, uid):
self.cursor.execute("SELECT * FROM processed_emails WHERE uid = ? AND notified = 1", (uid,))
def add_email(self, email_account, uid, notified):
date_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.cursor.execute("INSERT OR REPLACE INTO processed_emails (email_account, uid, notified, processed_date) VALUES (?, ?, ?, ?)",
(email_account, uid, notified, date_str))
self.connection.commit()
def is_email_notified(self, email_account, uid):
self.cursor.execute("SELECT * FROM processed_emails WHERE email_account = ? AND uid = ? AND notified = 1", (email_account, uid))
return bool(self.cursor.fetchone())
def delete_old_emails(self, days=7):
@ -131,8 +147,9 @@ class DatabaseHandler:
self.connection.close()
class EmailProcessor:
def __init__(self, mail):
def __init__(self, mail, email_account):
self.mail = mail
self.email_account = email_account
self.db_handler = DatabaseHandler() # Create a new db_handler for each instance
def fetch_unseen_emails(self):
@ -146,7 +163,7 @@ class EmailProcessor:
logging.info("Fetching the latest email...")
for message in self.fetch_unseen_emails():
uid = message.decode('utf-8')
if self.db_handler.is_email_notified(uid):
if self.db_handler.is_email_notified(self.email_account, uid): # Added email_account here
logging.info(f"Email UID {uid} already processed and notified, skipping...")
continue
@ -158,7 +175,7 @@ class EmailProcessor:
subject = email_message.get('Subject')
logging.info(f"Processing Email - UID: {uid}, Sender: {sender}, Subject: {subject}")
notifier.send_notification(email_message.get('From'), email_message.get('Subject'))
self.db_handler.add_email(uid, 1)
self.db_handler.add_email(self.email_account, uid, 1)
# Delete entries older than 7 days
self.db_handler.delete_old_emails()
@ -201,7 +218,7 @@ class NTFYNotificationProvider(NotificationProvider):
print(f"An error occurred while sending notification to {ntfy_url}: {str(e)}")
logging.error(f"An error occurred while sending notification to {ntfy_url} via NTFY: {str(e)}")
finally:
time.sleep(5) # Ensure a delay between notifications
time.sleep(2) # Ensure a delay between notifications
@ -331,9 +348,10 @@ class IMAPHandler:
logging.info(f"[{self.email_user}] IDLE mode stopped.")
def process_emails(self):
processor = EmailProcessor(self.mail)
processor = EmailProcessor(self.mail, self.email_user) # Pass the email_user (account) to the processor
processor.process()
class MultiIMAPHandler:
def __init__(self, accounts):
self.accounts = accounts

View file

@ -1,6 +1,6 @@
# NotiMail 📧
**Version 0.12 is here, read the changelog for more information!**
**Version 0.12.1 is here, read the changelog for more information!**
**Development is ongoing, and the project is in the early alpha stage - things may break!**
@ -137,48 +137,4 @@ With that, you should have NotiMail up and running on your system! Enjoy a more
## Changelog
### Version 0.12
#### New Features:
- **Multiple Folder Monitoring**: From this version onwards, NotiMail supports monitoring multiple folders for each configured email account. This allows users to, for example, monitor both the 'inbox' and 'junk' folders simultaneously. Each folder is treated as a separate connection, ensuring robust and comprehensive monitoring.
- **Configuration Validation**: Introduced command-line functions to validate the config.ini file. This ensures that your settings are correctly configured before you start the NotiMail service.
- **Notification Provider Testing**: You can now test the notification providers by sending a test notification to verify their correct setup and functioning.
- **IMAP Folder Listing**: Added a feature to list all IMAP folders of the configured mailboxes, providing better insights and facilitating precise folder monitoring.
#### Changes:
- **Configuration (`config.ini`)**: Introduced the ability to specify multiple folders for each email account using the `Folders` configuration key. If not specified, it defaults to the 'inbox' folder.
- **Logging Enhancements**: All logging and print statements now include the folder name, providing a clearer understanding of ongoing operations per folder.
- **Codebase**: Enhanced error handling, and improved logging to provide clearer insights into the application's operations.
### Version 0.11
#### New Features:
- Command-Line Flexibility: Now you can customize your config file path using -c or --config flags. The default remains 'config.ini'.
- Man Page: Introduced a man page for NotiMail, making it easier for users to understand usage and options.
- Dynamic Database Location: Configure your database path in the settings, allowing for more flexible deployments.
#### Changes:
- **Codebase:**
- Introduced `argparse` to enable command-line arguments. You can now specify the path to a configuration file using the `-c` or `--config` option.
- Moved the configuration reading to the top of the script to make it globally accessible.
- Updated the logging setup to use the log file location from the config file (`LogFileLocation`). If not set, it defaults to `notimail.log`.
- Modified the `DatabaseHandler` class to use the database location from the config file (`DataBaseLocation`). If not set, it defaults to `processed_emails.db`.
- Improved Logging: Logs now include thread names, providing clearer insights into operations.
- Stability Upgrades: Enhanced handling of 'BYE' responses from servers. Plus, in case of connection loss, the tool will now auto-retry to maintain smooth operations.
- Socket Timeout: Adjusted the default socket timeout setting to further optimize responsiveness.
- **Configuration (`config.ini`):**
- Introduced a new `[GENERAL]` section. You can specify `LogFileLocation` and `DataBaseLocation` within this section to set the desired paths for the log file and database, respectively.
### Version 0.10
- 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.
Moved to CHANGELOG.md