diff --git a/spaceapi/utils.py b/spaceapi/utils.py --- a/spaceapi/utils.py +++ b/spaceapi/utils.py @@ -1,6 +1,8 @@ import json import os.path import random +import smtplib +import email from functools import wraps from time import time @@ -16,9 +18,11 @@ if not os.path.exists(default_json_file_ elif not os.path.isfile(default_json_file): raise RuntimeError("default_v14.json is not a file!") +standard_open_message = "The space is now open!" +standard_close_message = "The space is now closed!" possible_open_tweets = ( - "The space is now open!", + standard_open_message, "The space is open! Come in and hack something!", "Yes, we're open! Come in and create something!", "Come by and hack something! We've just opened!", @@ -29,7 +33,7 @@ possible_open_tweets = ( ) possible_closed_tweets = ( - "The space is now closed!", + standard_close_message, "We're closed now! See you soon.", "Sorry, we are closed now!", "The ChaosStuff is now closed! Come back another time!", @@ -68,6 +72,20 @@ def post_toot(toot): api.status_post(toot, visibility="unlisted") +def post_email(subject, body): + if "EMAIL_PASS" in current_app.config: + smtpConn = smtplib.SMTP("EMAIL_HOST", port="EMAIL_PORT") + sslContext = ssl.create_default_context(Purpose.SERVER_AUTH) + smtpConn.starttls(sslContext) + smtpConn.login("EMAIL_USER", "EMAIL_PASS") + msg = email.message.EmailMessage(email.policy.SMTP) + msg['To'] = "EMAIL_ANNOUNCE_ADDRESS" + msg['From'] = "EMAIL_USER" + msg['Subject'] = subject + msg.set_content(body) + smtpConn.send_message(msg) + + class Singleton: def __new__(cls, *args, **kwargs): key = str(hash(cls)) @@ -174,12 +192,24 @@ class ActiveStatusv14(Singleton, dict): except Exception as e: current_app.logger.error("Sending toot failed! %s" % e, exc_info=True) + def send_email(self, value): + subject = ( + standard_open_message + if value + else standard_close_message + ) + try: + post_email(message) + except Exception as e: + current_app.logger.error("Sending email failed! %s" % e, exc_info=True) + def set_new_state(self, value=None, trigger_person=None, message=None): if value is not None and value != self["state"]["open"]: self["state"]["open"] = value self.send_tweet(value) + self.send_email(value) if not value: if "people_now_present" in self["sensors"]: