Automated merge with http://waqas.ath.cx/
[prosody.git] / plugins / mod_selftests.lua
1 -- Prosody IM v0.1
2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
4 -- 
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
9 -- 
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 -- GNU General Public License for more details.
14 -- 
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 --
19
20
21
22 local st = require "util.stanza";
23 local register_component = require "core.componentmanager".register_component;
24 local core_route_stanza = core_route_stanza;
25 local socket = require "socket";
26 local config = require "core.configmanager";
27 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" };
28
29 local open_pings = {};
30
31 local t_insert = table.insert;
32
33 local log = require "util.logger".init("mod_selftests");
34
35 local tests_jid = "self_tests@getjabber.ath.cx";
36 local host = "getjabber.ath.cx";
37
38 if not (tests_jid and host) then
39         for currhost in pairs(host) do
40                 if currhost ~= "localhost" then
41                         tests_jid, host = "self_tests@"..currhost, currhost;
42                 end
43         end
44 end
45
46 if tests_jid and host then
47         local bot = register_component(tests_jid,       function(origin, stanza, ourhost)
48                                                                                 local time = open_pings[stanza.attr.id];
49                                                                                 
50                                                                                 if time then
51                                                                                         log("info", "Ping reply from %s in %fs", tostring(stanza.attr.from), socket.gettime() - time);
52                                                                                 else
53                                                                                         log("info", "Unexpected reply: %s", stanza:pretty_print());
54                                                                                 end
55                                                                         end);
56
57
58         local our_origin = hosts[host];
59         module:add_event_hook("server-started", 
60                                         function ()
61                                                 local id = st.new_id();
62                                                 local ping_attr = { xmlns = 'urn:xmpp:ping' };
63                                                 local function send_ping(to)
64                                                         log("info", "Sending ping to %s", to);
65                                                         core_route_stanza(our_origin, st.iq{ to = to, from = tests_jid, id = id, type = "get" }:tag("ping", ping_attr));
66                                                         open_pings[id] = socket.gettime();
67                                                 end
68                                                 
69                                                 for _, host in ipairs(ping_hosts) do
70                                                         send_ping(host);
71                                                 end
72                                         end);
73 end