mod_muc_webchat_url: Fix default url
[prosody-modules.git] / mod_stanza_counter / mod_stanza_counter_http.lua
blob93a1bb3c5270c93889e9972604d21f6b8771bfbd
1 -- (C) 2011, Marco Cirillo (LW.Org)
2 -- Exposes stats on HTTP for the stanza counter module.
4 module:depends("http")
6 local base_path = module:get_option_string("stanza_counter_basepath", "/stanza-counter/")
8 -- http handlers
10 local r_200 = "\n<html>\n<head>\n<title>Prosody's Stanza Counter</title>\n<meta name=\"robots\" content=\"noindex, nofollow\" />\n</head>\n\n<body>\n<h3>Incoming and Outgoing stanzas divided per type</h3>\n<p><strong>Incoming IQs</strong>: %d<br/>\n<strong>Outgoing IQs</strong>: %d<br/>\n<strong>Incoming Messages</strong>: %d<br/>\n<strong>Outgoing Messages</strong>: %d<br/>\n<strong>Incoming Presences</strong>: %d<br/>\n<strong>Outgoing Presences</strong>: %d<p>\n</body>\n\n</html>\n"
12 local r_err = "\n<html>\n<head>\n<title>Prosody's Stanza Counter - Error %s</title>\n<meta name=\"robots\" content=\"noindex, nofollow\" />\n</head>\n\n<body>\n<h3>%s</h3>\n</body>\n\n</html>\n"
14 local function res(event, code, body, extras)
15 local response = event.response
17 if extras then
18 for header, data in pairs(extras) do response.headers[header] = data end
19 end
21 response.status_code = code
22 response:send(body)
23 end
25 local function req(event)
26 if not prosody.stanza_counter then
27 local err500 = r_err:format(event, 500, "Stats not found, is the counter module loaded?")
28 return res(500, err500) end
29 if method == "GET" then
30 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"],
31 prosody.stanza_counter.iq["outgoing"],
32 prosody.stanza_counter.message["incoming"],
33 prosody.stanza_counter.message["outgoing"],
34 prosody.stanza_counter.presence["incoming"],
35 prosody.stanza_counter.presence["outgoing"])
36 return res(event, 200, forge_res)
37 else
38 local err405 = r_err:format(405, "Only GET is supported")
39 return res(event, 405, err405, {["Allow"] = "GET"})
40 end
41 end
43 -- initialization.
45 module:provides("http", {
46 default_path = base_path,
47 route = {
48 ["GET /"] = req,
49 ["POST /"] = req