so... I have a simple chat client like so:
class ChatClient(sleekxmpp.ClientXMPP):
def __init__(self, jid, password, server):
sleekxmpp.ClientXMPP.__init__(self, jid, password, ssl=True)
self.add_event_handler("session_start", self.start)
self.register_plugin('xep_0030')
self.register_plugin('xep_0004')
self.register_plugin('xep_0060')
self.register_plugin('xep_0199')
self.ssl_version = ssl.PROTOCOL_SSLv3
self.connected = self.connect()
if self.connected:
self.process(threaded=True)
def start(self, event):
self.send_presence(priority = "-9001")
self.get_roster(blocking = True, timeout = 3)
def message(self, targets, msg):
for target in targets:
self.send_message(target, msg)
and I have an "verify" function to make sure you input your username/pass right:
def authenticate(username, password, server):
xmppuser = username + '@' + server
passTester = ChatClient(xmppuser, password)
passTester.disconnect(wait = True)
authenticated = passTester.authenticated
return authenticated
Now, the problem comes in where I have the chat client as threaded, I run into the situation where I try to check ChatClient.authenticated before the server had a chance to actually connect. As you can see, I tried to "wait" on the disconnect but there's nothing in the send queue so it disconnects right away.
An alternate I tried is this:
def authenticate(username, password, server):
xmppuser = username + '@' + server
passTester = ChatClient(xmppuser, password)
passTester.message('bogusName', 'ladfhkjdglkhjdfg')
passTester.disconnect(wait = True)
authenticated = passTester.authenticated
return authenticated
Now that I sent a bogus message the disconnect call has something to wait for. when I input a correct username/pass with this code, the disconnect waits for a message to get sent (which involves waiting for a real connection), sends nothing and ChatClient.authenticated is set to True!
Unfortunately, when I input a wrong username/pass the message never gets sent and the disconnect(wait=True) never disconnects as the message never gets sent.
Does anyone else have a more proper way to "authenticate"?