1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of nbxmpp.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
import logging
from nbxmpp.protocol import NS_PUBSUB
from nbxmpp.protocol import NS_PUBSUB_EVENT
from nbxmpp.protocol import NS_PUBSUB_PUBLISH_OPTIONS
from nbxmpp.protocol import NS_DATA
from nbxmpp.protocol import Node
from nbxmpp.protocol import Iq
from nbxmpp.protocol import isResultNode
from nbxmpp.structs import StanzaHandler
from nbxmpp.structs import PubSubEventData
from nbxmpp.structs import CommonResult
from nbxmpp.util import call_on_response
from nbxmpp.util import callback
from nbxmpp.util import raise_error
log = logging.getLogger('nbxmpp.m.pubsub')
class PubSub:
def __init__(self, client):
self._client = client
self.handlers = [
StanzaHandler(name='message',
callback=self._process_pubsub_base,
ns=NS_PUBSUB_EVENT,
priority=15),
]
def _process_pubsub_base(self, _con, stanza, properties):
properties.pubsub = True
event = stanza.getTag('event', namespace=NS_PUBSUB_EVENT)
delete = event.getTag('delete')
if delete is not None:
node = delete.getAttr('node')
properties.pubsub_event = PubSubEventData(
node, empty=True, deleted=True)
return
retract = event.getTag('retract')
if retract is not None:
node = retract.getAttr('node')
item = retract.getTag('item')
id_ = item.getAttr('id')
properties.pubsub_event = PubSubEventData(
node, id_, item, retracted=True)
return
items = event.getTag('items')
if items is not None:
if len(items.getChildren()) != 1:
log.warning('PubSub event with more than one item')
log.warning(stanza)
node = items.getAttr('node')
item = items.getTag('item')
if item is None:
return
id_ = item.getAttr('id')
properties.pubsub_event = PubSubEventData(node, id_, item)
@call_on_response('_default_response')
def publish(self, jid, node, item, id_=None, options=None):
query = Iq('set', to=jid)
pubsub = query.addChild('pubsub', namespace=NS_PUBSUB)
publish = pubsub.addChild('publish', {'node': node})
attrs = {}
if id_ is not None:
attrs = {'id': id_}
publish.addChild('item', attrs, [item])
if options:
publish = pubsub.addChild('publish-options')
publish.addChild(node=options)
return query
@callback
def _default_response(self, stanza):
if not isResultNode(stanza):
return raise_error(log.info, stanza)
return CommonResult(jid=stanza.getFrom())
def get_pubsub_request(jid, node, id_=None, max_items=None):
query = Iq('get', to=jid)
pubsub = query.addChild('pubsub', namespace=NS_PUBSUB)
items = pubsub.addChild('items', {'node': node})
if max_items is not None:
items.setAttr('max_items', max_items)
if id_ is not None:
items.addChild('item', {'id': id_})
return query
def get_pubsub_item(stanza):
pubsub_node = stanza.getTag('pubsub')
items_node = pubsub_node.getTag('items')
return items_node.getTag('item')
def get_bookmark_publish_options():
# TODO: Make generic
options = Node(NS_DATA + ' x',
attrs={'type': 'submit'})
field = options.addChild('field',
attrs={'var': 'FORM_TYPE', 'type': 'hidden'})
field.setTagData('value', NS_PUBSUB_PUBLISH_OPTIONS)
field = options.addChild('field', attrs={'var': 'pubsub#access_model'})
field.setTagData('value', 'whitelist')
return options
|