comparison src/plugins/plugin_exp_pubsub_schema.py @ 2350:388226e9c3ff

plugin schema: PubSub node schema, first draft node schema is a new SàT Pubsub experimental feature allowing to attach a schema to a node.
author Goffi <goffi@goffi.org>
date Wed, 06 Sep 2017 07:38:39 +0200
parents
children 41c7717b52cd
comparison
equal deleted inserted replaced
2349:78c05094c349 2350:388226e9c3ff
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for Pubsub Schemas
5 # Copyright (C) 2009-2017 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core import exceptions
22 from sat.core.constants import Const as C
23 from twisted.words.protocols.jabber import jid
24 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
25 from sat.core.log import getLogger
26 log = getLogger(__name__)
27 from wokkel import disco, iwokkel
28 from wokkel import data_form
29 from wokkel import generic
30 from zope.interface import implements
31
32 NS_SCHEMA = 'https://salut-a-toi/protocol/schema:0'
33 NS_SCHEMA_FORM = 'https://salut-a-toi/protocol/schema#schema:0'
34
35 PLUGIN_INFO = {
36 C.PI_NAME: "PubSub Schema",
37 C.PI_IMPORT_NAME: "PUBSUB_SCHEMA",
38 C.PI_TYPE: "EXP",
39 C.PI_PROTOCOLS: [],
40 C.PI_DEPENDENCIES: ["XEP-0060"],
41 C.PI_MAIN: "PubsubSchema",
42 C.PI_HANDLER: "yes",
43 C.PI_DESCRIPTION: _("""Handle Pubsub data schemas""")
44 }
45
46
47 class PubsubSchema(object):
48
49 def __init__(self, host):
50 log.info(_(u"PubSub Schema initialization"))
51 self.host = host
52 host.bridge.addMethod("psSchemaGet", ".plugin",
53 in_sign='sss', out_sign='s',
54 method=self._getSchema,
55 async=True
56 )
57 host.bridge.addMethod("psSchemaSet", ".plugin",
58 in_sign='ssss', out_sign='',
59 method=self._setSchema,
60 async=True
61 )
62
63 def getHandler(self, client):
64 return SchemaHandler()
65
66 def _getSchemaBridgeCb(self, schema_elt):
67 if schema_elt is None:
68 return u''
69 return schema_elt.toXml()
70
71 def _getSchema(self, service, nodeIdentifier, profile_key=C.PROF_KEY_NONE):
72 client = self.host.getClient(profile_key)
73 service = None if not service else jid.JID(service)
74 d = self.getSchema(client, service, nodeIdentifier)
75 d.addCallback(self._getSchemaBridgeCb)
76 return d
77
78 def _getSchemaCb(self, iq_elt):
79 try:
80 schema_elt = next(iq_elt.elements(NS_SCHEMA, 'schema'))
81 except StopIteration:
82 raise exceptions.DataError('missing <schema> element')
83 schema_form = data_form.findForm(schema_elt, NS_SCHEMA_FORM)
84 if schema_form is None:
85 # there is not schema on this node
86 return None
87 # we get again the form because we need all elements/namespaces
88 # while schema_form.toElement while only keep XEP-0004 elements
89 x_elt = next(schema_elt.elements(data_form.NS_X_DATA, 'x'))
90 return x_elt
91
92 def getSchema(self, client, service, nodeIdentifier):
93 """retrieve PubSub node schema
94
95 @param service(jid.JID, None): jid of PubSub service
96 None to use our PEP
97 @param nodeIdentifier(unicode): node to get schema from
98 @return (domish.Element, None): schema (<x> element)
99 None if not schema has been set on this node
100 """
101 iq_elt = client.IQ(u'get')
102 if service is not None:
103 iq_elt['to'] = service.full()
104 pubsub_elt = iq_elt.addElement((NS_SCHEMA, 'pubsub'))
105 schema_elt = pubsub_elt.addElement((NS_SCHEMA, 'schema'))
106 schema_elt['node'] = nodeIdentifier
107 d = iq_elt.send()
108 d.addCallback(self._getSchemaCb)
109 return d
110
111 def _setSchema(self, service, nodeIdentifier, schema, profile_key=C.PROF_KEY_NONE):
112 client = self.host.getClient(profile_key)
113 service = None if not service else jid.JID(service)
114 schema = generic.parseXml(schema.encode('utf-8'))
115 return self.setSchema(client, service, nodeIdentifier, schema)
116
117 def setSchema(self, client, service, nodeIdentifier, schema):
118 """set or replace PubSub node schema
119
120 @param schema(domish.Element, None): schema to set
121 None if schema need to be removed
122 """
123 iq_elt = client.IQ()
124 if service is not None:
125 iq_elt['to'] = service.full()
126 pubsub_elt = iq_elt.addElement((NS_SCHEMA, 'pubsub'))
127 schema_elt = pubsub_elt.addElement((NS_SCHEMA, 'schema'))
128 schema_elt['node'] = nodeIdentifier
129 if schema is not None:
130 schema_elt.addChild(schema)
131 return iq_elt.send()
132
133
134 class SchemaHandler(XMPPHandler):
135 implements(iwokkel.IDisco)
136
137 def getDiscoInfo(self, requestor, service, nodeIdentifier=''):
138 return [disco.DiscoFeature(NS_SCHEMA)]
139
140 def getDiscoItems(self, requestor, service, nodeIdentifier=''):
141 return []