annotate mod_register_json/mod_register_json.lua @ 584:1c2fc3b845f2

mod_admin_web: Clear commands in all cases new ones are loaded (thanks Maranda)
author Florian Zeitz <florob@babelmonkeys.de>
date Thu, 26 Jan 2012 19:38:03 +0100
parents 39a612eb4431
children 1004d7176be2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
1 -- Expose a simple servlet to handle user registrations from web pages
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
2 -- via JSON.
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
3 --
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
4 -- A Good chunk of the code is from mod_data_access.lua by Kim Alvefur
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
5 -- aka Zash.
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
6
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
7 local jid_prep = require "util.jid".prep
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
8 local jid_split = require "util.jid".split
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
9 local usermanager = require "core.usermanager"
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
10 local b64_decode = require "util.encodings".base64.decode
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
11 local json_decode = require "util.json".decode
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
12 local httpserver = require "net.httpserver"
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
13 local os_time = os.time
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
14 local nodeprep = require "util.encodings".stringprep.nodeprep
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
15
357
59345fd38ad9 mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents: 356
diff changeset
16 module.host = "*" -- HTTP/BOSH Servlets need to be global.
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
17
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
18 -- Pick up configuration.
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
19
560
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
20 local set_realm_name = module:get_option_string("reg_servlet_realm", "Restricted")
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
21 local throttle_time = module:get_option_number("reg_servlet_ttime", nil)
550
d8143f627f9f mod_register_json: modified code to employ get_option_set for true sets, and contains meta method
Marco Cirillo <maranda@lightwitch.org>
parents: 548
diff changeset
22 local whitelist = module:get_option_set("reg_servlet_wl", {})
d8143f627f9f mod_register_json: modified code to employ get_option_set for true sets, and contains meta method
Marco Cirillo <maranda@lightwitch.org>
parents: 548
diff changeset
23 local blacklist = module:get_option_set("reg_servlet_bl", {})
560
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
24 local ports = module:get_option_array("reg_servlet_ports", {{ port = 9280 }})
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
25 local recent_ips = {}
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
26
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
27 -- Begin
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
28
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
29 local function http_response(code, message, extra_headers)
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
30 local response = {
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
31 status = code .. " " .. message,
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
32 body = message .. "\n" }
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
33 if extra_headers then response.headers = extra_headers end
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
34 return response
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
35 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
36
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
37 local function handle_req(method, body, request)
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
38 if request.method ~= "POST" then
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
39 return http_response(405, "Bad method...", {["Allow"] = "POST"})
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
40 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
41 if not request.headers["authorization"] then
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
42 return http_response(401, "No... No...", {["WWW-Authenticate"]='Basic realm="'.. set_realm_name ..'"'})
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
43 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
44
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
45 local user, password = b64_decode(request.headers.authorization:match("[^ ]*$") or ""):match("([^:]*):(.*)")
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
46 user = jid_prep(user)
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
47 if not user or not password then return http_response(400, "What's this..?") end
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
48 local user_node, user_host = jid_split(user)
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
49 if not hosts[user_host] then return http_response(401, "Negative.") end
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
50
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
51 module:log("warn", "%s is authing to submit a new user registration data", user)
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
52 if not usermanager.test_password(user_node, user_host, password) then
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
53 module:log("warn", "%s failed authentication", user)
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
54 return http_response(401, "Who the hell are you?! Guards!")
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
55 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
56
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
57 local req_body
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
58 -- We check that what we have is valid JSON wise else we throw an error...
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
59 if not pcall(function() req_body = json_decode(body) end) then
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
60 module:log("debug", "JSON data submitted for user registration by %s failed to Decode.", user)
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
61 return http_response(400, "JSON Decoding failed.")
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
62 else
363
72a5778579c5 mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents: 362
diff changeset
63 -- Decode JSON data and check that all bits are there else throw an error
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
64 req_body = json_decode(body)
363
72a5778579c5 mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents: 362
diff changeset
65 if req_body["username"] == nil or req_body["password"] == nil or req_body["host"] == nil or req_body["ip"] == nil then
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
66 module:log("debug", "%s supplied an insufficent number of elements or wrong elements for the JSON registration", user)
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
67 return http_response(400, "Invalid syntax.")
363
72a5778579c5 mod_register_json: Let's call it the first commit, fixed all code errors (aka it works).
Marco Cirillo <maranda@lightwitch.org>
parents: 362
diff changeset
68 end
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
69 -- Check if user is an admin of said host
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
70 if not usermanager.is_admin(user, req_body["host"]) then
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
71 module:log("warn", "%s tried to submit registration data for %s but he's not an admin", user, req_body["host"])
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
72 return http_response(401, "I obey only to my masters... Have a nice day.")
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
73 else
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
74 -- Checks for both Throttling/Whitelist and Blacklist (basically copycatted from prosody's register.lua code)
550
d8143f627f9f mod_register_json: modified code to employ get_option_set for true sets, and contains meta method
Marco Cirillo <maranda@lightwitch.org>
parents: 548
diff changeset
75 if blacklist:contains(req_body["ip"]) then module:log("warn", "Attempt of reg. submission to the JSON servlet from blacklisted address: %s", req_body["ip"]) ; return http_response(403, "The specified address is blacklisted, sorry sorry.") end
d8143f627f9f mod_register_json: modified code to employ get_option_set for true sets, and contains meta method
Marco Cirillo <maranda@lightwitch.org>
parents: 548
diff changeset
76 if throttle_time and not whitelist:contains(req_body["ip"]) then
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
77 if not recent_ips[req_body["ip"]] then
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
78 recent_ips[req_body["ip"]] = { time = os_time(), count = 1 }
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
79 else
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
80 local ip = recent_ips[req_body["ip"]]
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
81 ip.count = ip.count + 1
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
82
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
83 if os_time() - ip.time < throttle_time then
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
84 ip.time = os_time()
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
85 module:log("warn", "JSON Registration request from %s has been throttled.", req_body["ip"])
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
86 return http_response(503, "Woah... How many users you want to register..? Request throttled, wait a bit and try again.")
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
87 end
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
88 ip.time = os_time()
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
89 end
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
90 end
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
91
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
92 -- We first check if the supplied username for registration is already there.
430
f0fafd19fd72 mod_register_json: changed pestered code to something less pestered. (added nodeprep)
Marco Cirillo <maranda@lightwitch.org>
parents: 429
diff changeset
93 -- And nodeprep the username
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
94 local username = nodeprep(req_body["username"])
430
f0fafd19fd72 mod_register_json: changed pestered code to something less pestered. (added nodeprep)
Marco Cirillo <maranda@lightwitch.org>
parents: 429
diff changeset
95 if not usermanager.user_exists(username, req_body["host"]) then
f0fafd19fd72 mod_register_json: changed pestered code to something less pestered. (added nodeprep)
Marco Cirillo <maranda@lightwitch.org>
parents: 429
diff changeset
96 if not username then
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
97 module:log("debug", "%s supplied an username containing invalid characters: %s", user, username)
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
98 return http_response(406, "Supplied username contains invalid characters, see RFC 6122.")
429
ea6641deec12 mod_register_json: added check for invalid characters in the username.
Marco Cirillo <maranda@lightwitch.org>
parents: 370
diff changeset
99 else
553
7310ceb7564f mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents: 551
diff changeset
100 local ok, error = usermanager.create_user(username, req_body["password"], req_body["host"])
7310ceb7564f mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents: 551
diff changeset
101 if ok then
7310ceb7564f mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents: 551
diff changeset
102 hosts[req_body["host"]].events.fire_event("user-registered", { username = username, host = req_body["host"], source = "mod_register_json", session = { ip = req_body["ip"] } })
7310ceb7564f mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents: 551
diff changeset
103 module:log("debug", "%s registration data submission for %s@%s is successful", user, username, req_body["host"])
7310ceb7564f mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents: 551
diff changeset
104 return http_response(200, "Done.")
7310ceb7564f mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents: 551
diff changeset
105 else
7310ceb7564f mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents: 551
diff changeset
106 module:log("error", "user creation failed: "..error)
7310ceb7564f mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents: 551
diff changeset
107 return http_response(500, "Encountered server error while creating the user: "..error)
7310ceb7564f mod_register_json: checking out if the user creation succeeded or not is good practice.
Marco Cirillo <maranda@lightwitch.org>
parents: 551
diff changeset
108 end
429
ea6641deec12 mod_register_json: added check for invalid characters in the username.
Marco Cirillo <maranda@lightwitch.org>
parents: 370
diff changeset
109 end
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
110 else
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
111 module:log("debug", "%s registration data submission for %s failed (user already exists)", user, username)
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
112 return http_response(409, "User already exists.")
361
146496a3be78 mod_register_json: Failed at JSON successful decode check, fixed with a code refactor.
Marco Cirillo <maranda@lightwitch.org>
parents: 360
diff changeset
113 end
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
114 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
115 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
116 end
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
117
359
5d22ebcb9ec5 mod_register_json: Changed log levels, added a few primitive throttling/whitelist/blacklist options
Marco Cirillo <maranda@lightwitch.org>
parents: 358
diff changeset
118 -- Set it up!
560
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
119 function cleanup() -- it could be better if module:hook("module-unloaded", ...) actually worked.
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
120 module:log("debug", "Cleaning up handlers and stuff as module is being unloaded.")
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
121 for _, options in ipairs(ports) do
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
122 if options.port then
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
123 httpserver.new.http_servers[options.port].handlers[options.path or "register_account"] = nil
370
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
124 end
16da8cd69715 mod_register_json: There again, finally found the right way to pass the ports table to be processed correctly.
Marco Cirillo <maranda@lightwitch.org>
parents: 369
diff changeset
125 end
560
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
126
561
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
127 -- if there are no handlers left clean and close the socket, doesn't work with server_event
566
39a612eb4431 mod_register_json: revert to use module:get_option_boolean
Marco Cirillo <maranda@lightwitch.org>
parents: 563
diff changeset
128 local event = module:get_option_boolen("use_libevent", false)
561
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
129
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
130 if not event then
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
131 for _, options in ipairs(ports) do
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
132 if options.port and not next(httpserver.new.http_servers[options.port].handlers) then
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
133 httpserver.new.http_servers[options.port] = nil
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
134 if options.interface then
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
135 for _, value in ipairs(options.interface) do
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
136 if server.getserver(value, options.port) then server.removeserver(value, options.port) end
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
137 end
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
138 else if server.getserver("*", options.port) then server.removeserver("*", options.port) end end
f2ec7149b005 mod_register_json: reworked the ccleanup function so that it doesn' try to close an unused listener port if server_event is in use (works only with select)
Marco Cirillo <maranda@lightwitch.org>
parents: 560
diff changeset
139 end
560
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
140 end
357
59345fd38ad9 mod_register_json: Changed a few bits to allow the service to be specified as standalone (by default uses port 9443), Q: Does it work?
Marco Cirillo <maranda@lightwitch.org>
parents: 356
diff changeset
141 end
560
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
142
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
143 prosody.events.remove_handler("module-unloaded", cleanup)
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
144 end
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
145
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
146 function setup()
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
147 for id, options in ipairs(ports) do
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
148 if not options.port then
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
149 if not options.ssl then ports[id].port = 9280
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
150 else ports[id].port = 9443 end
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
151 elseif options.port == 9280 and options.ssl then ports[id].port = 9443 end end
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
152 httpserver.new_from_config(ports, handle_req, { base = "register_account" })
b62f5e38f865 mod_register_json: added auto-cleanup logic to the module so it can at least be unloaded and reloaded without fuss. (Experimental: works with server_select and trunk)
Marco Cirillo <maranda@lightwitch.org>
parents: 553
diff changeset
153 prosody.events.add_handler("module-unloaded", cleanup)
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
154 end
369
29a8828243ce mod_register_json: Fixed http listener creation syntax. (Please document that in the API, that would avoid my brain overheating, thank you.)
Marco Cirillo <maranda@lightwitch.org>
parents: 365
diff changeset
155
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
156 if prosody.start_time then -- already started
529
84e992f70ba3 mod_register_json: stick to one code "punctuation" style.
Marco Cirillo <maranda@lightwitch.org>
parents: 430
diff changeset
157 setup()
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
158 else
536
09280dd0b22e mod_register_json: replaced prosody.events.add_handler with module:hook.
Marco Cirillo <maranda@lightwitch.org>
parents: 529
diff changeset
159 module:hook("server-started", setup)
355
a5da789b2e7d mod_register_json: First commit (needs tests).
Marco Cirillo <maranda@lightwitch.org>
parents:
diff changeset
160 end