Merge branch 'hacks' of github.com:tomstrummer/SleekXMPP

This commit is contained in:
Thom Nichols 2010-05-18 16:11:27 -04:00
commit 3e83b16a58
3 changed files with 48 additions and 23 deletions

View File

@ -1,11 +1,11 @@
#!/usr/bin/python2.5 #!/usr/bin/python2.5
""" """
SleekXMPP: The Sleek XMPP Library SleekXMPP: The Sleek XMPP Library
Copyright (C) 2010 Nathanael C. Fritz Copyright (C) 2010 Nathanael C. Fritz
This file is part of SleekXMPP. This file is part of SleekXMPP.
See the file license.txt for copying permission. See the file license.txt for copying permission.
""" """
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
from . basexmpp import basexmpp from . basexmpp import basexmpp
@ -54,12 +54,14 @@ class ClientXMPP(basexmpp, XMLStream):
self.plugin_config = plugin_config self.plugin_config = plugin_config
self.escape_quotes = escape_quotes self.escape_quotes = escape_quotes
self.set_jid(jid) self.set_jid(jid)
self.server = None
self.port = 5222 # not used if DNS SRV is used
self.plugin_whitelist = plugin_whitelist self.plugin_whitelist = plugin_whitelist
self.auto_reconnect = True self.auto_reconnect = True
self.srvsupport = srvsupport self.srvsupport = srvsupport
self.password = password self.password = password
self.registered_features = [] self.registered_features = []
self.stream_header = """<stream:stream to='%s' xmlns:stream='http://etherx.jabber.org/streams' xmlns='%s' version='1.0'>""" % (self.server,self.default_ns) self.stream_header = """<stream:stream to='%s' xmlns:stream='http://etherx.jabber.org/streams' xmlns='%s' version='1.0'>""" % (self.domain,self.default_ns)
self.stream_footer = "</stream:stream>" self.stream_footer = "</stream:stream>"
#self.map_namespace('http://etherx.jabber.org/streams', 'stream') #self.map_namespace('http://etherx.jabber.org/streams', 'stream')
#self.map_namespace('jabber:client', '') #self.map_namespace('jabber:client', '')
@ -88,12 +90,16 @@ class ClientXMPP(basexmpp, XMLStream):
def get(self, key, default): def get(self, key, default):
return self.plugin.get(key, default) return self.plugin.get(key, default)
def connect(self, address=tuple()): def connect(self, host=None, port=None):
"""Connect to the Jabber Server. Attempts SRV lookup, and if it fails, uses """Connect to the Jabber Server. Attempts SRV lookup, and if it fails, uses
the JID server.""" the JID server."""
if not address or len(address) < 2:
if host:
self.server = host
if port is None: port = self.port
else:
if not self.srvsupport: if not self.srvsupport:
logging.debug("Did not supply (address, port) to connect to and no SRV support is installed (http://www.dnspython.org). Continuing to attempt connection, using server hostname from JID.") logging.debug("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.")
else: else:
logging.debug("Since no address is supplied, attempting SRV lookup.") logging.debug("Since no address is supplied, attempting SRV lookup.")
try: try:
@ -115,12 +121,19 @@ class ClientXMPP(basexmpp, XMLStream):
picked = random.randint(0, intmax) picked = random.randint(0, intmax)
for priority in priorities: for priority in priorities:
if picked <= priority: if picked <= priority:
address = addresses[priority] (host,port) = addresses[priority]
break break
if not address: # if SRV lookup was successful, we aren't using a particular server.
self.server = None
if not host:
# if all else fails take server from JID. # if all else fails take server from JID.
address = (self.server, 5222) (host,port) = (self.domain, self.port)
result = XMLStream.connect(self, address[0], address[1], use_tls=True) self.server = None
logging.debug('Attempting connection to %s:%d', host, port )
#TODO option to not use TLS?
result = XMLStream.connect(self, host, port, use_tls=True)
if result: if result:
self.event("connected") self.event("connected")
else: else:

View File

