Skip to content

Commit

Permalink
Email notifications added
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbolvansky committed Oct 8, 2023
1 parent cead51f commit 2a3c993
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
[submodule "backend/file2hashcat"]
path = backend/file2hashcat
url = https://github.com/hashstation/file2hashcat
ignore = dirty
[submodule "backend/hashcat-utils"]
path = backend/hashcat-utils
url = https://github.com/hashcat/hashcat-utils
ignore = dirty
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ New features:
* Added column 'Username' in 'Job Info' -> 'Hashes'.
* Added column 'Username' in 'Hashes'.
* Added dropdown menu to specify input format.
* Added support for Telegram/Discord notifications.
* Added support for E-mail/Telegram/Discord notifications.

General improvements:
* Improved/reimplemeted generator and scheduling logic. Better precision, more predictable workunit sizes. Default desired seconds per workunit changed to 600 seconds.
Expand Down
2 changes: 1 addition & 1 deletion backend/src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def main():
def notifier():
with app.app_context():
for user in FcUser.query.all():
notify(user.id)
notify(user)

scheduler = APScheduler()
scheduler.init_app(app)
Expand Down
24 changes: 22 additions & 2 deletions backend/src/src/api/fitcrack/endpoints/notifications/notifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ def __init__(self, title, body):
self.title = title
self.body = body

def notify(userId):
def notify(user):
userId = user.id
settings = FcSettings.query.filter_by(user_id=userId).one()
apobj = apprise.Apprise()

if settings.discord_notifications:
apobj.add(settings.discord_webhook_url)
apobj.add('discord://' + settings.discord_webhook_id + '/' + settings.discord_webhook_token)

new_discord_notifications = FcNotification.query.filter(FcNotification.user_id == userId).filter(FcNotification.discord_sent == False)
for notif in new_discord_notifications:
Expand All @@ -38,4 +39,23 @@ def notify(userId):
apobj.notify(body=body)
notif.telegram_sent = True

db.session.commit()

if settings.email_notifications:
email_parts = settings.email_address.split('@')
if len(email_parts) != 2:
return

username = email_parts[0]
domain = email_parts[1]
apobj.add('mailto://' + username + ':' + settings.email_password + '@' + domain +'?from=' + user.username)

new_email_notifications = FcNotification.query.filter(FcNotification.user_id == userId).filter(FcNotification.email_sent == False)
for notif in new_email_notifications:
title = notif.source.name if notif.source else '<Removed Job>'
body = job_status_text_info_to_code_dict[notif.new_value]

apobj.notify(body=body, title=title)
notif.email_sent = True

db.session.commit()
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
settings_arguments.add_argument('workunit_status_update', type=int, help='', required=False, location='json')

settings_arguments.add_argument('discord_notifications', type=bool, help='', required=False, location='json')
settings_arguments.add_argument('discord_webhook_url', type=str, help='', required=False, location='json')
settings_arguments.add_argument('discord_webhook_id', type=str, help='', required=False, location='json')
settings_arguments.add_argument('discord_webhook_token', type=str, help='', required=False, location='json')

settings_arguments.add_argument('telegram_notifications', type=bool, help='', required=False, location='json')
settings_arguments.add_argument('telegram_bot_token', type=str, help='', required=False, location='json')
settings_arguments.add_argument('telegram_chat_id', type=str, help='', required=False, location='json')
settings_arguments.add_argument('telegram_chat_id', type=str, help='', required=False, location='json')

settings_arguments.add_argument('email_notifications', type=bool, help='', required=False, location='json')
settings_arguments.add_argument('email_address', type=str, help='', required=False, location='json')
settings_arguments.add_argument('email_password', type=str, help='', required=False, location='json')
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
'bench_runtime_limit': fields.Integer(),
'workunit_status_update': fields.Integer(),
'discord_notifications': fields.Boolean(),
'discord_webhook_url': fields.String(),
'discord_webhook_id': fields.String(),
'discord_webhook_token': fields.String(),
'telegram_notifications': fields.Boolean(),
'telegram_bot_token': fields.String(),
'tolegram_chat_id': fields.String(),
'telegram_chat_id': fields.String(),
'email_notifications': fields.Boolean(),
'email_address': fields.String(),
'email_password': fields.String(),
})
39 changes: 32 additions & 7 deletions backend/src/src/api/fitcrack/endpoints/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ def post(self):
workunit_status_update = args['workunit_status_update']

