Add files via upload

This commit is contained in:
David Rotermund 2024-04-09 20:09:22 +02:00 committed by GitHub
parent 6967542183
commit 81418455cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 228 additions and 60 deletions

View file

@ -0,0 +1,5 @@
{
"allowed_domains": [
"uni-bremen.de"
]
}

View file

@ -0,0 +1,5 @@
{
"blocked_users": [
""
]
}

View file

@ -1,8 +1,5 @@
# pip install email_validator
import imaplib
import json
import email.utils
from email_validator import validate_email, EmailNotValidError # type: ignore
def check_mails(
@ -64,58 +61,3 @@ def check_mails(
imap_connection.close()
return result
def process_emails(
mail_to_process: list[dict],
config_file: str = "allowed_domains.json",
blocked_user_file: str = "blocked_users.json",
) -> list[dict]:
result: list[dict] = []
with open(config_file, "r") as file:
allowed_domains: dict = json.load(file)
with open(blocked_user_file, "r") as file:
blocked_users: dict = json.load(file)
for mail in mail_to_process:
temp = email.utils.parseaddr(mail["from"])[1]
if (temp != "") and (temp is not None):
email_status: bool = False
try:
emailinfo = validate_email(temp, check_deliverability=False)
email_status = True
temp = emailinfo.normalized
except EmailNotValidError:
email_status = False
domain_found = False
if email_status:
for domain in allowed_domains["allowed_domains"]:
if temp.endswith(domain):
domain_found = True
if domain_found:
for blocked_user in blocked_users["blocked_users"]:
if temp == blocked_user:
domain_found = False
if domain_found:
from_validated_ab = email.utils.parseaddr(mail["from"])
from_validated = validate_email(
from_validated_ab[1], check_deliverability=False
)
result.append(
{
"from_a": from_validated_ab[0],
"from_b": from_validated.normalized,
"to": mail["to"],
"subject": mail["subject"],
}
)
return result

View file

@ -1 +1,7 @@
{"overleafdomain": "https://overleaf.neuro.uni-bremen.de"}
{
"overleafdomain": "https://overleaf.neuro.uni-bremen.de",
"user": "XXXXXX",
"password": "XXXXXX",
"host": "XXXXX",
"port": 993
}

View file

@ -0,0 +1,13 @@
import pymongo
def does_user_exists(email_to_find: str) -> bool:
client = pymongo.MongoClient("overleafmongo", 27017)
db = client.sharelatex
users = db.users
search_result = users.find_one({"email": email_to_find})
if search_result is None:
return False
else:
return True

26
user_tools/exec_docker.py Normal file
View file

@ -0,0 +1,26 @@
import docker
def identify_container(client, label: str = "overleaf") -> int | None:
containers = client.containers.list(all=True)
for container in containers:
if str(container.name) == label:
return container.id
return None
def docker_exec(exec_command: str = "ls") -> tuple[bool, str]:
client = docker.from_env()
docker_id = identify_container(client)
assert docker_id is not None
container = client.containers.get(docker_id)
command = f'/bin/bash -c "{exec_command}"'
try:
result = container.exec_run(command, stream=False)
result_string: str = result.output
return True, result_string
except docker.errors.APIError as e:
return False, str("")

View file

@ -8,7 +8,7 @@ import json
def get_activation_requests(remove_old_entries: bool = False) -> list[dict]:
results: list[dict] = []
filename = "last_run.pkl"
filename = "last_run_activations.pkl"
with open("config_mail.json", "r") as file:
config = json.load(file)

View file

@ -1,4 +1,5 @@
apk add mc
apk add bash
pip install docker
pip install pymongo
pip install email_validator

View file

@ -0,0 +1,68 @@
# pip install email_validator
import email.utils
from email_validator import validate_email # type: ignore
import email_validator
import json
def process_emails(
mail_to_process: list[dict],
config_file: str = "allowed_domains.json",
blocked_user_file: str = "blocked_users.json",
) -> list[dict]:
result: list[dict] = []
with open(config_file, "r") as file:
allowed_domains: dict = json.load(file)
with open(blocked_user_file, "r") as file:
blocked_users: dict = json.load(file)
for mail in mail_to_process:
temp = email.utils.parseaddr(mail["from"])[1]
if (temp != "") and (temp is not None):
email_status: bool = False
try:
emailinfo = validate_email(temp, check_deliverability=False)
email_status = True
temp = emailinfo.normalized
except email_validator.exceptions_types.EmailSyntaxError:
email_status = False
except email_validator.exceptions_types.EmailNotValidError:
email_status = False
domain_found = False
if email_status:
for domain in allowed_domains["allowed_domains"]:
if temp.endswith(domain):
domain_found = True
if domain_found:
for blocked_user in blocked_users["blocked_users"]:
if temp == blocked_user:
domain_found = False
if domain_found:
from_validated_ab = email.utils.parseaddr(mail["from"])
try:
from_validated = validate_email(
from_validated_ab[1], check_deliverability=False
)
result.append(
{
"from_a": from_validated_ab[0],
"from_b": from_validated.normalized,
"to": mail["to"],
"subject": mail["subject"],
}
)
except email_validator.exceptions_types.EmailSyntaxError:
pass
except email_validator.exceptions_types.EmailNotValidError:
pass
return result

View file

@ -0,0 +1,81 @@
import pymongo
import datetime
import os
import pickle
import email.utils
from email_validator import validate_email # type: ignore
import email_validator
def process_invitations(remove_old_entries: bool = False):
results: list[dict] = []
filename = "last_run_invitations.pkl"
now = datetime.datetime.now()
client = pymongo.MongoClient("overleafmongo", 27017)
db = client.sharelatex
project_invites = db.projectInvites
continue_at_time = None
if remove_old_entries:
if os.path.exists(filename):
with open(filename, "rb") as file:
continue_at_time = pickle.load(file)
query = {"expires": {"$gt": now}}
if continue_at_time is not None:
query["createdAt"] = {"$gt": continue_at_time}
newest = None
for project_invite in project_invites.find(query):
if newest is None:
newest = project_invite["createdAt"]
elif project_invite["createdAt"] > newest:
newest = project_invite["createdAt"]
# Freeze time. We don't want to misshandle item that are newer than the last check
if newest is not None:
if "createdAt" in query:
query["createdAt"] = {"$gt": continue_at_time, "$lte": newest}
else:
query["createdAt"] = {"$lte": newest}
# Find unique user ids
user_id_set = set()
for project_invite in project_invites.find(query):
user_id_set.add(project_invite["email"])
user_ids = list(user_id_set)
# Store the time stamp for newest
with open(filename, "wb") as file:
pickle.dump(newest, file)
for uid in user_ids:
from_validated_ab = email.utils.parseaddr(uid)
try:
from_validated = validate_email(
from_validated_ab[1], check_deliverability=False
)
results.append(
{
"from_a": None,
"from_b": from_validated.normalized,
"to": None,
"subject": None,
}
)
except email_validator.exceptions_types.EmailSyntaxError:
pass
except email_validator.exceptions_types.EmailNotValidError:
pass
return results
print(process_invitations())

21
user_tools/send_mail.py Normal file
View file

@ -0,0 +1,21 @@
import smtplib
from email.message import EmailMessage
def send_mail(
email_body: str,
email_subject: str,
email_from: str,
email_to: str,
smtpd_host: str = "overleafsmtpd",
):
msg = EmailMessage()
msg.set_content(email_body.decode("utf-8"))
msg["Subject"] = email_subject
msg["From"] = email_from
msg["To"] = email_to
s = smtplib.SMTP(smtpd_host)
s.send_message(msg)
s.quit()