@ -49,7 +49,7 @@ class basexmpp(object):
self.resource = '' self.resource = ''
self.jid = '' self.jid = ''
self.username = '' self.username = ''
self.server = '' self.domain = ''
self.plugin = {} self.plugin = {}
self.auto_authorize = True self.auto_authorize = True
self.auto_subscribe = True self.auto_subscribe = True
@ -84,7 +84,7 @@ class basexmpp(object):
self.resource = self.getjidresource(jid) self.resource = self.getjidresource(jid)
self.jid = self.getjidbare(jid) self.jid = self.getjidbare(jid)
self.username = jid.split('@', 1)[0] self.username = jid.split('@', 1)[0]
self.server = jid.split('@',1)[-1].split('/', 1)[0] self.domain = jid.split('@',1)[-1].split('/', 1)[0]
def registerPlugin(self, plugin, pconfig = {}): def registerPlugin(self, plugin, pconfig = {}):
"""Register a plugin not in plugins.__init__.__all__ but in the plugins """Register a plugin not in plugins.__init__.__all__ but in the plugins

View File

@ -69,6 +69,7 @@ class XMLStream(object):
self.filesocket = None self.filesocket = None
self.use_ssl = False self.use_ssl = False
self.use_tls = False self.use_tls = False
self.ca_certs=None
self.stream_header = "<stream>" self.stream_header = "<stream>"
self.stream_footer = "</stream>" self.stream_footer = "</stream>"
@ -112,7 +113,7 @@ class XMLStream(object):
self.socket.settimeout(None) self.socket.settimeout(None)
if self.use_ssl and self.ssl_support: if self.use_ssl and self.ssl_support:
logging.debug("Socket Wrapped for SSL") logging.debug("Socket Wrapped for SSL")
self.socket = ssl.wrap_socket(self.socket) self.socket = ssl.wrap_socket(self.socket,ca_certs=self.ca_certs)
try: try:
self.socket.connect(self.address) self.socket.connect(self.address)
#self.filesocket = self.socket.makefile('rb', 0) #self.filesocket = self.socket.makefile('rb', 0)
@ -131,8 +132,13 @@ class XMLStream(object):
if self.ssl_support: if self.ssl_support:
logging.info("Negotiating TLS") logging.info("Negotiating TLS")
self.realsocket = self.socket self.realsocket = self.socket
self.socket = ssl.wrap_socket(self.socket, ssl_version=ssl.PROTOCOL_TLSv1, do_handshake_on_connect=False) self.socket = ssl.wrap_socket(self.socket,
ssl_version=ssl.PROTOCOL_TLSv1,
do_handshake_on_connect=False,
ca_certs=self.ca_certs)
print "doing handshake..."
self.socket.do_handshake() self.socket.do_handshake()
print "got handshake..."
if sys.version_info < (3,0): if sys.version_info < (3,0):
from . filesocket import filesocket from . filesocket import filesocket
self.filesocket = filesocket(self.socket) self.filesocket = filesocket(self.socket)
@ -146,13 +152,19 @@ class XMLStream(object):
def process(self, threaded=True): def process(self, threaded=True):
for t in range(0, HANDLER_THREADS): for t in range(0, HANDLER_THREADS):
self.__thread['eventhandle%s' % t] = threading.Thread(name='eventhandle%s' % t, target=self._eventRunner) th = threading.Thread(name='eventhandle%s' % t, target=self._eventRunner)
self.__thread['eventhandle%s' % t].start() th.setDaemon(True)
self.__thread['sendthread'] = threading.Thread(name='sendthread', target=self._sendThread) self.__thread['eventhandle%s' % t] = th
self.__thread['sendthread'].start() th.start()
th = threading.Thread(name='sendthread', target=self._sendThread)
th.setDaemon(True)
self.__thread['sendthread'] = th
th.start()
if threaded: if threaded:
self.__thread['process'] = threading.Thread(name='process', target=self._process) th = threading.Thread(name='process', target=self._process)
self.__thread['process'].start() th.setDaemon(True)
self.__thread['process'] = th
th.start()
else: else:
self._process() self._process()
@ -288,7 +300,7 @@ class XMLStream(object):
self.state.set('tls',False) self.state.set('tls',False)
self.state.set('ssl',False) self.state.set('ssl',False)
time.sleep(1) time.sleep(1)
self.connect() self.connect(self.server,self.port)
def incoming_filter(self, xmlobj): def incoming_filter(self, xmlobj):
return xmlobj return xmlobj