Merge branch 'develop' into develop-1.1

This commit is contained in:
Lance Stout 2011-12-27 18:05:42 -05:00
commit 2f2ebb37e4
6 changed files with 289 additions and 39 deletions

View file

@ -42,46 +42,46 @@ def py2xml(*args):
def _py2xml(*args): def _py2xml(*args):
for x in args: for x in args:
val = ET.Element("value") val = ET.Element("{%s}value" % _namespace)
if x is None: if x is None:
nil = ET.Element("nil") nil = ET.Element("{%s}nil" % _namespace)
val.append(nil) val.append(nil)
elif type(x) is int: elif type(x) is int:
i4 = ET.Element("i4") i4 = ET.Element("{%s}i4" % _namespace)
i4.text = str(x) i4.text = str(x)
val.append(i4) val.append(i4)
elif type(x) is bool: elif type(x) is bool:
boolean = ET.Element("boolean") boolean = ET.Element("{%s}boolean" % _namespace)
boolean.text = str(int(x)) boolean.text = str(int(x))
val.append(boolean) val.append(boolean)
elif type(x) is str: elif type(x) is str:
string = ET.Element("string") string = ET.Element("{%s}string" % _namespace)
string.text = x string.text = x
val.append(string) val.append(string)
elif type(x) is float: elif type(x) is float:
double = ET.Element("double") double = ET.Element("{%s}double" % _namespace)
double.text = str(x) double.text = str(x)
val.append(double) val.append(double)
elif type(x) is rpcbase64: elif type(x) is rpcbase64:
b64 = ET.Element("base64") b64 = ET.Element("{%s}base64" % _namespace)
b64.text = x.encoded() b64.text = x.encoded()
val.append(b64) val.append(b64)
elif type(x) is rpctime: elif type(x) is rpctime:
iso = ET.Element("dateTime.iso8601") iso = ET.Element("{%s}dateTime.iso8601" % _namespace)
iso.text = str(x) iso.text = str(x)
val.append(iso) val.append(iso)
elif type(x) in (list, tuple): elif type(x) in (list, tuple):
array = ET.Element("array") array = ET.Element("{%s}array" % _namespace)
data = ET.Element("data") data = ET.Element("{%s}data" % _namespace)
for y in x: for y in x:
data.append(_py2xml(y)) data.append(_py2xml(y))
array.append(data) array.append(data)
val.append(array) val.append(array)
elif type(x) is dict: elif type(x) is dict:
struct = ET.Element("struct") struct = ET.Element("{%s}struct" % _namespace)
for y in x.keys(): for y in x.keys():
member = ET.Element("member") member = ET.Element("{%s}member" % _namespace)
name = ET.Element("name") name = ET.Element("{%s}name" % _namespace)
name.text = y name.text = y
member.append(name) member.append(name)
member.append(_py2xml(x[y])) member.append(_py2xml(x[y]))
@ -105,18 +105,18 @@ def _xml2py(value):
if value.find('{%s}int' % namespace) is not None: if value.find('{%s}int' % namespace) is not None:
return int(value.find('{%s}int' % namespace).text) return int(value.find('{%s}int' % namespace).text)
if value.find('{%s}boolean' % namespace) is not None: if value.find('{%s}boolean' % namespace) is not None:
return bool(value.find('{%s}boolean' % namespace).text) return bool(int(value.find('{%s}boolean' % namespace).text))
if value.find('{%s}string' % namespace) is not None: if value.find('{%s}string' % namespace) is not None:
return value.find('{%s}string' % namespace).text return value.find('{%s}string' % namespace).text
if value.find('{%s}double' % namespace) is not None: if value.find('{%s}double' % namespace) is not None:
return float(value.find('{%s}double' % namespace).text) return float(value.find('{%s}double' % namespace).text)
if value.find('{%s}base64') is not None: if value.find('{%s}base64' % namespace) is not None:
return rpcbase64(value.find('base64' % namespace).text) return rpcbase64(value.find('{%s}base64' % namespace).text.encode())
if value.find('{%s}Base64') is not None: if value.find('{%s}Base64' % namespace) is not None:
# Older versions of XEP-0009 used Base64 # Older versions of XEP-0009 used Base64
return rpcbase64(value.find('Base64' % namespace).text) return rpcbase64(value.find('{%s}Base64' % namespace).text.encode())
if value.find('{%s}dateTime.iso8601') is not None: if value.find('{%s}dateTime.iso8601' % namespace) is not None:
return rpctime(value.find('{%s}dateTime.iso8601')) return rpctime(value.find('{%s}dateTime.iso8601' % namespace).text)
if value.find('{%s}struct' % namespace) is not None: if value.find('{%s}struct' % namespace) is not None:
struct = {} struct = {}
for member in value.find('{%s}struct' % namespace).findall('{%s}member' % namespace): for member in value.find('{%s}struct' % namespace).findall('{%s}member' % namespace):
@ -138,13 +138,13 @@ class rpcbase64(object):
self.data = data self.data = data
def decode(self): def decode(self):
return base64.decodestring(self.data) return base64.b64decode(self.data)
def __str__(self): def __str__(self):
return self.decode() return self.decode().decode()
def encoded(self): def encoded(self):
return self.data return self.data.decode()

