aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/mod_selftests.lua
blob: 1a2190dc98934f8bd0ef4314d11560544aafa6ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- Prosody IM v0.2
-- Copyright (C) 2008 Matthew Wild
-- Copyright (C) 2008 Waqas Hussain
-- 
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2
-- of the License, or (at your option) any later version.
-- 
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
-- 
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
--



local st = require "util.stanza";
local register_component = require "core.componentmanager".register_component;
local core_route_stanza = core_route_stanza;
local socket = require "socket";
local config = require "core.configmanager";
local ping_hosts = config.get("*", "mod_selftests", "ping_hosts") or { "coversant.interop.xmpp.org", "djabberd.interop.xmpp.org", "djabberd-trunk.interop.xmpp.org", "ejabberd.interop.xmpp.org", "openfire.interop.xmpp.org" };

local open_pings = {};

local t_insert = table.insert;

local log = require "util.logger".init("mod_selftests");

local tests_jid = "self_tests@getjabber.ath.cx";
local host = "getjabber.ath.cx";

if not (tests_jid and host) then
	for currhost in pairs(host) do
		if currhost ~= "localhost" then
			tests_jid, host = "self_tests@"..currhost, currhost;
		end
	end
end

if tests_jid and host then
	local bot = register_component(tests_jid, 	function(origin, stanza, ourhost)
										local time = open_pings[stanza.attr.id];
										
										if time then
											log("info", "Ping reply from %s in %fs", tostring(stanza.attr.from), socket.gettime() - time);
										else
											log("info", "Unexpected reply: %s", stanza:pretty_print());
										end
									end);


	local our_origin = hosts[host];
	module:add_event_hook("server-started", 
					function ()
						local id = st.new_id();
						local ping_attr = { xmlns = 'urn:xmpp:ping' };
						local function send_ping(to)
							log("info", "Sending ping to %s", to);
							core_route_stanza(our_origin, st.iq{ to = to, from = tests_jid, id = id, type = "get" }:tag("ping", ping_attr));
							open_pings[id] = socket.gettime();
						end
						
						for _, host in ipairs(ping_hosts) do
							send_ping(host);
						end
					end);
end