# HG changeset patch # User Marco Cirillo # Date 1316560592 0 # Node ID e4a1f042538089c0104eaf70b97ff7d05f32985e # Parent b6abe463b4fcbbd06b0d6a70dc55d757ce3b4452 mod_stanza_counter: splitted plugin :/ diff -r b6abe463b4fc -r e4a1f0425380 mod_stanza_counter/mod_stanza_counter.lua --- a/mod_stanza_counter/mod_stanza_counter.lua Tue Sep 20 22:42:03 2011 +0000 +++ b/mod_stanza_counter/mod_stanza_counter.lua Tue Sep 20 23:16:32 2011 +0000 @@ -1,40 +1,7 @@ -- (C) 2011, Marco Cirillo (LW.Org) --- General Stanzas' Counter with web output. +-- General Stanzas' Counter. local jid_bare = require "util.jid".bare -local httpserver = require "net.httpserver" - -local ports = module:get_option_array("stanza_counter_ports" or {{ port = 5280 }}) - --- 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_405 = "\n\n\nProsody's Stanza Counter - Error\n\n\n\n\n

Bad Method ... I only support GET.

\n\n\n\n" - -local function res(code, r, h) - local response = { - status = code; - body = r; - } - - if h then response.headers = h; end - return response -end - -local function req(method, body, request) - 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(200, forge_res) - else - return res(405, r_405, {["Allow"] = "GET"}) - end -end -- Setup, Init functions. -- initialize function counter table on the global object on start @@ -46,14 +13,9 @@ } end --- init http interface -local function init_web() - httpserver.new_from_config(ports, req, { base = "stanza-counter" }) -end - -- Setup on server start local function setup() - init_counter(); init_web(); + init_counter(); end -- Basic Stanzas' Counters @@ -132,3 +94,4 @@ -- Hook server start to initialize the counter. module:hook("server-started", setup) + diff -r b6abe463b4fc -r e4a1f0425380 mod_stanza_counter/mod_stanza_counter_http.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_stanza_counter/mod_stanza_counter_http.lua Tue Sep 20 23:16:32 2011 +0000 @@ -0,0 +1,51 @@ +-- (C) 2011, Marco Cirillo (LW.Org) +-- Exposes stats on HTTP for the stanza counter module. + +module:set_global() + +local ports = module:get_option_array("stanza_counter_http_ports" or {{ port = 5280 }}) + +local httpserver = require "net.httpserver" + +-- 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(code, r, h) + local response = { + status = code; + body = r; + } + + if h then response.headers = h; end + return response +end + +local function req(method, body, request) + if not prosody.stanza_counter then + local err500 = r_err:format("500", "Stats not found, is the counter module loaded?") + return res(500, err500) + 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(200, forge_res) + else + local err405 = r_err:format("405", "Only GET is supported") + return res(405, err405, {["Allow"] = "GET"}) + end +end + +-- initialization. +-- init http interface +local function setup() + httpserver.new_from_config(ports, req, { base = "stanza-counter" }) +end + +-- hook server started +module:hook("server-started", setup)