some cleanup ni jabber.py

This commit is contained in:
Yann Leboulanger 2004-10-07 14:41:40 +00:00
parent 446dcc49a0
commit c16544c059

View file

@ -66,6 +66,7 @@ An example of usage for a simple client would be ( only psuedo code !)
import xmlstream import xmlstream
import sha, time import sha, time
import string
debug=xmlstream.debug debug=xmlstream.debug
@ -277,7 +278,7 @@ class Connection(xmlstream.Client):
if not self.handlers[name].has_key(ns): ns='' if not self.handlers[name].has_key(ns): ns=''
if not self.handlers[name].has_key(typ): typ='' if not self.handlers[name].has_key(typ): typ=''
if not self.handlers[name].has_key(typns): typns='' if not self.handlers[name].has_key(typns): typns=''
if typ==typns: typns='' if typ==typns: typns='' # we don't want to launch twice the same handler
chain=[] chain=[]
for key in ['default',typ,ns,typns]: # we will use all handlers: from very common to very particular for key in ['default',typ,ns,typns]: # we will use all handlers: from very common to very particular
@ -433,10 +434,13 @@ class Client(Connection):
self.send(Presence(type='unavailable')) self.send(Presence(type='unavailable'))
xmlstream.Client.disconnect(self) xmlstream.Client.disconnect(self)
def sendPresence(self,type=None,priority=None,show=None,status=None): def sendPresence(self,type=None,priority=None,show=None,status=None,signedStatus=None):
"""Sends a presence protocol element to the server. """Sends a presence protocol element to the server.
Used to inform the server that you are online""" Used to inform the server that you are online"""
self.send(Presence(type=type,priority=priority,show=show,status=status)) presence = Presence(type=type,priority=priority,show=show,status=status)
if signedStatus:
presence.setX(NS_XSIGNED).insertData(signedStatus)
self.send(presence)
sendInitPresence=sendPresence sendInitPresence=sendPresence
@ -612,12 +616,12 @@ class Client(Connection):
def requestRegInfo(self,agent=''): def requestRegInfo(self,agent=''):
"""Requests registration info from the server. """Requests registration info from the server.
Returns the Iq object received from the server.""" Returns the Iq object received from the server."""
# if agent: agent = agent + '.' if string.find(agent, self._host) == -1:
if agent: agent = agent + '.'
agent = + self._host
self._reg_info = {} self._reg_info = {}
# reg_iq = Iq(type='get', to = agent + self._host)
reg_iq = Iq(type='get', to = agent) reg_iq = Iq(type='get', to = agent)
reg_iq.setQuery(NS_REGISTER) reg_iq.setQuery(NS_REGISTER)
# self.DEBUG("Requesting reg info from %s%s:" % (agent, self._host), DBG_NODE_IQ)
self.DEBUG("Requesting reg info from %s:" % agent, DBG_NODE_IQ) self.DEBUG("Requesting reg info from %s:" % agent, DBG_NODE_IQ)
self.DEBUG(ustr(reg_iq),DBG_NODE_IQ) self.DEBUG(ustr(reg_iq),DBG_NODE_IQ)
return self.SendAndWaitForResponse(reg_iq) return self.SendAndWaitForResponse(reg_iq)
@ -637,11 +641,11 @@ class Client(Connection):
self._reg_info[key] = val self._reg_info[key] = val
def sendRegInfo(self, agent=None): def sendRegInfo(self, agent=''):
"""Sends the populated registration dictionary back to the server""" """Sends the populated registration dictionary back to the server"""
# if agent: agent = agent + '.' if string.find(agent, self._host) == -1:
if agent is None: agent = '' if agent: agent = agent + '.'
# reg_iq = Iq(to = agent + self._host, type='set') agent = agent + self._host
reg_iq = Iq(to = agent, type='set') reg_iq = Iq(to = agent, type='set')
q = reg_iq.setQuery(NS_REGISTER) q = reg_iq.setQuery(NS_REGISTER)
for info in self._reg_info.keys(): for info in self._reg_info.keys():
@ -649,22 +653,21 @@ class Client(Connection):
return self.SendAndWaitForResponse(reg_iq) return self.SendAndWaitForResponse(reg_iq)
def deregister(self, agent=None): def deregister(self, agent=''):
""" Send off a request to deregister with the server or with the given """ Send off a request to deregister with the server or with the given
agent. Returns True if successful, else False. agent. Returns True if successful, else False.
Note that you must be authorised before attempting to deregister. Note that you must be authorised before attempting to deregister.
""" """
if agent: if agent:
# agent = agent + '.' if string.find(agent, self._host) == -1:
# self.send(Presence(to=agent+self._host,type='unsubscribed')) # This is enough f.e. for icqv7t or jit agent = agent + '.' + self._host
self.send(Presence(to=agent,type='unsubscribed')) # This is enough f.e. for icqv7t or jit self.send(Presence(to=agent,type='unsubscribed')) # This is enough f.e. for icqv7t or jit
if agent is None: agent = '' else: agent = self._host
q = self.requestRegInfo() q = self.requestRegInfo()
kids = q.getQueryPayload() kids = q.getQueryPayload()
keyTag = kids.getTag("key") keyTag = kids.getTag("key")
# iq = Iq(to=agent+self._host, type="set")
iq = Iq(to=agent, type="set") iq = Iq(to=agent, type="set")
iq.setQuery(NS_REGISTER) iq.setQuery(NS_REGISTER)
iq.setQueryNode("") iq.setQueryNode("")
@ -731,12 +734,9 @@ class Client(Connection):
if not rep: if not rep:
return identities, features, items return identities, features, items
q = rep.getTag('service') q = rep.getTag('service')
if q:
identities = [q.attrs]
else:
identities = []
if not q: if not q:
return identities, features, items return identities, features, items
identities = [q.attrs]
for node in q.kids: for node in q.kids:
if node.getName() == 'ns': if node.getName() == 'ns':
features.append(node.getData()) features.append(node.getData())
@ -1442,10 +1442,6 @@ class JID:
__repr__ = __str__ __repr__ = __str__
def getBasic(self):
"""Returns a jid string with no resource"""
return self.node + '@' + self.domain
def getNode(self): def getNode(self):
"""Returns JID Node as string""" """Returns JID Node as string"""
return self.node return self.node