Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0191.py @ 3968:0dd79c6cc1d2
tools (strem): fix `mode` + add `pre_close_cb` callback:
rel 378
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 31 Oct 2022 04:04:32 +0100 |
parents | 24fbc4cad534 |
children | 524856bd7b19 |
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 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from sat.core.constants import Const as C |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from sat.core.i18n import _ |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from sat.core.log import getLogger |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from sat.core.core_types import SatXMPPEntity |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from sat.tools.utils import ensure_deferred |
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")) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 host.registerNamespace("blocking", NS_BLOCKING) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 self.host = host |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 host.bridge.addMethod( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 "blockingList", |
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", |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 method=self._blockList, |
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 ) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 host.bridge.addMethod( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 "blockingBlock", |
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 ) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 host.bridge.addMethod( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 "blockingUnblock", |
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 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 def getHandler(self, client): |
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 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 async def _blockList( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 self, |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 profile_key=C.PROF_KEY_NONE |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 ) -> List[str]: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 client = self.host.getClient(profile_key) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 blocked_jids = await self.blockList(client) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 return [j.full() for j in blocked_jids] |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 async def blockList(self, client: SatXMPPEntity) -> Set[jid.JID]: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 await self.host.checkFeature(client, NS_BLOCKING) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 iq_elt = client.IQ("get") |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 iq_elt.addElement((NS_BLOCKING, "blocklist")) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 iq_result_elt = await iq_elt.send() |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 try: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 blocklist_elt = next(iq_result_elt.elements(NS_BLOCKING, "blocklist")) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 except StopIteration: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 log.warning(f"missing <blocklist> element: {iq_result_elt.toXml()}") |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 return [] |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 blocked_jids = set() |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 for item_elt in blocklist_elt.elements(NS_BLOCKING, "item"): |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 try: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 blocked_jid = jid.JID(item_elt["jid"]) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 except (RuntimeError, AttributeError): |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 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
|
111 else: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 blocked_jids.add(blocked_jid) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 return blocked_jids |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 def _block( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 self, |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 entities: List[str], |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 profile_key: str = C.PROF_KEY_NONE |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 ) -> str: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 client = self.host.getClient(profile_key) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 return defer.ensureDeferred( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 self.block(client, [jid.JID(entity) for entity in entities]) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 ) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 async def block(self, client: SatXMPPEntity, entities: List[jid.JID]) -> None: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 await self.host.checkFeature(client, NS_BLOCKING) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 iq_elt = client.IQ("set") |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 block_elt = iq_elt.addElement((NS_BLOCKING, "block")) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 for entity in entities: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 item_elt = block_elt.addElement("item") |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 item_elt["jid"] = entity.full() |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 await iq_elt.send() |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 def _unblock( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 self, |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 entities: List[str], |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 profile_key: str = C.PROF_KEY_NONE |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 ) -> None: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 client = self.host.getClient(profile_key) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 return defer.ensureDeferred( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 self.unblock(client, [jid.JID(e) for e in entities]) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 ) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 async def unblock(self, client: SatXMPPEntity, entities: List[jid.JID]) -> None: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 await self.host.checkFeature(client, NS_BLOCKING) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 iq_elt = client.IQ("set") |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 unblock_elt = iq_elt.addElement((NS_BLOCKING, "unblock")) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 for entity in entities: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 item_elt = unblock_elt.addElement("item") |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 item_elt["jid"] = entity.full() |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 await iq_elt.send() |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 def onBlockPush(self, iq_elt: domish.Element, client: SatXMPPEntity) -> None: |
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 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
|
158 try: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 entity = jid.JID(item_elt["jid"]) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 except (KeyError, RuntimeError): |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 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
|
162 else: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 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
|
164 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 client.send(iq_result_elt) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 def onUnblockPush(self, iq_elt: domish.Element, client: SatXMPPEntity) -> None: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 # TODO: send notification to user |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 iq_elt.handled = True |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 items = list(iq_elt.unblock.elements(NS_BLOCKING, "item")) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 if not items: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 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
|
173 else: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 for item_elt in items: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 try: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 entity = jid.JID(item_elt["jid"]) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 except (KeyError, RuntimeError): |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 log.warning( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 f"invalid item received in unblock push: {item_elt.toXml()}" |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 ) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 else: |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 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
|
183 iq_result_elt = xmlstream.toResponse(iq_elt, "result") |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 client.send(iq_result_elt) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 |
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 @implementer(iwokkel.IDisco) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 class XEP_0191_Handler(xmlstream.XMPPHandler): |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 def __init__(self, plugin_parent: XEP_0191): |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 self.plugin_parent = plugin_parent |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 def connectionInitialized(self): |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 self.xmlstream.addObserver( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 IQ_BLOCK_PUSH, |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 self.plugin_parent.onBlockPush, |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 client=self.parent |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 ) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 self.xmlstream.addObserver( |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 IQ_UNBLOCK_PUSH, |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 self.plugin_parent.onUnblockPush, |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 client=self.parent |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 ) |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 def getDiscoInfo(self, requestor, service, nodeIdentifier=""): |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 return [disco.DiscoFeature(NS_BLOCKING)] |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 def getDiscoItems(self, requestor, service, nodeIdentifier=""): |
f8a0f3b65371
plugin XEP-0191: Blocking Command implementation:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 return [] |