3904
|
1 #!/usr/bin/env python3 |
|
2 |
|
3 # Libervia ActivityPub Gateway |
|
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
|
5 |
|
6 # This program is free software: you can redistribute it and/or modify |
|
7 # it under the terms of the GNU Affero General Public License as published by |
|
8 # the Free Software Foundation, either version 3 of the License, or |
|
9 # (at your option) any later version. |
|
10 |
|
11 # This program is distributed in the hope that it will be useful, |
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 # GNU Affero General Public License for more details. |
|
15 |
|
16 # You should have received a copy of the GNU Affero General Public License |
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
18 |
|
19 from twisted.words.protocols.jabber import jid |
|
20 from twisted.words.xish import domish |
|
21 from wokkel import data_form |
|
22 |
|
23 from sat.core.constants import Const as C |
|
24 from sat.core.core_types import SatXMPPEntity |
|
25 from sat.core.i18n import _ |
|
26 from sat.core.log import getLogger |
|
27 |
|
28 |
|
29 log = getLogger(__name__) |
|
30 NS_XMPP_JID_NODE_2_AP = "https://libervia.org/ap_gateway/xmpp_jid_node_2_ap_actor" |
|
31 |
|
32 class APAdHocService: |
|
33 """Ad-Hoc commands for AP Gateway""" |
|
34 |
|
35 def __init__(self, apg): |
|
36 self.host = apg.host |
|
37 self.apg = apg |
|
38 self._c = self.host.plugins["XEP-0050"] |
|
39 |
|
40 def init(self, client: SatXMPPEntity) -> None: |
|
41 self._c.addAdHocCommand( |
|
42 client, |
|
43 self.xmpp_jid_node_2_ap_actor, |
|
44 "Convert XMPP JID/Node to AP actor", |
|
45 node=NS_XMPP_JID_NODE_2_AP, |
|
46 allowed_magics=C.ENTITY_ALL, |
|
47 ) |
|
48 |
|
49 async def xmpp_jid_node_2_ap_actor( |
|
50 self, |
|
51 client: SatXMPPEntity, |
|
52 command_elt: domish.Element, |
|
53 session_data: dict, |
|
54 action: str, |
|
55 node: str |
|
56 ): |
|
57 try: |
|
58 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) |
|
59 command_form = data_form.Form.fromElement(x_elt) |
|
60 except StopIteration: |
|
61 command_form = None |
|
62 if command_form is None or len(command_form.fields) == 0: |
|
63 # root request |
|
64 status = self._c.STATUS.EXECUTING |
|
65 form = data_form.Form( |
|
66 "form", title="XMPP JID/node to AP actor conversion", |
|
67 formNamespace=NS_XMPP_JID_NODE_2_AP |
|
68 ) |
|
69 |
|
70 field = data_form.Field( |
|
71 "text-single", "jid", required=True |
|
72 ) |
|
73 form.addField(field) |
|
74 |
|
75 field = data_form.Field( |
|
76 "text-single", "node", required=False |
|
77 ) |
|
78 form.addField(field) |
|
79 |
|
80 payload = form.toElement() |
|
81 return payload, status, None, None |
|
82 else: |
|
83 xmpp_jid = jid.JID(command_form["jid"]) |
|
84 xmpp_node = command_form.get("node") |
|
85 actor = await self.apg.getAPAccountFromJidAndNode(xmpp_jid, xmpp_node) |
|
86 note = (self._c.NOTE.INFO, actor) |
|
87 status = self._c.STATUS.COMPLETED |
|
88 payload = None |
|
89 return (payload, status, None, note) |