test/muc_tester.lua

1
-- One-shot test client: joins the MUC on the remote server and repeatedly
2
-- sends "-ping" until the bot replies with a pong (or a timeout is hit).
3
-- Usage: lua5.4 muc_tester.lua [timeout-seconds]
4
local verse = require "verse";
5
require "verse.client";
6
 
7
verse.set_log_handler(print, { "warn", "error" });
8
 
9
local room = "test@muc.remotemuc";
10
local nick = "tester";
11
local timeout = tonumber(arg and arg[1]) or 60;
12
 
13
local c = verse.new();
14
c.connect_host = "prosody";
15
 
16
verse.add_task(timeout, function ()
17
	print("TEST FAIL: no pong after "..timeout.."s");
18
	os.exit(1);
19
end);
20
 
21
c:hook("ready", function ()
22
	c:send(verse.presence());
23
	c:send(verse.presence({ to = room.."/"..nick })
24
		:tag("x", { xmlns = "http://jabber.org/protocol/muc" })
25
			:tag("history", { maxstanzas = "0" }));
26
	-- Keep poking the room until the bot answers; re-send the join too, in
27
	-- case the room was recreated underneath us
28
	verse.add_task(2, function ()
29
		c:send(verse.presence({ to = room.."/"..nick })
30
			:tag("x", { xmlns = "http://jabber.org/protocol/muc" })
31
				:tag("history", { maxstanzas = "0" }));
32
		c:send(verse.message({ to = room, type = "groupchat" })
33
			:tag("body"):text("-ping"));
34
		return 5;
35
	end);
36
end);
37
 
38
c:hook("stanza", function (stanza)
39
	if stanza.name == "message" and stanza.attr.type == "groupchat" then
40
		local from_nick = (stanza.attr.from or ""):match("/(.+)$");
41
		local body = stanza:get_child_text("body");
42
		if body and from_nick and from_nick ~= nick and body:lower():match("pong") then
43
			print("Reply in "..room.." from "..from_nick..": "..body);
44
			print("TEST PASS");
45
			os.exit(0);
46
		end
47
	end
48
end);
49
 
50
c:hook("disconnected", function ()
51
	print("TEST FAIL: disconnected");
52
	os.exit(1);
53
end);
54
 
55
c:connect_client("tester@prosody", "testerpass");
56
 
57
verse.loop();