discord_notifications = args['discord_notifications']
discord_webhook_url = args['discord_webhook_url']
discord_webhook_id = args['discord_webhook_id']
discord_webhook_token = args['discord_webhook_token']
telegram_notifications = args['telegram_notifications']
telegram_bot_token = args['telegram_bot_token']
telegram_chat_id = args['telegram_chat_id']
email_notifications = args['email_notifications']
email_address = args['email_address']
email_password = args['email_password']

settings = FcSettings.query.filter_by(user_id=current_user.id).one_or_none()
if not settings:
Expand All @@ -66,8 +70,10 @@ def post(self):
if (workunit_status_update is not None): settings.workunit_status_update = workunit_status_update

if discord_notifications:
if not discord_webhook_url:
abort(400, 'Discord webhook URL is required when enabling Discord notifications.')
if not discord_webhook_id:
abort(400, 'Discord webhook ID is required when enabling Discord notifications.')
if not discord_webhook_token:
abort(400, 'Discord webhook token is required when enabling Discord notifications.')

if not settings.discord_notifications: # If discord notifications were previously disabled, mark all notifications as sent
FcNotification.query.filter(FcNotification.user_id == current_user.id).update({FcNotification.discord_sent: True})
Expand All @@ -76,21 +82,40 @@ def post(self):

if telegram_notifications:
if not telegram_bot_token:
abort(400, 'Telegram bot token is required when enabling Telegram notifications.')
abort(400, 'Telegram bot token is required when enabling Telegram notifications.')
if not telegram_chat_id:
abort(400, 'Telegram chat ID is required when enabling Telegram notifications.')
abort(400, 'Telegram chat ID is required when enabling Telegram notifications.')

if not settings.telegram_notifications:
FcNotification.query.filter(FcNotification.user_id == current_user.id).update({FcNotification.telegram_sent: True})

settings.telegram_notifications = telegram_notifications

if discord_webhook_url:
settings.discord_webhook_url = discord_webhook_url
if email_notifications:
if not email_address:
abort(400, 'E-mail address is required when enabling E-mail notifications.')
if not email_password:
abort(400, 'E-mail password is required when enabling E-mail notifications.')
if not "@" in email_address:
abort(400, 'Invalid e-mail address.')

if not settings.email_notifications:
FcNotification.query.filter(FcNotification.user_id == current_user.id).update({FcNotification.email_sent: True})

settings.email_notifications = email_notifications

if discord_webhook_id:
settings.discord_webhook_id = discord_webhook_id
if discord_webhook_token:
settings.discord_webhook_token = discord_webhook_token
if telegram_bot_token:
settings.telegram_bot_token = telegram_bot_token
if telegram_chat_id:
settings.telegram_chat_id = telegram_chat_id
if email_address:
settings.email_address = email_address
if email_password:
settings.email_password = email_password

db.session.commit()

Expand Down
7 changes: 6 additions & 1 deletion backend/src/src/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,14 @@ class FcSettings(Base):
workunit_status_update = Column(Integer, nullable=False, server_default=text("'5'"))

discord_notifications = Column(Integer, nullable=False, server_default=text("'0'"))
discord_webhook_url = Column(String(200), nullable=True)
discord_webhook_id = Column(String(200), nullable=True)
discord_webhook_token = Column(String(200), nullable=True)
telegram_notifications = Column(Integer, nullable=False, server_default=text("'0'"))
telegram_bot_token = Column(String(200), nullable=True)
telegram_chat_id = Column(String(200), nullable=True)
email_notifications = Column(Integer, nullable=False, server_default=text("'0'"))
email_address = Column(String(200), nullable=True)
email_password = Column(String(200), nullable=True)

user = relationship('FcUser')

