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