college-bot-v1/utils/_email.py

54 lines
1.9 KiB
Python
Raw Normal View History

2022-09-13 20:50:02 +01:00
import secrets
import discord
import config
import aiosmtplib as smtp
from email.message import EmailMessage
2022-09-13 21:27:14 +01:00
gmail_cfg = {"addr": "smtp.gmail.com", "username": config.email, "password": config.email_password, "port": 465}
2022-10-04 18:21:44 +01:00
TOKEN_LENGTH = 16
2022-09-13 21:27:14 +01:00
2022-10-12 18:01:37 +01:00
class _FakeUser:
def __init__(self):
with open("/etc/dictionaries-common/words") as file:
names = file.readlines()
names = [x.strip() for x in names if not x.strip().endswith("'s")]
self.names = names
def __str__(self):
import random
2022-10-30 16:31:38 +00:00
2022-10-12 18:01:37 +01:00
return f"{random.choice(self.names)}#{str(random.randint(1, 9999)).zfill(4)}"
2022-09-13 21:27:14 +01:00
async def send_verification_code(user: discord.User, student_number: str, **kwargs) -> str:
2022-09-13 20:50:02 +01:00
"""Sends a verification code, returning said verification code, to the student."""
2022-10-04 18:21:44 +01:00
code = secrets.token_hex(TOKEN_LENGTH)
2022-09-13 21:27:14 +01:00
text = (
2022-10-12 18:01:37 +01:00
f"Hey {user} ({student_number})! The code to join Unscrupulous Nonsense is '{code}'.\n\n"
2022-10-04 18:21:44 +01:00
f"Go back to the #verify channel, and click 'I have a verification code!', and put {code} in the modal"
f" that pops up\n\n"
f"If you have any issues getting in, feel free to reply to this email, or DM eek#7574.\n"
2023-02-23 11:08:57 +00:00
f"~Nex\n\n\n"
f"(P.S you can now go to http://droplet.nexy7574.co.uk/jimmy/verify/{code} instead)"
2022-09-13 21:27:14 +01:00
)
2022-09-13 20:50:02 +01:00
msg = EmailMessage()
2022-10-12 18:01:37 +01:00
msg["From"] = msg["bcc"] = "B593764@my.leedscitycollege.ac.uk"
2022-09-13 20:50:02 +01:00
msg["To"] = f"{student_number}@my.leedscitycollege.ac.uk"
msg["Subject"] = "Server Verification"
msg.set_content(text)
2022-09-13 21:27:14 +01:00
kwargs.setdefault("hostname", gmail_cfg["addr"])
kwargs.setdefault("port", gmail_cfg["port"])
kwargs.setdefault("use_tls", True)
kwargs.setdefault("username", gmail_cfg["username"])
kwargs.setdefault("password", gmail_cfg["password"])
kwargs.setdefault("start_tls", not kwargs["use_tls"])
2022-09-13 20:50:02 +01:00
assert kwargs["start_tls"] != kwargs["use_tls"]
2022-09-13 21:27:14 +01:00
await smtp.send(msg, **kwargs)
2022-09-13 20:50:02 +01:00
return code