View file

@ -113,6 +113,9 @@ class ACL:
def check(cls, rules, jid, resource): def check(cls, rules, jid, resource):
if rules is None: if rules is None:
return cls.DENY # No rules means no access! return cls.DENY # No rules means no access!
jid = str(jid) # Check the string representation of the JID.
if not jid:
return cls.DENY # Can't check an empty JID.
for rule in rules: for rule in rules:
policy = cls._check(rule, jid, resource) policy = cls._check(rule, jid, resource)
if policy is not None: if policy is not None:

View file

@ -22,7 +22,7 @@ class PubsubErrorCondition(ElementBase):
'max-items-exceeded', 'max-nodes-exceeded', 'max-items-exceeded', 'max-nodes-exceeded',
'nodeid-required', 'not-in-roster-group', 'nodeid-required', 'not-in-roster-group',
'not-subscribed', 'payload-too-big', 'not-subscribed', 'payload-too-big',
'payload-required' 'pending-subscription', 'payload-required', 'pending-subscription',
'presence-subscription-required', 'subid-required', 'presence-subscription-required', 'subid-required',
'too-many-subscriptions', 'unsupported')) 'too-many-subscriptions', 'unsupported'))
condition_ns = 'http://jabber.org/protocol/pubsub#errors' condition_ns = 'http://jabber.org/protocol/pubsub#errors'

View file

