Annotate

mod_stanza_counter/mod_stanza_counter.lua @ 435:b6abe463b4fc

mod_stanza_counter: "*" doesn't handle stanzas.
author Marco Cirillo <maranda@lightwitch.org>
date Tue, 20 Sep 2011 22:42:03 +0000
parent 434:ac4a6cc5776c
child 436:e4a1f0425380
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
1 -- (C) 2011, Marco Cirillo (LW.Org)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
2 -- General Stanzas' Counter with web output.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
3
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
4 local jid_bare = require "util.jid".bare
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
5 local httpserver = require "net.httpserver"
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
6
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
7 local ports = module:get_option_array("stanza_counter_ports" or {{ port = 5280 }})
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
8
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
9 -- http handlers
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
10
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
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"
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
12
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
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"
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
14
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
15 local function res(code, r, h)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
16 local response = {
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
17 status = code;
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
18 body = r;
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
19 }
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
20
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
21 if h then response.headers = h; end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
22 return response
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
23 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
24
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
25 local function req(method, body, request)
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
26 if method == "GET" then
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
27 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"],
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
28 prosody.stanza_counter.iq["outgoing"],
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
29 prosody.stanza_counter.message["incoming"],
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
30 prosody.stanza_counter.message["outgoing"],
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
31 prosody.stanza_counter.presence["incoming"],
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
32 prosody.stanza_counter.presence["outgoing"]);
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
33 return res(200, forge_res)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
34 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
35 return res(405, r_405, {["Allow"] = "GET"})
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
36 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
37 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
38
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
39 -- Setup, Init functions.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
40 -- initialize function counter table on the global object on start
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
41 local function init_counter()
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
42 prosody.stanza_counter = {
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
43 iq = { incoming=0, outgoing=0 },
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
44 message = { incoming=0, outgoing=0 },
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
45 presence = { incoming=0, outgoing=0 }
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
46 }
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
47 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
48
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
49 -- init http interface
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
50 local function init_web()
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
51 httpserver.new_from_config(ports, req, { base = "stanza-counter" })
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
52 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
53
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
54 -- Setup on server start
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
55 local function setup()
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
56 init_counter(); init_web();
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
57 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
58
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
59 -- Basic Stanzas' Counters
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
60 local function iq_callback(check)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
61 return function(self)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
62 local origin, stanza = self.origin, self.stanza
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
63 if not prosody.stanza_counter then init_counter() end
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
64 if check then
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
65 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil;
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
66 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
67 prosody.stanza_counter.iq["outgoing"] = prosody.stanza_counter.iq["outgoing"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
68 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
69 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
70 prosody.stanza_counter.iq["incoming"] = prosody.stanza_counter.iq["incoming"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
71 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
72 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
73 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
74
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
75 local function mes_callback(check)
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
76 return function(self)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
77 local origin, stanza = self.origin, self.stanza
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
78 if not prosody.stanza_counter then init_counter() end
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
79 if check then
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
80 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil;
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
81 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
82 prosody.stanza_counter.message["outgoing"] = prosody.stanza_counter.message["outgoing"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
83 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
84 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
85 prosody.stanza_counter.message["incoming"] = prosody.stanza_counter.message["incoming"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
86 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
87 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
88 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
89
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
90 local function pre_callback(check)
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
91 return function(self)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
92 local origin, stanza = self.origin, self.stanza
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
93 if not prosody.stanza_counter then init_counter() end
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
94 if check then
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
95 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil;
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
96 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
97 prosody.stanza_counter.presence["outgoing"] = prosody.stanza_counter.presence["outgoing"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
98 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
99 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
100 prosody.stanza_counter.presence["incoming"] = prosody.stanza_counter.presence["incoming"] + 1
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
101 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
102 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
103 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
104
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
105
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
106
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
107 -- Hook all pre-stanza events.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
108 module:hook("pre-iq/bare", iq_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
109 module:hook("pre-iq/full", iq_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
110 module:hook("pre-iq/host", iq_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
111
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
112 module:hook("pre-message/bare", mes_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
113 module:hook("pre-message/full", mes_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
114 module:hook("pre-message/host", mes_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
115
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
116 module:hook("pre-presence/bare", pre_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
117 module:hook("pre-presence/full", pre_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
118 module:hook("pre-presence/host", pre_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
119
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
120 -- Hook all stanza events.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
121 module:hook("iq/bare", iq_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
122 module:hook("iq/full", iq_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
123 module:hook("iq/host", iq_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
124
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
125 module:hook("message/bare", mes_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
126 module:hook("message/full", mes_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
127 module:hook("message/host", mes_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
128
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
129 module:hook("presence/bare", pre_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
130 module:hook("presence/full", pre_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
131 module:hook("presence/host", pre_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
132
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
133 -- Hook server start to initialize the counter.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
134 module:hook("server-started", setup)