Software /
code /
prosody-modules
Comparison
mod_stanza_counter/mod_stanza_counter.lua @ 436:e4a1f0425380
mod_stanza_counter: splitted plugin :/
author | Marco Cirillo <maranda@lightwitch.org> |
---|---|
date | Tue, 20 Sep 2011 23:16:32 +0000 |
parent | 435:b6abe463b4fc |
child | 534:3d6e0e037dab |
comparison
equal
deleted
inserted
replaced
435:b6abe463b4fc | 436:e4a1f0425380 |
---|---|
1 -- (C) 2011, Marco Cirillo (LW.Org) | 1 -- (C) 2011, Marco Cirillo (LW.Org) |
2 -- General Stanzas' Counter with web output. | 2 -- General Stanzas' Counter. |
3 | 3 |
4 local jid_bare = require "util.jid".bare | 4 local jid_bare = require "util.jid".bare |
5 local httpserver = require "net.httpserver" | |
6 | |
7 local ports = module:get_option_array("stanza_counter_ports" or {{ port = 5280 }}) | |
8 | |
9 -- http handlers | |
10 | |
11 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 | |
13 local r_405 = "\n<html>\n<head>\n<title>Prosody's Stanza Counter - Error</title>\n<meta name=\"robots\" content=\"noindex, nofollow\" />\n</head>\n\n<body>\n<h3>Bad Method ... I only support GET.</h3>\n</body>\n\n</html>\n" | |
14 | |
15 local function res(code, r, h) | |
16 local response = { | |
17 status = code; | |
18 body = r; | |
19 } | |
20 | |
21 if h then response.headers = h; end | |
22 return response | |
23 end | |
24 | |
25 local function req(method, body, request) | |
26 if method == "GET" then | |
27 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"], | |
28 prosody.stanza_counter.iq["outgoing"], | |
29 prosody.stanza_counter.message["incoming"], | |
30 prosody.stanza_counter.message["outgoing"], | |
31 prosody.stanza_counter.presence["incoming"], | |
32 prosody.stanza_counter.presence["outgoing"]); | |
33 return res(200, forge_res) | |
34 else | |
35 return res(405, r_405, {["Allow"] = "GET"}) | |
36 end | |
37 end | |
38 | 5 |
39 -- Setup, Init functions. | 6 -- Setup, Init functions. |
40 -- initialize function counter table on the global object on start | 7 -- initialize function counter table on the global object on start |
41 local function init_counter() | 8 local function init_counter() |
42 prosody.stanza_counter = { | 9 prosody.stanza_counter = { |
44 message = { incoming=0, outgoing=0 }, | 11 message = { incoming=0, outgoing=0 }, |
45 presence = { incoming=0, outgoing=0 } | 12 presence = { incoming=0, outgoing=0 } |
46 } | 13 } |
47 end | 14 end |
48 | 15 |
49 -- init http interface | |
50 local function init_web() | |
51 httpserver.new_from_config(ports, req, { base = "stanza-counter" }) | |
52 end | |
53 | |
54 -- Setup on server start | 16 -- Setup on server start |
55 local function setup() | 17 local function setup() |
56 init_counter(); init_web(); | 18 init_counter(); |
57 end | 19 end |
58 | 20 |
59 -- Basic Stanzas' Counters | 21 -- Basic Stanzas' Counters |
60 local function iq_callback(check) | 22 local function iq_callback(check) |
61 return function(self) | 23 return function(self) |
130 module:hook("presence/full", pre_callback(false), 140) | 92 module:hook("presence/full", pre_callback(false), 140) |
131 module:hook("presence/host", pre_callback(false), 140) | 93 module:hook("presence/host", pre_callback(false), 140) |
132 | 94 |
133 -- Hook server start to initialize the counter. | 95 -- Hook server start to initialize the counter. |
134 module:hook("server-started", setup) | 96 module:hook("server-started", setup) |
97 |