comparison mod_stanza_counter/mod_stanza_counter_http.lua @ 535:39c7115be370

mod_stanza_counter_http: replaced prosody.events.add_handler with module:hook.
author Marco Cirillo <maranda@lightwitch.org>
date Sun, 08 Jan 2012 03:52:38 +0000
parents d4adda950546
children b3f8435e661c
comparison
equal deleted inserted replaced
534:3d6e0e037dab 535:39c7115be370
13 13
14 local r_err = "\n<html>\n<head>\n<title>Prosody's Stanza Counter - Error %s</title>\n<meta name=\"robots\" content=\"noindex, nofollow\" />\n</head>\n\n<body>\n<h3>%s</h3>\n</body>\n\n</html>\n" 14 local r_err = "\n<html>\n<head>\n<title>Prosody's Stanza Counter - Error %s</title>\n<meta name=\"robots\" content=\"noindex, nofollow\" />\n</head>\n\n<body>\n<h3>%s</h3>\n</body>\n\n</html>\n"
15 15
16 local function res(code, r, h) 16 local function res(code, r, h)
17 local response = { 17 local response = {
18 status = code; 18 status = code,
19 body = r; 19 body = r
20 } 20 }
21 21
22 if h then response.headers = h; end 22 if h then response.headers = h end
23 return response 23 return response
24 end 24 end
25 25
26 local function req(method, body, request) 26 local function req(method, body, request)
27 if not prosody.stanza_counter then 27 if not prosody.stanza_counter then
31 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"], 31 local forge_res = r_200:format(prosody.stanza_counter.iq["incoming"],
32 prosody.stanza_counter.iq["outgoing"], 32 prosody.stanza_counter.iq["outgoing"],
33 prosody.stanza_counter.message["incoming"], 33 prosody.stanza_counter.message["incoming"],
34 prosody.stanza_counter.message["outgoing"], 34 prosody.stanza_counter.message["outgoing"],
35 prosody.stanza_counter.presence["incoming"], 35 prosody.stanza_counter.presence["incoming"],
36 prosody.stanza_counter.presence["outgoing"]); 36 prosody.stanza_counter.presence["outgoing"])
37 return res(200, forge_res) 37 return res(200, forge_res)
38 else 38 else
39 local err405 = r_err:format("405", "Only GET is supported") 39 local err405 = r_err:format("405", "Only GET is supported")
40 return res(405, err405, {["Allow"] = "GET"}) 40 return res(405, err405, {["Allow"] = "GET"})
41 end 41 end
45 -- init http interface 45 -- init http interface
46 local function setup() 46 local function setup()
47 httpserver.new_from_config(ports, req, { base = "stanza-counter" }) 47 httpserver.new_from_config(ports, req, { base = "stanza-counter" })
48 end 48 end
49 49
50 -- hook server started 50 -- set it
51 module:hook("server-started", setup) 51 if prosody.start_time then setup() else module:hook("server-started", setup) end