BDServer/modules/Email.py

264 lines
6.3 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:
Render.Error(server, "Wrong Code!")
return
if verifying[code] != user.get("email"):
Render.Error(server, "Wrong Code!")
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)
del verifying[code]
Render.Redirect(server, "/settings#email")
def SignPetition(server):
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"))
article = server.parsed.get("article", [""])[0]
print(article)
Articles = Render.allArticles()
if article not in Articles:
Render.AccessDenied(server)
return
Article = Articles[article]
code = server.parsed.get("code", [""])[0]
email = server.parsed.get("email", [""])[0]
if email in Article.get("petition", {}).get("signatures", []):
Render.Error(server, "You already signed!")
return
if not code:
html = html + """
<div class="middle_section_article">
<div class="dark_box">
<form action="/sign_petition">
<center>
<br>
You have recieved an email with a code.<br><br>
<small>It could be in spam.</small>
<br><br>
<input type="hidden" name="article" value='"""+article+"""'>
<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] = email
text = """
<center>
Your Verification Code
<br>
<div class="toot">"""+code+"""
</div>
</center>
"""
text = Format(text)
Send(email, "Verification Code", text)
Render.send(server, html, 200)
else:
# If we recieved the code.
if code not in verifying:
Render.Error(server, "Wrong Code!")
return
Article["petition"]["signed"] += 1
Article["petition"]["signatures"].append(verifying[code])
f = Set.Folder()
with open(f+"/tabs"+article+"/metadata.json", "w") as save:
json.dump(Article, save, indent=4)
del verifying[code]
Render.Redirect(server, article)
###
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