Package common :: Module pubsub
[hide private]
[frames] | no frames]

Source Code for Module common.pubsub

  1  # -*- coding:utf-8 -*- 
  2  ## src/common/pubsub.py 
  3  ## 
  4  ## Copyright (C) 2006 Tomasz Melcer <liori AT exroot.org> 
  5  ## Copyright (C) 2006-2010 Yann Leboulanger <asterix AT lagaule.org> 
  6  ## Copyright (C) 2007 Jean-Marie Traissard <jim AT lapin.org> 
  7  ## Copyright (C) 2008 Stephan Erb <steve-e AT h3c.de> 
  8  ## 
  9  ## This file is part of Gajim. 
 10  ## 
 11  ## Gajim is free software; you can redistribute it and/or modify 
 12  ## it under the terms of the GNU General Public License as published 
 13  ## by the Free Software Foundation; version 3 only. 
 14  ## 
 15  ## Gajim is distributed in the hope that it will be useful, 
 16  ## but WITHOUT ANY WARRANTY; without even the implied warranty of 
 17  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 18  ## GNU General Public License for more details. 
 19  ## 
 20  ## You should have received a copy of the GNU General Public License 
 21  ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. 
 22  ## 
 23   
 24  import xmpp 
 25  import gajim 
 26  import connection_handlers 
 27  import logging 
 28  log = logging.getLogger('gajim.c.pubsub') 
 29   
30 -class ConnectionPubSub:
31 - def __init__(self):
32 self.__callbacks={}
33
34 - def send_pb_subscription_query(self, jid, cb, *args, **kwargs):
35 if not self.connection or self.connected < 2: 36 return 37 query = xmpp.Iq('get', to=jid) 38 pb = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB) 39 pb.addChild('subscriptions') 40 41 id_ = self.connection.send(query) 42 43 self.__callbacks[id_]=(cb, args, kwargs)
44
45 - def send_pb_subscribe(self, jid, node, cb, *args, **kwargs):
46 if not self.connection or self.connected < 2: 47 return 48 our_jid = gajim.get_jid_from_account(self.name) 49 query = xmpp.Iq('set', to=jid) 50 pb = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB) 51 pb.addChild('subscribe', {'node': node, 'jid': our_jid}) 52 53 id_ = self.connection.send(query) 54 55 self.__callbacks[id_]=(cb, args, kwargs)
56
57 - def send_pb_unsubscribe(self, jid, node, cb, *args, **kwargs):
58 if not self.connection or self.connected < 2: 59 return 60 our_jid = gajim.get_jid_from_account(self.name) 61 query = xmpp.Iq('set', to=jid) 62 pb = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB) 63 pb.addChild('unsubscribe', {'node': node, 'jid': our_jid}) 64 65 id_ = self.connection.send(query) 66 67 self.__callbacks[id_]=(cb, args, kwargs)
68
69 - def send_pb_publish(self, jid, node, item, id_, options=None):
70 """ 71 Publish item to a node 72 """ 73 if not self.connection or self.connected < 2: 74 return 75 query = xmpp.Iq('set', to=jid) 76 e = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB) 77 p = e.addChild('publish', {'node': node}) 78 p.addChild('item', {'id': id_}, [item]) 79 if options: 80 p = e.addChild('publish-options') 81 p.addChild(node=options) 82 83 self.connection.send(query)
84
85 - def send_pb_retrieve(self, jid, node, cb=None, *args, **kwargs):
86 """ 87 Get items from a node 88 """ 89 if not self.connection or self.connected < 2: 90 return 91 query = xmpp.Iq('get', to=jid) 92 r = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB) 93 r = r.addChild('items', {'node': node}) 94 id_ = self.connection.send(query) 95 96 if cb: 97 self.__callbacks[id_]=(cb, args, kwargs)
98
99 - def send_pb_retract(self, jid, node, id_):
100 """ 101 Delete item from a node 102 """ 103 if not self.connection or self.connected < 2: 104 return 105 query = xmpp.Iq('set', to=jid) 106 r = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB) 107 r = r.addChild('retract', {'node': node, 'notify': '1'}) 108 r = r.addChild('item', {'id': id_}) 109 110 self.connection.send(query)
111
112 - def send_pb_purge(self, jid, node):
113 """ 114 Purge node: Remove all items 115 """ 116 if not self.connection or self.connected < 2: 117 return 118 query = xmpp.Iq('set', to=jid) 119 d = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB_OWNER) 120 d = d.addChild('purge', {'node': node}) 121 122 self.connection.send(query)
123
124 - def send_pb_delete(self, jid, node, on_ok=None, on_fail=None):
125 """ 126 Delete node 127 """ 128 if not self.connection or self.connected < 2: 129 return 130 query = xmpp.Iq('set', to=jid) 131 d = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB_OWNER) 132 d = d.addChild('delete', {'node': node}) 133 134 def response(con, resp, jid, node): 135 if resp.getType() == 'result' and on_ok: 136 on_ok(jid, node) 137 elif on_fail: 138 msg = resp.getErrorMsg() 139 on_fail(jid, node, msg)
140 141 self.connection.SendAndCallForResponse(query, response, {'jid': jid, 142 'node': node})
143
144 - def send_pb_create(self, jid, node, configure = False, configure_form = None):
145 """ 146 Create a new node 147 """ 148 if not self.connection or self.connected < 2: 149 return 150 query = xmpp.Iq('set', to=jid) 151 c = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB) 152 c = c.addChild('create', {'node': node}) 153 if configure: 154 conf = c.addChild('configure') 155 if configure_form is not None: 156 conf.addChild(node=configure_form) 157 158 self.connection.send(query)
159
160 - def send_pb_configure(self, jid, node, form):
161 if not self.connection or self.connected < 2: 162 return 163 query = xmpp.Iq('set', to=jid) 164 c = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB_OWNER) 165 c = c.addChild('configure', {'node': node}) 166 c.addChild(node=form) 167 168 self.connection.send(query)
169
170 - def _PubSubCB(self, conn, stanza):
171 log.debug('_PubsubCB') 172 try: 173 cb, args, kwargs = self.__callbacks.pop(stanza.getID()) 174 cb(conn, stanza, *args, **kwargs) 175 except Exception: 176 pass 177 178 pubsub = stanza.getTag('pubsub') 179 if not pubsub: 180 return 181 items = pubsub.getTag('items') 182 if not items: 183 return 184 item = items.getTag('item') 185 if not item: 186 return 187 storage = item.getTag('storage') 188 if storage: 189 ns = storage.getNamespace() 190 if ns == 'storage:bookmarks': 191 self._parse_bookmarks(storage, 'pubsub')
192
193 - def _PubSubErrorCB(self, conn, stanza):
194 log.debug('_PubsubErrorCB') 195 pubsub = stanza.getTag('pubsub') 196 if not pubsub: 197 return 198 items = pubsub.getTag('items') 199 if not items: 200 return 201 if items.getAttr('node') == 'storage:bookmarks': 202 # Receiving bookmarks from pubsub failed, so take them from xml 203 self.get_bookmarks(storage_type='xml')
204
205 - def request_pb_configuration(self, jid, node):
206 if not self.connection or self.connected < 2: 207 return 208 query = xmpp.Iq('get', to=jid) 209 e = query.addChild('pubsub', namespace=xmpp.NS_PUBSUB_OWNER) 210 e = e.addChild('configure', {'node': node}) 211 id_ = self.connection.getAnID() 212 query.setID(id_) 213 self.awaiting_answers[id_] = (connection_handlers.PEP_CONFIG,) 214 self.connection.send(query)
215