Expand Down Expand Up @@ -771,6 +775,7 @@ class FcNotification(Base):
seen = Column(Integer, server_default=text("'0'"))
discord_sent = Column(Integer, server_default=text("'0'"))
telegram_sent = Column(Integer, server_default=text("'0'"))
email_sent = Column(Integer, server_default=text("'0'"))
time = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP"))

source = relationship('FcJob')
Expand Down
39 changes: 36 additions & 3 deletions frontend/src/components/settings/settingsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,17 @@
class="mb-4 mt-0"
/>
<v-text-field
v-model="settings.discord_webhook_url"
v-model="settings.discord_webhook_id"
outlined
label="Discord webhook URL"
label="Discord webhook ID"
persistent-hint
class="mb-4 mt-0"
/>
<v-text-field
v-model="settings.discord_webhook_token"
outlined
label="Discord webhook token"
type="password"
persistent-hint
class="mb-4 mt-0"
/>
Expand All @@ -182,6 +190,7 @@
v-model="settings.telegram_bot_token"
outlined
label="Telegram bot token"
type="password"
persistent-hint
class="mb-4 mt-0"
/>
Expand All @@ -192,6 +201,30 @@
persistent-hint
class="mb-4 mt-0"
/>
<v-divider />
<v-switch
v-model="settings.email_notifications"
:loading="loading"
outlined
label="E-mail notifications"
persistent-hint
class="mb-4 mt-0"
/>
<v-text-field
v-model="settings.email_address"
outlined
label="E-mail address"
persistent-hint
class="mb-4 mt-0"
/>
<v-text-field
v-model="settings.email_password"
outlined
label="E-mail (app) password"
type="password"
persistent-hint
class="mb-4 mt-0"
/>
</v-card-text>
</v-card>
<v-expansion-panels flat class="mt-6">
Expand All @@ -202,7 +235,7 @@
<v-icon left>
mdi-puzzle
</v-icon>
<span class="text-h6">{{ open ? '' : 'Show '}}Advanced Settings</span>
<span class="text-h6">Advanced Settings</span>
</span>
</template>
</v-expansion-panel-header>
Expand Down
7 changes: 6 additions & 1 deletion server/sql/10_create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ CREATE TABLE IF NOT EXISTS `fc_notification` (
`seen` tinyint(1) DEFAULT '0',
`discord_sent` tinyint(1) DEFAULT '0',
`telegram_sent` tinyint(1) DEFAULT '0',
`email_sent` tinyint(1) DEFAULT '0',
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`),
Expand Down Expand Up @@ -497,10 +498,14 @@ CREATE TABLE IF NOT EXISTS `fc_settings` (
`workunit_status_update` int(10) unsigned NOT NULL DEFAULT '5',

`discord_notifications` tinyint(1) unsigned NOT NULL DEFAULT '0',
`discord_webhook_url` varchar(200) DEFAULT NULL,
`discord_webhook_id` varchar(200) DEFAULT NULL,
`discord_webhook_token` varchar(200) DEFAULT NULL,
`telegram_notifications` tinyint(1) unsigned NOT NULL DEFAULT '0',
`telegram_bot_token` varchar(200) DEFAULT NULL,
`telegram_chat_id` varchar(200) DEFAULT NULL,
`email_notifications` tinyint(1) unsigned NOT NULL DEFAULT '0',
`email_address` varchar(200) DEFAULT NULL,
`email_password` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Expand Down
2 changes: 1 addition & 1 deletion server/sql/20_create_triggers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ CREATE TRIGGER `job_notification` AFTER UPDATE ON `fc_job`
IF done THEN
LEAVE user_loop;
END IF;
INSERT INTO fc_notification VALUES (DEFAULT, userID, DEFAULT,NEW.id,OLD.status,NEW.status, DEFAULT, DEFAULT, DEFAULT, DEFAULT);
INSERT INTO fc_notification VALUES (DEFAULT, userID, DEFAULT,NEW.id,OLD.status,NEW.status, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT);
END LOOP;
CLOSE usersCursor;
END IF;
Expand Down

0 comments on commit 2a3c993

Please sign in to comment.