Comparison

plugins/muc/register.lib.lua @ 11200:bf8f2da84007

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 05 Nov 2020 22:31:25 +0100
parent 10794:4585fe53e21f
child 11286:336cba957c88
comparison
equal deleted inserted replaced
11199:6c7c50a4de32 11200:bf8f2da84007
13 if room._reserved_nicks then 13 if room._reserved_nicks then
14 return room._reserved_nicks; 14 return room._reserved_nicks;
15 end 15 end
16 module:log("debug", "Refreshing reserved nicks..."); 16 module:log("debug", "Refreshing reserved nicks...");
17 local reserved_nicks = {}; 17 local reserved_nicks = {};
18 for jid in room:each_affiliation() do 18 for jid, _, data in room:each_affiliation() do
19 local data = room._affiliation_data[jid];
20 local nick = data and data.reserved_nickname; 19 local nick = data and data.reserved_nickname;
21 module:log("debug", "Refreshed for %s: %s", jid, nick); 20 module:log("debug", "Refreshed for %s: %s", jid, nick);
22 if nick then 21 if nick then
23 reserved_nicks[nick] = jid; 22 reserved_nicks[nick] = jid;
24 end 23 end
52 event.reply:tag("feature", { var = "jabber:iq:register" }):up(); 51 event.reply:tag("feature", { var = "jabber:iq:register" }):up();
53 end); 52 end);
54 53
55 local registration_form = dataforms.new { 54 local registration_form = dataforms.new {
56 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#register" }, 55 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#register" },
57 { name = "muc#register_roomnick", type = "text-single", label = "Nickname"}, 56 { name = "muc#register_roomnick", type = "text-single", required = true, label = "Nickname"},
58 }; 57 };
59 58
60 local function enforce_nick_policy(event) 59 local function enforce_nick_policy(event)
61 local origin, stanza = event.origin, event.stanza; 60 local origin, stanza = event.origin, event.stanza;
62 local room = assert(event.room); -- FIXME 61 local room = assert(event.room); -- FIXME
65 -- Check if the chosen nickname is reserved 64 -- Check if the chosen nickname is reserved
66 local requested_nick = jid_resource(stanza.attr.to); 65 local requested_nick = jid_resource(stanza.attr.to);
67 local reserved_by = get_registered_jid(room, requested_nick); 66 local reserved_by = get_registered_jid(room, requested_nick);
68 if reserved_by and reserved_by ~= jid_bare(stanza.attr.from) then 67 if reserved_by and reserved_by ~= jid_bare(stanza.attr.from) then
69 module:log("debug", "%s attempted to use nick %s reserved by %s", stanza.attr.from, requested_nick, reserved_by); 68 module:log("debug", "%s attempted to use nick %s reserved by %s", stanza.attr.from, requested_nick, reserved_by);
70 local reply = st.error_reply(stanza, "cancel", "conflict"):up(); 69 local reply = st.error_reply(stanza, "cancel", "conflict", nil, room.jid):up();
71 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); 70 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
72 return true; 71 return true;
73 end 72 end
74 73
75 -- Check if the occupant has a reservation they must use 74 -- Check if the occupant has a reservation they must use
78 if nick then 77 if nick then
79 if event.occupant then 78 if event.occupant then
80 event.occupant.nick = jid_bare(event.occupant.nick) .. "/" .. nick; 79 event.occupant.nick = jid_bare(event.occupant.nick) .. "/" .. nick;
81 elseif event.dest_occupant.nick ~= jid_bare(event.dest_occupant.nick) .. "/" .. nick then 80 elseif event.dest_occupant.nick ~= jid_bare(event.dest_occupant.nick) .. "/" .. nick then
82 module:log("debug", "Attempt by %s to join as %s, but their reserved nick is %s", stanza.attr.from, requested_nick, nick); 81 module:log("debug", "Attempt by %s to join as %s, but their reserved nick is %s", stanza.attr.from, requested_nick, nick);
83 local reply = st.error_reply(stanza, "cancel", "not-acceptable"):up(); 82 local reply = st.error_reply(stanza, "cancel", "not-acceptable", nil, room.jid):up();
84 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); 83 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
85 return true; 84 return true;
86 end 85 end
87 end 86 end
88 end 87 end
102 101
103 local function handle_register_iq(room, origin, stanza) 102 local function handle_register_iq(room, origin, stanza)
104 local user_jid = jid_bare(stanza.attr.from) 103 local user_jid = jid_bare(stanza.attr.from)
105 local affiliation = room:get_affiliation(user_jid); 104 local affiliation = room:get_affiliation(user_jid);
106 if affiliation == "outcast" then 105 if affiliation == "outcast" then
107 origin.send(st.error_reply(stanza, "auth", "forbidden")); 106 origin.send(st.error_reply(stanza, "auth", "forbidden", room.jid));
108 return true; 107 return true;
109 elseif not (affiliation or allow_unaffiliated) then 108 elseif not (affiliation or allow_unaffiliated) then
110 origin.send(st.error_reply(stanza, "auth", "registration-required")); 109 origin.send(st.error_reply(stanza, "auth", "registration-required"));
111 return true; 110 return true;
112 end 111 end
133 end 132 end
134 origin.send(reply); 133 origin.send(reply);
135 return true; 134 return true;
136 end 135 end
137 local form_tag = query:get_child("x", "jabber:x:data"); 136 local form_tag = query:get_child("x", "jabber:x:data");
138 local reg_data = form_tag and registration_form:data(form_tag); 137 if not form_tag then
138 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing dataform"));
139 return true;
140 end
141 local form_type, err = dataforms.get_type(form_tag);
142 if not form_type then
143 origin.send(st.error_reply(stanza, "modify", "bad-request", "Error with form: "..err));
144 return true;
145 elseif form_type ~= "http://jabber.org/protocol/muc#register" then
146 origin.send(st.error_reply(stanza, "modify", "bad-request", "Error in form"));
147 return true;
148 end
149 local reg_data = registration_form:data(form_tag);
139 if not reg_data then 150 if not reg_data then
140 origin.send(st.error_reply(stanza, "modify", "bad-request", "Error in form")); 151 origin.send(st.error_reply(stanza, "modify", "bad-request", "Error in form"));
141 return true; 152 return true;
142 end 153 end
143 -- Is the nickname valid? 154 -- Is the nickname valid?
144 local desired_nick = resourceprep(reg_data["muc#register_roomnick"]); 155 local desired_nick = resourceprep(reg_data["muc#register_roomnick"], true);
145 if not desired_nick then 156 if not desired_nick then
146 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid Nickname")); 157 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid Nickname"));
147 return true; 158 return true;
148 end 159 end
149 -- Is the nickname currently in use by another user? 160 -- Is the nickname currently in use by another user?