plugin XEP-0045, quick_frontend + primitivus (chat): cleaning of XEP-0045 (first pass):

- bridge methods/signals now all start with "muc" to follow new convention
    - internal method use client instead of profile to follow new convention
    - removed excetpions from plugin XEP-0045 in favor of core.exceptions, NotReady added
    - cleaned/simplified several part of the code. checkClient removed as it is not needed anymore
    - self.clients map removed, muc data are now stored directly in client
    - getRoomEntityNick and getRoomNicksOfUsers are removed as they don't look sane.
        /!\ This break all room game plugins for the moment
    - use of uuid4 instead of uuid1 for getUniqueName, as host ID and current time are used for uuid1
This commit is contained in:
Goffi 2016-06-27 21:45:11 +02:00
commit 04515e5013

View file

@ -109,16 +109,18 @@ class XEP_0054(object):
return True
def isInRoom(self, entity_jid, profile):
def isInRoom(self, client, entity_jid):
"""Tell if an full jid is a member of a room
@param entity_jid(jid.JID): full jid of the entity
@return (bool): True if the bare jid of the entity is a room jid
"""
try:
return self.host.plugins['XEP-0045'].isRoom(entity_jid.userhostJID(), profile_key=profile)
except KeyError:
self.host.plugins['XEP-0045'].checkRoomJoined(client, entity_jid.userhostJID())
except exceptions.NotFound:
return False
else:
return True
def _fillCachedValues(self, profile):
#FIXME: this is really suboptimal, need to be reworked
@ -143,41 +145,39 @@ class XEP_0054(object):
log.debug(u"Deleting profile cache for avatars")
del self.cache[profile]
def updateCache(self, jid_, name, value, profile):
def updateCache(self, client, jid_, name, value):
"""update cache value
save value in memory in case of change
@param jid_(jid.JID): jid of the owner of the vcard
@param name(str): name of the item which changed
@param value(unicode): new value of the item
@param profile(unicode): profile which received the update
"""
if jid_.resource:
if not self.isInRoom(jid_, profile):
if not self.isInRoom(client, jid_):
# VCard are retrieved with bare jid
# but MUC room is a special case
jid_ = jid.userhostJID()
self.host.memory.updateEntityData(jid_, name, value, profile_key=profile)
self.host.memory.updateEntityData(jid_, name, value, profile_key=client.profile)
if name in CACHED_DATA:
jid_s = jid_.userhost()
self.cache[profile].setdefault(jid_s, {})[name] = value
self.cache[profile].force(jid_s)
self.cache[client.profile].setdefault(jid_s, {})[name] = value
self.cache[client.profile].force(jid_s)
def getCache(self, entity_jid, name, profile):
def getCache(self, client, entity_jid, name):
"""return cached value for jid
@param entity_jid: target contact
@param name: name of the value ('nick' or 'avatar')
@param profile: %(doc_profile)s
@return: wanted value or None"""
if entity_jid.resource:
if not self.isInRoom(entity_jid, profile):
if not self.isInRoom(client, entity_jid):
# VCard are retrieved with bare jid
# but MUC room is a special case
entity_jid = jid.userhostJID()
try:
data = self.host.memory.getEntityData(entity_jid, [name], profile)
data = self.host.memory.getEntityData(entity_jid, [name], client.profile)
except exceptions.UnknownEntityError:
return None
return data.get(name)
@ -217,7 +217,7 @@ class XEP_0054(object):
return image_hash
@defer.inlineCallbacks
def vCard2Dict(self, vcard, target, profile):
def vCard2Dict(self, client, vcard, target):
"""Convert a VCard to a dict, and save binaries"""
log.debug(_("parsing vcard"))
dictionary = {}
@ -227,7 +227,7 @@ class XEP_0054(object):
dictionary['fullname'] = unicode(elem)
elif elem.name == 'NICKNAME':
dictionary['nick'] = unicode(elem)
self.updateCache(target, 'nick', dictionary['nick'], profile)
self.updateCache(client, target, 'nick', dictionary['nick'])
elif elem.name == 'URL':
dictionary['website'] = unicode(elem)
elif elem.name == 'EMAIL':
@ -239,7 +239,7 @@ class XEP_0054(object):
if not dictionary["avatar"]: # can happen in case of e.g. empty photo elem
del dictionary['avatar']
else:
self.updateCache(target, 'avatar', dictionary['avatar'], profile)
self.updateCache(client, target, 'avatar', dictionary['avatar'])
else:
log.info(_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
@ -248,56 +248,52 @@ class XEP_0054(object):
# and we reset them
for datum in CACHED_DATA.difference(dictionary.keys()):
log.debug(u"reseting vcard datum [{datum}] for {entity}".format(datum=datum, entity=target.full()))
self.updateCache(target, datum, '', profile)
self.updateCache(client, target, datum, '')
defer.returnValue(dictionary)
def _VCardCb(self, answer, profile):
def _VCardCb(self, answer, client):
"""Called after the first get IQ"""
log.debug(_("VCard found"))
if answer.firstChildElement().name == "vCard":
_jid, steam = self.host.getJidNStream(profile)
try:
from_jid = jid.JID(answer["from"])
except KeyError:
from_jid = _jid.userhostJID()
d = self.vCard2Dict(answer.firstChildElement(), from_jid, profile)
d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data, profile))
from_jid = client.jid.userhostJID()
d = self.vCard2Dict(client, answer.firstChildElement(), from_jid)
d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data, client.profile))
else:
log.error(_("FIXME: vCard not found as first child element"))
self.host.bridge.actionResult("SUPPRESS", answer['id'], {}, profile) # FIXME: maybe an error message would be better
self.host.bridge.actionResult("SUPPRESS", answer['id'], {}, client.profile) # FIXME: maybe an error message would be better
def _VCardEb(self, failure, profile):
def _VCardEb(self, failure, client):
"""Called when something is wrong with registration"""
try:
self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}, profile) # FIXME: maybe an error message would be better
self.host.bridge.actionResult("SUPPRESS", failure.value.stanza['id'], {}, client.profile) # FIXME: maybe an error message would be better
log.warning(_(u"Can't find VCard of %s") % failure.value.stanza['from'])
self.updateCache(jid.JID(failure.value.stanza['from']), "avatar", '', profile)
self.updateCache(client, jid.JID(failure.value.stanza['from']), "avatar", '')
except (AttributeError, KeyError):
# 'ConnectionLost' object has no attribute 'stanza' + sometimes 'from' key doesn't exist
log.warning(_(u"Can't find VCard: %s") % failure.getErrorMessage())
def _getCard(self, target_s, profile_key=C.PROF_KEY_NONE):
return self.getCard(jid.JID(target_s), profile_key)
client = self.host.getClient(profile_key)
return self.getCard(client, jid.JID(target_s))
def getCard(self, target, profile_key=C.PROF_KEY_NONE):
def getCard(self, client, target):
"""Ask server for VCard
@param target(jid.JID): jid from which we want the VCard
@result: id to retrieve the profile
"""
current_jid, xmlstream = self.host.getJidNStream(profile_key)
if not xmlstream:
raise exceptions.ProfileUnknownError('Asking vcard for a non-existant or not connected profile ({})'.format(profile_key))
profile = self.host.memory.getProfileName(profile_key)
to_jid = target.userhostJID()
log.debug(_(u"Asking for %s's VCard") % to_jid.userhost())
reg_request = IQ(xmlstream, 'get')
reg_request["from"] = current_jid.full()
reg_request = client.IQ('get')
reg_request["from"] = client.jid.full()
reg_request["to"] = to_jid.userhost()
reg_request.addElement('vCard', NS_VCARD)
reg_request.send(to_jid.userhost()).addCallbacks(self._VCardCb, self._VCardEb, callbackArgs=[profile], errbackArgs=[profile])
reg_request.send(to_jid.userhost()).addCallbacks(self._VCardCb, self._VCardEb, callbackArgs=[client], errbackArgs=[client])
return reg_request["id"]
def getAvatarFile(self, avatar_hash):
@ -354,7 +350,7 @@ class XEP_0054(object):
def elementBuilt(result):
"""Called once the image is at the right size/format, and the vcard set element is build"""
set_avatar_elt, img_hash = result
self.updateCache(client.jid.userhostJID(), 'avatar', img_hash, client.profile)
self.updateCache(client, client.jid.userhostJID(), 'avatar', img_hash)
return set_avatar_elt.send().addCallback(lambda ignore: client.presence.available()) # FIXME: should send the current presence, not always "available" !
d.addCallback(elementBuilt)
@ -385,7 +381,7 @@ class XEP_0054_handler(XMPPHandler):
@param presend(domish.Element): <presence/> stanza
"""
from_jid = jid.JID(presence['from'])
if from_jid.resource and not self.plugin_parent.isInRoom(from_jid, self.parent.profile):
if from_jid.resource and not self.plugin_parent.isInRoom(self.parent, from_jid):
from_jid = from_jid.userhostJID()
#FIXME: wokkel's data_form should be used here
try:
@ -401,12 +397,12 @@ class XEP_0054_handler(XMPPHandler):
hash_ = str(photo_elt)
if not hash_:
return
old_avatar = self.plugin_parent.getCache(from_jid, 'avatar', self.parent.profile)
old_avatar = self.plugin_parent.getCache(self.parent, from_jid, 'avatar')
filename = self.plugin_parent._getFilename(hash_)
if not old_avatar or old_avatar != hash_:
if os.path.exists(filename):
log.debug(u"New avatar found for [{}], it's already in cache, we use it".format(from_jid.full()))
self.plugin_parent.updateCache(from_jid, 'avatar', hash_, self.parent.profile)
self.plugin_parent.updateCache(self.parent, from_jid, 'avatar', hash_)
else:
log.debug(u'New avatar found for [{}], requesting vcard'.format(from_jid.full()))
self.plugin_parent.getCard(from_jid, self.parent.profile)