annotate libervia/backend/plugins/plugin_xep_0191.py @ 4329:73d83cb53673

plugin blog import dotclear: repalce deprecated `cgi` by `html`
author Goffi <goffi@goffi.org>
date Wed, 20 Nov 2024 14:35:09 +0100
parents 0d7bb4df2343
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia plugin for XEP-0191
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2022 Jérôme Poisson (goffi@goffi.org)
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from typing import List, Set
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.words.protocols.jabber import xmlstream, jid
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.xish import domish
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.internet import defer
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from zope.interface import implementer
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from wokkel import disco, iwokkel
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
27 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 from libervia.backend.core.i18n import _
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
29 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
30 from libervia.backend.core.core_types import SatXMPPEntity
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
31 from libervia.backend.tools.utils import ensure_deferred
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 log = getLogger(__name__)
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 PLUGIN_INFO = {
3811
24fbc4cad534 plugin XEP-0191: fix wrong name and description
Goffi <goffi@goffi.org>
parents: 3787
diff changeset
36 C.PI_NAME: "Blokcing Commands",
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_IMPORT_NAME: "XEP-0191",
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_TYPE: C.PLUG_TYPE_XEP,
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_MODES: C.PLUG_MODE_BOTH,
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_PROTOCOLS: ["XEP-0191"],
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_DEPENDENCIES: ["XEP-0060", "XEP-0376"],
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_MAIN: "XEP_0191",
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_HANDLER: "yes",
3811
24fbc4cad534 plugin XEP-0191: fix wrong name and description
Goffi <goffi@goffi.org>
parents: 3787
diff changeset
44 C.PI_DESCRIPTION: _("""Implement the protocol to block users or whole domains"""),
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 }
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 NS_BLOCKING = "urn:xmpp:blocking"
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 IQ_BLOCK_PUSH = f'{C.IQ_SET}/block[@xmlns="{NS_BLOCKING}"]'
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 IQ_UNBLOCK_PUSH = f'{C.IQ_SET}/unblock[@xmlns="{NS_BLOCKING}"]'
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 class XEP_0191:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def __init__(self, host):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 log.info(_("Blocking Command initialization"))
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
56 host.register_namespace("blocking", NS_BLOCKING)
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.host = host
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
58 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
59 "blocking_list",
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 ".plugin",
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 in_sign="s",
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 out_sign="as",
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
63 method=self._block_list,
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 async_=True,
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
66 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
67 "blocking_block",
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 ".plugin",
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 in_sign="ass",
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 out_sign="",
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 method=self._block,
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 async_=True,
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
74 host.bridge.add_method(
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
75 "blocking_unblock",
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 ".plugin",
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 in_sign="ass",
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 out_sign="",
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 method=self._unblock,
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 async_=True,
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 )
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
83 def get_handler(self, client):
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return XEP_0191_Handler(self)
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 @ensure_deferred
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
87 async def _block_list(self, profile_key=C.PROF_KEY_NONE) -> List[str]:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
88 client = self.host.get_client(profile_key)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
89 blocked_jids = await self.block_list(client)
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 return [j.full() for j in blocked_jids]
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
91
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
92 async def block_list(self, client: SatXMPPEntity) -> Set[jid.JID]:
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
93 await self.host.check_feature(client, NS_BLOCKING)
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 iq_elt = client.IQ("get")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 iq_elt.addElement((NS_BLOCKING, "blocklist"))
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 iq_result_elt = await iq_elt.send()
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 try:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 blocklist_elt = next(iq_result_elt.elements(NS_BLOCKING, "blocklist"))
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 except StopIteration:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 log.warning(f"missing <blocklist> element: {iq_result_elt.toXml()}")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 return []
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 blocked_jids = set()
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 for item_elt in blocklist_elt.elements(NS_BLOCKING, "item"):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 try:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 blocked_jid = jid.JID(item_elt["jid"])
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 except (RuntimeError, AttributeError):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 log.warning(f"Invalid <item> element in block list: {item_elt.toXml()}")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 else:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 blocked_jids.add(blocked_jid)
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
110
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 return blocked_jids
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
112
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
113 def _block(self, entities: List[str], profile_key: str = C.PROF_KEY_NONE) -> str:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
114 client = self.host.get_client(profile_key)
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 return defer.ensureDeferred(
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 self.block(client, [jid.JID(entity) for entity in entities])
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 )
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
118
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 async def block(self, client: SatXMPPEntity, entities: List[jid.JID]) -> None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
120 await self.host.check_feature(client, NS_BLOCKING)
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 iq_elt = client.IQ("set")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 block_elt = iq_elt.addElement((NS_BLOCKING, "block"))
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 for entity in entities:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 item_elt = block_elt.addElement("item")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 item_elt["jid"] = entity.full()
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 await iq_elt.send()
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
127
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
128 def _unblock(self, entities: List[str], profile_key: str = C.PROF_KEY_NONE) -> None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
129 client = self.host.get_client(profile_key)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
130 return defer.ensureDeferred(self.unblock(client, [jid.JID(e) for e in entities]))
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 async def unblock(self, client: SatXMPPEntity, entities: List[jid.JID]) -> None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
133 await self.host.check_feature(client, NS_BLOCKING)
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 iq_elt = client.IQ("set")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 unblock_elt = iq_elt.addElement((NS_BLOCKING, "unblock"))
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 for entity in entities:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 item_elt = unblock_elt.addElement("item")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 item_elt["jid"] = entity.full()
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 await iq_elt.send()
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
140
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
141 def on_block_push(self, iq_elt: domish.Element, client: SatXMPPEntity) -> None:
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 # TODO: send notification to user
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 iq_elt.handled = True
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 for item_elt in iq_elt.block.elements(NS_BLOCKING, "item"):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 try:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 entity = jid.JID(item_elt["jid"])
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 except (KeyError, RuntimeError):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 log.warning(f"invalid item received in block push: {item_elt.toXml()}")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 else:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 log.info(f"{entity.full()} has been blocked for {client.profile}")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 client.send(iq_result_elt)
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
153
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 3811
diff changeset
154 def on_unblock_push(self, iq_elt: domish.Element, client: SatXMPPEntity) -> None:
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 # TODO: send notification to user
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 iq_elt.handled = True
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 items = list(iq_elt.unblock.elements(NS_BLOCKING, "item"))
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 if not items:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 log.info(f"All entities have been unblocked for {client.profile}")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 else:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 for item_elt in items:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 try:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 entity = jid.JID(item_elt["jid"])
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 except (KeyError, RuntimeError):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 log.warning(
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 f"invalid item received in unblock push: {item_elt.toXml()}"
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 )
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 else:
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 log.info(f"{entity.full()} has been unblocked for {client.profile}")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 client.send(iq_result_elt)
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
172
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
173
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 @implementer(iwokkel.IDisco)
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 class XEP_0191_Handler(xmlstream.XMPPHandler):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 def __init__(self, plugin_parent: XEP_0191):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 self.plugin_parent = plugin_parent
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
179
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 def connectionInitialized(self):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 self.xmlstream.addObserver(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
182 IQ_BLOCK_PUSH, self.plugin_parent.on_block_push, client=self.parent
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
183 )
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 self.xmlstream.addObserver(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
185 IQ_UNBLOCK_PUSH, self.plugin_parent.on_unblock_push, client=self.parent
3787
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 )
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
187
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 def getDiscoInfo(self, requestor, service, nodeIdentifier=""):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 return [disco.DiscoFeature(NS_BLOCKING)]
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
190
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 def getDiscoItems(self, requestor, service, nodeIdentifier=""):
f8a0f3b65371 plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 return []