Global reformatting with black.

This commit is contained in:
Goffi 2025-09-13 10:46:10 +02:00
commit 96bf3af324
3 changed files with 557 additions and 561 deletions

View file

@ -52,7 +52,13 @@ from libervia.backend.core.i18n import D_, _
from libervia.backend.core.log import getLogger
from libervia.backend.memory.persistent import LazyPersistentBinaryDict
from libervia.backend.memory.sqla import select
from libervia.backend.memory.sqla_mapping import AccessModel, Affiliation, PrivateIndBin, PublishModel, PubsubAffiliation
from libervia.backend.memory.sqla_mapping import (
AccessModel,
Affiliation,
PrivateIndBin,
PublishModel,
PubsubAffiliation,
)
from libervia.backend.models.core import MessageData
from libervia.backend.plugins.plugin_comp_email_gateway.pubsub_service import (
EmailGWPubsubService,
@ -96,8 +102,17 @@ PLUGIN_INFO = {
C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT,
C.PI_PROTOCOLS: [],
C.PI_DEPENDENCIES: [
"XEP-0033", "XEP-0077", "XEP-0106", "XEP-0277", "XEP-0498", "XEP-0499", "GRE", "GRE-MIME",
"GRE-OpenPGP", "PUBSUB_CACHE", "TEXT_SYNTAXES"
"XEP-0033",
"XEP-0077",
"XEP-0106",
"XEP-0277",
"XEP-0498",
"XEP-0499",
"GRE",
"GRE-MIME",
"GRE-OpenPGP",
"PUBSUB_CACHE",
"TEXT_SYNTAXES",
],
C.PI_RECOMMENDATIONS: [],
C.PI_MAIN: "EmailGatewayComponent",
@ -276,26 +291,30 @@ class EmailGatewayComponent(GetDataHandler):
assert to_email is not None
subject = "This is an encrypted message."
outer = MIMEMultipart('encrypted', protocol="application/pgp-encrypted")
outer = MIMEMultipart("encrypted", protocol="application/pgp-encrypted")
outer["Subject"] = subject
# FIXME: use credentials here.
outer["From"] = from_jid.userhost()
outer["To"] = to_email
outer["Content-Type"] = "multipart/encrypted; protocol=\"application/pgp-encrypted\""
outer["Content-Type"] = (
'multipart/encrypted; protocol="application/pgp-encrypted"'
)
version = MIMEApplication(
"Version: 1\n",
_subtype='pgp-encrypted',
_encoder=encoders.encode_7or8bit
_subtype="pgp-encrypted",
_encoder=encoders.encode_7or8bit,
)
version["Content-Description"] = "PGP/MIME version identification"
encrypted_part = MIMEApplication(
encrypted_payload,
_subtype='octet-stream',
_encoder=encoders.encode_7or8bit
_subtype="octet-stream",
_encoder=encoders.encode_7or8bit,
)
encrypted_part["Content-Description"] = "OpenPGP encrypted message"
encrypted_part["Content-Type"] = "application/octet-stream; name=\"encrypted.asc\""
encrypted_part["Content-Disposition"] = "inline; filename=\"encrypted.asc\""
encrypted_part["Content-Type"] = (
'application/octet-stream; name="encrypted.asc"'
)
encrypted_part["Content-Disposition"] = 'inline; filename="encrypted.asc"'
outer.attach(version)
outer.attach(encrypted_part)
body = outer.as_bytes()
@ -497,9 +516,7 @@ class EmailGatewayComponent(GetDataHandler):
msg = MIMEText(body, "plain", "UTF-8")
if subject is not None:
msg["Subject"] = subject
msg["From"] = formataddr(
(credentials.user_name or None, credentials.user_email)
)
msg["From"] = formataddr((credentials.user_name or None, credentials.user_email))
if extra.addresses:
assert extra.addresses.to
main_to_address = extra.addresses.to[0]
@ -810,7 +827,9 @@ class EmailGatewayComponent(GetDataHandler):
user_email = user_data.credentials.user_email
author_name, author_email = parseaddr(email["from"])
author_email = author_email.lower()
from_jid = jid.JID(None, (self._e.escape(author_email), self.client.jid.host, None))
from_jid = jid.JID(
None, (self._e.escape(author_email), self.client.jid.host, None)
)
# Get the email body
body_mime = email.get_body(("plain",))
@ -937,7 +956,7 @@ class EmailGatewayComponent(GetDataHandler):
client = self.client.get_virtual_client(from_jid)
# Now that the message is parsed, we check if it's a mailing list.
list_ids = email.get_all("list-id")
list_ids = email.get_all("list-id")
if list_ids:
try:
await self.handle_mailing_list(
@ -950,7 +969,7 @@ class EmailGatewayComponent(GetDataHandler):
user_jid,
body,
subject,
extra
extra,
)
except exceptions.DataError as e:
log.warning(f"Can't parse mailing list email: {e}.")
@ -963,6 +982,7 @@ class EmailGatewayComponent(GetDataHandler):
{"": subject} if subject else None,
extra=extra,
)
def parse_references(self, email: EmailMessage) -> list[str]:
"""Extract message IDs from the "References" header.
@ -970,7 +990,7 @@ class EmailGatewayComponent(GetDataHandler):
@returns: Message IDs
"""
references = []
for header in email.get_all('references', []):
for header in email.get_all("references", []):
for token in header.split():
_, address = parseaddr(token)
if address:
@ -984,7 +1004,7 @@ class EmailGatewayComponent(GetDataHandler):
node: str,
item_id: str,
author_name: str,
author_email: str
author_email: str,
) -> MbData:
"""Convert an email to blog data.
@ -994,16 +1014,18 @@ class EmailGatewayComponent(GetDataHandler):
tags = set()
# Title
title = email.get('subject', '')
title = email.get("subject", "")
def strip_tags(m):
value = m.group()[1:-1].strip().lower()
if value:
tags.add(value)
return ""
title = re.sub(r"\[.+?\]", strip_tags, title).strip()
# Dates
date_header = email.get('Date')
date_header = email.get("Date")
if date_header:
try:
published = date_utils.date_parse(date_header)
@ -1023,10 +1045,10 @@ class EmailGatewayComponent(GetDataHandler):
for part in email.walk():
content_type = part.get_content_type()
content_disposition = part.get('Content-Disposition', '').lower()
content_disposition = part.get("Content-Disposition", "").lower()
# Skip attachments
if 'attachment' in content_disposition:
if "attachment" in content_disposition:
continue
payload = part.get_payload(decode=True)
@ -1034,15 +1056,15 @@ class EmailGatewayComponent(GetDataHandler):
continue
payload = cast(bytes, payload)
charset = part.get_content_charset() or 'utf-8'
charset = part.get_content_charset() or "utf-8"
try:
payload_text = payload.decode(charset, errors='replace')
payload_text = payload.decode(charset, errors="replace")
except (LookupError, TypeError):
payload_text = payload.decode('utf-8', errors='replace')
payload_text = payload.decode("utf-8", errors="replace")
if content_type == 'text/plain':
if content_type == "text/plain":
plain_parts.append(payload_text)
elif content_type == 'text/html':
elif content_type == "text/html":
html_parts.append(payload_text)
# Prefer first valid parts for safety
@ -1055,17 +1077,17 @@ class EmailGatewayComponent(GetDataHandler):
payload = email.get_payload(decode=True)
if payload is not None:
payload = cast(bytes, payload)
charset = email.get_content_charset() or 'utf-8'
charset = email.get_content_charset() or "utf-8"
try:
content_text = payload.decode(charset, errors='replace')
content_text = payload.decode(charset, errors="replace")
except (LookupError, TypeError):
content_text = payload.decode('utf-8', errors='replace')
content_text = payload.decode("utf-8", errors="replace")
content_type = email.get_content_type()
if content_type == 'text/plain':
if content_type == "text/plain":
content = content_text
elif content_type == 'text/html':
elif content_type == "text/html":
content_xhtml = content_text
if content_xhtml is None:
@ -1086,13 +1108,11 @@ class EmailGatewayComponent(GetDataHandler):
content_xhtml=content_xhtml,
author=author_name,
author_email=author_email,
tags=list(tags)
tags=list(tags),
)
async def mb_data_to_email(
self,
credentials: Credentials,
mb_data: MbData
self, credentials: Credentials, mb_data: MbData
) -> EmailMessage:
"""Convert blog data to an email message.
@ -1113,28 +1133,28 @@ class EmailGatewayComponent(GetDataHandler):
if mb_data.title:
title_parts.append(mb_data.title)
email['Subject'] = ' '.join(title_parts)
email["Subject"] = " ".join(title_parts)
if tags:
email['Keywords'] = ', '.join(tags)
email["Keywords"] = ", ".join(tags)
# From
# FIXME: Check email according to sender.
if mb_data.author and mb_data.author_email:
email['From'] = f"{mb_data.author} <{mb_data.author_email}>"
email["From"] = f"{mb_data.author} <{mb_data.author_email}>"
elif mb_data.author:
email['From'] = f"{mb_data.author} <{credentials.user_email}>"
email["From"] = f"{mb_data.author} <{credentials.user_email}>"
elif mb_data.author_email:
email['From'] = mb_data.author_email
email["From"] = mb_data.author_email
else:
email['From'] = credentials.user_email
email["From"] = credentials.user_email
# Message ID
if mb_data.id:
email['Message-ID'] = f"<{mb_data.id}>"
email["Message-ID"] = f"<{mb_data.id}>"
else:
msg_id = make_msgid()
email['Message-ID'] = msg_id
email["Message-ID"] = msg_id
mb_data.id = msg_id[1:-1]
# In-Reply-To
@ -1142,20 +1162,20 @@ class EmailGatewayComponent(GetDataHandler):
# Use the first reply-to reference
in_reply_to = mb_data.in_reply_tos[0]
if in_reply_to.ref:
email['In-Reply-To'] = f"<{in_reply_to.ref}>"
email["In-Reply-To"] = f"<{in_reply_to.ref}>"
# Dates
date_set = False
if mb_data.published is not None:
try:
email['Date'] = formatdate(mb_data.published, localtime=True)
email["Date"] = formatdate(mb_data.published, localtime=True)
date_set = True
except Exception:
pass
if not date_set and mb_data.updated is not None:
try:
email['Date'] = formatdate(mb_data.updated, localtime=True)
email["Date"] = formatdate(mb_data.updated, localtime=True)
except Exception:
pass
@ -1172,7 +1192,7 @@ class EmailGatewayComponent(GetDataHandler):
)
email.set_content(mb_data.content)
email.add_alternative(xhtml_content, subtype='html')
email.add_alternative(xhtml_content, subtype="html")
elif mb_data.content:
email.set_content(mb_data.content)
else:
@ -1190,8 +1210,8 @@ class EmailGatewayComponent(GetDataHandler):
email: EmailMessage,
user_jid: jid.JID,
body: str,
subject: str|None,
extra: dict
subject: str | None,
extra: dict,
) -> None:
"""Handle emails from mailing lists.
@ -1224,16 +1244,15 @@ class EmailGatewayComponent(GetDataHandler):
create_kwargs={
"access_model": AccessModel.whitelist,
"publish_model": PublishModel.publishers,
"affiliations": [PubsubAffiliation(
entity = user_jid,
affiliation = Affiliation.owner
)],
"subscribed": True
"affiliations": [
PubsubAffiliation(entity=user_jid, affiliation=Affiliation.owner)
],
"subscribed": True,
},
)
assert root_node is not None
in_reply_to_value = ''.join(email.get_all("in-reply-to", []))
in_reply_to_value = "".join(email.get_all("in-reply-to", []))
__, in_reply_to = parseaddr(in_reply_to_value)
references = self.parse_references(email)
if in_reply_to:
@ -1269,14 +1288,12 @@ class EmailGatewayComponent(GetDataHandler):
Comment(
uri=parent_node_uri,
service=pubsub_service,
node=parent_node_name
node=parent_node_name,
)
]
],
)
await G.storage.cache_pubsub_items(
client,
root_node,
[await empty_item_data.to_element(client)]
client, root_node, [await empty_item_data.to_element(client)]
)
parent_node = await G.storage.get_pubsub_node(
client,
@ -1288,11 +1305,10 @@ class EmailGatewayComponent(GetDataHandler):
"access_model": AccessModel.whitelist,
"publish_model": PublishModel.publishers,
"subscribed": True,
"affiliations": [PubsubAffiliation(
entity = user_jid,
affiliation = Affiliation.owner
)],
"parent_node": root_node
"affiliations": [
PubsubAffiliation(entity=user_jid, affiliation=Affiliation.owner)
],
"parent_node": root_node,
},
)
else:
@ -1300,24 +1316,19 @@ class EmailGatewayComponent(GetDataHandler):
parent_node_name = cast(str, root_node.name)
mb_data = self.email_to_mb_data(
email,
pubsub_service,
parent_node_name,
message_id,
author_name,
author_email
email, pubsub_service, parent_node_name, message_id, author_name, author_email
)
mb_data.comments.append(
Comment(
service = pubsub_service,
node = self._mb.get_comments_node(message_id),
service=pubsub_service,
node=self._mb.get_comments_node(message_id),
)
)
await G.storage.cache_pubsub_items(
client,
parent_node,
[await mb_data.to_element(client)],
[mb_data.model_dump(mode="json")]
[mb_data.model_dump(mode="json")],
)
async def handle_attachment(self, part: EmailMessage, recipient_jid: jid.JID) -> None:
@ -1393,9 +1404,7 @@ class EmailGatewayComponent(GetDataHandler):
partial(self.on_new_email, user_data, from_jid.userhostJID()),
connected,
)
reactor.connectTCP(
credentials.imap_host, int(credentials.imap_port), factory
)
reactor.connectTCP(credentials.imap_host, int(credentials.imap_port), factory)
await connected
async def _on_registration_submit(
@ -1436,9 +1445,9 @@ class EmailGatewayComponent(GetDataHandler):
return True
self.validate_imap_smtp_form(submit_form)
credentials = Credentials(**{
key: field.value for key, field in submit_form.fields.items()
})
credentials = Credentials(
**{key: field.value for key, field in submit_form.fields.items()}
)
user_data = self.users_data.get(from_jid)
if user_data is None:
# The user is not in cache, we cache current credentials.
@ -1460,19 +1469,14 @@ class EmailGatewayComponent(GetDataHandler):
await self.storage.aset(key, credentials)
return True
async def on_relayed_encryption_data(
self,
client: SatXMPPEntity,
iq_elt: domish.Element,
form: data_form.Form
self, client: SatXMPPEntity, iq_elt: domish.Element, form: data_form.Form
) -> None:
from_jid = jid.JID(iq_elt["from"]).userhostJID()
credentials = await self.get_credentials(from_jid)
form.addField(data_form.Field(var="sender_id", value=credentials.user_email))
@implementer(iwokkel.IDisco)
class EmailGatewayHandler(XMPPHandler):
@ -1482,28 +1486,13 @@ class EmailGatewayHandler(XMPPHandler):
"result",
formNamespace="urn:xmpp:data-policy:0",
fields=(
data_form.Field(
"list-single",
"auth_data",
"plain"
),
data_form.Field(
"list-single",
"data-transmission",
"encrypted"
),
data_form.Field(
"text-single",
"encryption_algorithm",
"TLS"
),
data_form.Field("list-single", "auth_data", "plain"),
data_form.Field("list-single", "data-transmission", "encrypted"),
data_form.Field("text-single", "encryption_algorithm", "TLS"),
data_form.Field(
"text-single",
"data_retention",
"0",
),
),
),
@ -1515,7 +1504,6 @@ class EmailGatewayHandler(XMPPHandler):
"text-multi",
"extra_info",
"This gateway acts as a relay to external IMAP/SMTP servers. Data policies depend entirely on the external server chosen by the user. This gateway does not store or process user data.",
),
),
),

View file

@ -22,7 +22,7 @@ from twisted.mail import imap4
class Credentials(BaseModel):
model_config = ConfigDict(extra='forbid')
model_config = ConfigDict(extra="forbid")
user_name: str
user_email: str
smtp_host: str
@ -36,7 +36,6 @@ class Credentials(BaseModel):
imap_password: str
@dataclass
class UserData:
credentials: Credentials

View file

@ -31,8 +31,14 @@ from libervia.backend.core.log import getLogger
from libervia.backend.memory.sqla_mapping import AccessModel, Affiliation
from libervia.backend.plugins.plugin_pubsub_cache import PubsubCache
from libervia.backend.plugins.plugin_xep_0498 import NodeData
from libervia.backend.plugins.plugin_xep_0499 import ExtDiscoOptions, ExtDiscoResult, DiscoPubsubItem, DiscoPubsubNode
from libervia.backend.plugins.plugin_xep_0499 import (
ExtDiscoOptions,
ExtDiscoResult,
DiscoPubsubItem,
DiscoPubsubNode,
)
from libervia.backend.tools.utils import ensure_deferred
if TYPE_CHECKING:
from . import EmailGatewayComponent
@ -77,10 +83,7 @@ class EmailGWPubsubResource(pubsub.PubSubResource):
return defer.ensureDeferred(self.get_nodes(requestor, service, nodeIdentifier))
async def get_nodes(
self,
requestor: jid.JID,
service: jid.JID,
node_id: str
self, requestor: jid.JID, service: jid.JID, node_id: str
) -> list[str]:
nodes = await G.storage.get_pubsub_nodes(self.client, self.client.jid)
return [self._pfs.namespace] + [cast(str, node.name) for node in nodes]
@ -103,10 +106,8 @@ class EmailGWPubsubResource(pubsub.PubSubResource):
return node_data.to_elements(), None
async def items_from_mailing_list(
self,
request: rsm.PubSubRequest,
requestor_jid: jid.JID
) -> tuple[list[domish.Element], rsm.RSMResponse|None]:
self, request: rsm.PubSubRequest, requestor_jid: jid.JID
) -> tuple[list[domish.Element], rsm.RSMResponse | None]:
"""Handle items coming from mailing lists.
@param request: Pubsub request.
@ -118,10 +119,7 @@ class EmailGWPubsubResource(pubsub.PubSubResource):
"""
node = request.nodeIdentifier
node = await G.storage.get_pubsub_node(
self.client,
self.client.jid,
node,
with_affiliations=True
self.client, self.client.jid, node, with_affiliations=True
)
if node is None:
raise error.StanzaError("item-not-found")
@ -133,9 +131,8 @@ class EmailGWPubsubResource(pubsub.PubSubResource):
for affiliation in node.affiliations:
if (
affiliation.entity == requestor_jid
and affiliation.affiliation in {
Affiliation.owner, Affiliation.publisher, Affiliation.member
}
and affiliation.affiliation
in {Affiliation.owner, Affiliation.publisher, Affiliation.member}
):
break
else:
@ -151,7 +148,7 @@ class EmailGWPubsubResource(pubsub.PubSubResource):
request.maxItems,
request.itemIdentifiers,
request.subscriptionIdentifier,
request.rsm
request.rsm,
)
if rsm_data := metadata.get("rsm"):
rsm_response = rsm.RSMResponse(**rsm_data)
@ -159,7 +156,7 @@ class EmailGWPubsubResource(pubsub.PubSubResource):
rsm_response = None
return (
[cast(domish.Element, ps_item.data) for ps_item in pubsub_items],
rsm_response
rsm_response,
)
@ensure_deferred
@ -241,7 +238,7 @@ class EmailGWPubsubService(rsm.PubSubService):
self,
client: SatXMPPComponent,
iq_elt: domish.Element,
ext_disco_options: ExtDiscoOptions
ext_disco_options: ExtDiscoOptions,
) -> ExtDiscoResult:
"""Handle pubsub extended disco"""
query_elt = iq_elt.query
@ -253,25 +250,26 @@ class EmailGWPubsubService(rsm.PubSubService):
nodes = await G.storage.get_pubsub_nodes(
client,
service=client.jid,
node_name = node,
node_name=node,
linked_node=ext_disco_options.linked_nodes,
depth=depth,
with_items="items" in ext_disco_options.type
with_items="items" in ext_disco_options.type,
)
if depth == 0:
# If depth == 0, we don't want to use `from_sqlalchemy_full_hierarchy` to
# avoid returning children, and because children `parent` and `child_nodes`
# fields are not loaded.
return ExtDiscoResult([
DiscoPubsubNode.from_sqlalchemy(node, self.client.jid)
for node in nodes
])
return ExtDiscoResult(
[DiscoPubsubNode.from_sqlalchemy(node, self.client.jid) for node in nodes]
)
else:
return ExtDiscoResult([
DiscoPubsubNode.from_sqlalchemy_full_hierarchy(node, self.client.jid)
for node in nodes
])
return ExtDiscoResult(
[
DiscoPubsubNode.from_sqlalchemy_full_hierarchy(node, self.client.jid)
for node in nodes
]
)
@ensure_deferred
async def getDiscoInfo(