Comparison

mod_stanza_counter/mod_stanza_counter.lua @ 434:ac4a6cc5776c

mod_stanza_counter: cleanup, corrections. (prosody starts, web stats work)
author Marco Cirillo <maranda@lightwitch.org>
date Tue, 20 Sep 2011 22:05:14 +0000
parent 433:967757965dbd
child 435:b6abe463b4fc
comparison
equal deleted inserted replaced
433:967757965dbd 434:ac4a6cc5776c
4 local jid_bare = require "util.jid".bare 4 local jid_bare = require "util.jid".bare
5 local httpserver = require "net.httpserver" 5 local httpserver = require "net.httpserver"
6 6
7 module.host = "*" -- Needs to be global for stats web wise. 7 module.host = "*" -- Needs to be global for stats web wise.
8 8
9 local ports = module:get_option_array("stanza_counter_ports" or {{ port = 5280; base = "stanza-counter" }}) 9 local ports = module:get_option_array("stanza_counter_ports" or {{ port = 5280 }})
10 10
11 -- http handlers 11 -- http handlers
12 12
13 local r_200 = "\n 13 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"
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>\n"
31 14
32 local r_405 = "\n 15 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"
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 </html>\n"
44 16
45 local function res(code, r, h) 17 local function res(code, r, h)
46 local response = { 18 local response = {
47 status = code; 19 status = code;
48 body = r; 20 body = r;
51 if h then response.headers = h; end 23 if h then response.headers = h; end
52 return response 24 return response
53 end 25 end
54 26
55 local function req(method, body, request) 27 local function req(method, body, request)
56 if method = "GET" then 28 if method == "GET" then
57 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"], 29 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"],
58 prosody.stanza_counter.iq["outgoing"], 30 prosody.stanza_counter.iq["outgoing"],
59 prosody.stanza_counter.message["incoming"], 31 prosody.stanza_counter.message["incoming"],
60 prosody.stanza_counter.message["outgoing"], 32 prosody.stanza_counter.message["outgoing"],
61 prosody.stanza_counter.presence["incoming"], 33 prosody.stanza_counter.presence["incoming"],
76 } 48 }
77 end 49 end
78 50
79 -- init http interface 51 -- init http interface
80 local function init_web() 52 local function init_web()
81 httpserver.new_from_config(ports, req) 53 httpserver.new_from_config(ports, req, { base = "stanza-counter" })
82 end 54 end
83 55
84 -- Setup on server start 56 -- Setup on server start
85 local function setup() 57 local function setup()
86 init_counter(); init_web(); 58 init_counter(); init_web();
89 -- Basic Stanzas' Counters 61 -- Basic Stanzas' Counters
90 local function iq_callback(check) 62 local function iq_callback(check)
91 return function(self) 63 return function(self)
92 local origin, stanza = self.origin, self.stanza 64 local origin, stanza = self.origin, self.stanza
93 if not prosody.stanza_counter then init_counter() end 65 if not prosody.stanza_counter then init_counter() end
94 if check 66 if check then
95 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil; 67 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil;
96 else 68 else
97 prosody.stanza_counter.iq["outgoing"] = prosody.stanza_counter.iq["outgoing"] + 1 69 prosody.stanza_counter.iq["outgoing"] = prosody.stanza_counter.iq["outgoing"] + 1
98 end 70 end
99 else 71 else
100 prosody.stanza_counter.iq["incoming"] = prosody.stanza_counter.iq["incoming"] + 1 72 prosody.stanza_counter.iq["incoming"] = prosody.stanza_counter.iq["incoming"] + 1
101 end 73 end
102 end 74 end
103 end 75 end
104 76
105 local function message_callback(check) 77 local function mes_callback(check)
106 return function(self) 78 return function(self)
107 local origin, stanza = self.origin, self.stanza 79 local origin, stanza = self.origin, self.stanza
108 if not prosody.stanza_counter then init_counter() end 80 if not prosody.stanza_counter then init_counter() end
109 if check 81 if check then
110 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil; 82 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil;
111 else 83 else
112 prosody.stanza_counter.message["outgoing"] = prosody.stanza_counter.message["outgoing"] + 1 84 prosody.stanza_counter.message["outgoing"] = prosody.stanza_counter.message["outgoing"] + 1
113 end 85 end
114 else 86 else
115 prosody.stanza_counter.message["incoming"] = prosody.stanza_counter.message["incoming"] + 1 87 prosody.stanza_counter.message["incoming"] = prosody.stanza_counter.message["incoming"] + 1
116 end 88 end
117 end 89 end
118 end 90 end
119 91
120 local function presence_callback(check) 92 local function pre_callback(check)
121 return function(self) 93 return function(self)
122 local origin, stanza = self.origin, self.stanza 94 local origin, stanza = self.origin, self.stanza
123 if not prosody.stanza_counter then init_counter() end 95 if not prosody.stanza_counter then init_counter() end
124 if check 96 if check then
125 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil; 97 if not stanza.attr.to or hosts[jid_bare(stanza.attr.to)] then return nil;
126 else 98 else
127 prosody.stanza_counter.presence["outgoing"] = prosody.stanza_counter.presence["outgoing"] + 1 99 prosody.stanza_counter.presence["outgoing"] = prosody.stanza_counter.presence["outgoing"] + 1
128 end 100 end
129 else 101 else