annotate src/plugins/plugin_xep_0092.py @ 2307:8fa7edd0da24

plugin Pubsub Hook: first draft: This new plugin allow to attach an external action to a Pubsub event (i.e. notification). Hook can be persitent accross restarts, or temporary (will be deleted on profile disconnection). Only Python files are handled for now. In the future, it may make sense to move hooks in a generic plugin which could be used by ad-hoc commands, messages, pubsub, etc.
author Goffi <goffi@goffi.org>
date Wed, 05 Jul 2017 15:05:47 +0200
parents a543eda2c923
children 8b37a62336c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
3
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT plugin for Software Version (XEP-0092)
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
6
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
11
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
16
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
19
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
952
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
22 from twisted.internet import reactor, defer
971
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
23 from twisted.words.protocols.jabber import jid
945
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
24 from wokkel import compat
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sat.core import exceptions
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 971
diff changeset
26 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 971
diff changeset
27 log = getLogger(__name__)
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
28
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
29 NS_VERSION = "jabber:iq:version"
952
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
30 TIMEOUT = 10
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
31
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
32 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
33 C.PI_NAME: "Software Version Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
34 C.PI_IMPORT_NAME: "XEP-0092",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
35 C.PI_TYPE: "XEP",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
36 C.PI_PROTOCOLS: ["XEP-0092"],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
37 C.PI_DEPENDENCIES: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
38 C.PI_RECOMMENDATIONS: [C.TEXT_CMDS],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
39 C.PI_MAIN: "XEP_0092",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
40 C.PI_HANDLER: "no", # version is already handler in core.xmpp module
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1963
diff changeset
41 C.PI_DESCRIPTION: _("""Implementation of Software Version""")
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
42 }
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
43
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
44
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
45 class XEP_0092(object):
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
46
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 971
diff changeset
48 log.info(_("Plugin XEP_0092 initialization"))
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
49 self.host = host
971
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
50 host.bridge.addMethod("getSoftwareVersion", ".plugin", in_sign='ss', out_sign='(sss)', method=self._getVersion, async=True)
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
51 try:
928
73873e9b56f7 plugin XEP-0045: added user information to /whois text command
Goffi <goffi@goffi.org>
parents: 926
diff changeset
52 self.host.plugins[C.TEXT_CMDS].addWhoIsCb(self._whois, 50)
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
53 except KeyError:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 971
diff changeset
54 log.info(_("Text commands not available"))
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
55
971
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
56 def _getVersion(self, entity_jid_s, profile_key):
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
57 def prepareForBridge(data):
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
58 name, version, os = data
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
59 return (name or '', version or '', os or '')
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
60 d = self.getVersion(jid.JID(entity_jid_s), profile_key)
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
61 d.addCallback(prepareForBridge)
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
62 return d
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
63
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
64 def getVersion(self, jid_, profile_key=C.PROF_KEY_NONE):
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
65 """ Ask version of the client that jid_ is running
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
66 @param jid_: jid from who we want to know client's version
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
67 @param profile_key: %(doc_profile_key)s
971
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 952
diff changeset
68 @return (tuple): a defered which fire a tuple with the following data (None if not available):
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
69 - name: Natural language name of the software
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
70 - version: specific version of the software
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
71 - os: operating system of the queried entity
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
72 """
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
73 client = self.host.getClient(profile_key)
952
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
74 def getVersion(dummy):
945
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
75 iq_elt = compat.IQ(client.xmlstream, 'get')
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
76 iq_elt['to'] = jid_.full()
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
77 iq_elt.addElement("query", NS_VERSION)
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
78 d = iq_elt.send()
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
79 d.addCallback(self._gotVersion)
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
80 return d
2148
a543eda2c923 core (memory/disco): getInfos now handle node + use client instead of profile in many methods
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
81 d = self.host.checkFeature(client, NS_VERSION, jid_)
945
899dc9cd0f35 plugin XEP-0092: feature check before requesting version
Goffi <goffi@goffi.org>
parents: 941
diff changeset
82 d.addCallback(getVersion)
952
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
83 reactor.callLater(TIMEOUT, d.cancel) # XXX: timeout needed because some clients don't answer the IQ
920
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return d
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
85
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def _gotVersion(self, iq_elt):
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
87 try:
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
88 query_elt = iq_elt.elements(NS_VERSION, 'query').next()
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
89 except StopIteration:
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
90 raise exceptions.DataError
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
91 ret = []
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
92 for name in ('name', 'version', 'os'):
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
93 try:
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
94 data_elt = query_elt.elements(NS_VERSION, name).next()
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
95 ret.append(unicode(data_elt))
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
96 except StopIteration:
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
97 ret.append(None)
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
98
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
99 return tuple(ret)
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
100
45dffd67a18a plugin XEP-0092: new plugin (software version)
Goffi <goffi@goffi.org>
parents:
diff changeset
101
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
102 def _whois(self, client, whois_msg, mess_data, target_jid):
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
103 """ Add software/OS information to whois """
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
104 def versionCb(version_data):
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
105 name, version, os = version_data
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
106 if name:
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
107 whois_msg.append(_("Client name: %s") % name)
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
108 if version:
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
109 whois_msg.append(_("Client version: %s") % version)
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
110 if os:
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
111 whois_msg.append(_("Operating system: %s") % os)
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
112 def versionEb(failure):
952
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
113 failure.trap(exceptions.FeatureNotFound, defer.CancelledError)
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
114 if failure.check(failure,exceptions.FeatureNotFound):
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
115 whois_msg.append(_("Software version not available"))
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
116 else:
91836a647515 plugin XEP-0092: use of checkFeature instead of hasFeature + timeout + message adapted to failure if software version is not available
Goffi <goffi@goffi.org>
parents: 945
diff changeset
117 whois_msg.append(_("Client software version request timeout"))
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
118
1963
a2bc5089c2eb backend, frontends: message refactoring (huge commit):
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
119 d = self.getVersion(target_jid, client.profile)
926
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
120 d.addCallbacks(versionCb, versionEb)
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
121 return d
d609581bf74a plugin text commands: refactoring, text now only contain main commands, and other plugin can add commands themselve:
Goffi <goffi@goffi.org>
parents: 920
diff changeset
122