1 { writeScriptBin, writeText, python3, connectTo ? "localhost" }:
3 dummyFile = writeText "dummy-file" ''
6 Please find this *really* important attachment.
11 in writeScriptBin "send-message" ''
12 #!${(python3.withPackages (ps: [ ps.slixmpp ])).interpreter}
16 from types import MethodType
18 from slixmpp import ClientXMPP
19 from slixmpp.exceptions import IqError, IqTimeout
22 class CthonTest(ClientXMPP):
24 def __init__(self, jid, password):
25 ClientXMPP.__init__(self, jid, password)
26 self.add_event_handler("session_start", self.session_start)
27 self.test_succeeded = False
29 async def session_start(self, event):
31 # Exceptions in event handlers are printed to stderr but not
32 # propagated, they do not make the script terminate with a non-zero
33 # exit code. We use the `test_succeeded` flag as a workaround and
34 # check it later at the end of the script to exit with a proper
36 # Additionally, this flag ensures that this event handler has been
37 # actually run by ClientXMPP, which may well not be the case.
38 await self.test_xmpp_server()
39 self.test_succeeded = True
41 # Even if an exception happens in `test_xmpp_server()`, we still
42 # need to disconnect explicitly, otherwise the process will hang
44 self.disconnect(wait=True)
46 async def test_xmpp_server(self):
47 log = logging.getLogger(__name__)
50 # Sending a test message
51 self.send_message(mto="azurediamond@example.com", mbody="Hello, this is dog.", mtype="chat")
52 log.info('Message sent')
54 # Test http upload (XEP_0363)
56 url = await self['xep_0363'].upload_file("${dummyFile}",timeout=10)
58 log.error("ERROR: Cannot run upload command. XEP_0363 seems broken")
60 log.info('Upload success!')
63 # TODO: use join_muc_wait() after slixmpp 1.8.0 is released.
64 self.plugin['xep_0045'].join_muc('testMucRoom', 'cthon98')
65 log.info('MUC join success!')
66 log.info('XMPP SCRIPT TEST SUCCESS')
68 def timeout_handler(signalnum, stackframe):
69 print('ERROR: xmpp-sendmessage timed out')
72 if __name__ == '__main__':
73 signal.signal(signal.SIGALRM, timeout_handler)
75 logging.basicConfig(level=logging.DEBUG,
76 format='%(levelname)-8s %(message)s')
78 ct = CthonTest('cthon98@example.com', 'nothunter2')
79 ct.register_plugin('xep_0071')
80 ct.register_plugin('xep_0128')
82 ct.register_plugin('xep_0363')
84 ct.register_plugin('xep_0045')
85 ct.connect(("${connectTo}", 5222))
86 ct.process(forever=False)
88 if not ct.test_succeeded: