xmpp.py 1008 Bytes
Newer Older
ale's avatar
ale committed
1
2
import threading
import time
ale's avatar
ale committed
3
4
5
from ai_diagnostics import xmpp


ale's avatar
ale committed
6
7
8
TIMEOUT = 30


ale's avatar
ale committed
9
10
11
12
13
14
def probe_xmpp(ctx):
    bot = xmpp.SendMsgBot(ctx['credentials']['username'],
                          ctx['credentials']['password'])
    bot.register_plugin('xep_0030')
    bot.register_plugin('xep_0199')

ale's avatar
ale committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    # We can't reliably time out the XMPP probe on certain failures,
    # so we just shut down the client altogether after a timeout.
    done = threading.Event()

    def _timeout():
        time.sleep(TIMEOUT)
        if not done.isSet():
            bot.disconnect()
    threading.Thread(target=_timeout, daemon=True).start()

    try:
        ctx.log('connecting to %s:5222', ctx['server'])
        if bot.connect((ctx['server'], 5222), reattempt=False):
            ctx.log('connection successful, setting presence')
            bot.process(block=True)
            ctx.log('disconnected successfully')
        else:
            ctx.log('unable to connect')
    finally:
        done.set()
        bot.disconnect()