use NEC to handle bookmarks via pubsub.

This commit is contained in:
Yann Leboulanger 2010-08-27 23:53:17 +02:00
parent cb0f30f4b4
commit d54401d404
2 changed files with 82 additions and 90 deletions

View File

@ -43,6 +43,40 @@ import datetime
import common.xmpp
import common.caps_cache as capscache
# This cass is needed in pubsub.py, so we need to create it before importing
# pubsub
class BookmarksHelper:
def parse_bookmarks(self):
self.bookmarks = []
confs = self.base_event.storage_node.getTags('conference')
for conf in confs:
autojoin_val = conf.getAttr('autojoin')
if autojoin_val is None: # not there (it's optional)
autojoin_val = False
minimize_val = conf.getAttr('minimize')
if minimize_val is None: # not there (it's optional)
minimize_val = False
print_status = conf.getTagData('print_status')
if not print_status:
print_status = conf.getTagData('show_status')
try:
jid = helpers.parse_jid(conf.getAttr('jid'))
except common.helpers.InvalidFormat:
log.warn('Invalid JID: %s, ignoring it' % conf.getAttr('jid'))
continue
bm = {'name': conf.getAttr('name'),
'jid': jid,
'autojoin': autojoin_val,
'minimize': minimize_val,
'password': conf.getTagData('password'),
'nick': conf.getTagData('nick'),
'print_status': print_status}
bm_jids = [b['jid'] for b in self.bookmarks]
if bm['jid'] not in bm_jids:
self.bookmarks.append(bm)
from common import helpers
from common import gajim
from common import exceptions
@ -1196,49 +1230,6 @@ ConnectionJingle, ConnectionIBBytestream):
self.seclabel_catalogues[to] = [[], None, None]
self.seclabel_catalogues[to][0].append(callback)
def _parse_bookmarks(self, storage, storage_type):
"""
storage_type can be 'pubsub' or 'xml' to tell from where we got bookmarks
"""
# Bookmarked URLs and Conferences
# http://www.xmpp.org/extensions/xep-0048.html
resend_to_pubsub = False
confs = storage.getTags('conference')
for conf in confs:
autojoin_val = conf.getAttr('autojoin')
if autojoin_val is None: # not there (it's optional)
autojoin_val = False
minimize_val = conf.getAttr('minimize')
if minimize_val is None: # not there (it's optional)
minimize_val = False
print_status = conf.getTagData('print_status')
if not print_status:
print_status = conf.getTagData('show_status')
try:
bm = {'name': conf.getAttr('name'),
'jid': helpers.parse_jid(conf.getAttr('jid')),
'autojoin': autojoin_val,
'minimize': minimize_val,
'password': conf.getTagData('password'),
'nick': conf.getTagData('nick'),
'print_status': print_status}
except common.helpers.InvalidFormat:
log.warn('Invalid JID: %s, ignoring it' % conf.getAttr('jid'))
continue
bm_jids = [b['jid'] for b in self.bookmarks]
if bm['jid'] not in bm_jids:
self.bookmarks.append(bm)
if storage_type == 'xml':
# We got a bookmark that was not in pubsub
resend_to_pubsub = True
self.dispatch('BOOKMARKS', self.bookmarks)
if storage_type == 'pubsub':
# We gor bookmarks from pubsub, now get those from xml to merge them
self.get_bookmarks(storage_type='xml')
if self.pubsub_supported and resend_to_pubsub:
self.store_bookmarks('pubsub')
def _rosterSetCB(self, con, iq_obj):
log.debug('rosterSetCB')
gajim.nec.push_incoming_event(RosterSetReceivedEvent(None, conn=self,
@ -2740,38 +2731,6 @@ class PrivateStorageReceivedEvent(nec.NetworkIncomingEvent):
self.namespace = self.storage_node.getNamespace()
return True
class BookmarksHelper:
def parse_bookmarks(self):
self.bookmarks = []
confs = self.base_event.storage_node.getTags('conference')
for conf in confs:
autojoin_val = conf.getAttr('autojoin')
if autojoin_val is None: # not there (it's optional)
autojoin_val = False
minimize_val = conf.getAttr('minimize')
if minimize_val is None: # not there (it's optional)
minimize_val = False
print_status = conf.getTagData('print_status')
if not print_status:
print_status = conf.getTagData('show_status')
try:
jid = helpers.parse_jid(conf.getAttr('jid'))
except common.helpers.InvalidFormat:
log.warn('Invalid JID: %s, ignoring it' % conf.getAttr('jid'))
continue
bm = {'name': conf.getAttr('name'),
'jid': jid,
'autojoin': autojoin_val,
'minimize': minimize_val,
'password': conf.getTagData('password'),
'nick': conf.getTagData('nick'),
'print_status': print_status}
bm_jids = [b['jid'] for b in self.bookmarks]
if bm['jid'] not in bm_jids:
self.bookmarks.append(bm)
class PrivateStorageBookmarksReceivedEvent(nec.NetworkIncomingEvent,
BookmarksHelper):
name = 'private-storage-bookmarks-received'
@ -2786,7 +2745,8 @@ BookmarksHelper):
class BookmarksReceivedEvent(nec.NetworkIncomingEvent):
name = 'bookmarks-received'
base_network_events = ['private-storage-bookmarks-received']
base_network_events = ['private-storage-bookmarks-received',
'pubsub-bookmarks-received']
def generate(self):
self.conn = self.base_event.conn

View File

@ -24,12 +24,17 @@
import xmpp
import gajim
import connection_handlers
import nec
import ged
import logging
log = logging.getLogger('gajim.c.pubsub')
class ConnectionPubSub:
def __init__(self):
self.__callbacks={}
gajim.nec.register_incoming_event(PubsubBookmarksReceivedEvent)
gajim.ged.register_event_handler('pubsub-bookmarks-received',
ged.CORE, self._nec_pubsub_bookmarks_received)
def send_pb_subscription_query(self, jid, cb, *args, **kwargs):
if not self.connection or self.connected < 2:
@ -174,21 +179,16 @@ class ConnectionPubSub:
cb(conn, stanza, *args, **kwargs)
except Exception:
pass
gajim.nec.push_incoming_event(PubsubReceivedEvent(None,
conn=self, iq_obj=stanza))
pubsub = stanza.getTag('pubsub')
if not pubsub:
return
items = pubsub.getTag('items')
if not items:
return
item = items.getTag('item')
if not item:
return
storage = item.getTag('storage')
if storage:
ns = storage.getNamespace()
if ns == 'storage:bookmarks':
self._parse_bookmarks(storage, 'pubsub')
def _nec_pubsub_bookmarks_received(self, obj):
bm_jids = [b['jid'] for b in self.bookmarks]
for bm in obj.bookmarks:
if bm['jid'] not in bm_jids:
self.bookmarks.append(bm)
# We got bookmarks from pubsub, now get those from xml to merge them
self.get_bookmarks(storage_type='xml')
def _PubSubErrorCB(self, conn, stanza):
log.debug('_PubsubErrorCB')
@ -212,3 +212,35 @@ class ConnectionPubSub:
query.setID(id_)
self.awaiting_answers[id_] = (connection_handlers.PEP_CONFIG,)
self.connection.send(query)
class PubsubReceivedEvent(nec.NetworkIncomingEvent):
name = 'pubsub-received'
base_network_events = []
def generate(self):
self.pubsub_node = self.iq_obj.getTag('pubsub')
if not self.pubsub_node:
return
self.items_node = self.pubsub_node.getTag('items')
if not self.items_node:
return
self.item_node = self.items_node.getTag('item')
if not self.item_node:
return
return True
class PubsubBookmarksReceivedEvent(nec.NetworkIncomingEvent,
connection_handlers.BookmarksHelper):
name = 'pubsub-bookmarks-received'
base_network_events = ['pubsub-received']
def generate(self):
self.conn = self.base_event.conn
storage = self.base_event.item_node.getTag('storage')
if not storage:
return
ns = storage.getNamespace()
if ns != 'storage:bookmarks':
return
self.parse_bookmarks()
return True