added plugin xep-0115: entity capabilities

This commit is contained in:
Goffi 2011-02-03 01:27:57 +01:00
commit 72e279c1f7
3 changed files with 227 additions and 7 deletions

201
src/plugins/plugin_xep_0115.py Executable file
View file

@ -0,0 +1,201 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
SAT plugin for managing xep-0115
Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from logging import debug, info, error, warning
from twisted.words.xish import domish
from twisted.words.protocols.jabber import client, jid, xmlstream
from twisted.words.protocols.jabber import error as jab_error
from twisted.words.protocols.jabber.xmlstream import IQ
import os.path
import types
from zope.interface import implements
from wokkel import disco, iwokkel
from hashlib import sha1
from base64 import b64encode
try:
from twisted.words.protocols.xmlstream import XMPPHandler
except ImportError:
from wokkel.subprotocols import XMPPHandler
PRESENCE = '/presence'
NS_ENTITY_CAPABILITY = 'http://jabber.org/protocol/caps'
CAPABILITY_UPDATE = PRESENCE + '/c[@xmlns="' + NS_ENTITY_CAPABILITY + '"]'
PLUGIN_INFO = {
"name": "XEP 0115 Plugin",
"import_name": "XEP_0115",
"type": "XEP",
"protocols": ["XEP-0115"],
"dependencies": [],
"main": "XEP_0115",
"handler": "yes",
"description": _("""Implementation of entity capabilities""")
}
class HashGenerationError(Exception):
pass
class ByteIdentity(object):
"""This class manage identity as bytes (needed for i;octet sort),
it is used for the hash generation"""
def __init__(self, identity, lang=None):
assert isinstance(identity, disco.DiscoIdentity)
self.category = identity.category.encode('utf-8')
self.idType = identity.type.encode('utf-8')
self.name = identity.name.encode('utf-8') if identity.name else ''
self.lang = lang.encode('utf-8') if lang else ''
def __str__(self):
return "%s/%s/%s/%s" % (self.category, self.idType, self.lang, self.name)
class XEP_0115():
cap_hash = None
def __init__(self, host):
info(_("Plugin XEP_0115 initialization"))
self.host = host
host.trigger.add("Disco Handled", self.checkHash)
self.hash_cache = host.memory.getPrivate("entity_capabilities_cache") or {} #key = hash or jid
self.jid_hash = {} #jid to hash mapping, map to a discoInfo if the hash method is unknown
def checkHash(self, profile):
if not XEP_0115.cap_hash:
XEP_0115.cap_hash = self.generateHash(profile)
else:
self.presenceHack(profile)
return True
def getHandler(self, profile):
return XEP_0115_handler(self, profile)
def presenceHack(self, profile):
"""modify SatPresenceProtocol to add capabilities data"""
client=self.host.getClient(profile)
presenceInst = client.presence
def hacked_send(self, obj):
obj.addChild(self._c_elt)
self._legacy_send(obj)
new_send = types.MethodType(hacked_send, presenceInst, presenceInst.__class__)
presenceInst._legacy_send = presenceInst.send
presenceInst.send = new_send
c_elt = domish.Element((NS_ENTITY_CAPABILITY,'c'))
c_elt['hash']='sha-1'
c_elt['node']='http://wiki.goffi.org/wiki/Salut_%C3%A0_Toi'
c_elt['ver']=XEP_0115.cap_hash
presenceInst._c_elt = c_elt
def generateHash(self, profile_key="@DEFAULT@"):
"""This method generate a sha1 hash as explained in xep-0115 #5.1
it then store it in XEP_0115.cap_hash"""
profile = self.host.memory.getProfileName(profile_key)
if not profile:
error ('Requesting hash for an inexistant profile')
raise HashGenerationError
client = self.host.getClient(profile_key)
if not client:
error ('Requesting hash for an inexistant client')
raise HashGenerationError
def generateHash_2(services, profile):
_s=[]
byte_identities = [ByteIdentity(identity) for identity in filter(lambda x:isinstance(x,disco.DiscoIdentity),services)] #FIXME: lang must be managed here
byte_identities.sort(key=lambda i:i.lang)
byte_identities.sort(key=lambda i:i.idType)
byte_identities.sort(key=lambda i:i.category)
for identity in byte_identities:
_s.append(str(identity))
_s.append('<')
byte_features = [feature.encode('utf-8') for feature in filter(lambda x:isinstance(x,disco.DiscoFeature),services)]
byte_features.sort() #XXX: the default sort has the same behaviour as the requested RFC 4790 i;octet sort
for feature in byte_features:
_s.append(feature)
_s.append('<')
#TODO: manage XEP-0128 data form here
XEP_0115.cap_hash = b64encode(sha1(''.join(_s)).digest())
debug(_('Capability hash generated: [%s]') % XEP_0115.cap_hash)
self.presenceHack(profile)
services = client.discoHandler.info(client.jid, client.jid, '').addCallback(generateHash_2, profile)
class XEP_0115_handler(XMPPHandler):
implements(iwokkel.IDisco)
def __init__(self, plugin_parent, profile):
self.plugin_parent = plugin_parent
self.host = plugin_parent.host
self.profile = profile
def connectionInitialized(self):
self.xmlstream.addObserver(CAPABILITY_UPDATE, self.update)
def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
return [disco.DiscoFeature(NS_ENTITY_CAPABILITY)]
def getDiscoItems(self, requestor, target, nodeIdentifier=''):
return []
def _updateCache(self, discoResult, from_jid, key):
"""Actually update the cache
@param discoResult: result of the requestInfo
@param known_hash: True if it's a hash method we know, we don't save the cache else"""
if key:
self.plugin_parent.jid_hash[from_jid] = key
self.plugin_parent.hash_cache[key] = discoResult
else:
#No key, that mean unknown hash method
self.plugin_parent.jid_hash[from_jid] = discoResult
self.host.memory.setPrivate("entity_capabilities_cache", self.plugin_parent.hash_cache)
def update(self, presence):
"""
Manage the capabilities of the entity
Check if we know the version of this capatilities
and get the capibilities if necessary
"""
from_jid = jid.JID(presence['from'])
c_elem = filter (lambda x:x.name == "c", presence.elements())[0] #We only want the "c" element
try:
ver=c_elem['ver']
hash=c_elem['hash']
node=c_elem['node']
except KeyError:
warning('Received invalid capabilities tag')
return
if not self.plugin_parent.jid_hash.has_key(from_jid):
if self.plugin_parent.hash_cache.has_key(ver):
#we know that hash, we just link it with the jid
self.plugin_parent.jid_hash[from_jid] = ver
else:
if hash!='sha-1':
#unknown hash method
warning('Unknown hash for entity capabilities: [%s]' % hash)
self.parent.disco.requestInfo(from_jid).addCallback(self._updateCache, from_jid, ver if hash=='sha-1' else None )
#TODO: me must manage the full algorithm described at XEP-0115 #5.4 part 3

