This file is indexed.

/usr/share/pyshared/archipelcore/pubsub.py is in archipel-core 0.6.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# -*- coding: utf-8 -*-
#
# pubsub.py
#
# Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
# This file is part of ArchipelProject
# http://archipelproject.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import types
import xmpp


XMPP_PUBSUB_VAR_TITLE                                       = "pubsub#title"
XMPP_PUBSUB_VAR_DELIVER_NOTIFICATION                        = "pubsub#deliver_notifications"
XMPP_PUBSUB_VAR_DELIVER_PAYLOADS                            = "pubsub#deliver_payloads"
XMPP_PUBSUB_VAR_PERSIST_ITEMS                               = "pubsub#persist_items"
XMPP_PUBSUB_VAR_MAX_ITEMS                                   = "pubsub#max_items"
XMPP_PUBSUB_VAR_ITEM_EXPIRE                                 = "pubsub#item_expire"
XMPP_PUBSUB_VAR_ACCESS_MODEL                                = "pubsub#access_model"
XMPP_PUBSUB_VAR_ROSTER_GROUP_ALLOWED                        = "pubsub#roster_groups_allowed"
XMPP_PUBSUB_VAR_PUBLISH_MODEL                               = "pubsub#publish_model"
XMPP_PUBSUB_VAR_PURGE_OFFLINE                               = "pubsub#purge_offline"
XMPP_PUBSUB_VAR_SEND_LAST_PUBLISHED_ITEM                    = "pubsub#send_last_published_item"
XMPP_PUBSUB_VAR_PRESENCE_BASED_DELIVERY                     = "pubsub#presence_based_delivery"
XMPP_PUBSUB_VAR_NOTIFICATION_TYPE                           = "pubsub#notification_type"
XMPP_PUBSUB_VAR_NOTIFY_CONFIG                               = "pubsub#notify_config"
XMPP_PUBSUB_VAR_NOTIFY_DELETE                               = "pubsub#notify_delete"
XMPP_PUBSUB_VAR_NOTIFY_RECTRACT                             = "pubsub#notify_retract"
XMPP_PUBSUB_VAR_NOTIFY_SUB                                  = "pubsub#notify_sub"
XMPP_PUBSUB_VAR_MAX_PAYLOAD_SIZE                            = "pubsub#max_payload_size"
XMPP_PUBSUB_VAR_TYPE                                        = "pubsub#type"
XMPP_PUBSUB_VAR_BODY_XSLT                                   = "pubsub#body_xslt"
XMPP_PUBSUB_VAR_ITEM_REPLY                                  = "pubsub#itemreply"

XMPP_PUBSUB_VAR_ACCESS_MODEL_OPEN                           = "open"
XMPP_PUBSUB_VAR_ACCESS_MODEL_ROSTER                         = "roster"
XMPP_PUBSUB_VAR_ACCESS_MODEL_AUTHORIZE                      = "authorize"
XMPP_PUBSUB_VAR_ACCESS_MODEL_WHITELIST                      = "whitelist"
XMPP_PUBSUB_VAR_SEND_LAST_PUBLISHED_ITEM_NEVER              = "never"
XMPP_PUBSUB_VAR_SEND_LAST_PUBLISHED_ITEM_ON_SUB             = "on_sub"
XMPP_PUBSUB_VAR_SEND_LAST_PUBLISHED_ITEM_ON_SUB_PRESENCE    = "on_sub_and_presence"
XMPP_PUBSUB_VAR_ITEM_REPLY_OWNER                            = "owner"
XMPP_PUBSUB_VAR_ITEM_REPLY_PUBLISHER                        = "publisher"

XMPP_PUBSUB_AFFILIATION_OWNER                              = "owner"
XMPP_PUBSUB_AFFILIATION_PUBLISHER                          = "publisher"
XMPP_PUBSUB_AFFILIATION_PUBLISHERONLY                      = "publisher-only"
XMPP_PUBSUB_AFFILIATION_MEMBER                             = "member"
XMPP_PUBSUB_AFFILIATION_NONE                               = "none"
XMPP_PUBSUB_AFFILIATION_OUTCAST                            = "outcast"


