mirror of
https://github.com/hrfee/jellyfin-accounts.git
synced 2024-06-26 07:57:45 +02:00
The admin page now has the option to send an invite to an email address. Since there are now two email types (invites and pw resets), the new sections have been added to config.ini, and email_template and email_plaintext have been renamed to email_html and email_text respectively.
62 lines
2.1 KiB
Python
Executable File
62 lines
2.1 KiB
Python
Executable File
import time
|
|
import json
|
|
from watchdog.observers import Observer
|
|
from watchdog.events import FileSystemEventHandler
|
|
from jellyfin_accounts.email import Mailgun, Smtp
|
|
from __main__ import config
|
|
from __main__ import email_log as log
|
|
|
|
|
|
class Watcher:
|
|
def __init__(self, dir):
|
|
self.observer = Observer()
|
|
self.dir = str(dir)
|
|
|
|
def run(self):
|
|
event_handler = Handler()
|
|
self.observer.schedule(event_handler, self.dir, recursive=True)
|
|
try:
|
|
self.observer.start()
|
|
except NotADirectoryError:
|
|
log.error(f'Directory {self.dir} does not exist')
|
|
try:
|
|
while True:
|
|
time.sleep(5)
|
|
except:
|
|
self.observer.stop()
|
|
log.info('Watchdog stopped')
|
|
|
|
|
|
class Handler(FileSystemEventHandler):
|
|
@staticmethod
|
|
def on_any_event(event):
|
|
if event.is_directory:
|
|
return None
|
|
elif (event.event_type == 'modified' and
|
|
'passwordreset' in event.src_path):
|
|
log.debug(f'Password reset file: {event.src_path}')
|
|
with open(event.src_path, 'r') as f:
|
|
reset = json.load(f)
|
|
log.info(f'New password reset for {reset["UserName"]}')
|
|
try:
|
|
with open(config['files']['emails'], 'r') as f:
|
|
emails = json.load(f)
|
|
address = emails[reset['UserName']]
|
|
method = config['email']['method']
|
|
if method == 'mailgun':
|
|
email = Mailgun(address)
|
|
elif method == 'smtp':
|
|
email = Smtp(address)
|
|
if email.construct_reset(reset):
|
|
email.send()
|
|
except (FileNotFoundError,
|
|
json.decoder.JSONDecodeError,
|
|
IndexError) as e:
|
|
err = f'{address}: Failed: ' + type(e).__name__
|
|
log.error(err)
|
|
|
|
def start():
|
|
log.info(f'Monitoring {config["password_resets"]["watch_directory"]}')
|
|
w = Watcher(config['password_resets']['watch_directory'])
|
|
w.run()
|