View file

@ -82,7 +82,8 @@ class SatXMPPClient(client.XMPPClient):
self.host_app = host_app
def _authd(self, xmlstream):
print "SatXMPPClient"
if not self.host_app.trigger.point("XML Initialized", xmlstream, self.profile):
return
client.XMPPClient._authd(self, xmlstream)
self.__connected=True
info (_("********** [%s] CONNECTED **********") % self.profile)
@ -93,8 +94,6 @@ class SatXMPPClient(client.XMPPClient):
def streamInitialized(self):
"""Called after _authd"""
debug (_("XML stream is initialized"))
if not self.host_app.trigger.point("XML Initialized", self.xmlstream, self.profile):
return
self.keep_alife = task.LoopingCall(self.xmlstream.send, " ") #Needed to avoid disconnection (specially with openfire)
self.keep_alife.start(180)
@ -102,12 +101,15 @@ class SatXMPPClient(client.XMPPClient):
self.disco.setHandlerParent(self)
self.discoHandler = disco.DiscoHandler()
self.discoHandler.setHandlerParent(self)
if not self.host_app.trigger.point("Disco Handled", self.profile):
return
self.roster.requestRoster()
self.presence.available()
self.disco.requestInfo(jid.JID(self.host_app.memory.getParamA("Server", "Connection", profile_key=self.profile))).addCallback(self.host_app.serverDisco) #FIXME: use these informations
self.disco.requestInfo(jid.JID(self.host_app.memory.getParamA("Server", "Connection", profile_key=self.profile))).addCallback(self.host_app.serverDisco, self.profile) #FIXME: use these informations
def initializationFailed(self, reason):
print ("initializationFailed: %s" % reason)
@ -345,7 +347,6 @@ class SAT(service.Service):
self.menus = {} #used to know which new menus are wanted by plugins
self.memory=Memory(self)
self.server_features=[] #XXX: temp dic, need to be transfered into self.memory in the future
self.bridge=DBusBridge()
self.bridge.register("getVersion", lambda: self.get_const('client_version'))
@ -672,11 +673,11 @@ class SAT(service.Service):
## callbacks ##
def serverDisco(self, disco):
def serverDisco(self, disco, profile):
"""xep-0030 Discovery Protocol."""
for feature in disco.features:
debug (_("Feature found: %s"),feature)
self.server_features.append(feature)
self.memory.addServerFeature(feature, profile)
for cat, type in disco.identities:
debug (_("Identity found: [%(category)s/%(type)s] %(identity)s") % {'category':cat, 'type':type, 'identity':disco.identities[(cat,type)]})

View file

@ -397,6 +397,7 @@ class Memory:
self.params=Param(host)
self.history={} #used to store chat history (key: short jid)
self.private={} #used to store private value
self.server_features={} #used to store discovery's informations
host.set_const('savefile_history', SAVEFILE_HISTORY)
host.set_const('savefile_private', SAVEFILE_PRIVATE)
self.load()
@ -537,6 +538,23 @@ class Memory:
return self.private[key]
return None
def addServerFeature(self, feature, profile):
"""Add a feature discovered from server
@param feature: string of the feature
@param profile: which profile is using this server ?"""
if not self.server_features.has_key(profile):
self.server_features[profile] = []
self.server_features[profile].append(feature)
def hasServerFeature(self, feature, profile_key='@DEFAULT@'):
"""Tell if the server of the profile has the required feature"""
profile = self.getProfileName(profile_key)
if not profile:
error (_('Trying find server feature for a non-existant profile'))
return
assert(self.server_features.has_key(profile))
return feature in self.server_features[profile]
def addContact(self, contact_jid, attributes, groups, profile_key='@DEFAULT@'):
debug("Memory addContact: %s",contact_jid.userhost())