Mercurial > libervia-backend
comparison sat/plugins/plugin_xep_0199.py @ 2689:d715d912afac
plugin XEP-0199: implementation of XMPP Ping
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 10 Nov 2018 10:16:38 +0100 |
parents | |
children | 003b8b4b56a7 |
comparison
equal
deleted
inserted
replaced
2688:943e78e18882 | 2689:d715d912afac |
---|---|
1 #!/usr/bin/env python2 | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for Delayed Delivery (XEP-0199) | |
5 # Copyright (C) 2009-2018 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 sat.core.i18n import _, D_ | |
22 from sat.core.log import getLogger | |
23 | |
24 log = getLogger(__name__) | |
25 from sat.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 implements | |
29 import time | |
30 | |
31 | |
32 PLUGIN_INFO = { | |
33 C.PI_NAME: u"XMPP PING", | |
34 C.PI_IMPORT_NAME: u"XEP-0199", | |
35 C.PI_TYPE: u"XEP", | |
36 C.PI_PROTOCOLS: [u"XEP-199"], | |
37 C.PI_MAIN: "XEP_0199", | |
38 C.PI_HANDLER: u"yes", | |
39 C.PI_DESCRIPTION: D_(u"""Implementation of XMPP Ping"""), | |
40 } | |
41 | |
42 NS_PING = u"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.addMethod( | |
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(_(u"Text commands not available")) | |
57 else: | |
58 self.text_cmds.registerTextCommands(self) | |
59 | |
60 def getHandler(self, client): | |
61 return XEP_0199_handler(self) | |
62 | |
63 def _pingRaiseIfFailure(self, pong): | |
64 """If ping didn't succeed, raise the failure, else return pong delay""" | |
65 if pong[0] != u"PONG": | |
66 raise pong[0] | |
67 return pong[1] | |
68 | |
69 def _ping(self, jid_s, profile): | |
70 client = self.host.getClient(profile) | |
71 entity_jid = jid.JID(jid_s) | |
72 d = self.ping(client, entity_jid) | |
73 d.addCallback(self._pingRaiseIfFailure) | |
74 return d | |
75 | |
76 def _pingCb(self, iq_result, send_time): | |
77 receive_time = time.time() | |
78 return (u"PONG", receive_time - send_time) | |
79 | |
80 def _pingEb(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._pingCb, send_time) | |
98 d.addErrback(self._pingEb, 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] == u"PONG": | |
106 txt_cmd.feedBack(client, u"PONG ({time} s)".format(time=pong[1]), mess_data) | |
107 else: | |
108 txt_cmd.feedBack( | |
109 client, _(u"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.feedBack(client, _(u'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 onPingRequest(self, iq_elt, client): | |
134 log.info(_(u"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 class XEP_0199_handler(xmlstream.XMPPHandler): | |
142 implements(iwokkel.IDisco) | |
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.onPingRequest, 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 [] |