-- (C) 2011, Marco Cirillo (LW.Org) -- Exposes stats on HTTP for the stanza counter module. module:depends("http") local base_path = module:get_option_array("stanza_counter_basepath", "/stanza-counter/") -- http handlers local r_200 = "\n\n\nProsody's Stanza Counter\n\n\n\n\n

Incoming and Outgoing stanzas divided per type

\n

Incoming IQs: %d
\nOutgoing IQs: %d
\nIncoming Messages: %d
\nOutgoing Messages: %d
\nIncoming Presences: %d
\nOutgoing Presences: %d

\n\n\n\n" local r_err = "\n\n\nProsody's Stanza Counter - Error %s\n\n\n\n\n

%s

\n\n\n\n" local function res(event, code, body, extras) local response = event.response if extras then for header, data in pairs(extras) do response.headers[header] = data end end response.status_code = code response:send(body) end local function req(event) if not prosody.stanza_counter then local err500 = r_err:format(event, 500, "Stats not found, is the counter module loaded?") return res(500, err500) end if method == "GET" then local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"], prosody.stanza_counter.iq["outgoing"], prosody.stanza_counter.message["incoming"], prosody.stanza_counter.message["outgoing"], prosody.stanza_counter.presence["incoming"], prosody.stanza_counter.presence["outgoing"]) return res(event, 200, forge_res) else local err405 = r_err:format(405, "Only GET is supported") return res(event, 405, err405, {["Allow"] = "GET"}) end end -- initialization. module:provides("http", { default_path = base_path, route = { ["GET /"] = req, ["POST /"] = req } })