2020-07-12 18:53:04 +00:00
|
|
|
# Watches Jellyfin for password resets and sends emails.
|
2020-04-12 20:25:27 +00:00
|
|
|
import time
|
|
|
|
import json
|
|
|
|
from watchdog.observers import Observer
|
|
|
|
from watchdog.events import FileSystemEventHandler
|
2020-04-19 21:35:51 +00:00
|
|
|
from jellyfin_accounts.email import Mailgun, Smtp
|
2020-04-20 19:37:39 +00:00
|
|
|
from jellyfin_accounts.web_api import jf
|
2020-06-16 19:07:47 +00:00
|
|
|
from jellyfin_accounts import config, data_store
|
|
|
|
from jellyfin_accounts import email_log as log
|
2020-04-12 20:25:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2020-04-19 21:35:51 +00:00
|
|
|
try:
|
|
|
|
self.observer.start()
|
|
|
|
except NotADirectoryError:
|
2020-06-21 19:29:53 +00:00
|
|
|
log.error(f"Directory {self.dir} does not exist")
|
2020-04-12 20:25:27 +00:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
time.sleep(5)
|
|
|
|
except:
|
|
|
|
self.observer.stop()
|
2020-06-21 19:29:53 +00:00
|
|
|
log.info("Watchdog stopped")
|
2020-04-12 20:25:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Handler(FileSystemEventHandler):
|
|
|
|
@staticmethod
|
|
|
|
def on_any_event(event):
|
|
|
|
if event.is_directory:
|
|
|
|
return None
|
2020-06-21 19:29:53 +00:00
|
|
|
elif event.event_type == "modified" and "passwordreset" in event.src_path:
|
|
|
|
log.debug(f"Password reset file: {event.src_path}")
|
2020-04-25 21:08:59 +00:00
|
|
|
time.sleep(1)
|
2020-06-21 19:29:53 +00:00
|
|
|
with open(event.src_path, "r") as f:
|
2020-04-12 20:25:27 +00:00
|
|
|
reset = json.load(f)
|
|
|
|
log.info(f'New password reset for {reset["UserName"]}')
|
|
|
|
try:
|
2020-06-21 19:29:53 +00:00
|
|
|
id = jf.getUsers(reset["UserName"], public=False)["Id"]
|
2020-06-14 16:58:18 +00:00
|
|
|
address = data_store.emails[id]
|
2020-06-21 19:29:53 +00:00
|
|
|
if address != "":
|
|
|
|
method = config["email"]["method"]
|
|
|
|
if method == "mailgun":
|
2020-06-14 16:58:18 +00:00
|
|
|
email = Mailgun(address)
|
2020-06-21 19:29:53 +00:00
|
|
|
elif method == "smtp":
|
2020-06-14 16:58:18 +00:00
|
|
|
email = Smtp(address)
|
|
|
|
if email.construct_reset(reset):
|
|
|
|
email.send()
|
|
|
|
else:
|
|
|
|
raise IndexError
|
2020-06-21 19:29:53 +00:00
|
|
|
except (
|
|
|
|
FileNotFoundError,
|
|
|
|
json.decoder.JSONDecodeError,
|
|
|
|
IndexError,
|
|
|
|
) as e:
|
|
|
|
err = f"{address}: Failed: " + type(e).__name__
|
2020-04-12 20:25:27 +00:00
|
|
|
log.error(err)
|
|
|
|
|
2020-06-21 19:29:53 +00:00
|
|
|
|
2020-04-12 20:25:27 +00:00
|
|
|
def start():
|
2020-04-19 21:35:51 +00:00
|
|
|
log.info(f'Monitoring {config["password_resets"]["watch_directory"]}')
|
2020-06-21 19:29:53 +00:00
|
|
|
w = Watcher(config["password_resets"]["watch_directory"])
|
2020-04-12 20:25:27 +00:00
|
|
|
w.run()
|