1 -- (C) 2011, Marco Cirillo (LW.Org)
2 -- General Stanzas' Counter.
4 local jid_bare
= require
"util.jid".bare
6 -- Setup, Init functions.
7 -- initialize function counter table on the global object on start
8 local function init_counter()
9 prosody
.stanza_counter
= {
10 iq
= { incoming
=0, outgoing
=0 },
11 message
= { incoming
=0, outgoing
=0 },
12 presence
= { incoming
=0, outgoing
=0 }
16 -- Setup on server start
17 local function setup() init_counter() end
19 -- Basic Stanzas' Counters
20 local function iq_callback(check
)
22 local origin
, stanza
= self
.origin
, self
.stanza
23 if not prosody
.stanza_counter
then init_counter() end
25 if not stanza
.attr
.to
or hosts
[jid_bare(stanza
.attr
.to
)] then return nil
27 prosody
.stanza_counter
.iq
["outgoing"] = prosody
.stanza_counter
.iq
["outgoing"] + 1
30 prosody
.stanza_counter
.iq
["incoming"] = prosody
.stanza_counter
.iq
["incoming"] + 1
35 local function mes_callback(check
)
37 local origin
, stanza
= self
.origin
, self
.stanza
38 if not prosody
.stanza_counter
then init_counter() end
40 if not stanza
.attr
.to
or hosts
[jid_bare(stanza
.attr
.to
)] then return nil
42 prosody
.stanza_counter
.message
["outgoing"] = prosody
.stanza_counter
.message
["outgoing"] + 1
45 prosody
.stanza_counter
.message
["incoming"] = prosody
.stanza_counter
.message
["incoming"] + 1
50 local function pre_callback(check
)
52 local origin
, stanza
= self
.origin
, self
.stanza
53 if not prosody
.stanza_counter
then init_counter() end
55 if not stanza
.attr
.to
or hosts
[jid_bare(stanza
.attr
.to
)] then return nil
57 prosody
.stanza_counter
.presence
["outgoing"] = prosody
.stanza_counter
.presence
["outgoing"] + 1
60 prosody
.stanza_counter
.presence
["incoming"] = prosody
.stanza_counter
.presence
["incoming"] + 1
65 -- Hook all pre-stanza events.
66 module
:hook("pre-iq/bare", iq_callback(true), 140)
67 module
:hook("pre-iq/full", iq_callback(true), 140)
68 module
:hook("pre-iq/host", iq_callback(true), 140)
70 module
:hook("pre-message/bare", mes_callback(true), 140)
71 module
:hook("pre-message/full", mes_callback(true), 140)
72 module
:hook("pre-message/host", mes_callback(true), 140)
74 module
:hook("pre-presence/bare", pre_callback(true), 140)
75 module
:hook("pre-presence/full", pre_callback(true), 140)
76 module
:hook("pre-presence/host", pre_callback(true), 140)
78 -- Hook all stanza events.
79 module
:hook("iq/bare", iq_callback(false), 140)
80 module
:hook("iq/full", iq_callback(false), 140)
81 module
:hook("iq/host", iq_callback(false), 140)
83 module
:hook("message/bare", mes_callback(false), 140)
84 module
:hook("message/full", mes_callback(false), 140)
85 module
:hook("message/host", mes_callback(false), 140)
87 module
:hook("presence/bare", pre_callback(false), 140)
88 module
:hook("presence/full", pre_callback(false), 140)
89 module
:hook("presence/host", pre_callback(false), 140)
91 -- Hook server start to initialize the counter.
92 module
:hook("server-started", setup
)