use NEC to handle bookmarks via pubsub.
This commit is contained in:
parent
cb0f30f4b4
commit
d54401d404
|
@ -43,6 +43,40 @@ import datetime
|
||||||
import common.xmpp
|
import common.xmpp
|
||||||
import common.caps_cache as capscache
|
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 helpers
|
||||||
from common import gajim
|
from common import gajim
|
||||||
from common import exceptions
|
from common import exceptions
|
||||||
|
@ -1196,49 +1230,6 @@ ConnectionJingle, ConnectionIBBytestream):
|
||||||
self.seclabel_catalogues[to] = [[], None, None]
|
self.seclabel_catalogues[to] = [[], None, None]
|
||||||
self.seclabel_catalogues[to][0].append(callback)
|
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):
|
def _rosterSetCB(self, con, iq_obj):
|
||||||
log.debug('rosterSetCB')
|
log.debug('rosterSetCB')
|
||||||
gajim.nec.push_incoming_event(RosterSetReceivedEvent(None, conn=self,
|
gajim.nec.push_incoming_event(RosterSetReceivedEvent(None, conn=self,
|
||||||
|
@ -2740,38 +2731,6 @@ class PrivateStorageReceivedEvent(nec.NetworkIncomingEvent):
|
||||||
self.namespace = self.storage_node.getNamespace()
|
self.namespace = self.storage_node.getNamespace()
|
||||||
return True
|
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,
|
class PrivateStorageBookmarksReceivedEvent(nec.NetworkIncomingEvent,
|
||||||
BookmarksHelper):
|
BookmarksHelper):
|
||||||
name = 'private-storage-bookmarks-received'
|
name = 'private-storage-bookmarks-received'
|
||||||
|
@ -2786,7 +2745,8 @@ BookmarksHelper):
|
||||||
|
|
||||||
class BookmarksReceivedEvent(nec.NetworkIncomingEvent):
|
class BookmarksReceivedEvent(nec.NetworkIncomingEvent):
|
||||||
name = 'bookmarks-received'
|
name = 'bookmarks-received'
|
||||||
base_network_events = ['private-storage-bookmarks-received']
|
base_network_events = ['private-storage-bookmarks-received',
|
||||||
|
'pubsub-bookmarks-received']
|
||||||
|
|
||||||
def generate(self):
|
def generate(self):
|
||||||
self.conn = self.base_event.conn
|
self.conn = self.base_event.conn
|
||||||
|
|
|
@ -24,12 +24,17 @@
|
||||||
import xmpp
|
import xmpp
|
||||||
import gajim
|
import gajim
|
||||||
import connection_handlers
|
import connection_handlers
|
||||||
|
import nec
|
||||||
|
import ged
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger('gajim.c.pubsub')
|
log = logging.getLogger('gajim.c.pubsub')
|
||||||
|
|
||||||
class ConnectionPubSub:
|
class ConnectionPubSub:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.__callbacks={}
|
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):
|
def send_pb_subscription_query(self, jid, cb, *args, **kwargs):
|
||||||
if not self.connection or self.connected < 2:
|
if not self.connection or self.connected < 2:
|
||||||
|
@ -174,21 +179,16 @@ class ConnectionPubSub:
|
||||||
cb(conn, stanza, *args, **kwargs)
|
cb(conn, stanza, *args, **kwargs)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
gajim.nec.push_incoming_event(PubsubReceivedEvent(None,
|
||||||
|
conn=self, iq_obj=stanza))
|
||||||
|
|
||||||
pubsub = stanza.getTag('pubsub')
|
def _nec_pubsub_bookmarks_received(self, obj):
|
||||||
if not pubsub:
|
bm_jids = [b['jid'] for b in self.bookmarks]
|
||||||
return
|
for bm in obj.bookmarks:
|
||||||
items = pubsub.getTag('items')
|
if bm['jid'] not in bm_jids:
|
||||||
if not items:
|
self.bookmarks.append(bm)
|
||||||
return
|
# We got bookmarks from pubsub, now get those from xml to merge them
|
||||||
item = items.getTag('item')
|
self.get_bookmarks(storage_type='xml')
|
||||||
if not item:
|
|
||||||
return
|
|
||||||
storage = item.getTag('storage')
|
|
||||||
if storage:
|
|
||||||
ns = storage.getNamespace()
|
|
||||||
if ns == 'storage:bookmarks':
|
|
||||||
self._parse_bookmarks(storage, 'pubsub')
|
|
||||||
|
|
||||||
def _PubSubErrorCB(self, conn, stanza):
|
def _PubSubErrorCB(self, conn, stanza):
|
||||||
log.debug('_PubsubErrorCB')
|
log.debug('_PubsubErrorCB')
|
||||||
|
@ -212,3 +212,35 @@ class ConnectionPubSub:
|
||||||
query.setID(id_)
|
query.setID(id_)
|
||||||
self.awaiting_answers[id_] = (connection_handlers.PEP_CONFIG,)
|
self.awaiting_answers[id_] = (connection_handlers.PEP_CONFIG,)
|
||||||
self.connection.send(query)
|
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
|
Loading…
Reference in New Issue