Mercurial > prosody-modules
comparison mod_block_registrations/mod_block_registrations.lua @ 1053:cabbcc1997d9
mod_block_registrations: Allow restricting registrations of certain user accounts, and/or ensure that registered accounts conform to a given pattern
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Thu, 06 Jun 2013 15:52:36 +0100 |
| parents | |
| children | dbaa67babeb4 |
comparison
equal
deleted
inserted
replaced
| 1052:80f0a3231c59 | 1053:cabbcc1997d9 |
|---|---|
| 1 local st = require "util.stanza"; | |
| 2 local nodeprep = require "util.encodings".stringprep.nodeprep; | |
| 3 | |
| 4 local block_users = module:get_option_set("block_registrations_users", { "admin" }); | |
| 5 local block_patterns = module:get_option_set("block_registrations_matching", {}); | |
| 6 local require_pattern = module:get_option_string("block_registrations_require"); | |
| 7 | |
| 8 function is_blocked(username) | |
| 9 -- Check if the username is simply blocked | |
| 10 if block_users:contains(username) then return true; end | |
| 11 | |
| 12 for pattern in block_patterns do | |
| 13 if username:match(pattern) then | |
| 14 return true; | |
| 15 end | |
| 16 end | |
| 17 -- Not blocked, but check that username matches allowed pattern | |
| 18 if require_pattern and not username:match(require_pattern) then | |
| 19 return true; | |
| 20 end | |
| 21 end | |
| 22 | |
| 23 module:hook("stanza/iq/jabber:iq:register:query", function(event) | |
| 24 local session, stanza = event.origin, event.stanza; | |
| 25 | |
| 26 if stanza.attr.type == "set" then | |
| 27 local query = stanza.tags[1]; | |
| 28 local username = nodeprep(query:get_child_text("username")); | |
| 29 if username and is_blocked(username) then | |
| 30 session.send(st.error_reply(stanza, "modify", "policy-violation", "Username is blocked")); | |
| 31 return true; | |
| 32 end | |
| 33 end | |
| 34 end, 10); |