@ -76,7 +76,7 @@ def format_datetime(time_obj):
return '%sZ' % timestamp return '%sZ' % timestamp
return timestamp return timestamp
def date(year=None, month=None, day=None): def date(year=None, month=None, day=None, obj=False):
""" """
Create a date only timestamp for the given instant. Create a date only timestamp for the given instant.
@ -86,6 +86,8 @@ def date(year=None, month=None, day=None):
year -- Integer value of the year (4 digits) year -- Integer value of the year (4 digits)
month -- Integer value of the month month -- Integer value of the month
day -- Integer value of the day of the month. day -- Integer value of the day of the month.
obj -- If True, return the date object instead
of a formatted string. Defaults to False.
""" """
today = dt.datetime.utcnow() today = dt.datetime.utcnow()
if year is None: if year is None:
@ -94,9 +96,12 @@ def date(year=None, month=None, day=None):
month = today.month month = today.month
if day is None: if day is None:
day = today.day day = today.day
return format_date(dt.date(year, month, day)) value = dt.date(year, month, day)
if obj:
return value
return format_date(value)
def time(hour=None, min=None, sec=None, micro=None, offset=None): def time(hour=None, min=None, sec=None, micro=None, offset=None, obj=False):
""" """
Create a time only timestamp for the given instant. Create a time only timestamp for the given instant.
@ -110,6 +115,8 @@ def time(hour=None, min=None, sec=None, micro=None, offset=None):
offset -- Either a positive or negative number of seconds offset -- Either a positive or negative number of seconds
to offset from UTC to match a desired timezone, to offset from UTC to match a desired timezone,
or a tzinfo object. or a tzinfo object.
obj -- If True, return the time object instead
of a formatted string. Defaults to False.
""" """
now = dt.datetime.utcnow() now = dt.datetime.utcnow()
if hour is None: if hour is None:
@ -124,12 +131,14 @@ def time(hour=None, min=None, sec=None, micro=None, offset=None):
offset = tzutc() offset = tzutc()
elif not isinstance(offset, dt.tzinfo): elif not isinstance(offset, dt.tzinfo):
offset = tzoffset(None, offset) offset = tzoffset(None, offset)
time = dt.time(hour, min, sec, micro, offset) value = dt.time(hour, min, sec, micro, offset)
return format_time(time) if obj:
return value
return format_time(value)
def datetime(year=None, month=None, day=None, hour=None, def datetime(year=None, month=None, day=None, hour=None,
min=None, sec=None, micro=None, offset=None, min=None, sec=None, micro=None, offset=None,
separators=True): separators=True, obj=False):
""" """
Create a datetime timestamp for the given instant. Create a datetime timestamp for the given instant.
@ -146,6 +155,8 @@ def datetime(year=None, month=None, day=None, hour=None,
offset -- Either a positive or negative number of seconds offset -- Either a positive or negative number of seconds
to offset from UTC to match a desired timezone, to offset from UTC to match a desired timezone,
or a tzinfo object. or a tzinfo object.
obj -- If True, return the datetime object instead
of a formatted string. Defaults to False.
""" """
now = dt.datetime.utcnow() now = dt.datetime.utcnow()
if year is None: if year is None:
@ -167,9 +178,11 @@ def datetime(year=None, month=None, day=None, hour=None,
elif not isinstance(offset, dt.tzinfo): elif not isinstance(offset, dt.tzinfo):
offset = tzoffset(None, offset) offset = tzoffset(None, offset)
date = dt.datetime(year, month, day, hour, value = dt.datetime(year, month, day, hour,
min, sec, micro, offset) min, sec, micro, offset)
return format_datetime(date) if obj:
return value
return format_datetime(value)
class xep_0082(base_plugin): class xep_0082(base_plugin):

View file

@ -1190,6 +1190,7 @@ class XMLStream(object):
shutdown = True shutdown = True
except SyntaxError as e: except SyntaxError as e:
log.error("Error reading from XML stream.") log.error("Error reading from XML stream.")
shutdown = True
self.exception(e) self.exception(e)
except Socket.error as serr: except Socket.error as serr:
self.event('socket_error', serr) self.event('socket_error', serr)

View file

@ -6,12 +6,16 @@
See the file LICENSE for copying permission. See the file LICENSE for copying permission.
""" """
import base64
from sleekxmpp.plugins.xep_0009.stanza.RPC import RPCQuery, MethodCall, \ from sleekxmpp.plugins.xep_0009.stanza.RPC import RPCQuery, MethodCall, \
MethodResponse MethodResponse
from sleekxmpp.plugins.xep_0009.binding import py2xml from sleekxmpp.plugins.xep_0009.binding import py2xml, xml2py, rpcbase64, \
rpctime
from sleekxmpp.stanza.iq import Iq from sleekxmpp.stanza.iq import Iq
from sleekxmpp.test.sleektest import SleekTest from sleekxmpp.test.sleektest import SleekTest
from sleekxmpp.xmlstream.stanzabase import register_stanza_plugin from sleekxmpp.xmlstream.stanzabase import register_stanza_plugin
from sleekxmpp.xmlstream.tostring import tostring
import unittest import unittest
@ -51,5 +55,234 @@ class TestJabberRPC(SleekTest):
</iq> </iq>
""", use_values=False) """, use_values=False)
def testConvertNil(self):
params = [None]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<nil />
</value>
</param>
</params>
""")
self.assertTrue(self.compare(expected_xml, params_xml),
"Nil to XML conversion\nExpected: %s\nGot: %s" % (
tostring(expected_xml), tostring(params_xml)))
self.assertEqual(params, xml2py(expected_xml),
"XML to nil conversion")
def testConvertBoolean(self):
params = [True, False]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<boolean>1</boolean>
</value>
</param>
<param>
<value>
<boolean>0</boolean>
</value>
</param>
</params>
""")
self.assertTrue(self.compare(expected_xml, params_xml),
"Boolean to XML conversion\nExpected: %s\nGot: %s" % (
tostring(expected_xml), tostring(params_xml)))
self.assertEqual(params, xml2py(expected_xml),
"XML to boolean conversion")
def testConvertString(self):
params = ["'This' & \"That\""]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<string>&apos;This&apos; &amp; &quot;That&quot;</string>
</value>
</param>
</params>
""")
self.assertTrue(self.compare(expected_xml, params_xml),
"String to XML conversion\nExpected: %s\nGot: %s" % (
tostring(expected_xml), tostring(params_xml)))
self.assertEqual(params, xml2py(expected_xml),
"XML to string conversion")
def testConvertInteger(self):
params = [32767, -32768]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<i4>32767</i4>
</value>
</param>
<param>
<value>
<i4>-32768</i4>
</value>
</param>
</params>
""")
alternate_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<int>32767</int>
</value>
</param>
<param>
<value>
<int>-32768</int>
</value>
</param>
</params>
""")
self.assertTrue(self.compare(expected_xml, params_xml),
"Integer to XML conversion\nExpected: %s\nGot: %s" % (
tostring(expected_xml), tostring(params_xml)))
self.assertEqual(params, xml2py(expected_xml),
"XML to boolean conversion")
self.assertEqual(params, xml2py(alternate_xml),
"Alternate XML to boolean conversion")
def testConvertDouble(self):
params = [3.14159265]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<double>3.14159265</double>
</value>
</param>
</params>
""")
self.assertTrue(self.compare(expected_xml, params_xml),
"Double to XML conversion\nExpected: %s\nGot: %s" % (
tostring(expected_xml), tostring(params_xml)))
self.assertEqual(params, xml2py(expected_xml),
"XML to double conversion")
def testConvertBase64(self):
params = [rpcbase64(base64.b64encode(b"Hello, world!"))]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<base64>SGVsbG8sIHdvcmxkIQ==</base64>
</value>
</param>
</params>
""")
alternate_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<Base64>SGVsbG8sIHdvcmxkIQ==</Base64>
</value>
</param>
</params>
""")
self.assertTrue(self.compare(expected_xml, params_xml),
"Base64 to XML conversion\nExpected: %s\nGot: %s" % (
tostring(expected_xml), tostring(params_xml)))
self.assertEqual(list(map(lambda x: x.decode(), params)),
list(map(lambda x: x.decode(), xml2py(expected_xml))),
"XML to base64 conversion")
self.assertEqual(list(map(lambda x: x.decode(), params)),
list(map(lambda x: x.decode(), xml2py(alternate_xml))),
"Alternate XML to base64 conversion")
def testConvertDateTime(self):
params = [rpctime("20111220T01:50:00")]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<dateTime.iso8601>20111220T01:50:00</dateTime.iso8601>
</value>
</param>
</params>
""")
self.assertTrue(self.compare(expected_xml, params_xml),
"DateTime to XML conversion\nExpected: %s\nGot: %s" % (
tostring(expected_xml), tostring(params_xml)))
self.assertEqual(list(map(lambda x: x.iso8601(), params)),
list(map(lambda x: x.iso8601(), xml2py(expected_xml))),
None)
def testConvertArray(self):
params = [[1,2,3], ('a', 'b', 'c')]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<array>
<data>
<value><i4>1</i4></value>
<value><i4>2</i4></value>
<value><i4>3</i4></value>
</data>
</array>
</value>
</param>
<param>
<value>
<array>
<data>
<value><string>a</string></value>
<value><string>b</string></value>
<value><string>c</string></value>
</data>
</array>
</value>
</param>
</params>
""")
self.assertTrue(self.compare(expected_xml, params_xml),
"Array to XML conversion\nExpected: %s\nGot: %s" % (
tostring(expected_xml), tostring(params_xml)))
self.assertEqual(list(map(list, params)), xml2py(expected_xml),
"XML to array conversion")
def testConvertStruct(self):
params = [{"foo": "bar", "baz": False}]
params_xml = py2xml(*params)
expected_xml = self.parse_xml("""
<params xmlns="jabber:iq:rpc">
<param>
<value>
<struct>
<member>
<name>foo</name>
<value><string>bar</string></value>
</member>
<member>
<name>baz</name>
<value><boolean>0</boolean></value>
</member>
</struct>
</value>
</param>
</params>
""")
self.assertTrue(self.compare(expected_xml, params_xml),
"Struct to XML conversion\nExpected: %s\nGot: %s" % (
tostring(expected_xml), tostring(params_xml)))
self.assertEqual(params, xml2py(expected_xml),
"XML to struct conversion")
suite = unittest.TestLoader().loadTestsFromTestCase(TestJabberRPC) suite = unittest.TestLoader().loadTestsFromTestCase(TestJabberRPC)