mod_ping: Convert from Windows line endings
[prosody.git] / plugins / mod_selftests.lua
1 -- Prosody IM v0.4
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10
11 local st = require "util.stanza";
12 local register_component = require "core.componentmanager".register_component;
13 local core_route_stanza = core_route_stanza;
14 local socket = require "socket";
15 local config = require "core.configmanager";
16 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" };
17
18 local open_pings = {};
19
20 local t_insert = table.insert;
21
22 local log = require "util.logger".init("mod_selftests");
23
24 local tests_jid = "self_tests@getjabber.ath.cx";
25 local host = "getjabber.ath.cx";
26
27 if not (tests_jid and host) then
28         for currhost in pairs(host) do
29                 if currhost ~= "localhost" then
30                         tests_jid, host = "self_tests@"..currhost, currhost;
31                 end
32         end
33 end
34
35 if tests_jid and host then
36         local bot = register_component(tests_jid,       function(origin, stanza, ourhost)
37                                                                                 local time = open_pings[stanza.attr.id];
38                                                                                 
39                                                                                 if time then
40                                                                                         log("info", "Ping reply from %s in %fs", tostring(stanza.attr.from), socket.gettime() - time);
41                                                                                 else
42                                                                                         log("info", "Unexpected reply: %s", stanza:pretty_print());
43                                                                                 end
44                                                                         end);
45
46
47         local our_origin = hosts[host];
48         module:add_event_hook("server-started", 
49                                         function ()
50                                                 local id = st.new_id();
51                                                 local ping_attr = { xmlns = 'urn:xmpp:ping' };
52                                                 local function send_ping(to)
53                                                         log("info", "Sending ping to %s", to);
54                                                         core_route_stanza(our_origin, st.iq{ to = to, from = tests_jid, id = id, type = "get" }:tag("ping", ping_attr));
55                                                         open_pings[id] = socket.gettime();
56                                                 end
57                                                 
58                                                 for _, host in ipairs(ping_hosts) do
59                                                         send_ping(host);
60                                                 end
61                                         end);
62 end