2009-06-03 22:56:51 +00:00
#!/usr/bin/python2.5
"""
2010-05-12 20:51:14 +00:00
SleekXMPP : The Sleek XMPP Library
Copyright ( C ) 2010 Nathanael C . Fritz
This file is part of SleekXMPP .
2009-06-03 22:56:51 +00:00
2010-05-12 20:51:14 +00:00
See the file license . txt for copying permission .
2009-06-03 22:56:51 +00:00
"""
2010-07-01 13:46:12 +00:00
from __future__ import absolute_import , unicode_literals
2009-06-03 22:56:51 +00:00
from . basexmpp import basexmpp
from xml . etree import cElementTree as ET
from . xmlstream . xmlstream import XMLStream
from . xmlstream . xmlstream import RestartStream
from . xmlstream . matcher . xmlmask import MatchXMLMask
from . xmlstream . matcher . xpath import MatchXPath
from . xmlstream . handler . callback import Callback
import logging
import base64
import sys
import random
2010-06-18 13:51:29 +00:00
from xml . etree . cElementTree import tostring
2009-06-03 22:56:51 +00:00
srvsupport = True
try :
import dns . resolver
2010-05-13 18:39:32 +00:00
import dns . rdatatype
2010-07-09 21:23:02 +00:00
import dns . exception
2009-06-03 22:56:51 +00:00
except ImportError :
srvsupport = False
class ClientXMPP ( basexmpp , XMLStream ) :
""" SleekXMPP ' s client class. Use only for good, not evil. """
def __init__ ( self , jid , password , ssl = False , plugin_config = { } , plugin_whitelist = [ ] , escape_quotes = True ) :
XMLStream . __init__ ( self )
self . default_ns = ' jabber:client '
basexmpp . __init__ ( self )
self . plugin_config = plugin_config
self . escape_quotes = escape_quotes
self . set_jid ( jid )
2010-05-12 20:51:14 +00:00
self . port = 5222 # not used if DNS SRV is used
2009-06-03 22:56:51 +00:00
self . plugin_whitelist = plugin_whitelist
self . auto_reconnect = True
self . srvsupport = srvsupport
self . password = password
self . registered_features = [ ]
2010-05-12 20:51:14 +00:00
self . stream_header = """ <stream:stream to= ' %s ' xmlns:stream= ' http://etherx.jabber.org/streams ' xmlns= ' %s ' version= ' 1.0 ' > """ % ( self . domain , self . default_ns )
2009-06-03 22:56:51 +00:00
self . stream_footer = " </stream:stream> "
#self.map_namespace('http://etherx.jabber.org/streams', 'stream')
#self.map_namespace('jabber:client', '')
self . features = [ ]
2009-12-11 01:29:46 +00:00
#TODO: Use stream state here
2009-06-03 22:56:51 +00:00
self . authenticated = False
self . sessionstarted = False
2010-06-02 04:40:52 +00:00
self . bound = False
2010-06-02 04:44:54 +00:00
self . bindfail = False
2010-06-18 13:51:29 +00:00
XMLStream . registerHandler ( self , Callback ( ' Stream Features ' , MatchXPath ( ' { http://etherx.jabber.org/streams}features ' ) , self . _handleStreamFeatures , thread = True ) )
XMLStream . registerHandler ( self , Callback ( ' Roster Update ' , MatchXPath ( ' { %s }iq/ { jabber:iq:roster}query ' % self . default_ns ) , self . _handleRoster , thread = True ) )
2010-06-30 18:30:18 +00:00
#SASL Auth handlers
basexmpp . add_handler ( self , " <challenge xmlns= ' urn:ietf:params:xml:ns:xmpp-sasl ' /> " , self . handler_sasl_digest_md5_auth , instream = True )
basexmpp . add_handler ( self , " <response xmlns= ' urn:ietf:params:xml:ns:xmpp-sasl ' /> " , self . handler_sasl_digest_md5_auth_fail , instream = True )
basexmpp . add_handler ( self , " <success xmlns= ' urn:ietf:params:xml:ns:xmpp-sasl ' /> " , self . handler_auth_success , instream = True )
basexmpp . add_handler ( self , " <failure xmlns= ' urn:ietf:params:xml:ns:xmpp-sasl ' /> " , self . handler_auth_fail , instream = True )
2010-03-26 21:02:10 +00:00
#self.registerHandler(Callback('Roster Update', MatchXMLMask("<presence xmlns='%s' type='subscribe' />" % self.default_ns), self._handlePresenceSubscribe, thread=True))
2009-06-03 22:56:51 +00:00
self . registerFeature ( " <starttls xmlns= ' urn:ietf:params:xml:ns:xmpp-tls ' /> " , self . handler_starttls , True )
self . registerFeature ( " <mechanisms xmlns= ' urn:ietf:params:xml:ns:xmpp-sasl ' /> " , self . handler_sasl_auth , True )
self . registerFeature ( " <bind xmlns= ' urn:ietf:params:xml:ns:xmpp-bind ' /> " , self . handler_bind_resource )
2009-06-24 06:09:20 +00:00
self . registerFeature ( " <session xmlns= ' urn:ietf:params:xml:ns:xmpp-session ' /> " , self . handler_start_session )
2009-06-03 22:56:51 +00:00
#self.registerStanzaExtension('PresenceStanza', PresenceStanzaType)
#self.register_plugins()
def __getitem__ ( self , key ) :
2009-09-01 17:24:52 +00:00
if key in self . plugin :
2009-06-03 22:56:51 +00:00
return self . plugin [ key ]
else :
logging . warning ( """ Plugin " %s " is not loaded. """ % key )
return False
def get ( self , key , default ) :
return self . plugin . get ( key , default )
2010-05-12 20:51:14 +00:00
def connect ( self , host = None , port = None ) :
2009-06-03 22:56:51 +00:00
""" Connect to the Jabber Server. Attempts SRV lookup, and if it fails, uses
2010-07-09 21:25:11 +00:00
the JID server . You can optionally specify a host / port if you ' re not using
DNS and want to connect to a server address that is different from the XMPP domain . """
2010-05-12 20:51:14 +00:00
2010-06-02 02:51:49 +00:00
if self . state [ ' connected ' ] : return True
2010-07-09 21:25:11 +00:00
if host : # if a host was specified, don't attempt a DNS lookup.
2010-05-12 20:51:14 +00:00
if port is None : port = self . port
else :
2009-06-03 22:56:51 +00:00
if not self . srvsupport :
2010-07-09 21:25:11 +00:00
logging . warn ( " Did not supply (address, port) to connect to and no SRV support is installed (http://www.dnspython.org). Continuing to attempt connection, using domain from JID. " )
2009-06-03 22:56:51 +00:00
else :
logging . debug ( " Since no address is supplied, attempting SRV lookup. " )
try :
2010-07-09 21:25:11 +00:00
answers = dns . resolver . query ( " _xmpp-client._tcp. %s " % self . domain , dns . rdatatype . SRV )
2009-06-03 22:56:51 +00:00
except dns . resolver . NXDOMAIN :
2010-07-09 21:25:11 +00:00
logging . info ( " No appropriate SRV record found for %s . Using domain as server address. " , self . domain )
2010-07-09 21:23:02 +00:00
except dns . exception . DNSException :
# this could be a timeout or other DNS error. Worth retrying?
2010-07-09 21:25:11 +00:00
logging . exception ( " DNS error during SRV query for %s . Using domain as server address. " , self . domain )
2009-06-03 22:56:51 +00:00
else :
# pick a random answer, weighted by priority
# there are less verbose ways of doing this (random.choice() with answer * priority), but I chose this way anyway
# suggestions are welcome
addresses = { }
intmax = 0
priorities = [ ]
for answer in answers :
intmax + = answer . priority
addresses [ intmax ] = ( answer . target . to_text ( ) [ : - 1 ] , answer . port )
priorities . append ( intmax ) # sure, I could just do priorities = addresses.keys()\n priorities.sort()
picked = random . randint ( 0 , intmax )
for priority in priorities :
if picked < = priority :
2010-05-12 20:51:14 +00:00
( host , port ) = addresses [ priority ]
2009-06-03 22:56:51 +00:00
break
2010-05-12 20:51:14 +00:00
if not host :
2009-06-03 22:56:51 +00:00
# if all else fails take server from JID.
2010-05-12 20:51:14 +00:00
( host , port ) = ( self . domain , self . port )
logging . debug ( ' Attempting connection to %s : %d ' , host , port )
2010-07-09 20:12:32 +00:00
result = XMLStream . connect ( self , host , port )
2009-06-03 22:56:51 +00:00
if result :
self . event ( " connected " )
else :
2009-08-31 22:46:31 +00:00
logging . warning ( " Failed to connect " )
2009-06-03 22:56:51 +00:00
self . event ( " disconnected " )
return result
# overriding reconnect and disconnect so that we can get some events
# should events be part of or required by xmlstream? Maybe that would be cleaner
def reconnect ( self ) :
2010-06-02 18:17:36 +00:00
self . disconnect ( reconnect = True )
2009-06-03 22:56:51 +00:00
2010-07-02 20:46:34 +00:00
def disconnect ( self , reconnect = False , error = False ) :
2009-06-03 22:56:51 +00:00
self . event ( " disconnected " )
Fixes for disconnection problems detailed in http://github.com/fritzy/SleekXMPP/issues/#issue/20
Fixes to both ClientXMPP & xmlstream. ClientXMPP was not tracking the changes to authenticated and sessionstarted after the client was disconnected.
xmlstream had some funkyness with state in the _process method that was cleaned up and hopefully made a little cleaner.
Also changed a DNS issue that was occuring that rendered me unable to disconnect. I would recieve the following error upon reconnect.
Exception in thread process:
Exception in thread process:
Traceback (most recent call last):
File "/usr/local/lib/python2.6/threading.py", line 532, in __bootstrap_inner
self.run()
File "/usr/local/lib/python2.6/threading.py", line 484, in run
self.__target(*self.__args, **self.__kwargs)
File "/home/macdiesel/tmp/workspace/SleekXMPP/sleekxmpp/xmlstream/xmlstream.py", line 202, in _process
self.reconnect()
File "/home/macdiesel/tmp/workspace/SleekXMPP/sleekxmpp/__init__.py", line 134, in reconnect
XMLStream.reconnect(self)
File "/home/macdiesel/tmp/workspace/SleekXMPP/sleekxmpp/xmlstream/xmlstream.py", line 289, in reconnect
self.connect()
File "/home/macdiesel/tmp/workspace/SleekXMPP/sleekxmpp/__init__.py", line 99, in connect
answers = dns.resolver.query("_xmpp-client._tcp.%s" % self.server, "SRV")
File "/usr/local/lib/python2.6/site-packages/dns/resolver.py", line 732, in query
return get_default_resolver().query(qname, rdtype, rdclass, tcp, source)
File "/usr/local/lib/python2.6/site-packages/dns/resolver.py", line 617, in query
source=source)
File "/usr/local/lib/python2.6/site-packages/dns/query.py", line 113, in udp
wire = q.to_wire()
File "/usr/local/lib/python2.6/site-packages/dns/message.py", line 404, in to_wire
r.add_question(rrset.name, rrset.rdtype, rrset.rdclass)
File "/usr/local/lib/python2.6/site-packages/dns/renderer.py", line 152, in add_question
self.output.write(struct.pack("!HH", rdtype, rdclass))
TypeError: unsupported operand type(s) for &: 'unicode' and 'long'
Seems I was getting this error when calling line 99 in ClientXMPP. You can't bit-shift a 1 and a string and this is why this error is coming up. I removed the "SRV" argument and used the default of 1. not sure exactly what this should be so it may need to be fixed back before it's merged back to trunk.
The line in question:
answers = dns.resolver.query("_xmpp-client._tcp.%s" % self.server, "SRV")
2010-05-04 18:03:38 +00:00
self . authenticated = False
self . sessionstarted = False
2010-07-02 20:46:34 +00:00
XMLStream . disconnect ( self , reconnect , error )
2010-07-06 18:16:46 +00:00
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 )
2009-06-03 22:56:51 +00:00
def registerFeature ( self , mask , pointer , breaker = False ) :
""" Register a stream feature. """
self . registered_features . append ( ( MatchXMLMask ( mask ) , pointer , breaker ) )
def updateRoster ( self , jid , name = None , subscription = None , groups = [ ] ) :
""" Add or change a roster item. """
2009-12-11 01:29:46 +00:00
iq = self . Iq ( ) . setValues ( { ' type ' : ' set ' } )
iq [ ' roster ' ] = { jid : { ' name ' : name , ' subscription ' : subscription , ' groups ' : groups } }
#self.send(iq, self.Iq().setValues({'id': iq['id']}))
r = iq . send ( )
return r [ ' type ' ] == ' result '
2009-06-03 22:56:51 +00:00
def getRoster ( self ) :
""" Request the roster be sent. """
2010-03-26 20:55:03 +00:00
iq = self . Iq ( ) . setValues ( { ' type ' : ' get ' } ) . enable ( ' roster ' ) . send ( )
self . _handleRoster ( iq , request = True )
2009-06-03 22:56:51 +00:00
def _handleStreamFeatures ( self , features ) :
2010-06-02 02:51:49 +00:00
logging . debug ( ' handling stream features ' )
2009-09-25 17:35:10 +00:00
self . features = [ ]
2009-06-24 06:09:20 +00:00
for sub in features . xml :
self . features . append ( sub . tag )
2009-06-03 22:56:51 +00:00
for subelement in features . xml :
for feature in self . registered_features :
if feature [ 0 ] . match ( subelement ) :
#if self.maskcmp(subelement, feature[0], True):
2010-06-02 02:51:49 +00:00
# This calls the feature handler & optionally breaks
2009-06-03 22:56:51 +00:00
if feature [ 1 ] ( subelement ) and feature [ 2 ] : #if breaker, don't continue
return True
def handler_starttls ( self , xml ) :
2010-06-02 02:51:49 +00:00
logging . debug ( ' TLS start handler; SSL support: %s ' , self . ssl_support )
2009-09-25 17:35:10 +00:00
if not self . authenticated and self . ssl_support :
2010-06-02 02:51:49 +00:00
_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 )
2010-07-06 18:16:46 +00:00
self . sendRaw ( self . tostring ( xml ) , priority = 1 , init = True )
2009-06-03 22:56:51 +00:00
return True
else :
logging . warning ( " The module tlslite is required in to some servers, and has not been found. " )
return False
def handler_tls_start ( self , xml ) :
logging . debug ( " Starting TLS " )
if self . startTLS ( ) :
raise RestartStream ( )
def handler_sasl_auth ( self , xml ) :
2009-09-25 17:35:10 +00:00
if ' { urn:ietf:params:xml:ns:xmpp-tls}starttls ' in self . features :
return False
2009-06-03 22:56:51 +00:00
logging . debug ( " Starting SASL Auth " )
sasl_mechs = xml . findall ( ' { urn:ietf:params:xml:ns:xmpp-sasl}mechanism ' )
if len ( sasl_mechs ) :
for sasl_mech in sasl_mechs :
self . features . append ( " sasl: %s " % sasl_mech . text )
2010-06-18 13:51:29 +00:00
if ' sasl:DIGEST-MD5 ' in self . features :
2010-07-06 18:16:46 +00:00
self . sendRaw ( " <auth xmlns= ' urn:ietf:params:xml:ns:xmpp-sasl ' mechanism= ' DIGEST-MD5 ' /> " , priority = 1 , init = True )
2010-06-18 13:51:29 +00:00
elif ' sasl:PLAIN ' in self . features :
2010-01-08 06:03:02 +00:00
if sys . version_info < ( 3 , 0 ) :
2010-07-06 18:16:46 +00:00
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 )
2010-01-08 06:03:02 +00:00
else :
2010-07-06 18:16:46 +00:00
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 )
2009-06-03 22:56:51 +00:00
else :
2010-07-12 16:55:53 +00:00
logging . error ( " No appropriate login method: %s " , sasl_mechs )
self . handler_auth_fail ( xml )
return False
2009-06-03 22:56:51 +00:00
return True
2010-06-18 13:51:29 +00:00
def handler_sasl_digest_md5_auth ( self , xml ) :
2010-07-12 16:55:53 +00:00
challenge = [ item . split ( ' = ' , 1 ) for item in base64 . b64decode ( xml . text ) . replace ( " \" " , " " ) . split ( ' , ' , 6 ) ]
challenge = dict ( challenge )
logging . debug ( " MD5 auth challenge: %s " , challenge )
if challenge . get ( ' rspauth ' ) : #authenticated success... send response
self . sendRaw ( """ <response xmlns= ' urn:ietf:params:xml:ns:xmpp-sasl ' /> """ , priority = 1 , init = True )
return
2010-07-02 17:31:55 +00:00
2010-07-12 16:55:53 +00:00
#TODO: use realm if supplied by server, use default qop unless supplied by server
#Realm, nonce, qop should all be present
if not challenge . get ( ' qop ' ) or not challenge . get ( ' nonce ' ) :
logging . error ( " Error during digest-md5 authentication. Challenge missing critical information. Challenge: %s " % base64 . b64decode ( xml . text ) )
self . handler_auth_fail ( xml )
return
#TODO: charset can be either UTF-8 or if not present use ISO 8859-1 defaulting for UTF-8 for now
#Compute the cnonce - a unique hex string only used in this request
cnonce = " "
for i in range ( 7 ) :
cnonce + = hex ( int ( random . random ( ) * 65536 * 4096 ) ) [ 2 : ]
cnonce = base64 . encodestring ( cnonce ) [ 0 : - 1 ]
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 . sendRaw ( " <response xmlns= ' urn:ietf:params:xml:ns:xmpp-sasl ' > %s </response> " % base64 . encodestring ( response ) [ : - 1 ] ,
priority = 1 , init = True )
2010-06-18 13:51:29 +00:00
def handler_sasl_digest_md5_auth_fail ( self , xml ) :
2010-07-12 16:55:53 +00:00
self . authenticated = False
2010-06-18 13:51:29 +00:00
self . handler_auth_fail ( xml )
2009-06-03 22:56:51 +00:00
def handler_auth_success ( self , xml ) :
2010-06-02 02:51:49 +00:00
logging . debug ( " Authentication successful. " )
2009-06-03 22:56:51 +00:00
self . authenticated = True
self . features = [ ]
raise RestartStream ( )
def handler_auth_fail ( self , xml ) :
2010-06-02 02:51:49 +00:00
logging . warning ( " Authentication failed. " )
2010-06-18 13:51:29 +00:00
logging . debug ( tostring ( xml , ' utf-8 ' ) )
2009-06-03 22:56:51 +00:00
self . disconnect ( )
self . event ( " failed_auth " )
def handler_bind_resource ( self , xml ) :
logging . debug ( " Requesting resource: %s " % self . resource )
res = ET . Element ( ' resource ' )
res . text = self . resource
2010-07-12 17:07:24 +00:00
xml . append ( res )
iq = self . makeIqSet ( xml )
2010-07-06 18:16:46 +00:00
response = iq . send ( priority = 2 , init = True )
2009-12-11 01:29:46 +00:00
#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 )
2010-06-02 04:40:52 +00:00
self . bound = True
2009-06-03 22:56:51 +00:00
logging . info ( " Node set to: %s " % self . fulljid )
2010-06-02 04:44:54 +00:00
if " { urn:ietf:params:xml:ns:xmpp-session}session " not in self . features or self . bindfail :
2009-06-24 06:09:20 +00:00
logging . debug ( " Established Session " )
self . sessionstarted = True
self . event ( " session_start " )
2009-06-03 22:56:51 +00:00
def handler_start_session ( self , xml ) :
2010-06-02 04:40:52 +00:00
if self . authenticated and self . bound :
2010-04-08 06:10:32 +00:00
iq = self . makeIqSet ( xml )
2010-07-06 18:16:46 +00:00
response = iq . send ( priority = 2 , init = True )
2009-06-03 22:56:51 +00:00
logging . debug ( " Established Session " )
self . sessionstarted = True
self . event ( " session_start " )
2010-06-02 04:44:54 +00:00
else :
2010-07-06 18:16:46 +00:00
logging . warn ( " Bind has failed; not starting session! " )
2010-06-02 04:44:54 +00:00
self . bindfail = True
2009-06-03 22:56:51 +00:00
2010-03-26 20:55:03 +00:00
def _handleRoster ( self , iq , request = False ) :
if iq [ ' type ' ] == ' set ' or ( iq [ ' type ' ] == ' result ' and request ) :
for jid in iq [ ' roster ' ] [ ' items ' ] :
if not jid in self . roster :
self . roster [ jid ] = { ' groups ' : [ ] , ' name ' : ' ' , ' subscription ' : ' none ' , ' presence ' : { } , ' in_roster ' : True }
self . roster [ jid ] . update ( iq [ ' roster ' ] [ ' items ' ] [ jid ] )
if iq [ ' type ' ] == ' set ' :
self . send ( self . Iq ( ) . setValues ( { ' type ' : ' result ' , ' id ' : iq [ ' id ' ] } ) . enable ( ' roster ' ) )
2009-12-11 01:29:46 +00:00
self . event ( " roster_update " , iq )
2010-06-18 13:51:29 +00:00
2010-06-30 17:54:53 +00:00
def md5 ( data ) :
2010-06-18 13:51:29 +00:00
try :
import hashlib
2010-06-30 17:54:53 +00:00
md5 = hashlib . md5 ( data )
2010-06-18 13:51:29 +00:00
except ImportError :
import md5
2010-06-30 17:54:53 +00:00
md5 = md5 . new ( data )
2010-06-18 13:51:29 +00:00
return md5 . digest ( )
2010-06-30 17:54:53 +00:00
def md5digest ( data ) :
try :
import hashlib
md5 = hashlib . md5 ( data )
except ImportError :
import md5
md5 = md5 . new ( data )
2010-07-01 13:17:45 +00:00
return md5 . hexdigest ( )