mod_muc_webchat_url: Fix default url
[prosody-modules.git] / mod_stanza_counter / mod_stanza_counter.lua
blob9b2c95cc0fe0d0aaf47da188ed0d873f54cddd42
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 }
14 end
16 -- Setup on server start
17 local function setup() init_counter() end
19 -- Basic Stanzas' Counters
20 local function iq_callback(check)
21 return function(self)
22 local origin, stanza = self.origin, self.stanza
23 if not prosody.stanza_counter then init_counter() end
24 if check then
25 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil
26 else
27 prosody.stanza_counter.iq["outgoing"] = prosody.stanza_counter.iq["outgoing"] + 1
28 end
29 else
30 prosody.stanza_counter.iq["incoming"] = prosody.stanza_counter.iq["incoming"] + 1
31 end
32 end
33 end
35 local function mes_callback(check)
36 return function(self)
37 local origin, stanza = self.origin, self.stanza
38 if not prosody.stanza_counter then init_counter() end
39 if check then
40 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil
41 else
42 prosody.stanza_counter.message["outgoing"] = prosody.stanza_counter.message["outgoing"] + 1
43 end
44 else
45 prosody.stanza_counter.message["incoming"] = prosody.stanza_counter.message["incoming"] + 1
46 end
47 end
48 end
50 local function pre_callback(check)
51 return function(self)
52 local origin, stanza = self.origin, self.stanza
53 if not prosody.stanza_counter then init_counter() end
54 if check then
55 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil
56 else
57 prosody.stanza_counter.presence["outgoing"] = prosody.stanza_counter.presence["outgoing"] + 1
58 end
59 else
60 prosody.stanza_counter.presence["incoming"] = prosody.stanza_counter.presence["incoming"] + 1
61 end
62 end
63 end
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)