comparison libervia/backend/plugins/plugin_xep_0199.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_xep_0199.py@524856bd7b19
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT plugin for Delayed Delivery (XEP-0199)
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 from libervia.backend.core.i18n import _, D_
22 from libervia.backend.core.log import getLogger
23
24 log = getLogger(__name__)
25 from libervia.backend.core.constants import Const as C
26 from wokkel import disco, iwokkel
27 from twisted.words.protocols.jabber import xmlstream, jid
28 from zope.interface import implementer
29 import time
30
31
32 PLUGIN_INFO = {
33 C.PI_NAME: "XMPP PING",
34 C.PI_IMPORT_NAME: "XEP-0199",
35 C.PI_TYPE: "XEP",
36 C.PI_PROTOCOLS: ["XEP-199"],
37 C.PI_MAIN: "XEP_0199",
38 C.PI_HANDLER: "yes",
39 C.PI_DESCRIPTION: D_("""Implementation of XMPP Ping"""),
40 }
41
42 NS_PING = "urn:xmpp:ping"
43 PING_REQUEST = C.IQ_GET + '/ping[@xmlns="' + NS_PING + '"]'
44
45
46 class XEP_0199(object):
47
48 def __init__(self, host):
49 log.info(_("XMPP Ping plugin initialization"))
50 self.host = host
51 host.bridge.add_method(
52 "ping", ".plugin", in_sign='ss', out_sign='d', method=self._ping, async_=True)
53 try:
54 self.text_cmds = self.host.plugins[C.TEXT_CMDS]
55 except KeyError:
56 log.info(_("Text commands not available"))
57 else:
58 self.text_cmds.register_text_commands(self)
59
60 def get_handler(self, client):
61 return XEP_0199_handler(self)
62
63 def _ping_raise_if_failure(self, pong):
64 """If ping didn't succeed, raise the failure, else return pong delay"""
65 if pong[0] != "PONG":
66 raise pong[0]
67 return pong[1]
68
69 def _ping(self, jid_s, profile):
70 client = self.host.get_client(profile)
71 entity_jid = jid.JID(jid_s)
72 d = self.ping(client, entity_jid)
73 d.addCallback(self._ping_raise_if_failure)
74 return d
75
76 def _ping_cb(self, iq_result, send_time):
77 receive_time = time.time()
78 return ("PONG", receive_time - send_time)
79
80 def _ping_eb(self, failure_, send_time):
81 receive_time = time.time()
82 return (failure_.value, receive_time - send_time)
83
84 def ping(self, client, entity_jid):
85 """Ping an XMPP entity
86
87 @param entity_jid(jid.JID): entity to ping
88 @return (tuple[(unicode,failure), float]): pong data:
89 - either u"PONG" if it was successful, or failure
90 - delay between sending time and reception time
91 """
92 iq_elt = client.IQ("get")
93 iq_elt["to"] = entity_jid.full()
94 iq_elt.addElement((NS_PING, "ping"))
95 d = iq_elt.send()
96 send_time = time.time()
97 d.addCallback(self._ping_cb, send_time)
98 d.addErrback(self._ping_eb, send_time)
99 return d
100
101 def _cmd_ping_fb(self, pong, client, mess_data):
102 """Send feedback to client when pong data is received"""
103 txt_cmd = self.host.plugins[C.TEXT_CMDS]
104
105 if pong[0] == "PONG":
106 txt_cmd.feed_back(client, "PONG ({time} s)".format(time=pong[1]), mess_data)
107 else:
108 txt_cmd.feed_back(
109 client, _("ping error ({err_msg}). Response time: {time} s")
110 .format(err_msg=pong[0], time=pong[1]), mess_data)
111
112 def cmd_ping(self, client, mess_data):
113 """ping an entity
114
115 @command (all): [JID]
116 - JID: jid of the entity to ping
117 """
118 if mess_data["unparsed"].strip():
119 try:
120 entity_jid = jid.JID(mess_data["unparsed"].strip())
121 except RuntimeError:
122 txt_cmd = self.host.plugins[C.TEXT_CMDS]
123 txt_cmd.feed_back(client, _('Invalid jid: "{entity_jid}"').format(
124 entity_jid=mess_data["unparsed"].strip()), mess_data)
125 return False
126 else:
127 entity_jid = mess_data["to"]
128 d = self.ping(client, entity_jid)
129 d.addCallback(self._cmd_ping_fb, client, mess_data)
130
131 return False
132
133 def on_ping_request(self, iq_elt, client):
134 log.info(_("XMPP PING received from {from_jid} [{profile}]").format(
135 from_jid=iq_elt["from"], profile=client.profile))
136 iq_elt.handled = True
137 iq_result_elt = xmlstream.toResponse(iq_elt, "result")
138 client.send(iq_result_elt)
139
140
141 @implementer(iwokkel.IDisco)
142 class XEP_0199_handler(xmlstream.XMPPHandler):
143
144 def __init__(self, plugin_parent):
145 self.plugin_parent = plugin_parent
146
147 def connectionInitialized(self):
148 self.xmlstream.addObserver(
149 PING_REQUEST, self.plugin_parent.on_ping_request, client=self.parent
150 )
151
152 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
153 return [disco.DiscoFeature(NS_PING)]
154
155 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
156 return []