annotate libervia/backend/plugins/plugin_xep_0433.py @ 4360:5ea4f5f28082

plugin XEP-0433: Extended Channel Search implementation: Implements client part of XEP-0433, and add its results to plugin JID Search.
author Goffi <goffi@goffi.org>
date Fri, 11 Apr 2025 18:19:28 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4360
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin for Extended Channel Search (XEP-0433)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 import difflib
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from typing import Any, Final, Iterator, Self, cast
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from pydantic import BaseModel, Field, ConfigDict, RootModel, model_validator
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.internet import defer
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import jid
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import data_form
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from libervia.backend.core import exceptions
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from libervia.backend.core.constants import Const as C
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from libervia.backend.core.core_types import SatXMPPEntity
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from libervia.backend.core.i18n import _
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from libervia.backend.core.log import getLogger
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from libervia.backend.models.types import JIDType
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from libervia.backend.plugins import plugin_misc_jid_search
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from libervia.backend.plugins.plugin_xep_0059 import RSMRequest
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 log = getLogger(__name__)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 # Namespaces
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 NS_CHANNEL_SEARCH: Final[str] = "urn:xmpp:channel-search:0"
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 NS_SEARCH: Final[str] = f"{NS_CHANNEL_SEARCH}:search"
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 NS_SEARCH_PARAMS: Final[str] = f"{NS_CHANNEL_SEARCH}:search-params"
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 NS_ORDER: Final[str] = f"{NS_CHANNEL_SEARCH}:order"
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 NS_ERROR: Final[str] = f"{NS_CHANNEL_SEARCH}:error"
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 # Common sort keys
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 SORT_ADDRESS: Final[str] = f"{{{NS_ORDER}}}address"
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 SORT_NUSERS: Final[str] = f"{{{NS_ORDER}}}nusers"
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 PLUGIN_INFO = {
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_NAME: "Extended Channel Search",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 C.PI_IMPORT_NAME: "XEP-0433",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 C.PI_TYPE: "XEP",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 C.PI_MODES: C.PLUG_MODE_BOTH,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 C.PI_DEPENDENCIES: ["XEP-0059", "JID_SEARCH"],
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 C.PI_RECOMMENDATIONS: [],
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 C.PI_MAIN: "XEP_0433",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 C.PI_HANDLER: "no",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 C.PI_DESCRIPTION: _("Cross-domain search for public group chats"),
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 }
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 class SearchRequest(BaseModel):
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 """Parameters for channel search request."""
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 model_config = ConfigDict(extra="forbid")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 query: str | None = Field(None, alias="q")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 all: bool = False
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 sinname: bool = True
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 sindescription: bool = True
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 sinaddr: bool = True
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 min_users: int | None = Field(default=None, ge=0)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 types: list[str] = []
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 key: str = SORT_ADDRESS
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 rsm: RSMRequest | None = None
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
75
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 @model_validator(mode="after")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def check_conflicts(self) -> Self:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 if self.all and self.query:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 raise ValueError('Cannot combine "all" with search query')
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 return self
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 @classmethod
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 def from_element(cls, element: domish.Element) -> Self:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 """Parse from XMPP data form element."""
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 form = data_form.Form.fromElement(element)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 if form.formNamespace != NS_SEARCH_PARAMS:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 raise ValueError("Invalid FORM_TYPE")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
88
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 kwargs = {}
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 if "q" in form:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 kwargs["query"] = form["q"]
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 if "all" in form:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 kwargs["all"] = form["all"]
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 if "min_users" in form:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 try:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 kwargs["min_users"] = int(form["min_users"])
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 except ValueError:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 raise ValueError("Invalid min_users value")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 for field in ["sinname", "sindescription", "sinaddr", "types", "key"]:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 if field in form:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 kwargs[field] = form[field]
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 return cls(**kwargs)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 def to_form(self) -> data_form.Form:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 """Convert to "submit" data form"""
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 form = data_form.Form("submit", formNamespace=NS_SEARCH_PARAMS)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 # Add fields with original XML field names
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 if self.query is not None:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 form.addField(data_form.Field(var="q", value=self.query))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 if self.all:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 form.addField(data_form.Field("boolean", "all", value=True))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 if not self.sinname:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 form.addField(data_form.Field("boolean", "sinname", value=False))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if not self.sindescription:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 form.addField(data_form.Field("boolean", "sindescription", value=False))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 if not self.sinaddr:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 form.addField(data_form.Field("boolean", "sinaddr", value=False))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 if self.min_users is not None:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 form.addField(data_form.Field(var="min_users", value=str(self.min_users)))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 if self.types:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 form.addField(data_form.Field("list-multi", "types", values=self.types))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 if self.key != SORT_ADDRESS:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 form.addField(data_form.Field("list-single", "key", value=self.key))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 return form
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 def to_element(self) -> domish.Element:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 """Convert to XMPP data form submission."""
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 form = self.to_form()
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 search_elt = domish.Element((NS_SEARCH, "search"))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 search_elt.addChild(form.toElement())
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 if self.rsm is not None:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 search_elt.addChild(self.rsm.to_element())
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 return search_elt
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 class SearchItem(BaseModel):
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 """Represents a single channel search result."""
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 address: JIDType
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 name: str | None = None
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 description: str | None = None
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 language: str | None = None
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 nusers: int | None = Field(default=None, ge=0)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 service_type: str | None = None
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 is_open: bool | None = None
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 anonymity_mode: str | None = None
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 @classmethod
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 def from_element(cls, element: domish.Element) -> Self:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 """Parse from <item> element."""
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 if not (element.name == "item" and element.uri == NS_SEARCH):
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 raise ValueError("Invalid channel item element")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 address = element.getAttribute("address")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 if not address:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 raise ValueError("Missing required address attribute")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 data: dict[str, Any] = {"address": jid.JID(address)}
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 for child in element.elements():
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176 if child.uri != NS_SEARCH:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 continue
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 content = str(child)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 match (name := child.name.replace("-", "_")):
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 case "nusers":
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 data[name] = int(content)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 case "is_open":
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 data[name] = content.lower() == "true"
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 case "service_type" | "anonymity_mode" if content:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 data[name] = content
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 case _:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 data[name] = content
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 return cls(**data)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 def to_element(self) -> domish.Element:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 """Convert to <item> element."""
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 item = domish.Element((NS_SEARCH, "item"))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 item["address"] = str(self.address)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
196
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 field_mappings = {
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 "name": "name",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 "description": "description",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 "language": "language",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 "nusers": "nusers",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 "service_type": "service-type",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 "anonymity_mode": "anonymity-mode",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 }
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
205
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 for field, element_name in field_mappings.items():
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
207 value = getattr(self, field)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 if value is not None:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 elem = item.addElement(element_name, NS_SEARCH)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 elem.addContent(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 str(value).lower() if isinstance(value, bool) else str(value)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
213
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 if self.is_open is not None:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 item.addElement(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 ("is-open", NS_SEARCH), content="true" if self.is_open else "false"
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
218
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 return item
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
220
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
221
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 class SearchItems(RootModel):
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 root: list[SearchItem]
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 def __iter__(self) -> Iterator[SearchItem]: # type: ignore
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 return iter(self.root)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
227
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
228 def __getitem__(self, item) -> str:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 return self.root[item]
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
230
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 def __len__(self) -> int:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 return len(self.root)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
233
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
234 def append(self, item: SearchItem) -> None:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 self.root.append(item)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
236
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 def sort(self, key=None, reverse=False) -> None:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 self.root.sort(key=key, reverse=reverse) # type: ignore
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
239
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
240 @classmethod
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 def from_element(cls, element: domish.Element) -> Self:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 if element.name == "result" and element.uri == NS_SEARCH:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 result_elt = element
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 else:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 try:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
246 result_elt = next(element.elements(NS_SEARCH, "result"))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 except StopIteration:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 raise exceptions.NotFound("No <result> element found.")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
249 items = []
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 for item_elt in result_elt.elements(NS_SEARCH, "item"):
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 items.append(SearchItem.from_element(item_elt))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 return cls(items)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
253
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 def to_element(self) -> domish.Element:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 result_elt = domish.Element((NS_SEARCH, "result"))
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 for search_item in self.root:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 result_elt.addChild(search_item.to_element())
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 return result_elt
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
259
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
260
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 class XEP_0433:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
262 """Implementation of XEP-0433 Extended Channel Search."""
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
263
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
264 namespace: Final[str] = NS_CHANNEL_SEARCH
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
265
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 def __init__(self, host: Any):
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
267 log.info(f"Plugin {PLUGIN_INFO[C.PI_NAME]!r} initialization.")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 self.host = host
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 host.trigger.add(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
270 "JID_SEARCH_perform_search", self.jid_search_perform_search_trigger
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 self.allow_external = C.bool(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 host.memory.config_get(None, "allow_external_search", C.BOOL_FALSE)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
274 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
275 self.group_chat_search_default_jid = jid.JID(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
276 host.memory.config_get(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
277 None, "group_chat_search_default_jid", "api@search.jabber.network"
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
278 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
279 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
280
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
281 host.bridge.add_method(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
282 "extended_search_request",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 ".plugin",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 in_sign="sss",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
285 out_sign="s",
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
286 method=self._search,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
287 async_=True,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
289
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
290 async def jid_search_perform_search_trigger(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
291 self,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
292 client: SatXMPPEntity,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 search_term: str,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
294 options: plugin_misc_jid_search.Options,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 sequence_matcher: difflib.SequenceMatcher,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
296 matches: plugin_misc_jid_search.SearchItems,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 ) -> bool:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
298 if options.groupchat and self.allow_external:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
299 log.debug(f"Search {search_term!r} at {self.group_chat_search_default_jid}.")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
300 try:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 external_items = await self.search(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
302 client,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 self.group_chat_search_default_jid,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
304 SearchRequest(q=search_term),
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
305 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 except Exception as e:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
307 log.warning(f"Can't do external search: {e}.")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
308 return True
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
309 for search_item in external_items:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
310 room_search_item = plugin_misc_jid_search.RoomSearchItem(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
311 entity=search_item.address,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
312 name=(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
313 search_item.name
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
314 or search_item.address.user
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
315 or search_item.address.full()
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
316 ),
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
317 local=False,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
318 service_type=search_item.service_type,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
319 is_open=search_item.is_open,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
320 anonymity_mode=search_item.anonymity_mode,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
321 description=search_item.description,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
322 language=search_item.language,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
323 nusers=search_item.nusers,
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
324 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
325 matches.append(room_search_item)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
326 return True
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
327
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
328 def _search(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
329 self, target: str, search_request: str, profile: str
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
330 ) -> defer.Deferred[str]:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
331 client = self.host.get_client(profile)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
332 d = defer.ensureDeferred(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
333 self.search(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
334 client, jid.JID(target), SearchRequest.model_validate_json(search_request)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
335 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
336 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
337 d.addCallback(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
338 lambda search_items: search_items.model_dump_json(exclude_none=True)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
339 )
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
340 d = cast(defer.Deferred[str], d)
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
341 return d
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
342
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
343 async def search(
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
344 self, client: SatXMPPEntity, target: jid.JID, search_request: SearchRequest
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
345 ) -> SearchItems:
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
346 """Do a Search"""
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 iq_elt = client.IQ("get")
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 iq_elt.addChild(search_request.to_element())
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
349 iq_result_elt = await iq_elt.send(target.full())
5ea4f5f28082 plugin XEP-0433: Extended Channel Search implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
350 return SearchItems.from_element(iq_result_elt)