diff --git a/.gitignore b/.gitignore index 0fcc40a..de4fe8f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ __pycache__/ -token \ No newline at end of file +config.json \ No newline at end of file diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..6a52bdd --- /dev/null +++ b/config.example.json @@ -0,0 +1,8 @@ +{ + "token": "groupme api token", + "smtp_server": "mail.example.com", + "smtp_username": "groupme@example.com", + "smtp_password": "your_password", + "from_addr": "groupme@example.com", + "to_addr": "user@example.com" +} \ No newline at end of file diff --git a/groupme_sync/__main__.py b/groupme_sync/__main__.py index e8b3fcd..f9521e0 100644 --- a/groupme_sync/__main__.py +++ b/groupme_sync/__main__.py @@ -1,14 +1,41 @@ from sys import argv +import json from .groupme import GroupMe +from .emailwrap import send_email def main() -> None: filename = argv[1] with open(filename, "r") as file: - token = file.readlines()[0].rstrip() + data = json.loads(file.read()) - chats = GroupMe(token) + chats = GroupMe(data["token"]) for message in chats: + title = "[GroupMe] {} sent a message in {}".format(message["name"], message["group_name"]) + body = """ + Greetings, + + {} sent a message in group {} -- it reads as follows: + + {} + + Much regards, + the internal beepboop.systems mail system + """.format( + message["name"], + message["group_name"], + message["text"] + ) + + send_email( + title=title, + body=body, + smtp_server=data["smtp_server"], + smtp_username=data["smtp_username"], + smtp_password=data["smtp_password"], + from_addr=data["from_addr"], + to_addr=data["to_addr"], + ) print(message) main() \ No newline at end of file diff --git a/groupme_sync/emailwrap.py b/groupme_sync/emailwrap.py new file mode 100644 index 0000000..b416669 --- /dev/null +++ b/groupme_sync/emailwrap.py @@ -0,0 +1,30 @@ +import smtplib +import ssl +from email.mime.text import MIMEText +from email.mime.multipart import MIMEMultipart + +def send_email(title: str, body: str, + smtp_server: str, smtp_username: str, smtp_password: str, + from_addr: str, to_addr: str): + """ + Send an email to an SMTP server with starttls, and some other + stuff. If you need to do anything different, just write your + own implementation. + """ + ctx = ssl.create_default_context() + + with smtplib.SMTP(smtp_server, port=587) as server: + server.starttls(context=ctx) + server.ehlo() + server.login(smtp_username, smtp_password) + + message = MIMEMultipart("alternative") + message["Subject"] = title + message["From"] = from_addr + message["To"] = to_addr + part = MIMEText(body, "plain") + message.attach(part) + + server.sendmail( + from_addr, to_addr, message.as_string() + ) \ No newline at end of file