Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0060.py @ 301:e33b3a777f10
plugin xep-0060: added getEntry method
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 21 Feb 2011 01:36:52 +0100 |
parents | 7c79d4a8c9e6 |
children | 2b52a5da0978 |
rev | line source |
---|---|
283
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 SAT plugin for Publish-Subscribe (xep-0060) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 it under the terms of the GNU General Public License as published by |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 GNU General Public License for more details. |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from logging import debug, info, error |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.internet import protocol |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.words.protocols.jabber import client, jid |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.words.protocols.jabber import error as jab_error |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 import twisted.internet.error |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from wokkel import disco, iwokkel, pubsub |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from zope.interface import implements |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 PLUGIN_INFO = { |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 "name": "Publish-Subscribe", |
291 | 34 "import_name": "XEP-0060", |
283
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 "type": "XEP", |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 "protocols": ["XEP-0060"], |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 "dependencies": [], |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 "main": "XEP_0060", |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 "handler": "yes", |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 "description": _("""Implementation of PubSub Protocol""") |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 } |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 class XEP_0060(): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 def __init__(self, host): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 info(_("PubSub plugin initialization")) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 self.host = host |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 self.managedNodes=[] |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 self.clients={} |
301
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
50 """host.bridge.addMethod("getItems", ".communication", in_sign='ssa{ss}s', out_sign='as', method=self.getItems, |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
51 async = True, |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
52 doc = { 'summary':'retrieve items', |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
53 'param_0':'service: pubsub service', |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
54 'param_1':'node: node identifier', |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
55 'param_2':'\n'.join(['options: can be:', |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
56 '- max_items: see XEP-0060 #6.5.7', |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
57 '- sub_id: subscription identifier, see XEP-0060 #7.2.2.2']), |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
58 'param_3':'%(doc_profile)s', |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
59 'return':'array of raw XML (content of the items)' |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
60 })""" |
283
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 def getHandler(self, profile): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 self.clients[profile] = SatPubSubClient(self.host, self) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 return self.clients[profile] |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 def addManagedNode(self, node_name, callback): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 """Add a handler for a namespace |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 @param namespace: NS of the handler (will appear in disco info) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 @param callback: method to call when the handler is found |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 @param profile: profile which manage this handler""" |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 self.managedNodes.append((node_name, callback)) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
286
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
73 def publish(self, service, nodeIdentifier, items=None, profile_key='@DEFAULT@'): |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
74 profile = self.host.memory.getProfileName(profile_key) |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
75 if not profile: |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
76 err_mess = _('Trying to publish items with an unknown profile key [%s]') % profile_key |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
77 error(err_mess) |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
78 raise Exception(err_mess) |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
79 try: |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
80 client = self.clients[profile] |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
81 except KeyError: |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
82 err_mess = _('INTERNAL ERROR: no handler for required profile') |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
83 error(err_mess) |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
84 raise Exception(err_mess) |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
85 client.publish(service, nodeIdentifier, items, client.parent.jid) |
3b382fa0ac28
plugin xep-0163: added mood publishing
Goffi <goffi@goffi.org>
parents:
283
diff
changeset
|
86 |
301
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
87 def getItems(self, service, node, max_items=None, sub_id=None, profile_key='@DEFAULT@'): |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
88 profile = self.host.memory.getProfileName(profile_key) |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
89 if not profile: |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
90 err_mess = _('Trying to get items with an unknown profile key [%s]') % profile_key |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
91 error(err_mess) |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
92 raise Exception(err_mess) |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
93 try: |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
94 client = self.clients[profile] |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
95 except KeyError: |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
96 err_mess = _('INTERNAL ERROR: no handler for required profile') |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
97 error(err_mess) |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
98 raise Exception(err_mess) |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
99 return client.items(service, node, max_items, sub_id, client.parent.jid) |
e33b3a777f10
plugin xep-0060: added getEntry method
Goffi <goffi@goffi.org>
parents:
291
diff
changeset
|
100 |
283
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 class SatPubSubClient(pubsub.PubSubClient): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 implements(disco.IDisco) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 def __init__(self, host, parent_plugin): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 self.host=host |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 self.parent_plugin = parent_plugin |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 pubsub.PubSubClient.__init__(self) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 def connectionInitialized(self): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 pubsub.PubSubClient.connectionInitialized(self) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 def itemsReceived(self, event): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 for node in self.parent_plugin.managedNodes: |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 if event.nodeIdentifier == node[0]: |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 return node[1](event, self.parent.profile) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 def deleteReceived(self, event): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 import pdb |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 pdb.set_trace() |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 def purgeReceived(self, event): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 import pdb |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 pdb.set_trace() |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 _disco_info = [] |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 self.host.trigger.point("PubSub Disco Info", _disco_info, self.parent.profile) |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 return _disco_info |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 def getDiscoItems(self, requestor, target, nodeIdentifier=''): |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 return [] |
68cd30d982a5
core: added plugins for PubSub et PEP (first drafts)
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 |