Software / code / prosody
Comparison
plugins/mod_register.lua @ 6054:7a5ddbaf758d
Merge 0.9->0.10
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 02 Apr 2014 17:41:38 +0100 |
| parent | 5776:bd0ff8ae98a8 |
| child | 7017:ff734a602886 |
comparison
equal
deleted
inserted
replaced
| 6053:2f93a04564b2 | 6054:7a5ddbaf758d |
|---|---|
| 1 -- Prosody IM | 1 -- Prosody IM |
| 2 -- Copyright (C) 2008-2010 Matthew Wild | 2 -- Copyright (C) 2008-2010 Matthew Wild |
| 3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 -- Copyright (C) 2008-2010 Waqas Hussain |
| 4 -- | 4 -- |
| 5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
| 6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
| 7 -- | 7 -- |
| 8 | 8 |
| 9 | 9 |
| 70 | 70 |
| 71 module:add_feature("jabber:iq:register"); | 71 module:add_feature("jabber:iq:register"); |
| 72 | 72 |
| 73 local register_stream_feature = st.stanza("register", {xmlns="http://jabber.org/features/iq-register"}):up(); | 73 local register_stream_feature = st.stanza("register", {xmlns="http://jabber.org/features/iq-register"}):up(); |
| 74 module:hook("stream-features", function(event) | 74 module:hook("stream-features", function(event) |
| 75 local session, features = event.origin, event.features; | 75 local session, features = event.origin, event.features; |
| 76 | 76 |
| 77 -- Advertise registration to unauthorized clients only. | 77 -- Advertise registration to unauthorized clients only. |
| 78 if not(allow_registration) or session.type ~= "c2s_unauthed" then | 78 if not(allow_registration) or session.type ~= "c2s_unauthed" then |
| 79 return | 79 return |
| 80 end | 80 end |
| 100 local old_session_close = session.close; | 100 local old_session_close = session.close; |
| 101 session.close = function(session, ...) | 101 session.close = function(session, ...) |
| 102 session.send(st.reply(stanza)); | 102 session.send(st.reply(stanza)); |
| 103 return old_session_close(session, ...); | 103 return old_session_close(session, ...); |
| 104 end | 104 end |
| 105 | 105 |
| 106 local ok, err = usermanager_delete_user(username, host); | 106 local ok, err = usermanager_delete_user(username, host); |
| 107 | 107 |
| 108 if not ok then | 108 if not ok then |
| 109 module:log("debug", "Removing user account %s@%s failed: %s", username, host, err); | 109 module:log("debug", "Removing user account %s@%s failed: %s", username, host, err); |
| 110 session.close = old_session_close; | 110 session.close = old_session_close; |
| 111 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err)); | 111 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err)); |
| 112 return true; | 112 return true; |
| 113 end | 113 end |
| 114 | 114 |
| 115 module:log("info", "User removed their account: %s@%s", username, host); | 115 module:log("info", "User removed their account: %s@%s", username, host); |
| 116 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session }); | 116 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session }); |
| 117 else | 117 else |
| 118 local username = nodeprep(query:get_child("username"):get_text()); | 118 local username = nodeprep(query:get_child_text("username")); |
| 119 local password = query:get_child("password"):get_text(); | 119 local password = query:get_child_text("password"); |
| 120 if username and password then | 120 if username and password then |
| 121 if username == session.username then | 121 if username == session.username then |
| 122 if usermanager_set_password(username, password, session.host) then | 122 if usermanager_set_password(username, password, session.host) then |
| 123 session.send(st.reply(stanza)); | 123 session.send(st.reply(stanza)); |
| 124 else | 124 else |
| 168 return data; | 168 return data; |
| 169 end | 169 end |
| 170 end | 170 end |
| 171 | 171 |
| 172 local recent_ips = {}; | 172 local recent_ips = {}; |
| 173 local min_seconds_between_registrations = module:get_option("min_seconds_between_registrations"); | 173 local min_seconds_between_registrations = module:get_option_number("min_seconds_between_registrations"); |
| 174 local whitelist_only = module:get_option("whitelist_registration_only"); | 174 local whitelist_only = module:get_option_boolean("whitelist_registration_only"); |
| 175 local whitelisted_ips = module:get_option("registration_whitelist") or { "127.0.0.1" }; | 175 local whitelisted_ips = module:get_option_set("registration_whitelist", { "127.0.0.1" })._items; |
| 176 local blacklisted_ips = module:get_option("registration_blacklist") or {}; | 176 local blacklisted_ips = module:get_option_set("registration_blacklist", {})._items; |
| 177 | |
| 178 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end | |
| 179 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end | |
| 180 | 177 |
| 181 module:hook("stanza/iq/jabber:iq:register:query", function(event) | 178 module:hook("stanza/iq/jabber:iq:register:query", function(event) |
| 182 local session, stanza = event.origin, event.stanza; | 179 local session, stanza = event.origin, event.stanza; |
| 183 | 180 |
| 184 if not(allow_registration) or session.type ~= "c2s_unauthed" then | 181 if not(allow_registration) or session.type ~= "c2s_unauthed" then |
| 207 if not recent_ips[session.ip] then | 204 if not recent_ips[session.ip] then |
| 208 recent_ips[session.ip] = { time = os_time(), count = 1 }; | 205 recent_ips[session.ip] = { time = os_time(), count = 1 }; |
| 209 else | 206 else |
| 210 local ip = recent_ips[session.ip]; | 207 local ip = recent_ips[session.ip]; |
| 211 ip.count = ip.count + 1; | 208 ip.count = ip.count + 1; |
| 212 | 209 |
| 213 if os_time() - ip.time < min_seconds_between_registrations then | 210 if os_time() - ip.time < min_seconds_between_registrations then |
| 214 ip.time = os_time(); | 211 ip.time = os_time(); |
| 215 session.send(st.error_reply(stanza, "wait", "not-acceptable")); | 212 session.send(st.error_reply(stanza, "wait", "not-acceptable")); |
| 216 return true; | 213 return true; |
| 217 end | 214 end |