From 4a94aeba49cd2a30ee4d3e71feee5df785b83760 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Tue, 7 Aug 2012 19:33:17 -0700 Subject: [PATCH 1/6] Save a user's chosen, persistent nickname in the MUC roster data as 'alt_nick' The use of elements in MUCs is now discouraged in XEP-0172, however. --- sleekxmpp/plugins/xep_0045.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sleekxmpp/plugins/xep_0045.py b/sleekxmpp/plugins/xep_0045.py index 7cd25b2a..45f2915f 100644 --- a/sleekxmpp/plugins/xep_0045.py +++ b/sleekxmpp/plugins/xep_0045.py @@ -156,6 +156,7 @@ class XEP_0045(BasePlugin): entry = pr['muc'].getStanzaValues() entry['show'] = pr['show'] entry['status'] = pr['status'] + entry['alt_nick'] = pr['nick'] if pr['type'] == 'unavailable': if entry['nick'] in self.rooms[entry['room']]: del self.rooms[entry['room']][entry['nick']] From 4e12e228cb2fbc6fe2941070fe1ea44e01e4a9fb Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 10 Aug 2012 12:40:28 -0700 Subject: [PATCH 2/6] Fix tracking service name for DIGEST-MD5 --- .../features/feature_mechanisms/mechanisms.py | 2 +- sleekxmpp/xmlstream/resolver.py | 21 +++++++++++-------- sleekxmpp/xmlstream/xmlstream.py | 7 +++++-- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/sleekxmpp/features/feature_mechanisms/mechanisms.py b/sleekxmpp/features/feature_mechanisms/mechanisms.py index 9a391628..dae2f59f 100644 --- a/sleekxmpp/features/feature_mechanisms/mechanisms.py +++ b/sleekxmpp/features/feature_mechanisms/mechanisms.py @@ -109,7 +109,7 @@ class FeatureMechanisms(BasePlugin): elif value == 'realm': result[value] = self.xmpp.boundjid.domain elif value == 'service-name': - result[value] = self.xmpp.address[0] + result[value] = self.xmpp._service_name elif value == 'service': result[value] = 'xmpp' elif value in creds: diff --git a/sleekxmpp/xmlstream/resolver.py b/sleekxmpp/xmlstream/resolver.py index 0d7a8c0d..394daa64 100644 --- a/sleekxmpp/xmlstream/resolver.py +++ b/sleekxmpp/xmlstream/resolver.py @@ -102,7 +102,7 @@ def resolve(host, port=None, service=None, proto='tcp', try: # If `host` is an IPv4 literal, we can return it immediately. ipv4 = socket.inet_aton(host) - yield (host, port) + yield (host, host, port) except socket.error: pass @@ -112,7 +112,7 @@ def resolve(host, port=None, service=None, proto='tcp', # it immediately. if hasattr(socket, 'inet_pton'): ipv6 = socket.inet_pton(socket.AF_INET6, host) - yield (host, port) + yield (host, host, port) except socket.error: pass @@ -128,16 +128,16 @@ def resolve(host, port=None, service=None, proto='tcp', results = [] if host == 'localhost': if use_ipv6: - results.append(('::1', port)) - results.append(('127.0.0.1', port)) + results.append((host, '::1', port)) + results.append((host, '127.0.0.1', port)) if use_ipv6: for address in get_AAAA(host, resolver=resolver): - results.append((address, port)) + results.append((host, address, port)) for address in get_A(host, resolver=resolver): - results.append((address, port)) + results.append((host, address, port)) - for address, port in results: - yield address, port + for host, address, port in results: + yield host, address, port def get_A(host, resolver=None): @@ -297,7 +297,10 @@ def get_SRV(host, port, service, proto='tcp', resolver=None): for running_sum in sums: if running_sum >= selected: rec = sums[running_sum] - sorted_recs.append((rec.target.to_text(), rec.port)) + host = rec.target.to_text() + if host.endswith('.'): + host = host[:-1] + sorted_recs.append((host, rec.port)) answers[priority].remove(rec) break diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index f72171a1..8f8e94fd 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -192,6 +192,7 @@ class XMLStream(object): #: The expected name of the server, for validation. self._expected_server_name = '' + self._service_name = '' #: The desired, or actual, address of the connected server. self.address = (host, int(port)) @@ -473,8 +474,10 @@ class XMLStream(object): if self.default_domain: try: - self.address = self.pick_dns_answer(self.default_domain, - self.address[1]) + host, address, port = self.pick_dns_answer(self.default_domain, + self.address[1]) + self.address = (address, port) + self._service_name = host except StopIteration: log.debug("No remaining DNS records to try.") self.dns_answers = None From d11a67702e9f28fecbe4460f82b6c0ab0bdd740c Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 10 Aug 2012 12:41:02 -0700 Subject: [PATCH 3/6] Exit transition immediately if already in the desired state. --- sleekxmpp/thirdparty/statemachine.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sleekxmpp/thirdparty/statemachine.py b/sleekxmpp/thirdparty/statemachine.py index 33d9b828..1f01a92a 100644 --- a/sleekxmpp/thirdparty/statemachine.py +++ b/sleekxmpp/thirdparty/statemachine.py @@ -29,7 +29,7 @@ class StateMachine(object): if state in self.__states: raise IndexError("The state '%s' is already in the StateMachine." % state) self.__states.append(state) - finally: + finally: self.lock.release() @@ -90,6 +90,9 @@ class StateMachine(object): log.debug("Could not acquire lock") return False + if self.__current_state == to_state: + return True + while not self.__current_state in from_states: # detect timeout: remainder = start + wait - time.time() @@ -108,7 +111,7 @@ class StateMachine(object): # some 'false' value returned from func, # indicating that transition should not occur: - if not return_val: + if not return_val: return return_val log.debug(' ==== TRANSITION %s -> %s', self.__current_state, to_state) @@ -193,9 +196,9 @@ class StateMachine(object): while not self.__current_state in states: # detect timeout: remainder = start + wait - time.time() - if remainder > 0: + if remainder > 0: self.lock.wait(remainder) - else: + else: self.lock.release() return False self.lock.release() @@ -241,7 +244,7 @@ class _StateCtx: while not self.state_machine[self.from_state] or not self.state_machine.lock.acquire(False): # detect timeout: remainder = start + self.wait - time.time() - if remainder > 0: + if remainder > 0: self.state_machine.lock.wait(remainder) else: log.debug('StateMachine timeout while waiting for state: %s', self.from_state) From 230465b94637ff83cd40e8a2a2fe117d61d5fe79 Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 10 Aug 2012 12:41:29 -0700 Subject: [PATCH 4/6] Fix unicode conversion utility. --- sleekxmpp/util/misc_ops.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sleekxmpp/util/misc_ops.py b/sleekxmpp/util/misc_ops.py index 1dcc2b91..9bb1db4b 100644 --- a/sleekxmpp/util/misc_ops.py +++ b/sleekxmpp/util/misc_ops.py @@ -4,6 +4,8 @@ import hashlib def unicode(text): if sys.version_info < (3, 0): + if isinstance(text, str): + text = text.decode('utf-8') import __builtin__ return __builtin__.unicode(text) return str(text) From 814a50e36feb73ced9152680560261addf3ff0ea Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 10 Aug 2012 14:11:44 -0700 Subject: [PATCH 5/6] Fix handling state machine lock when quick exiting. --- sleekxmpp/thirdparty/statemachine.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sleekxmpp/thirdparty/statemachine.py b/sleekxmpp/thirdparty/statemachine.py index 1f01a92a..4b5ecd6b 100644 --- a/sleekxmpp/thirdparty/statemachine.py +++ b/sleekxmpp/thirdparty/statemachine.py @@ -83,16 +83,16 @@ class StateMachine(object): if not to_state in self.__states: raise ValueError("StateMachine does not contain to_state %s." % to_state) + if self.__current_state == to_state: + return True + start = time.time() while not self.lock.acquire(False): time.sleep(.001) if (start + wait - time.time()) <= 0.0: - log.debug("Could not acquire lock") + log.debug("==== Could not acquire lock in %s sec: %s -> %s ", wait, self.__current_state, to_state) return False - if self.__current_state == to_state: - return True - while not self.__current_state in from_states: # detect timeout: remainder = start + wait - time.time() From f7a710e55bf433a378dee4bd05f6ceb59f2e9e2d Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Fri, 10 Aug 2012 14:12:05 -0700 Subject: [PATCH 6/6] Add abort() method to kill the session and stop all processing without properly closing the stream. --- sleekxmpp/xmlstream/xmlstream.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/sleekxmpp/xmlstream/xmlstream.py b/sleekxmpp/xmlstream/xmlstream.py index 8f8e94fd..f56c360e 100644 --- a/sleekxmpp/xmlstream/xmlstream.py +++ b/sleekxmpp/xmlstream/xmlstream.py @@ -726,6 +726,20 @@ class XMLStream(object): self.event("disconnected", direct=True) return True + def abort(self): + self.session_started_event.clear() + self.stop.set() + if self._disconnect_wait_for_threads: + self._wait_for_threads() + try: + self.socket.shutdown(Socket.SHUT_RDWR) + self.socket.close() + self.filesocket.close() + except Socket.error: + pass + self.state.transition_any(['connected', 'disconnected'], 'disconnected', func=lambda: True) + self.event("killed", direct=True) + def reconnect(self, reattempt=True, wait=False, send_close=True): """Reset the stream's state and reconnect to the server.""" log.debug("reconnecting...")