class TNPubSubNode:

    def __init__(self, xmppclient, pubsubserver, nodename):
        """
        Initialize the TNPubSubNode.
        @type xmppclient: xmpp.Dispatcher
        @param xmppclient: the xmppclient connection to user
        @type pubsubserver: string
        @param xmppclient: the string containing the JID of the pubsub server
        @type nodename: string
        @param nodename: the name of the pubsub node
        """
        self.xmppclient     = xmppclient
        self.pubsubserver   = pubsubserver
        self.nodename       = nodename
        self.recovered      = False
        self.content        = None
        self.affiliations   = {}
        self.subscriptions  = []


    ### Node management

    def recover(self, wait=False):
        """
        Get the current pubsub node and wait for response. If not already recovered, ask to server.
        @type wait: Boolean
        @param wait: if True, recovering will be blockant (IE, execution interrupted until recovering)
        @rtype: Boolean
        @return: True in case of success
        """
        try:
            self.retrieve_items(wait=wait)
            return self.retrieve_items(wait=wait)
        except Exception as ex:
            return False

    def retrieve_items(self, callback=None, wait=False):
        """
        Retrieve or update the content of the node.
        @type wait: Boolean
        @param wait: if True, recovering will be blockant (IE, execution interrupted until recovering)
        @rtype: Boolean
        @return: True in case of success
        """
        iq           = xmpp.Iq(typ="get", to=self.pubsubserver)
        iq_pubsub    = iq.addChild(name='pubsub', namespace="http://jabber.org/protocol/pubsub")
        iq_pubsub.addChild(name="items", attrs={"node": self.nodename})

        def _did_retrieve_items(conn, resp, callback=None):
            ret = False
            if resp.getType() == "result":
                self.content = resp.getTag("pubsub").getTag("items").getTags("item")
                self.recovered = True
                ret = True
            if callback:
                callback(resp)
            return ret

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_retrieve_items(None, resp, callback)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_retrieve_items, args={"callback": callback})
            return True

    def create(self, callback=None, wait=False):
        """
        Create node on server if not exists.
        @type wait: Boolean
        @param wait: if True, recovering will be blockant (IE, execution interrupted until recovering)
        @rtype: Boolean
        @return: True in case of success
        """
        if self.recovered:
            raise Exception("PUBSUB: can't create. Node %s already exists." % self.nodename)

        iq = xmpp.Iq(typ="set", to=self.pubsubserver)
        iq.addChild(name="pubsub", namespace=xmpp.protocol.NS_PUBSUB).addChild(name="create", attrs={"node": self.nodename})

        def _did_create(conn, resp, callback=None, wait=False):
            ret = False
            if resp.getType() == "result":
                self.recover(wait=wait)
                ret = True
            if callback:
                callback(resp)
            return ret

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_create(None, resp, callback, wait)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_create, args={"callback": callback, "wait": wait})
            return True

    def delete(self, callback=None, wait=False):
        """
        Delete the node from server if exists.
        @type wait: Boolean
        @param wait: if True, recovering will be blockant (IE, execution interrupted until recovering)
        @rtype: Boolean
        @return: True in case of success
        """
        if not self.recovered:
            raise Exception("PUBSUB: Can't delete. Node %s doesn't exists." % self.nodename)
        iq = xmpp.Iq(typ="set", to=self.pubsubserver)
        iq.addChild(name="pubsub", namespace=xmpp.protocol.NS_PUBSUB + "#owner").addChild(name="delete", attrs={"node": self.nodename})

        def _did_delete(conn, resp, callback):
            ret = False
            if resp.getType() == "result":
                ret = True
            if callback:
                callback(resp)
            return ret

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_delete(None, resp, callback)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_delete, args={"callback": callback})
            return True

    def configure(self, options, callback=None, wait=False):
        """
        Configure the node.
        @type options: dict
        @param options: dictionary containing options: value for the pubsub configuration
        @type wait: Boolean
        @param wait: if True, recovering will be blockant (IE, execution interrupted until recovering)
        @rtype: Boolean
        @return: True in case of success
        """
        if not self.recovered:
            raise Exception("PUBSUB: can't configure. Node %s doesn't exists." % self.nodename)
        iq = xmpp.Iq(typ="set", to=self.pubsubserver)
        pubsub = iq.addChild("pubsub", namespace=xmpp.protocol.NS_PUBSUB + "#owner")
        configure = pubsub.addChild("configure", attrs={"node": self.nodename})
        x = configure.addChild("x", namespace=xmpp.protocol.NS_DATA, attrs={"type": "submit"})
        x.addChild("field", attrs={"var": "FORM_TYPE", "type": "hidden"}).addChild("value").setData("http://jabber.org/protocol/pubsub#node_config")
        for key, value in options.items():
            field = x.addChild("field", attrs={"var": key})
            if type(value) == types.ListType:
                for v in value:
                    field.addChild("value").setData(v)
            else:
                field.addChild("value").setData(str(value))

        def _did_configure(conn, resp, callback):
            ret = False
            if resp.getType() == "result":
                ret = True
            if callback:
                callback(resp)
            return ret

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_configure(None, resp, callback)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_configure, args={"callback": callback})
            return True


    ### Item management

    def get_items(self):
        """
        Return an array of all items.
        @rtype: list
        @return: list of pubsub's xmpp.Nonde
        """
        return self.content

    def get_item(self, item_id):
        """
        Return the item with the given item id
        @type item_id: string
        @param item_id: the pubsub node id
        """
        for n in self.content:
            if n.getAttr("id").lower() == item_id.lower():
                return n
        return None

    def add_item(self, itemcontentnode, callback=None, wait=False):
        """
        Add a leaf item xmpp.node to the node and will trigger callback if any
        on server answer.
        @type itemcontentnode: xmpp.Node
        @param itemcontentnode: the node to publish on the pubsub
        @type callback: function
        @param callback: if not None, callback will be called after publication
        """
        if not self.recovered:
            raise Exception("PUBSUB: can't add item. Node %s doesn't exists." % self.nodename)
        iq          = xmpp.Iq(typ="set", to=self.pubsubserver)
        pubsub      = iq.addChild("pubsub", namespace=xmpp.protocol.NS_PUBSUB)
        publish     = pubsub.addChild("publish", attrs={"node": self.nodename})
        item        = publish.addChild("item")
        item.addChild(node=itemcontentnode)

        def _did_publish_item(conn, resp, callback, item):
            ret = False
            if resp.getType() == "result":
                item.setAttr("id", resp.getTag("pubsub").getTag("publish").getTag("item").getAttr("id"))
                self.content.append(item)
                ret = True
            if callback:
                return callback(resp)
            return ret

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_publish_item(None, resp, callback, item)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_publish_item, args={"callback": callback, "item": item})
            return True


    def remove_item(self, item_id, callback=None, user_info=None, wait=False):
        """
        Remove an item according to its ID.
        @type item_id: string
        @param item_id: the id of the node to remove
        @type callback: function
        @param callback: if not None, callback will be called after retraction
        @type user_info: Object
        @param user_info: random info to pass to the callback
        """
        iq = xmpp.Iq(typ="set", to=self.pubsubserver)
        pubsub = iq.addChild("pubsub", namespace=xmpp.protocol.NS_PUBSUB)
        retract = pubsub.addChild("retract", attrs={"node": self.nodename})
        item = retract.addChild("item", attrs={"id": item_id})

        for item in self.content:
            if item.getAttr("id") == item_id:
                self.content.remove(item)
                break

        def _did_remove_item(conn, resp, callback, user_info):
            ret = False
            if resp.getType() == "result":
                ret = True
            if callback:
                return callback(resp, user_info)
            return ret

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_remove_item(None, resp, callback, user_info)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_remove_item, args={"callback": callback, "user_info": user_info})



    ## Subscription management

    def retrieve_subscriptions(self, callback=None, wait=False):
        """
        Recover the subscriptions
        @type callback: function
        @param callback: if not None, callback will be called after retrieval
        @type wait: Boolean
        @param wait: if True, action will be done in sync mode
        """
        iq  = xmpp.Iq(typ="get", to=self.pubsubserver)
        pubsub = iq.addChild("pubsub", namespace=xmpp.protocol.NS_PUBSUB)
        pubsub.addChild("subscriptions", attrs={"node": self.nodename})

        def _did_retrieve_subscription(conn, resp, callback):
            ret = False
            if resp.getType() == "result":
                self.subscriptions = []
                for subscription in resp.getTag("pubsub").getTag("subscriptions").getTags("subscription"):
                    self.subscriptions.append(subscription.getAttr("subid"))
                ret = True
            return ret

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_retrieve_subscription(None, resp, callback)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_retrieve_subscription, args={"callback": callback})
            return True

    def subscribe(self, jid, callback=None, unique=True, wait=False):
        """
        Subscribe to the node.
        @type jid: xmpp.Protocol.JID
        @param jid: the JID of the subscriber
        @type callback: function
        @param callback: the callback that will be called when an item is published
        @type unique: Boolean
        @param unique: if True, it will subscribe only if no subscription is already done
        @type wait: Boolean
        @param wait: if True, action will be done in sync mode
        """
        self.subscriber_callback = callback
        self.subscriber_jid = jid

        if unique:
            if len(self.subscriptions) == 0:
                self.retrieve_subscriptions(wait=True)
            if not len(self.subscriptions) == 0:
                self.xmppclient.RegisterHandler('message', self._on_pubsub_event, ns=xmpp.protocol.NS_PUBSUB+"#event", typ="headline")
                return;

        iq = xmpp.Iq(typ="set", to=self.pubsubserver)
        pubsub = iq.addChild("pubsub", namespace=xmpp.protocol.NS_PUBSUB)
        pubsub.addChild("subscribe", attrs={"node": self.nodename, "jid": jid.getStripped()})

        def _did_subscribe(conn, resp, callback):
            ret = False
            if resp.getType() == "result":
                self.xmppclient.RegisterHandler('message', self._on_pubsub_event, ns=xmpp.protocol.NS_PUBSUB+"#event", typ="headline")
                ret = True
            return ret

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_subscribe(None, resp, callback)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_subscribe, args={"callback": callback})
            return True


        self.xmppclient.send(iq)

    def _on_pubsub_event(self, conn, event):
        """
        Trigger the callback for events.
        """
        def on_retrieve(resp):
            if resp.getType() == "result":
                node = event.getTag("event").getTag("items").getAttr("node")
                if node == self.nodename and self.subscriber_callback and event.getTo().getStripped() == self.subscriber_jid.getStripped():
                    self.subscriber_callback(event)

        self.retrieve_items(callback=on_retrieve)

    def unsubscribe(self, jid, subID, callback=None, wait=False):
        """
        Unsubscribe from a node.
        @type jid: xmpp.JID
        @param jid: the JID of the entity to unsubscribe
        @type subID: String
        @param subID: the subscription ID to remove. If None, all subscriptions will be removed
        @param callback: the callback that will be called when an item is published
        @type wait: Boolean
        @param wait: if True, action will be done in sync mode
        """
        self.subscriber_callback = None
        self.subscriber_jid = None

        def _did_unsubscribe(conn, resp, callback):
            self.xmppclient.UnregisterHandler('message', self._on_pubsub_event, ns=xmpp.protocol.NS_PUBSUB+"#event", typ="headline")

        iq = xmpp.Iq(typ="set", to=self.pubsubserver)
        pubsub = iq.addChild("pubsub", namespace=xmpp.protocol.NS_PUBSUB)
        pubsub.addChild("unsubscribe", attrs={"node": self.nodename, "jid": jid.getStripped(), "subid": subID})

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_unsubscribe(None, resp, callback)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_unsubscribe, args={"callback": callback})
            return True


    ## Affilication management

    def fetch_affiliations(self, callback=None, wait=False):
        """
        fetch the affiliations for the node
        @type callback: Function
        @param callback: the callback function
        @type wait: Boolean
        @param wait: if true, wait for answer
        """
        iq = xmpp.Node("iq", attrs={"type": "get", "to": self.pubsubserver})
        pubsubNode = iq.addChild("pubsub", namespace="http://jabber.org/protocol/pubsub#owner")
        affNode = pubsubNode.addChild("affiliations", attrs={"node": self.nodename})

        def _did_fetch_affiliations(conn, resp, callback):
            ret = False
            if resp.getType() == "result":
                self.affiliations = {}
                for affiliation in resp.getTag("pubsub").getTag("affiliations").getTags("affiliation"):
                    self.affiliations[affiliation.getAttr("jid")] = affiliation.getAttr("affiliation")
                ret = True
            if callback:
                return callback(resp)
            return ret

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_fetch_affiliations(None, resp, callback)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_fetch_affiliations, args={"callback": callback})
            return True

    def set_affiliation(self, jid, affiliation, callback=None, wait=False):
        """
        set an affiliation for the node
        @type jid: xmpp.JID
        @param jid: the jid to change affiliation
        @type affiliation: String
        @param affiliation: the affiliation
        @type callback: Function
        @param callback: the callback function
        @type wait: Boolean
        @param wait: if true, wait for answer
        """
        iq = xmpp.Node("iq", attrs={"type": "set", "to": self.pubsubserver})
        pubsubNode = iq.addChild("pubsub", namespace="http://jabber.org/protocol/pubsub#owner")
        affNode = pubsubNode.addChild("affiliations", attrs={"node": self.nodename})
        affNode.addChild("affiliation", attrs={"jid": jid.getStripped(), "affiliation": affiliation})

        def _did_set_affiliations(conn, resp, callback, wait):
            ret = False
            if resp.getType() == "result":
                self.fetch_affiliations(wait=wait)
                ret = True
            if callback:
                return callback(resp)
            return ret

        if wait:
            resp = self.xmppclient.SendAndWaitForResponse(iq)
            return _did_set_affiliations(None, resp, callback, wait)
        else:
            self.xmppclient.SendAndCallForResponse(iq, func=_did_set_affiliations, args={"callback": callback, "wait": wait})
            return True