summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Nichols <tmnichols@gmail.com>2010-07-06 14:16:46 -0400
committerTom Nichols <tmnichols@gmail.com>2010-07-06 14:16:46 -0400
commit259dffeb6e4da0b879784ae9642f43c01b95811b (patch)
tree495255dc66de2ff35f2fb0f0fb1f4aa0a55d4e13
parent0a30e6c0175b13cef400d4cba27eaf145b07d365 (diff)
downloadslixmpp-259dffeb6e4da0b879784ae9642f43c01b95811b.tar.gz
slixmpp-259dffeb6e4da0b879784ae9642f43c01b95811b.tar.bz2
slixmpp-259dffeb6e4da0b879784ae9642f43c01b95811b.tar.xz
slixmpp-259dffeb6e4da0b879784ae9642f43c01b95811b.zip
send now has a priority and an 'init' parameter to denote stanzas that may be sent prior to session establishment.
-rw-r--r--sleekxmpp/__init__.py36
-rw-r--r--sleekxmpp/basexmpp.py4
-rw-r--r--sleekxmpp/stanza/iq.py6
-rw-r--r--sleekxmpp/xmlstream/stanzabase.py6
-rw-r--r--sleekxmpp/xmlstream/xmlstream.py13
5 files changed, 35 insertions, 30 deletions
diff --git a/sleekxmpp/__init__.py b/sleekxmpp/__init__.py
index 3b2b5e0f..f967d0ba 100644
--- a/sleekxmpp/__init__.py
+++ b/sleekxmpp/__init__.py
@@ -164,6 +164,12 @@ class ClientXMPP(basexmpp, XMLStream):
self.authenticated = False
self.sessionstarted = False
XMLStream.disconnect(self, reconnect, error)
+
+ def sendRaw(self, data, priority=5, init=False):
+ if not init and not self.sessionstarted:
+ logging.warn("Attempt to send stanza before session has started:\n%s", data)
+ return False
+ XMLStream.sendRaw(self, data, priority, init)
def registerFeature(self, mask, pointer, breaker = False):
"""Register a stream feature."""
@@ -201,7 +207,7 @@ class ClientXMPP(basexmpp, XMLStream):
_stanza = "<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls' />"
if not self.event_handlers.get(_stanza,None): # don't add handler > once
self.add_handler( _stanza, self.handler_tls_start, instream=True )
- self.sendPriorityRaw(self.tostring(xml))
+ self.sendRaw(self.tostring(xml), priority=1, init=True)
return True
else:
logging.warning("The module tlslite is required in to some servers, and has not been found.")
@@ -221,12 +227,16 @@ class ClientXMPP(basexmpp, XMLStream):
for sasl_mech in sasl_mechs:
self.features.append("sasl:%s" % sasl_mech.text)
if 'sasl:DIGEST-MD5' in self.features:
- self.sendPriorityRaw("""<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>""")
+ self.sendRaw("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='DIGEST-MD5'/>", priority=1, init=True)
elif 'sasl:PLAIN' in self.features:
if sys.version_info < (3,0):
- self.sendPriorityRaw("""<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>""" % base64.b64encode(b'\x00' + bytes(self.username) + b'\x00' + bytes(self.password)).decode('utf-8'))
+ self.sendRaw("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>" \
+ % base64.b64encode(b'\x00' + bytes(self.username) + b'\x00' + bytes(self.password)).decode('utf-8'),
+ priority=1, init=True)
else:
- self.sendPriorityRaw("""<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>""" % base64.b64encode(b'\x00' + bytes(self.username, 'utf-8') + b'\x00' + bytes(self.password, 'utf-8')).decode('utf-8'))
+ self.sendRaw("<auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='PLAIN'>%s</auth>" \
+ % base64.b64encode(b'\x00' + bytes(self.username, 'utf-8') + b'\x00' + bytes(self.password, 'utf-8')).decode('utf-8'),
+ priority=1, init=True)
else:
logging.error("No appropriate login method.")
self.disconnect()
@@ -235,14 +245,10 @@ class ClientXMPP(basexmpp, XMLStream):
return True
def handler_sasl_digest_md5_auth(self, xml):
- logging.debug(tostring(xml))
- logging.debug(xml)
- logging.debug(type(xml).__name__)
-
if self.digest_auth_started == False:
challenge = [item.split('=', 1) for item in base64.b64decode(xml.text).replace("\"", "").split(',', 6) ]
challenge = dict(challenge)
- logging.debug(challenge)
+ logging.debug("MD5 auth challenge: %s", challenge)
#Realm, nonce, qop should all be present
if not challenge['realm'] or not challenge['qop'] or not challenge['nonce']:
@@ -259,8 +265,10 @@ class ClientXMPP(basexmpp, XMLStream):
a1 = b"%s:%s:%s" %(md5("%s:%s:%s" % (self.username, self.domain, self.password)), challenge["nonce"].encode("UTF-8"), cnonce.encode("UTF-8") )
a2 = "AUTHENTICATE:xmpp/%s" %self.domain
responseHash = md5digest("%s:%s:00000001:%s:auth:%s" %(md5digest(a1), challenge["nonce"], cnonce, md5digest(a2) ) )
- response = '''charset=utf-8,username="%s",realm="%s",nonce="%s",nc=00000001,cnonce="%s",digest-uri="%s",response=%s,qop=%s,''' %(self.username, self.domain, challenge["nonce"], cnonce, "xmpp/%s" % self.domain, responseHash, challenge["qop"])
- self.sendPriorityRaw("""<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>""" %base64.encodestring(response)[:-1])
+ response = 'charset=utf-8,username="%s",realm="%s",nonce="%s",nc=00000001,cnonce="%s",digest-uri="%s",response=%s,qop=%s,' \
+ % (self.username, self.domain, challenge["nonce"], cnonce, "xmpp/%s" % self.domain, responseHash, challenge["qop"])
+ self.sendRaw("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>" % base64.encodestring(response)[:-1],
+ priority=1, init=True )
else:
logging.warn("handler_sasl_digest_md5_auth called while digest_auth_started is True (has already begun)")
@@ -287,7 +295,7 @@ class ClientXMPP(basexmpp, XMLStream):
res.text = self.resource
xml.append(res)
iq.append(xml)
- response = iq.send()
+ response = iq.send(priority=2,init=True)
#response = self.send(iq, self.Iq(sid=iq['id']))
self.set_jid(response.xml.find('{urn:ietf:params:xml:ns:xmpp-bind}bind/{urn:ietf:params:xml:ns:xmpp-bind}jid').text)
self.bound = True
@@ -300,12 +308,12 @@ class ClientXMPP(basexmpp, XMLStream):
def handler_start_session(self, xml):
if self.authenticated and self.bound:
iq = self.makeIqSet(xml)
- response = iq.send()
+ response = iq.send(priority=2,init=True)
logging.debug("Established Session")
self.sessionstarted = True
self.event("session_start")
else:
- #bind probably hasn't happened yet
+ logging.warn("Bind has failed; not starting session!")
self.bindfail = True
def _handleRoster(self, iq, request=False):
diff --git a/sleekxmpp/basexmpp.py b/sleekxmpp/basexmpp.py
index 25eeedb0..a0dca9d9 100644
--- a/sleekxmpp/basexmpp.py
+++ b/sleekxmpp/basexmpp.py
@@ -112,7 +112,7 @@ class basexmpp(object):
except:
logging.exception("Unable to load plugin: %s", plugin )
-
+
def register_plugins(self):
"""Initiates all plugins in the plugins/__init__.__all__"""
if self.plugin_whitelist:
@@ -138,7 +138,7 @@ class basexmpp(object):
self.registerHandler(XMLCallback('add_handler_%s' % self.getNewId(), MatchXMLMask(mask), pointer, threaded, disposable, instream))
def getId(self):
- return "%x".upper() % self.id
+ return "%X" % self.id
def sendXML(self, data, mask=None, timeout=10):
return self.send(self.tostring(data), mask, timeout)
diff --git a/sleekxmpp/stanza/iq.py b/sleekxmpp/stanza/iq.py
index 26f09268..2b3ca83b 100644
--- a/sleekxmpp/stanza/iq.py
+++ b/sleekxmpp/stanza/iq.py
@@ -67,11 +67,11 @@ class Iq(RootStanza):
self.xml.remove(child)
return self
- def send(self, block=True, timeout=10, priority=False):
+ def send(self, block=True, timeout=10, priority=5, init=False):
if block and self['type'] in ('get', 'set'):
waitfor = Waiter('IqWait_%s' % self['id'], MatcherId(self['id']))
self.stream.registerHandler(waitfor)
- StanzaBase.send(self, priority)
+ StanzaBase.send(self, priority, init)
return waitfor.wait(timeout)
else:
- return StanzaBase.send(self, priority)
+ return StanzaBase.send(self, priority, init)
diff --git a/sleekxmpp/xmlstream/stanzabase.py b/sleekxmpp/xmlstream/stanzabase.py
index 34513807..b9267b88 100644
--- a/sleekxmpp/xmlstream/stanzabase.py
+++ b/sleekxmpp/xmlstream/stanzabase.py
@@ -383,7 +383,7 @@ class StanzaBase(ElementBase):
def exception(self, e):
logging.error(traceback.format_tb(e))
- def send(self, priority=False):
- if priority: self.stream.sendPriorityRaw(self.__str__())
- else: self.stream.sendRaw(self.__str__())
+ def send(self, priority=5, init=False):
+ self.stream.sendRaw(self.__str__(), priority, init)
+
diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py
index fd0b0fa0..59b0cc62 100644
--- a/sleekxmpp/xmlstream/xmlstream.py
+++ b/sleekxmpp/xmlstream/xmlstream.py
@@ -219,7 +219,7 @@ class XMLStream(object):
while not self.quit.is_set():
if not self.state.ensure('connected',wait=2, block_on_transition=True): continue
try:
- self.sendPriorityRaw(self.stream_header)
+ self.sendRaw(self.stream_header, priority=0, init=True)
self.__readXML() # this loops until the stream is terminated.
except socket.timeout:
# TODO currently this will re-send a stream header if this exception occurs.
@@ -299,12 +299,9 @@ class XMLStream(object):
reconnect = (self.should_reconnect and not self.quit.is_set())
self.disconnect(reconnect=reconnect, error=True)
- def sendRaw(self, data):
- self.sendqueue.put((1, data))
- return True
-
- def sendPriorityRaw(self, data):
- self.sendqueue.put((0, data))
+ def sendRaw( self, data, priority=5, init=False ):
+ if not self.state.ensure('connected'): return False
+ self.sendqueue.put((priority, data))
return True
def disconnect(self, reconnect=False, error=False):
@@ -316,7 +313,7 @@ class XMLStream(object):
logging.debug("Disconnecting...")
# don't send a footer on error; if the stream is already closed,
# this won't get sent until the stream is re-initialized!
- if not error: self.sendRaw(self.stream_footer) #send end of stream
+ if not error: self.sendRaw(self.stream_footer,init=True) #send end of stream
try:
# self.socket.shutdown(socket.SHUT_RDWR)
self.socket.close()