Skip to content

Commit

Permalink
Update dependencies, fix type issues, and update email address parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCat467 committed Dec 23, 2024
1 parent 4b516cc commit 8ed9f4e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repos:
hooks:
- id: black
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.3
rev: v0.8.4
hooks:
- id: ruff
types: [file]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ keywords = ["minecraft", "mineos", "api", "server", "lua"]
dependencies = [
"httpx~=0.28.0",
"hypercorn[trio]~=0.17.3",
"quart~=0.19.9",
"quart~=0.20.0",
"quart-trio~=0.11.1",
"trio~=0.27.0",
"Werkzeug~=3.1.2",
Expand Down
46 changes: 29 additions & 17 deletions src/market_server/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@

import inspect
import math
import re
import time
import traceback
import uuid
from collections import Counter, deque
from email import message_from_string
from email.errors import HeaderParseError
from email.headerregistry import Address, UniqueAddressHeader
from email.policy import default as email_default_policy
from email.headerregistry import Address
from email.utils import parseaddr
from enum import IntEnum
from secrets import token_urlsafe
from typing import TYPE_CHECKING, Any, Final, NamedTuple, cast
Expand All @@ -59,6 +58,10 @@
7: "The Unlicense",
}

EMAIL_REGEX: Final = re.compile(
r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$",
)


class PUBLICATION_CATEGORY(IntEnum): # noqa: N801
"""Publication category enums."""
Expand Down Expand Up @@ -308,22 +311,31 @@ class Publication(NamedTuple):

def parse_email_address(string: str) -> Address | None:
"""Parse email address from string."""
try:
msg = message_from_string(f"To: {string}", policy=email_default_policy)
to = msg["to"]
except (IndexError, HeaderParseError):
# Strip whitespace from the input string
string = string.strip()

# Check if the string matches the email regex
if not EMAIL_REGEX.match(string):
return None
if not to:

# Use parseaddr to extract the email address and name
name, email = parseaddr(string)

# Check if the email is valid
if not email:
return None
if not isinstance(to, UniqueAddressHeader):
raise RuntimeError(
f"During email parsing, got {to!r} ({type(to)}) instead of UniqueAddressHeader as email address group.",
)
value = to.addresses[0]
assert isinstance(value, Address)
if not value.username or not value.domain:

# Create an Address object
try:
address = Address(addr_spec=email, display_name=name)
except ValueError:
return None
return value

# Validate the Address object
if not address.username or not address.domain:
return None

return address


def parse_int(string: str | int) -> int | None:
Expand Down
4 changes: 2 additions & 2 deletions src/market_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def find_ip() -> str:
schema_v_2_04 = schema.Version_2_04(DATA_PATH)


@app.route("/MineOSAPI/<version>/<script>.php", methods=("POST", "GET")) # type: ignore[type-var]
@app.route("/MineOSAPI/<version>/<script>.php", methods=("POST", "GET"))
async def handle_script(
version: str,
script: str,
Expand Down Expand Up @@ -268,7 +268,7 @@ def pretty_format(text: str) -> str:
return text


@app.post("/debug") # type: ignore[type-var]
@app.post("/debug")
async def handle_debug_post() -> (
tuple[AsyncIterator[str], int] | AsyncIterator[str]
):
Expand Down

0 comments on commit 8ed9f4e

Please sign in to comment.