Jabberpy V0.5

This commit is contained in:
Yann Leboulanger 2004-03-06 04:11:57 +00:00
parent cf2c7b5037
commit fa49d30545
2 changed files with 108 additions and 106 deletions

View File

@ -189,12 +189,12 @@ def ustr(what):
return r return r
xmlstream.ustr = ustr xmlstream.ustr = ustr
class NodeProcessed(Exception): pass # currently only for Connection._expectedIqHandler class NodeProcessed(Exception): pass # currently only for Connection._expectedIqHandler
class Connection(xmlstream.Client): class Connection(xmlstream.Client):
"""Forms the base for both Client and Component Classes""" """Forms the base for both Client and Component Classes"""
def __init__(self, host, port, namespace, def __init__(self, host, port, namespace,
debug=False, log=False, connection=xmlstream.TCP, hostIP=None, proxy=None): debug=[], log=False, connection=xmlstream.TCP, hostIP=None, proxy=None):
xmlstream.Client.__init__(self, host, port, namespace, xmlstream.Client.__init__(self, host, port, namespace,
debug=debug, log=log, debug=debug, log=log,
@ -276,7 +276,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=''
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
@ -410,7 +410,7 @@ class Connection(xmlstream.Client):
class Client(Connection): class Client(Connection):
"""Class for managing a client connection to a jabber server.""" """Class for managing a client connection to a jabber server."""
def __init__(self, host, port=5222, debug=False, log=False, def __init__(self, host, port=5222, debug=[], log=False,
connection=xmlstream.TCP, hostIP=None, proxy=None): connection=xmlstream.TCP, hostIP=None, proxy=None):
Connection.__init__(self, host, port, NS_CLIENT, debug, log, Connection.__init__(self, host, port, NS_CLIENT, debug, log,
@ -592,7 +592,6 @@ class Client(Connection):
if groups != None: if groups != None:
for group in groups: for group in groups:
item.insertTag('group').insertData(group) item.insertTag('group').insertData(group)
# self.send(iq)
dummy = self.SendAndWaitForResponse(iq) # Do we need to wait?? dummy = self.SendAndWaitForResponse(iq) # Do we need to wait??
@ -656,14 +655,16 @@ class Client(Connection):
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 + '.' # agent = agent + '.'
self.send(Presence(to=agent+self._host,type='unsubscribed')) # This is enough f.e. for icqv7t or jit # self.send(Presence(to=agent+self._host,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 = '' if agent is None: agent = ''
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+self._host, type="set")
iq = Iq(to=agent, type="set")
iq.setQuery(NS_REGISTER) iq.setQuery(NS_REGISTER)
iq.setQueryNode("") iq.setQueryNode("")
q = iq.getQueryNode() q = iq.getQueryNode()
@ -808,7 +809,7 @@ class Protocol(xmlstream.Node):
def getX(self,index=0): def getX(self,index=0):
"""Returns the x namespace, optionally passed an index if there are """Returns the x namespace, optionally passed an index if there are
multiple tags.""" multiple tags."""
try: return self.getXNodes('x')[index].namespace try: return self.getXNodes()[index].namespace
except: return None except: return None
@ -1342,7 +1343,7 @@ class JID:
def getBasic(self): def getBasic(self):
"""Returns a jid string with no resource""" """Returns a jid string with no resource"""
return self.node + '@' + self.domain return self.node + '@' + self.domain
def getNode(self): def getNode(self):
"""Returns JID Node as string""" """Returns JID Node as string"""
@ -1395,7 +1396,7 @@ class JID:
class Component(Connection): class Component(Connection):
"""docs to come soon... """ """docs to come soon... """
def __init__(self, host, port, connection=xmlstream.TCP, def __init__(self, host, port, connection=xmlstream.TCP,
debug=False, log=False, ns=NS_COMP_ACCEPT, hostIP=None, proxy=None): debug=[], log=False, ns=NS_COMP_ACCEPT, hostIP=None, proxy=None):
Connection.__init__(self, host, port, namespace=ns, debug=debug, Connection.__init__(self, host, port, namespace=ns, debug=debug,
log=log, connection=connection, hostIP=hostIP, proxy=proxy) log=log, connection=connection, hostIP=hostIP, proxy=proxy)
self._auth_OK = False self._auth_OK = False

View File

@ -28,7 +28,7 @@ case.
""" """
# $Id: xmlstream.py,v 1.42 2004/01/08 15:47:40 snakeru Exp $ # $Id$
import time, sys, re, socket import time, sys, re, socket
from select import select from select import select
@ -37,7 +37,7 @@ import xml.parsers.expat
import debug import debug
_debug=debug _debug=debug
VERSION = "0.5-rc1" VERSION = "0.5"
False = 0 False = 0
True = 1 True = 1
@ -93,7 +93,7 @@ class Node:
if parent: self.parent = parent if parent: self.parent = parent
# if self.parent and not self.namespace: self.namespace=self.parent.namespace # Doesn't checked if this neccessary # if self.parent and not self.namespace: self.namespace=self.parent.namespace # Doesn't checked if this neccessary
for attr in attrs.keys(): for attr in attrs.keys():
self.attrs[attr]=attrs[attr] self.attrs[attr]=attrs[attr]
@ -101,7 +101,7 @@ class Node:
for i in payload: for i in payload:
if type(i)==type(self): self.insertNode(i) if type(i)==type(self): self.insertNode(i)
else: self.insertXML(i) else: self.insertXML(i)
# self.insertNode(Node(node=i)) # Alternative way. Needs perfomance testing. # self.insertNode(Node(node=i)) # Alternative way. Needs perfomance testing.
def setParent(self, node): def setParent(self, node):
"Set the nodes parent node." "Set the nodes parent node."
@ -199,12 +199,13 @@ class Node:
s = s + "</" + self.name + ">" s = s + "</" + self.name + ">"
return s return s
def getTag(self, name): def getTag(self, name, index=None):
"""Returns a child node with tag name. Returns None """Returns a child node with tag name. Returns None
if not found.""" if not found."""
for node in self.kids: for node in self.kids:
if node.getName() == name: if node.getName() == name:
return node if not index: return node
if index is not None: index-=1
return None return None
def getTags(self, name): def getTags(self, name):
@ -379,7 +380,7 @@ class Stream(NodeBuilder):
data = self.read() data = self.read()
self._parser.Parse(data) self._parser.Parse(data)
return len(data) return len(data)
return '0' # Zero means that nothing received but link is alive. return '0' # Zero means that nothing received but link is alive.
def disconnect(self): def disconnect(self):
"""Close the stream and socket""" """Close the stream and socket"""