comparison mod_stanza_counter/mod_stanza_counter.lua @ 432:23a6289a00bd

mod_stanza_counter: initial draft.
author Marco Cirillo <maranda@lightwitch.org>
date Tue, 20 Sep 2011 21:15:29 +0000
parents
children 967757965dbd
comparison
equal deleted inserted replaced
431:fb7898826026 432:23a6289a00bd
1 -- (C) 2011, Marco Cirillo (LW.Org)
2 -- General Stanzas' Counter with web output.
3
4 local jid_bare = require "util.jid".bare
5 local httpserver = require "net.httpserver"
6
7 module.host = "*" -- Needs to be global for stats web wise.
8
9 local ports = module:get_option_array("stanza_counter_ports" or {{ port = 5280; base = "stanza-counter" }})
10
11 -- http handlers
12
13 local r_200 = "\n
14 <html>\n
15 <head>\n
16 <title>Prosody's Stanza Counter</title>\n
17 <meta name=\"robots\" content=\"noindex, nofollow\" />\n
18 </head>\n
19 \n
20 <body>\n
21 <h3>Incoming and Outgoing stanzas divided per type</h3>\n
22 <p><strong>Incoming IQs</strong>: %d<br/>\n
23 <strong>Outgoing IQs</strong>: %d<br/>\n
24 <strong>Incoming Messages</strong>: %d<br/>\n
25 <strong>Outgoing Messages</strong>: %d<br/>\n
26 <strong>Incoming Presences</strong>: %d<br/>\n
27 <strong>Outgoing Presences</strong>: %d<p>\n
28 </body>\n
29 \n
30 </html>"
31
32 local r_405 = "\n
33 <html>\n
34 <head>\n
35 <title>Prosody's Stanza Counter - Error</title>\n
36 <meta name=\"robots\" content=\"noindex, nofollow\" />\n
37 </head>\n
38 \n
39 <body>\n
40 <h3>Bad Method ... I only support GET.</h3>\n
41 </body>\n
42 \n"
43
44 local function res(code, r, h)
45 local response = {
46 status = code;
47 body = r;
48 }
49
50 if h then response.headers = h; end
51 return response
52 end
53
54 local function req(method, body, request)
55 if method = "GET" then
56 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"],
57 prosody.stanza_counter.iq["outgoing"],
58 prosody.stanza_counter.message["incoming"],
59 prosody.stanza_counter.message["outgoing"],
60 prosody.stanza_counter.presence["incoming"],
61 prosody.stanza_counter.presence["outgoing"]);
62 return res(200, forge_res)
63 else
64 return res(405, r_405, {["Allow"] = "GET"})
65 end
66 end
67
68 -- Setup, Init functions.
69 -- initialize function counter table on the global object on start
70 local function init_counter()
71 prosody.stanza_counter = {
72 iq = { incoming=0, outgoing=0 },
73 message = { incoming=0, outgoing=0 },
74 presence = { incoming=0, outgoing=0 }
75 }
76 end
77
78 -- init http interface
79 local function init_web()
80 httpserver.new_from_config(ports, req)
81 end
82
83 -- Setup on server start
84 local function setup()
85 init_counter(); init_web();
86 end
87
88 -- Basic Stanzas' Counters
89 local function iq_callback(check)
90 return function(self)
91 local origin, stanza = self.origin, self.stanza
92 if not prosody.stanza_counter then init_counter() end
93 if check
94 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil;
95 else
96 prosody.stanza_counter.iq["outgoing"] = prosody.stanza_counter.iq["outgoing"] + 1
97 end
98 else
99 prosody.stanza_counter.iq["incoming"] = prosody.stanza_counter.iq["incoming"] + 1
100 end
101 end
102 end
103
104 local function message_callback(check)
105 return function(self)
106 local origin, stanza = self.origin, self.stanza
107 if not prosody.stanza_counter then init_counter() end
108 if check
109 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil;
110 else
111 prosody.stanza_counter.message["outgoing"] = prosody.stanza_counter.message["outgoing"] + 1
112 end
113 else
114 prosody.stanza_counter.message["incoming"] = prosody.stanza_counter.message["incoming"] + 1
115 end
116 end
117 end
118
119 local function presence_callback(check)
120 return function(self)
121 local origin, stanza = self.origin, self.stanza
122 if not prosody.stanza_counter then init_counter() end
123 if check
124 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil;
125 else
126 prosody.stanza_counter.presence["outgoing"] = prosody.stanza_counter.presence["outgoing"] + 1
127 end
128 else
129 prosody.stanza_counter.presence["incoming"] = prosody.stanza_counter.presence["incoming"] + 1
130 end
131 end
132 end
133
134
135
136 -- Hook all pre-stanza events.
137 module:hook("pre-iq/bare", iq_callback(true), 140)
138 module:hook("pre-iq/full", iq_callback(true), 140)
139 module:hook("pre-iq/host", iq_callback(true), 140)
140
141 module:hook("pre-message/bare", mes_callback(true), 140)
142 module:hook("pre-message/full", mes_callback(true), 140)
143 module:hook("pre-message/host", mes_callback(true), 140)
144
145 module:hook("pre-presence/bare", pre_callback(true), 140)
146 module:hook("pre-presence/full", pre_callback(true), 140)
147 module:hook("pre-presence/host", pre_callback(true), 140)
148
149 -- Hook all stanza events.
150 module:hook("iq/bare", iq_callback(false), 140)
151 module:hook("iq/full", iq_callback(false), 140)
152 module:hook("iq/host", iq_callback(false), 140)
153
154 module:hook("message/bare", mes_callback(false), 140)
155 module:hook("message/full", mes_callback(false), 140)
156 module:hook("message/host", mes_callback(false), 140)
157
158 module:hook("presence/bare", pre_callback(false), 140)
159 module:hook("presence/full", pre_callback(false), 140)
160 module:hook("presence/host", pre_callback(false), 140)
161
162 -- Hook server start to initialize the counter.
163 module:hook("server-started", setup)