annotate mod_stanza_counter/mod_stanza_counter.lua @ 515:e98fe28c50b0

mod_host_guard: added exceptions/whitelisting to the blockall logic (makes little sense otherwise has s2s_disallow = true does the same)
author Marco Cirillo <maranda@lightwitch.org>
date Tue, 20 Dec 2011 20:19:53 +0000
parents e4a1f0425380
children 3d6e0e037dab
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)
436
e4a1f0425380 mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents: 435
diff changeset
2 -- General Stanzas' Counter.
432
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
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
6 -- Setup, Init functions.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
7 -- initialize function counter table on the global object on start
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
8 local function init_counter()
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
9 prosody.stanza_counter = {
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
10 iq = { incoming=0, outgoing=0 },
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
11 message = { incoming=0, outgoing=0 },
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
12 presence = { incoming=0, outgoing=0 }
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
13 }
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
14 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
15
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
16 -- Setup on server start
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
17 local function setup()
436
e4a1f0425380 mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents: 435
diff changeset
18 init_counter();
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
19 end
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 -- Basic Stanzas' Counters
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
22 local function iq_callback(check)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
23 return function(self)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
24 local origin, stanza = self.origin, self.stanza
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
25 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
26 if check then
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
27 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
28 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
29 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
30 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
31 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
32 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
33 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
34 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
35 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
36
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
37 local function mes_callback(check)
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
38 return function(self)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
39 local origin, stanza = self.origin, self.stanza
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
40 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
41 if check then
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
42 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
43 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
44 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
45 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
46 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
47 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
48 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
49 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
50 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
51
434
ac4a6cc5776c mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
Marco Cirillo <maranda@lightwitch.org>
parents: 433
diff changeset
52 local function pre_callback(check)
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
53 return function(self)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
54 local origin, stanza = self.origin, self.stanza
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
55 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
56 if check then
432
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
57 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
58 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
59 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
60 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
61 else
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
62 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
63 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
64 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
65 end
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
66
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
67
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
68
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
69 -- Hook all pre-stanza events.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
70 module:hook("pre-iq/bare", iq_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
71 module:hook("pre-iq/full", iq_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
72 module:hook("pre-iq/host", iq_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
73
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
74 module:hook("pre-message/bare", mes_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
75 module:hook("pre-message/full", mes_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
76 module:hook("pre-message/host", mes_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
77
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
78 module:hook("pre-presence/bare", pre_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
79 module:hook("pre-presence/full", pre_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
80 module:hook("pre-presence/host", pre_callback(true), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
81
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
82 -- Hook all stanza events.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
83 module:hook("iq/bare", iq_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
84 module:hook("iq/full", iq_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
85 module:hook("iq/host", iq_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
86
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
87 module:hook("message/bare", mes_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
88 module:hook("message/full", mes_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
89 module:hook("message/host", mes_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
90
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
91 module:hook("presence/bare", pre_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
92 module:hook("presence/full", pre_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
93 module:hook("presence/host", pre_callback(false), 140)
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
94
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
95 -- Hook server start to initialize the counter.
23a6289a00bd mod_stanza_counter: initial draft.
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
96 module:hook("server-started", setup)
436
e4a1f0425380 mod_stanza_counter: splitted plugin :/
Marco Cirillo <maranda@lightwitch.org>
parents: 435
diff changeset
97