164 lines
3.9 KiB
Python
164 lines
3.9 KiB
Python
|
import smtplib
|
||
|
from email.mime.text import MIMEText
|
||
|
from email.mime.multipart import MIMEMultipart
|
||
|
|
||
|
import json
|
||
|
|
||
|
from modules import Set
|
||
|
from modules.Common import *
|
||
|
|
||
|
from modules import Render
|
||
|
|
||
|
verifying = {}
|
||
|
|
||
|
def Send(reciever, subject, html):
|
||
|
|
||
|
config = Set.Load()
|
||
|
Email = config.get("email")
|
||
|
|
||
|
if not Email:
|
||
|
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" No email credentials!")
|
||
|
|
||
|
try: port = int(Email.get("port", ""))
|
||
|
except: port = 587
|
||
|
|
||
|
message = MIMEMultipart()
|
||
|
message["From"] = Email["visible"]
|
||
|
message["To"] = reciever
|
||
|
message["Subject"] = subject
|
||
|
|
||
|
message.attach(MIMEText(html, "html"))
|
||
|
|
||
|
# Send the email
|
||
|
with smtplib.SMTP(Email["server"], port) as server:
|
||
|
|
||
|
server.starttls()
|
||
|
|
||
|
server.login(Email["sender"],
|
||
|
Email["password"])
|
||
|
|
||
|
server.sendmail(Email["sender"],
|
||
|
reciever,
|
||
|
message.as_string())
|
||
|
|
||
|
print('Email "'+subject+'" was sent to', reciever)
|
||
|
|
||
|
def VerifyPage(server):
|
||
|
|
||
|
user = Render.validate(server.cookie)
|
||
|
|
||
|
# Authorization check
|
||
|
if not user:
|
||
|
Render.AccessDenied(server)
|
||
|
return
|
||
|
|
||
|
config = Set.Load()
|
||
|
|
||
|
# Generating <head>
|
||
|
html = Render.head(title = "Verify Email",
|
||
|
description = "Verify Email",
|
||
|
config = config
|
||
|
)
|
||
|
|
||
|
html = html + Render.Button(config.get("title", "My Website"), "/", image=config.get("favicon", "/icon/internet"))
|
||
|
|
||
|
code = server.parsed.get("code", [""])[0]
|
||
|
if not code:
|
||
|
|
||
|
|
||
|
|
||
|
html = html + """
|
||
|
|
||
|
<div class="middle_section_article">
|
||
|
|
||
|
<div class="dark_box">
|
||
|
|
||
|
<form action="email_verify">
|
||
|
|
||
|
<center>
|
||
|
<br>
|
||
|
You have recieved an email with a code.<br><br>
|
||
|
<small>It could be in spam.</small>
|
||
|
<br><br>
|
||
|
<input class="button" name="code" required placeholder="Your code...">
|
||
|
<br>
|
||
|
<button class="button" type="submit">
|
||
|
<img alt="[icon ok]" style="vertical-align: middle" src="/icon/ok">
|
||
|
Verify
|
||
|
</button>
|
||
|
|
||
|
</center>
|
||
|
|
||
|
</form>
|
||
|
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
"""
|
||
|
code = RandString(10)
|
||
|
verifying[code] = user.get("email")
|
||
|
|
||
|
text = """
|
||
|
<center>
|
||
|
Your Verification Code
|
||
|
<br>
|
||
|
<div class="toot">"""+code+"""
|
||
|
</div>
|
||
|
</center>
|
||
|
"""
|
||
|
|
||
|
text = Format(text)
|
||
|
|
||
|
Send(user.get("email", ""), "Verification Code", text)
|
||
|
|
||
|
Render.send(server, html, 200)
|
||
|
|
||
|
else:
|
||
|
|
||
|
# If we recieved the code.
|
||
|
|
||
|
if code not in verifying:
|
||
|
print("code not in verifying")
|
||
|
Render.AccessDenied(server)
|
||
|
return
|
||
|
|
||
|
|
||
|
if verifying[code] != user.get("email"):
|
||
|
print("email wrong")
|
||
|
Render.AccessDenied(server)
|
||
|
return
|
||
|
|
||
|
user["email_verified"] = True
|
||
|
|
||
|
f = Set.Folder()
|
||
|
folder = f+"/accounts"
|
||
|
with open(folder+"/"+user.get("username", "")+".json", "w") as save:
|
||
|
json.dump(user, save, indent=4)
|
||
|
|
||
|
Render.Redirect(server, "/settings#email")
|
||
|
|
||
|
def Format(text):
|
||
|
|
||
|
config = Set.Load()
|
||
|
|
||
|
favicon = config.get("favicon", "")
|
||
|
if favicon.startswith("/"):
|
||
|
favicon = "https://"+config.get("domain", "")+favicon
|
||
|
|
||
|
css = open(config.get("css", "default.css"), "r")
|
||
|
html = '<style>'+css.read()+'</style>'
|
||
|
html = html + '<div class="dark_box"><center>'
|
||
|
|
||
|
if favicon:
|
||
|
html = html + '<img alt="[favicon]" style="height:40px;vertical-align: middle" src="'+favicon+'">'
|
||
|
|
||
|
html = html + " "+config.get("title", "My Website")
|
||
|
html = html + "</center><br></div>"
|
||
|
html = html + '<div class="dark_box">'
|
||
|
html = html + text
|
||
|
html = html + '</div>'
|
||
|
|
||
|
return html
|
||
|
|
||
|
|