mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
* python 2.6 compatibility
This commit is contained in:
parent
0af468b435
commit
a8ff3586d3
5 changed files with 40 additions and 13 deletions
|
@ -19,7 +19,7 @@
|
||||||
along with SleekXMPP; if not, write to the Free Software
|
along with SleekXMPP; if not, write to the Free Software
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
"""
|
"""
|
||||||
from __future__ import absolute_import
|
from __future__ import absolute_import, unicode_literals
|
||||||
from . basexmpp import basexmpp
|
from . basexmpp import basexmpp
|
||||||
from xml.etree import cElementTree as ET
|
from xml.etree import cElementTree as ET
|
||||||
from . xmlstream.xmlstream import XMLStream
|
from . xmlstream.xmlstream import XMLStream
|
||||||
|
@ -201,7 +201,10 @@ class ClientXMPP(basexmpp, XMLStream):
|
||||||
for sasl_mech in sasl_mechs:
|
for sasl_mech in sasl_mechs:
|
||||||
self.features.append("sasl:%s" % sasl_mech.text)
|
self.features.append("sasl:%s" % sasl_mech.text)
|
||||||
if 'sasl:PLAIN' in self.features:
|
if 'sasl:PLAIN' in self.features:
|
||||||
self.send("""<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'))
|
if sys.version_info < (3,0):
|
||||||
|
self.send("""<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'))
|
||||||
|
else:
|
||||||
|
self.send("""<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'))
|
||||||
else:
|
else:
|
||||||
logging.error("No appropriate login method.")
|
logging.error("No appropriate login method.")
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
along with SleekXMPP; if not, write to the Free Software
|
along with SleekXMPP; if not, write to the Free Software
|
||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
"""
|
"""
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement, unicode_literals
|
||||||
|
|
||||||
|
|
||||||
from xml.etree import cElementTree as ET
|
from xml.etree import cElementTree as ET
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
from . import base
|
from . import base
|
||||||
import queue
|
try:
|
||||||
|
import queue
|
||||||
|
except ImportError:
|
||||||
|
import Queue as queue
|
||||||
import logging
|
import logging
|
||||||
from .. stanzabase import StanzaBase
|
from .. stanzabase import StanzaBase
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ class MatchXMLMask(base.MatcherBase):
|
||||||
#TODO require namespaces
|
#TODO require namespaces
|
||||||
if source == None: #if element not found (happens during recursive check below)
|
if source == None: #if element not found (happens during recursive check below)
|
||||||
return False
|
return False
|
||||||
if type(maskobj) == type(str()): #if the mask is a string, make it an xml obj
|
if not hasattr(maskobj, 'attrib'): #if the mask is a string, make it an xml obj
|
||||||
try:
|
try:
|
||||||
maskobj = cElementTree.fromstring(maskobj)
|
maskobj = cElementTree.fromstring(maskobj)
|
||||||
except ExpatError:
|
except ExpatError:
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
from __future__ import with_statement
|
from __future__ import with_statement, unicode_literals
|
||||||
import queue
|
try:
|
||||||
|
import queue
|
||||||
|
except ImportError:
|
||||||
|
import Queue as queue
|
||||||
from . import statemachine
|
from . import statemachine
|
||||||
from . stanzabase import StanzaBase
|
from . stanzabase import StanzaBase
|
||||||
from xml.etree import cElementTree
|
from xml.etree import cElementTree
|
||||||
|
@ -15,10 +18,15 @@ import xml.sax.saxutils
|
||||||
HANDLER_THREADS = 1
|
HANDLER_THREADS = 1
|
||||||
|
|
||||||
ssl_support = True
|
ssl_support = True
|
||||||
try:
|
#try:
|
||||||
import ssl
|
import ssl
|
||||||
except ImportError:
|
#except ImportError:
|
||||||
ssl_support = False
|
# ssl_support = False
|
||||||
|
import sys
|
||||||
|
if sys.version_info < (3, 0):
|
||||||
|
#monkey patch broken filesocket object
|
||||||
|
from . import filesocket
|
||||||
|
socket._fileobject = filesocket.filesocket
|
||||||
|
|
||||||
|
|
||||||
class RestartStream(Exception):
|
class RestartStream(Exception):
|
||||||
|
@ -89,11 +97,13 @@ class XMLStream(object):
|
||||||
self.use_tls = use_tls
|
self.use_tls = use_tls
|
||||||
self.state.set('is client', True)
|
self.state.set('is client', True)
|
||||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
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)
|
||||||
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)
|
self.filesocket = self.socket.makefile('rb', 0)
|
||||||
self.state.set('connected', True)
|
self.state.set('connected', True)
|
||||||
return True
|
return True
|
||||||
|
@ -111,7 +121,11 @@ class XMLStream(object):
|
||||||
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)
|
||||||
self.socket.do_handshake()
|
self.socket.do_handshake()
|
||||||
self.filesocket = self.socket.makefile('rb', 0)
|
if sys.version_info < (3,0):
|
||||||
|
from . filesocket import filesocket
|
||||||
|
self.filesocket = filesocket(self.socket)
|
||||||
|
else:
|
||||||
|
self.filesocket = self.socket.makefile('rb', 0)
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
logging.warning("Tried to enable TLS, but ssl module not found.")
|
logging.warning("Tried to enable TLS, but ssl module not found.")
|
||||||
|
@ -180,6 +194,7 @@ class XMLStream(object):
|
||||||
"Parses the incoming stream, adding to xmlin queue as it goes"
|
"Parses the incoming stream, adding to xmlin queue as it goes"
|
||||||
#build cElementTree object from expat was we go
|
#build cElementTree object from expat was we go
|
||||||
#self.filesocket = self.socket.makefile('rb', 0)
|
#self.filesocket = self.socket.makefile('rb', 0)
|
||||||
|
#print self.filesocket.read(1024) #self.filesocket._sock.recv(1024)
|
||||||
edepth = 0
|
edepth = 0
|
||||||
root = None
|
root = None
|
||||||
for (event, xmlobj) in cElementTree.iterparse(self.filesocket, (b'end', b'start')):
|
for (event, xmlobj) in cElementTree.iterparse(self.filesocket, (b'end', b'start')):
|
||||||
|
@ -208,11 +223,14 @@ class XMLStream(object):
|
||||||
logging.debug("SEND: %s" % data)
|
logging.debug("SEND: %s" % data)
|
||||||
try:
|
try:
|
||||||
self.socket.send(bytes(data, "utf-8"))
|
self.socket.send(bytes(data, "utf-8"))
|
||||||
|
except TypeError:
|
||||||
|
self.socket.send(bytes(data))
|
||||||
#except socket.error,(errno, strerror):
|
#except socket.error,(errno, strerror):
|
||||||
except:
|
except:
|
||||||
self.state.set('connected', False)
|
self.state.set('connected', False)
|
||||||
if self.state.reconnect:
|
if self.state.reconnect:
|
||||||
logging.error("Disconnected. Socket Error.")
|
logging.error("Disconnected. Socket Error.")
|
||||||
|
traceback.print_exc()
|
||||||
self.disconnect(reconnect=True)
|
self.disconnect(reconnect=True)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
@ -277,7 +295,10 @@ class XMLStream(object):
|
||||||
except queue.Empty:
|
except queue.Empty:
|
||||||
event = None
|
event = None
|
||||||
if event is not None:
|
if event is not None:
|
||||||
etype, handler, *args = event
|
etype = event[0]
|
||||||
|
handler = event[1]
|
||||||
|
args = event[2:]
|
||||||
|
#etype, handler, *args = event #python 3.x way
|
||||||
if etype == 'stanza':
|
if etype == 'stanza':
|
||||||
try:
|
try:
|
||||||
handler.run(args[0])
|
handler.run(args[0])
|
||||||
|
|
Loading…
Reference in a new issue