Software /
code /
prosody
Comparison
plugins/muc/muc.lib.lua @ 6233:f400a4cdf352
Merge with daurnimator
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 17 May 2014 18:17:34 +0100 |
parent | 6178:e12b13a46878 |
parent | 6232:d7dc71d9171d |
child | 6254:da4c04df90e3 |
comparison
equal
deleted
inserted
replaced
6178:e12b13a46878 | 6233:f400a4cdf352 |
---|---|
7 -- COPYING file in the source package for more information. | 7 -- COPYING file in the source package for more information. |
8 -- | 8 -- |
9 | 9 |
10 local select = select; | 10 local select = select; |
11 local pairs, ipairs = pairs, ipairs; | 11 local pairs, ipairs = pairs, ipairs; |
12 | 12 local next = next; |
13 local gettime = os.time; | 13 local setmetatable = setmetatable; |
14 local datetime = require "util.datetime"; | |
15 | 14 |
16 local dataform = require "util.dataforms"; | 15 local dataform = require "util.dataforms"; |
17 | 16 local iterators = require "util.iterators"; |
18 local jid_split = require "util.jid".split; | 17 local jid_split = require "util.jid".split; |
19 local jid_bare = require "util.jid".bare; | 18 local jid_bare = require "util.jid".bare; |
20 local jid_prep = require "util.jid".prep; | 19 local jid_prep = require "util.jid".prep; |
21 local st = require "util.stanza"; | 20 local st = require "util.stanza"; |
22 local log = require "util.logger".init("mod_muc"); | 21 local log = require "util.logger".init("mod_muc"); |
23 local t_insert, t_remove = table.insert, table.remove; | |
24 local setmetatable = setmetatable; | |
25 local base64 = require "util.encodings".base64; | 22 local base64 = require "util.encodings".base64; |
26 local md5 = require "util.hashes".md5; | 23 local md5 = require "util.hashes".md5; |
27 | 24 |
28 local default_history_length, max_history_length = 20, math.huge; | 25 local occupant_lib = module:require "muc/occupant" |
29 | 26 local muc_util = module:require "muc/util"; |
30 local get_filtered_presence do | 27 local is_kickable_error = muc_util.is_kickable_error; |
31 local presence_filters = { | 28 local valid_roles, valid_affiliations = muc_util.valid_roles, muc_util.valid_affiliations; |
32 ["http://jabber.org/protocol/muc"] = true; | |
33 ["http://jabber.org/protocol/muc#user"] = true; | |
34 } | |
35 local function presence_filter(tag) | |
36 if presence_filters[tag.attr.xmlns] then | |
37 return nil; | |
38 end | |
39 return tag; | |
40 end | |
41 function get_filtered_presence(stanza) | |
42 return st.clone(stanza):maptags(presence_filter); | |
43 end | |
44 end | |
45 | |
46 local is_kickable_error do | |
47 local kickable_error_conditions = { | |
48 ["gone"] = true; | |
49 ["internal-server-error"] = true; | |
50 ["item-not-found"] = true; | |
51 ["jid-malformed"] = true; | |
52 ["recipient-unavailable"] = true; | |
53 ["redirect"] = true; | |
54 ["remote-server-not-found"] = true; | |
55 ["remote-server-timeout"] = true; | |
56 ["service-unavailable"] = true; | |
57 ["malformed error"] = true; | |
58 }; | |
59 function is_kickable_error(stanza) | |
60 local cond = select(2, stanza:get_error()) or "malformed error"; | |
61 return kickable_error_conditions[cond]; | |
62 end | |
63 end | |
64 | 29 |
65 local room_mt = {}; | 30 local room_mt = {}; |
66 room_mt.__index = room_mt; | 31 room_mt.__index = room_mt; |
67 | 32 |
68 function room_mt:__tostring() | 33 function room_mt:__tostring() |
72 function room_mt:get_occupant_jid(real_jid) | 37 function room_mt:get_occupant_jid(real_jid) |
73 return self._jid_nick[real_jid] | 38 return self._jid_nick[real_jid] |
74 end | 39 end |
75 | 40 |
76 function room_mt:get_default_role(affiliation) | 41 function room_mt:get_default_role(affiliation) |
77 if affiliation == "owner" or affiliation == "admin" then | 42 local role = module:fire_event("muc-get-default-role", { |
43 room = self; | |
44 affiliation = affiliation; | |
45 affiliation_rank = valid_affiliations[affiliation or "none"]; | |
46 }); | |
47 return role, valid_roles[role or "none"]; | |
48 end | |
49 module:hook("muc-get-default-role", function(event) | |
50 if event.affiliation_rank >= valid_affiliations.admin then | |
78 return "moderator"; | 51 return "moderator"; |
79 elseif affiliation == "member" then | 52 elseif event.affiliation_rank >= valid_affiliations.none then |
80 return "participant"; | 53 return "participant"; |
81 elseif not affiliation then | 54 end |
82 if not self:get_members_only() then | 55 end); |
83 return self:get_moderated() and "visitor" or "participant"; | 56 |
84 end | 57 --- Occupant functions |
85 end | 58 function room_mt:new_occupant(bare_real_jid, nick) |
86 end | 59 local occupant = occupant_lib.new(bare_real_jid, nick); |
87 | 60 local affiliation = self:get_affiliation(bare_real_jid); |
88 function room_mt:lock() | 61 occupant.role = self:get_default_role(affiliation); |
89 self.locked = true | 62 return occupant; |
90 end | 63 end |
91 function room_mt:unlock() | 64 |
92 module:fire_event("muc-room-unlocked", { room = self }); | 65 function room_mt:get_occupant_by_nick(nick) |
93 self.locked = nil | 66 local occupant = self._occupants[nick]; |
94 end | 67 if occupant == nil then return nil end |
95 function room_mt:is_locked() | 68 return occupant_lib.copy(occupant); |
96 return not not self.locked | 69 end |
97 end | 70 |
98 | 71 do |
99 function room_mt:route_to_occupant(o_data, stanza) | 72 local function next_copied_occupant(occupants, occupant_jid) |
73 local next_occupant_jid, raw_occupant = next(occupants, occupant_jid); | |
74 if next_occupant_jid == nil then return nil end | |
75 return next_occupant_jid, occupant_lib.copy(raw_occupant); | |
76 end | |
77 function room_mt:each_occupant(read_only) | |
78 return next_copied_occupant, self._occupants, nil; | |
79 end | |
80 end | |
81 | |
82 function room_mt:get_occupant_by_real_jid(real_jid) | |
83 local occupant_jid = self:get_occupant_jid(real_jid); | |
84 if occupant_jid == nil then return nil end | |
85 return self:get_occupant_by_nick(occupant_jid); | |
86 end | |
87 | |
88 function room_mt:save_occupant(occupant) | |
89 occupant = occupant_lib.copy(occupant); -- So that occupant can be modified more | |
90 local id = occupant.nick | |
91 | |
92 -- Need to maintain _jid_nick secondary index | |
93 local old_occupant = self._occupants[id]; | |
94 if old_occupant then | |
95 for real_jid in pairs(old_occupant.sessions) do | |
96 self._jid_nick[real_jid] = nil; | |
97 end | |
98 end | |
99 if occupant.role ~= nil and next(occupant.sessions) then | |
100 for real_jid, presence in occupant:each_session() do | |
101 self._jid_nick[real_jid] = occupant.nick; | |
102 end | |
103 else | |
104 occupant = nil | |
105 end | |
106 self._occupants[id] = occupant | |
107 end | |
108 | |
109 function room_mt:route_to_occupant(occupant, stanza) | |
100 local to = stanza.attr.to; | 110 local to = stanza.attr.to; |
101 for jid in pairs(o_data.sessions) do | 111 for jid, pr in occupant:each_session() do |
102 stanza.attr.to = jid; | 112 if pr.attr.type ~= "unavailable" then |
103 self:_route_stanza(stanza); | 113 stanza.attr.to = jid; |
114 self:route_stanza(stanza); | |
115 end | |
104 end | 116 end |
105 stanza.attr.to = to; | 117 stanza.attr.to = to; |
106 end | 118 end |
107 | 119 |
108 function room_mt:broadcast_presence(stanza, sid, code, nick) | 120 -- actor is the attribute table |
109 stanza = get_filtered_presence(stanza); | 121 local function add_item(x, affiliation, role, jid, nick, actor, reason) |
110 local occupant = self._occupants[stanza.attr.from]; | 122 x:tag("item", {affiliation = affiliation; role = role; jid = jid; nick = nick;}) |
111 stanza:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) | 123 if actor then |
112 :tag("item", {affiliation=occupant.affiliation or "none", role=occupant.role or "none", nick=nick}):up(); | 124 x:tag("actor", actor):up() |
113 if code then | 125 end |
114 stanza:tag("status", {code=code}):up(); | 126 if reason then |
115 end | 127 x:tag("reason"):text(reason):up() |
116 self:broadcast_except_nick(stanza, stanza.attr.from); | 128 end |
117 stanza:tag("status", {code='110'}):up(); | 129 x:up(); |
118 stanza.attr.to = sid; | 130 return x |
119 self:_route_stanza(stanza); | 131 end |
120 end | 132 |
121 function room_mt:broadcast_message(stanza, historic) | 133 -- actor is (real) jid |
122 module:fire_event("muc-broadcast-message", {room = self, stanza = stanza, historic = historic}); | 134 function room_mt:build_item_list(occupant, x, is_anonymous, nick, actor, reason) |
135 local affiliation = self:get_affiliation(occupant.bare_jid) or "none"; | |
136 local role = occupant.role or "none"; | |
137 local actor_attr; | |
138 if actor then | |
139 actor_attr = {nick = select(3,jid_split(self:get_occupant_jid(actor)))}; | |
140 end | |
141 if is_anonymous then | |
142 add_item(x, affiliation, role, nil, nick, actor_attr, reason); | |
143 else | |
144 if actor_attr then | |
145 actor_attr.jid = actor; | |
146 end | |
147 for real_jid, session in occupant:each_session() do | |
148 add_item(x, affiliation, role, real_jid, nick, actor_attr, reason); | |
149 end | |
150 end | |
151 return x | |
152 end | |
153 | |
154 function room_mt:broadcast_message(stanza) | |
155 module:fire_event("muc-broadcast-message", {room = self, stanza = stanza}); | |
123 self:broadcast(stanza); | 156 self:broadcast(stanza); |
124 end | 157 end |
125 | 158 |
126 -- add to history | |
127 module:hook("muc-broadcast-message", function(event) | |
128 if event.historic then | |
129 local room = event.room | |
130 local history = room._data['history']; | |
131 if not history then history = {}; room._data['history'] = history; end | |
132 local stanza = st.clone(event.stanza); | |
133 stanza.attr.to = ""; | |
134 local ts = gettime(); | |
135 local stamp = datetime.datetime(ts); | |
136 stanza:tag("delay", {xmlns = "urn:xmpp:delay", from = module.host, stamp = stamp}):up(); -- XEP-0203 | |
137 stanza:tag("x", {xmlns = "jabber:x:delay", from = module.host, stamp = datetime.legacy()}):up(); -- XEP-0091 (deprecated) | |
138 local entry = { stanza = stanza, timestamp = ts }; | |
139 t_insert(history, entry); | |
140 while #history > room:get_historylength() do t_remove(history, 1) end | |
141 end | |
142 end) | |
143 | |
144 function room_mt:broadcast_except_nick(stanza, nick) | |
145 return self:broadcast(stanza, function(rnick, occupant) return rnick ~= nick end) | |
146 end | |
147 | |
148 -- Broadcast a stanza to all occupants in the room. | 159 -- Broadcast a stanza to all occupants in the room. |
149 -- optionally checks conditional called with nicl | 160 -- optionally checks conditional called with (nick, occupant) |
150 function room_mt:broadcast(stanza, cond_func) | 161 function room_mt:broadcast(stanza, cond_func) |
151 for nick, occupant in pairs(self._occupants) do | 162 for nick, occupant in self:each_occupant() do |
152 if cond_func == nil or cond_func(nick, occupant) then | 163 if cond_func == nil or cond_func(nick, occupant) then |
153 self:route_to_occupant(occupant, stanza) | 164 self:route_to_occupant(occupant, stanza) |
154 end | 165 end |
155 end | 166 end |
156 end | 167 end |
157 | 168 |
158 function room_mt:send_occupant_list(to) | 169 local function can_see_real_jids(whois, occupant) |
159 local current_nick = self:get_occupant_jid(to); | 170 if whois == "anyone" then |
160 for occupant, o_data in pairs(self._occupants) do | 171 return true; |
161 if occupant ~= current_nick then | 172 elseif whois == "moderators" then |
162 local pres = get_filtered_presence(o_data.sessions[o_data.jid]); | 173 return valid_roles[occupant.role or "none"] >= valid_roles.moderator; |
163 pres.attr.to, pres.attr.from = to, occupant; | 174 end |
164 pres:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) | 175 end |
165 :tag("item", {affiliation=o_data.affiliation or "none", role=o_data.role or "none"}):up(); | 176 |
166 self:_route_stanza(pres); | 177 local function get_base_presence(occupant) |
167 end | 178 if occupant.role ~= nil then |
168 end | 179 -- Try to use main jid's presence |
169 end | 180 local pr = occupant:get_presence(); |
170 | 181 if pr ~= nil then |
171 local function parse_history(stanza) | 182 return st.clone(pr); |
172 local x_tag = stanza:get_child("x", "http://jabber.org/protocol/muc"); | 183 end |
173 local history_tag = x_tag and x_tag:get_child("history", "http://jabber.org/protocol/muc"); | 184 end |
174 if not history_tag then | 185 return st.presence {from = occupant.nick; type = "unavailable";}; |
175 return nil, 20, nil | 186 end |
176 end | 187 |
177 | 188 -- Broadcasts an occupant's presence to the whole room |
178 local maxchars = tonumber(history_tag.attr.maxchars); | 189 -- Takes the x element that goes into the stanzas |
179 | 190 function room_mt:publicise_occupant_status(occupant, base_x, nick, actor, reason) |
180 local maxstanzas = tonumber(history_tag.attr.maxstanzas); | 191 -- Build real jid and (optionally) occupant jid template presences |
181 | 192 local function get_presence(is_anonymous) |
182 -- messages received since the UTC datetime specified | 193 local x = st.clone(base_x); |
183 local since = history_tag.attr.since; | 194 self:build_item_list(occupant, x, is_anonymous, nick, actor, reason); |
184 if since then | 195 return get_base_presence(occupant):add_child(x), x; |
185 since = datetime.parse(since); | 196 end |
186 end | 197 local full_p, full_x = get_presence(false); |
187 | 198 local anon_p, anon_x; |
188 -- messages received in the last "X" seconds. | 199 local function get_anon_p() |
189 local seconds = tonumber(history_tag.attr.seconds); | 200 if anon_p == nil then |
190 if seconds then | 201 anon_p, anon_x = get_presence(true); |
191 seconds = gettime() - seconds | 202 end |
192 if since then | 203 return anon_p, anon_x; |
193 since = math.max(since, seconds); | 204 end |
194 else | 205 |
195 since = seconds; | 206 local whois = self:get_whois(); |
196 end | 207 |
197 end | 208 -- General populance |
198 | 209 for nick, n_occupant in self:each_occupant() do |
199 return maxchars, maxstanzas, since | 210 if nick ~= occupant.nick then |
200 end | 211 local pr; |
201 | 212 if can_see_real_jids(whois, occupant) or occupant.bare_jid == n_occupant.bare_jid then |
202 module:hook("muc-get-history", function(event) | 213 pr = full_p; |
203 local room = event.room | 214 else |
204 local history = room._data['history']; -- send discussion history | 215 pr = get_anon_p(); |
205 if not history then return nil end | 216 end |
206 local history_len = #history | 217 self:route_to_occupant(n_occupant, pr); |
207 | 218 end |
208 local to = event.to | 219 end |
209 local maxchars = event.maxchars | 220 |
210 local maxstanzas = event.maxstanzas or history_len | 221 -- Presences for occupant itself |
211 local since = event.since | 222 full_x:tag("status", {code = "110";}):up(); |
212 local n = 0; | 223 if occupant.role == nil then |
213 local charcount = 0; | 224 -- They get an unavailable |
214 for i=history_len,1,-1 do | 225 self:route_to_occupant(occupant, full_p); |
215 local entry = history[i]; | 226 else |
216 if maxchars then | 227 -- use their own presences as templates |
217 if not entry.chars then | 228 for full_jid, pr in occupant:each_session() do |
218 entry.stanza.attr.to = ""; | 229 pr = st.clone(pr); |
219 entry.chars = #tostring(entry.stanza); | 230 pr.attr.to = full_jid; |
220 end | 231 -- You can always see your own full jids |
221 charcount = charcount + entry.chars + #to; | 232 pr:add_child(full_x); |
222 if charcount > maxchars then break; end | 233 self:route_stanza(pr); |
223 end | 234 end |
224 if since and since > entry.timestamp then break; end | 235 end |
225 if n + 1 > maxstanzas then break; end | 236 end |
226 n = n + 1; | 237 |
227 end | 238 function room_mt:send_occupant_list(to, filter) |
228 | 239 local to_bare = jid_bare(to); |
229 local i = history_len-n+1 | 240 local is_anonymous = true; |
230 function event:next_stanza() | 241 local whois = self:get_whois(); |
231 if i > history_len then return nil end | 242 if whois ~= "anyone" then |
232 local entry = history[i] | 243 local affiliation = self:get_affiliation(to); |
233 local msg = entry.stanza | 244 if affiliation ~= "admin" and affiliation ~= "owner" then |
234 msg.attr.to = to; | 245 local occupant = self:get_occupant_by_real_jid(to); |
235 i = i + 1 | 246 if not occupant or can_see_real_jids(whois, occupant) then |
236 return msg | 247 is_anonymous = false; |
237 end | 248 end |
238 return true; | 249 end |
239 end) | 250 end |
240 | 251 for occupant_jid, occupant in self:each_occupant() do |
241 function room_mt:send_history(to, stanza) | 252 if filter == nil or filter(occupant_jid, occupant) then |
242 local maxchars, maxstanzas, since = parse_history(stanza) | 253 local x = st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'}); |
243 local event = { | 254 self:build_item_list(occupant, x, is_anonymous and to_bare ~= occupant.bare_jid); -- can always see your own jids |
244 room = self; | 255 local pres = st.clone(occupant:get_presence()); |
245 to = to; -- `to` is required to calculate the character count for `maxchars` | 256 pres.attr.to = to; |
246 maxchars = maxchars, maxstanzas = maxstanzas, since = since; | 257 pres:add_child(x); |
247 next_stanza = function() end; -- events should define this iterator | 258 self:route_stanza(pres); |
248 } | 259 end |
249 module:fire_event("muc-get-history", event) | |
250 for msg in event.next_stanza , event do | |
251 self:_route_stanza(msg); | |
252 end | 260 end |
253 end | 261 end |
254 | 262 |
255 function room_mt:get_disco_info(stanza) | 263 function room_mt:get_disco_info(stanza) |
256 local count = 0; for _ in pairs(self._occupants) do count = count + 1; end | 264 local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#info"); |
257 return st.reply(stanza):query("http://jabber.org/protocol/disco#info") | 265 local form = dataform.new { |
258 :tag("identity", {category="conference", type="text", name=self:get_name()}):up() | 266 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#roominfo" }; |
259 :tag("feature", {var="http://jabber.org/protocol/muc"}):up() | 267 }; |
260 :tag("feature", {var=self:get_password() and "muc_passwordprotected" or "muc_unsecured"}):up() | 268 module:fire_event("muc-disco#info", {room = self; reply = reply; form = form;}); |
261 :tag("feature", {var=self:get_moderated() and "muc_moderated" or "muc_unmoderated"}):up() | 269 reply:add_child(form:form(nil, "result")); |
262 :tag("feature", {var=self:get_members_only() and "muc_membersonly" or "muc_open"}):up() | 270 return reply; |
263 :tag("feature", {var=self:get_persistent() and "muc_persistent" or "muc_temporary"}):up() | 271 end |
264 :tag("feature", {var=self:get_hidden() and "muc_hidden" or "muc_public"}):up() | 272 module:hook("muc-disco#info", function(event) |
265 :tag("feature", {var=self:get_whois() ~= "anyone" and "muc_semianonymous" or "muc_nonanonymous"}):up() | 273 event.reply:tag("feature", {var = "http://jabber.org/protocol/muc"}):up(); |
266 :add_child(dataform.new({ | 274 end); |
267 { name = "FORM_TYPE", type = "hidden", value = "http://jabber.org/protocol/muc#roominfo" }, | 275 module:hook("muc-disco#info", function(event) |
268 { name = "muc#roominfo_description", label = "Description", value = "" }, | 276 local count = iterators.count(event.room:each_occupant()); |
269 { name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) } | 277 table.insert(event.form, { name = "muc#roominfo_occupants", label = "Number of occupants", value = tostring(count) }); |
270 }):form({["muc#roominfo_description"] = self:get_description()}, 'result')) | 278 end); |
271 ; | 279 |
272 end | |
273 function room_mt:get_disco_items(stanza) | 280 function room_mt:get_disco_items(stanza) |
274 local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items"); | 281 local reply = st.reply(stanza):query("http://jabber.org/protocol/disco#items"); |
275 for room_jid in pairs(self._occupants) do | 282 for room_jid in self:each_occupant() do |
276 reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up(); | 283 reply:tag("item", {jid = room_jid, name = room_jid:match("/(.*)")}):up(); |
277 end | 284 end |
278 return reply; | 285 return reply; |
279 end | 286 end |
280 | 287 |
281 function room_mt:get_subject() | |
282 return self._data['subject'], self._data['subject_from'] | |
283 end | |
284 local function create_subject_message(subject) | |
285 return st.message({type='groupchat'}) | |
286 :tag('subject'):text(subject):up(); | |
287 end | |
288 function room_mt:send_subject(to) | |
289 local from, subject = self:get_subject() | |
290 if subject then | |
291 local msg = create_subject_message(subject) | |
292 msg.attr.from = from | |
293 msg.attr.to = to | |
294 self:_route_stanza(msg); | |
295 end | |
296 end | |
297 function room_mt:set_subject(current_nick, subject) | |
298 if subject == "" then subject = nil; end | |
299 self._data['subject'] = subject; | |
300 self._data['subject_from'] = current_nick; | |
301 if self.save then self:save(); end | |
302 local msg = create_subject_message(subject) | |
303 msg.attr.from = current_nick | |
304 self:broadcast_message(msg, false); | |
305 return true; | |
306 end | |
307 | |
308 function room_mt:handle_kickable(origin, stanza) | 288 function room_mt:handle_kickable(origin, stanza) |
289 local real_jid = stanza.attr.from; | |
290 local occupant = self:get_occupant_by_real_jid(real_jid); | |
291 if occupant == nil then return nil; end | |
309 local type, condition, text = stanza:get_error(); | 292 local type, condition, text = stanza:get_error(); |
310 local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error"); | 293 local error_message = "Kicked: "..(condition and condition:gsub("%-", " ") or "presence error"); |
311 if text then | 294 if text then |
312 error_message = error_message..": "..text; | 295 error_message = error_message..": "..text; |
313 end | 296 end |
314 local kick_stanza = st.presence({type='unavailable', from=stanza.attr.from, to=stanza.attr.to}) | 297 occupant:set_session(real_jid, st.presence({type="unavailable"}) |
315 :tag('status'):text(error_message); | 298 :tag('status'):text(error_message)); |
316 self:handle_unavailable_to_occupant(origin, kick_stanza); -- send unavailable | 299 self:save_occupant(occupant); |
317 return true; | 300 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}) |
318 end | 301 :tag("status", {code = "307"}) |
319 | 302 self:publicise_occupant_status(occupant, x); |
320 function room_mt:set_name(name) | 303 return true; |
321 if name == "" or type(name) ~= "string" or name == (jid_split(self.jid)) then name = nil; end | 304 end |
322 if self._data.name ~= name then | 305 |
323 self._data.name = name; | 306 -- Give the room creator owner affiliation |
324 if self.save then self:save(true); end | 307 module:hook("muc-room-pre-create", function(event) |
325 end | 308 event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner"); |
326 end | 309 end, -1); |
327 function room_mt:get_name() | 310 |
328 return self._data.name or jid_split(self.jid); | 311 -- check if user is banned |
329 end | |
330 function room_mt:set_description(description) | |
331 if description == "" or type(description) ~= "string" then description = nil; end | |
332 if self._data.description ~= description then | |
333 self._data.description = description; | |
334 if self.save then self:save(true); end | |
335 end | |
336 end | |
337 function room_mt:get_description() | |
338 return self._data.description; | |
339 end | |
340 function room_mt:set_password(password) | |
341 if password == "" or type(password) ~= "string" then password = nil; end | |
342 if self._data.password ~= password then | |
343 self._data.password = password; | |
344 if self.save then self:save(true); end | |
345 end | |
346 end | |
347 function room_mt:get_password() | |
348 return self._data.password; | |
349 end | |
350 function room_mt:set_moderated(moderated) | |
351 moderated = moderated and true or nil; | |
352 if self._data.moderated ~= moderated then | |
353 self._data.moderated = moderated; | |
354 if self.save then self:save(true); end | |
355 end | |
356 end | |
357 function room_mt:get_moderated() | |
358 return self._data.moderated; | |
359 end | |
360 function room_mt:set_members_only(members_only) | |
361 members_only = members_only and true or nil; | |
362 if self._data.members_only ~= members_only then | |
363 self._data.members_only = members_only; | |
364 if self.save then self:save(true); end | |
365 end | |
366 end | |
367 function room_mt:get_members_only() | |
368 return self._data.members_only; | |
369 end | |
370 function room_mt:set_persistent(persistent) | |
371 persistent = persistent and true or nil; | |
372 if self._data.persistent ~= persistent then | |
373 self._data.persistent = persistent; | |
374 if self.save then self:save(true); end | |
375 end | |
376 end | |
377 function room_mt:get_persistent() | |
378 return self._data.persistent; | |
379 end | |
380 function room_mt:set_hidden(hidden) | |
381 hidden = hidden and true or nil; | |
382 if self._data.hidden ~= hidden then | |
383 self._data.hidden = hidden; | |
384 if self.save then self:save(true); end | |
385 end | |
386 end | |
387 function room_mt:get_hidden() | |
388 return self._data.hidden; | |
389 end | |
390 function room_mt:get_public() | |
391 return not self:get_hidden(); | |
392 end | |
393 function room_mt:set_public(public) | |
394 return self:set_hidden(not public); | |
395 end | |
396 function room_mt:set_changesubject(changesubject) | |
397 changesubject = changesubject and true or nil; | |
398 if self._data.changesubject ~= changesubject then | |
399 self._data.changesubject = changesubject; | |
400 if self.save then self:save(true); end | |
401 end | |
402 end | |
403 function room_mt:get_changesubject() | |
404 return self._data.changesubject; | |
405 end | |
406 function room_mt:get_historylength() | |
407 return self._data.history_length or default_history_length; | |
408 end | |
409 function room_mt:set_historylength(length) | |
410 length = math.min(tonumber(length) or default_history_length, max_history_length or math.huge); | |
411 if length == default_history_length then | |
412 length = nil; | |
413 end | |
414 self._data.history_length = length; | |
415 end | |
416 | |
417 | |
418 local valid_whois = { moderators = true, anyone = true }; | |
419 | |
420 function room_mt:set_whois(whois) | |
421 if valid_whois[whois] and self._data.whois ~= whois then | |
422 self._data.whois = whois; | |
423 if self.save then self:save(true); end | |
424 end | |
425 end | |
426 | |
427 function room_mt:get_whois() | |
428 return self._data.whois; | |
429 end | |
430 | |
431 function room_mt:handle_unavailable_to_occupant(origin, stanza) | |
432 local from = stanza.attr.from; | |
433 local current_nick = self:get_occupant_jid(from); | |
434 if not current_nick then | |
435 return true; -- discard | |
436 end | |
437 local pr = get_filtered_presence(stanza); | |
438 pr.attr.from = current_nick; | |
439 log("debug", "%s leaving %s", current_nick, self.jid); | |
440 self._jid_nick[from] = nil; | |
441 local occupant = self._occupants[current_nick]; | |
442 local new_jid = next(occupant.sessions); | |
443 if new_jid == from then new_jid = next(occupant.sessions, new_jid); end | |
444 if new_jid then | |
445 local jid = occupant.jid; | |
446 occupant.jid = new_jid; | |
447 occupant.sessions[from] = nil; | |
448 pr.attr.to = from; | |
449 pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) | |
450 :tag("item", {affiliation=occupant.affiliation or "none", role='none'}):up() | |
451 :tag("status", {code='110'}):up(); | |
452 self:_route_stanza(pr); | |
453 if jid ~= new_jid then | |
454 pr = st.clone(occupant.sessions[new_jid]) | |
455 :tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) | |
456 :tag("item", {affiliation=occupant.affiliation or "none", role=occupant.role or "none"}); | |
457 pr.attr.from = current_nick; | |
458 self:broadcast_except_nick(pr, current_nick); | |
459 end | |
460 else | |
461 occupant.role = 'none'; | |
462 self:broadcast_presence(pr, from); | |
463 self._occupants[current_nick] = nil; | |
464 module:fire_event("muc-occupant-left", { room = self; nick = current_nick; }); | |
465 end | |
466 return true; | |
467 end | |
468 | |
469 function room_mt:handle_occupant_presence(origin, stanza) | |
470 local from = stanza.attr.from; | |
471 local pr = get_filtered_presence(stanza); | |
472 local current_nick = stanza.attr.to | |
473 pr.attr.from = current_nick; | |
474 log("debug", "%s broadcasted presence", current_nick); | |
475 self._occupants[current_nick].sessions[from] = pr; | |
476 self:broadcast_presence(pr, from); | |
477 return true; | |
478 end | |
479 | |
480 function room_mt:handle_change_nick(origin, stanza, current_nick, to) | |
481 local from = stanza.attr.from; | |
482 local occupant = self._occupants[current_nick]; | |
483 local is_multisession = next(occupant.sessions, next(occupant.sessions)); | |
484 if self._occupants[to] or is_multisession then | |
485 log("debug", "%s couldn't change nick", current_nick); | |
486 local reply = st.error_reply(stanza, "cancel", "conflict"):up(); | |
487 reply.tags[1].attr.code = "409"; | |
488 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
489 return true; | |
490 else | |
491 local to_nick = select(3, jid_split(to)); | |
492 log("debug", "%s (%s) changing nick to %s", current_nick, occupant.jid, to); | |
493 local p = st.presence({type='unavailable', from=current_nick}); | |
494 self:broadcast_presence(p, from, '303', to_nick); | |
495 self._occupants[current_nick] = nil; | |
496 self._occupants[to] = occupant; | |
497 self._jid_nick[from] = to; | |
498 local pr = get_filtered_presence(stanza); | |
499 pr.attr.from = to; | |
500 self._occupants[to].sessions[from] = pr; | |
501 self:broadcast_presence(pr, from); | |
502 return true; | |
503 end | |
504 end | |
505 | |
506 module:hook("muc-occupant-pre-join", function(event) | 312 module:hook("muc-occupant-pre-join", function(event) |
507 return module:fire_event("muc-occupant-pre-join/affiliation", event) | |
508 or module:fire_event("muc-occupant-pre-join/password", event) | |
509 or module:fire_event("muc-occupant-pre-join/locked", event) | |
510 or module:fire_event("muc-occupant-pre-join/nick-conflict", event) | |
511 end, -1) | |
512 | |
513 module:hook("muc-occupant-pre-join/password", function(event) | |
514 local room, stanza = event.room, event.stanza; | 313 local room, stanza = event.room, event.stanza; |
515 local from, to = stanza.attr.from, stanza.attr.to; | 314 local affiliation = room:get_affiliation(stanza.attr.from); |
516 local password = stanza:get_child("x", "http://jabber.org/protocol/muc"); | 315 if affiliation == "outcast" then |
517 password = password and password:get_child_text("password", "http://jabber.org/protocol/muc"); | 316 local reply = st.error_reply(stanza, "auth", "forbidden"):up(); |
518 if not password or password == "" then password = nil; end | |
519 if room:get_password() ~= password then | |
520 local from, to = stanza.attr.from, stanza.attr.to; | |
521 log("debug", "%s couldn't join due to invalid password: %s", from, to); | |
522 local reply = st.error_reply(stanza, "auth", "not-authorized"):up(); | |
523 reply.tags[1].attr.code = "401"; | |
524 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
525 return true; | |
526 end | |
527 end, -1) | |
528 | |
529 module:hook("muc-occupant-pre-join/nick-conflict", function(event) | |
530 local room, stanza = event.room, event.stanza; | |
531 local from, to = stanza.attr.from, stanza.attr.to; | |
532 local occupant = room._occupants[to] | |
533 if occupant -- occupant already exists | |
534 and jid_bare(from) ~= jid_bare(occupant.jid) then -- and has different bare real jid | |
535 log("debug", "%s couldn't join due to nick conflict: %s", from, to); | |
536 local reply = st.error_reply(stanza, "cancel", "conflict"):up(); | |
537 reply.tags[1].attr.code = "409"; | |
538 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
539 return true; | |
540 end | |
541 end, -1) | |
542 | |
543 module:hook("muc-occupant-pre-join/locked", function(event) | |
544 if event.room:is_locked() then -- Deny entry | |
545 event.origin.send(st.error_reply(event.stanza, "cancel", "item-not-found")); | |
546 return true; | |
547 end | |
548 end, -1) | |
549 | |
550 function room_mt:handle_join(origin, stanza) | |
551 local from, to = stanza.attr.from, stanza.attr.to; | |
552 local affiliation = self:get_affiliation(from); | |
553 if affiliation == nil and next(self._affiliations) == nil then -- new room, no owners | |
554 affiliation = "owner"; | |
555 self._affiliations[jid_bare(from)] = affiliation; | |
556 if self:is_locked() and not stanza:get_child("x", "http://jabber.org/protocol/muc") then | |
557 self:unlock(); -- Older groupchat protocol doesn't lock | |
558 end | |
559 end | |
560 if module:fire_event("muc-occupant-pre-join", { | |
561 room = self; | |
562 origin = origin; | |
563 stanza = stanza; | |
564 affiliation = affiliation; | |
565 }) then return true; end | |
566 log("debug", "%s joining as %s", from, to); | |
567 | |
568 local role = self:get_default_role(affiliation) | |
569 if role then -- new occupant | |
570 local is_merge = not not self._occupants[to] | |
571 if not is_merge then | |
572 self._occupants[to] = {affiliation=affiliation, role=role, jid=from, sessions={[from]=get_filtered_presence(stanza)}}; | |
573 else | |
574 self._occupants[to].sessions[from] = get_filtered_presence(stanza); | |
575 end | |
576 self._jid_nick[from] = to; | |
577 self:send_occupant_list(from); | |
578 local pr = get_filtered_presence(stanza); | |
579 pr.attr.from = to; | |
580 pr:tag("x", {xmlns='http://jabber.org/protocol/muc#user'}) | |
581 :tag("item", {affiliation=affiliation or "none", role=role or "none"}):up(); | |
582 if not is_merge then | |
583 self:broadcast_except_nick(pr, to); | |
584 end | |
585 pr:tag("status", {code='110'}):up(); | |
586 if self:get_whois() == 'anyone' then | |
587 pr:tag("status", {code='100'}):up(); | |
588 end | |
589 if self:is_locked() then | |
590 pr:tag("status", {code='201'}):up(); | |
591 end | |
592 pr.attr.to = from; | |
593 self:_route_stanza(pr); | |
594 self:send_history(from, stanza); | |
595 self:send_subject(from); | |
596 return true; | |
597 end | |
598 end | |
599 | |
600 -- registration required for entering members-only room | |
601 module:hook("muc-occupant-pre-join/affiliation", function(event) | |
602 if event.affiliation == nil and event.room:get_members_only() then | |
603 local reply = st.error_reply(event.stanza, "auth", "registration-required"):up(); | |
604 reply.tags[1].attr.code = "407"; | |
605 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
606 return true; | |
607 end | |
608 end, -1) | |
609 | |
610 -- banned | |
611 module:hook("muc-occupant-pre-join/affiliation", function(event) | |
612 if event.affiliation == "outcast" then | |
613 local reply = st.error_reply(event.stanza, "auth", "forbidden"):up(); | |
614 reply.tags[1].attr.code = "403"; | 317 reply.tags[1].attr.code = "403"; |
615 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | 318 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); |
616 return true; | 319 return true; |
617 end | 320 end |
618 end, -1) | 321 end, -10); |
619 | |
620 function room_mt:handle_available_to_occupant(origin, stanza) | |
621 local from, to = stanza.attr.from, stanza.attr.to; | |
622 local current_nick = self:get_occupant_jid(from); | |
623 if current_nick then | |
624 --if #pr == #stanza or current_nick ~= to then -- commented because google keeps resending directed presence | |
625 if current_nick == to then -- simple presence | |
626 return self:handle_occupant_presence(origin, stanza) | |
627 else -- change nick | |
628 return self:handle_change_nick(origin, stanza, current_nick, to) | |
629 end | |
630 --else -- possible rejoin | |
631 -- log("debug", "%s had connection replaced", current_nick); | |
632 -- self:handle_to_occupant(origin, st.presence({type='unavailable', from=from, to=to}) | |
633 -- :tag('status'):text('Replaced by new connection'):up()); -- send unavailable | |
634 -- self:handle_to_occupant(origin, stanza); -- resend available | |
635 --end | |
636 else -- enter room | |
637 return self:handle_join(origin, stanza) | |
638 end | |
639 end | |
640 | 322 |
641 function room_mt:handle_presence_to_occupant(origin, stanza) | 323 function room_mt:handle_presence_to_occupant(origin, stanza) |
642 local type = stanza.attr.type; | 324 local type = stanza.attr.type; |
643 if type == "error" then -- error, kick em out! | 325 if type == "error" then -- error, kick em out! |
644 return self:handle_kickable(origin, stanza) | 326 return self:handle_kickable(origin, stanza) |
645 elseif type == "unavailable" then -- unavailable | 327 elseif type == nil or type == "unavailable" then |
646 return self:handle_unavailable_to_occupant(origin, stanza) | 328 local real_jid = stanza.attr.from; |
647 elseif not type then -- available | 329 local bare_jid = jid_bare(real_jid); |
648 return self:handle_available_to_occupant(origin, stanza) | 330 local orig_occupant, dest_occupant; |
331 local is_new_room = next(self._affiliations) == nil; | |
332 if is_new_room then | |
333 if type == "unavailable" then return true; end -- Unavailable from someone not in the room | |
334 if module:fire_event("muc-room-pre-create", { | |
335 room = self; | |
336 origin = origin; | |
337 stanza = stanza; | |
338 }) then return true; end | |
339 else | |
340 orig_occupant = self:get_occupant_by_real_jid(real_jid); | |
341 if type == "unavailable" and orig_occupant == nil then return true; end -- Unavailable from someone not in the room | |
342 end | |
343 local is_first_dest_session; | |
344 if type == "unavailable" then | |
345 -- dest_occupant = nil | |
346 elseif orig_occupant and orig_occupant.nick == stanza.attr.to then -- Just a presence update | |
347 log("debug", "presence update for %s from session %s", orig_occupant.nick, real_jid); | |
348 dest_occupant = orig_occupant; | |
349 else | |
350 local dest_jid = stanza.attr.to; | |
351 dest_occupant = self:get_occupant_by_nick(dest_jid); | |
352 if dest_occupant == nil then | |
353 log("debug", "no occupant found for %s; creating new occupant object for %s", dest_jid, real_jid); | |
354 is_first_dest_session = true; | |
355 dest_occupant = self:new_occupant(bare_jid, dest_jid); | |
356 else | |
357 is_first_dest_session = false; | |
358 end | |
359 end | |
360 local is_last_orig_session; | |
361 if orig_occupant ~= nil then | |
362 -- Is there are least 2 sessions? | |
363 local iter, ob, last = orig_occupant:each_session(); | |
364 is_last_orig_session = iter(ob, iter(ob, last)) == nil; | |
365 end | |
366 | |
367 local event, event_name = { | |
368 room = self; | |
369 origin = origin; | |
370 stanza = stanza; | |
371 is_first_session = is_first_dest_session; | |
372 is_last_session = is_last_orig_session; | |
373 }; | |
374 if orig_occupant == nil then | |
375 event_name = "muc-occupant-pre-join"; | |
376 event.is_new_room = is_new_room; | |
377 elseif dest_occupant == nil then | |
378 event_name = "muc-occupant-pre-leave"; | |
379 else | |
380 event_name = "muc-occupant-pre-change"; | |
381 end | |
382 if module:fire_event(event_name, event) then return true; end | |
383 | |
384 -- Check for nick conflicts | |
385 if dest_occupant ~= nil and not is_first_dest_session and bare_jid ~= jid_bare(dest_occupant.bare_jid) then -- new nick or has different bare real jid | |
386 log("debug", "%s couldn't join due to nick conflict: %s", real_jid, dest_occupant.nick); | |
387 local reply = st.error_reply(stanza, "cancel", "conflict"):up(); | |
388 reply.tags[1].attr.code = "409"; | |
389 origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"})); | |
390 return true; | |
391 end | |
392 | |
393 -- Send presence stanza about original occupant | |
394 if orig_occupant ~= nil and orig_occupant ~= dest_occupant then | |
395 local orig_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); | |
396 local dest_nick; | |
397 if dest_occupant == nil then -- Session is leaving | |
398 log("debug", "session %s is leaving occupant %s", real_jid, orig_occupant.nick); | |
399 orig_occupant.role = nil; | |
400 orig_occupant:set_session(real_jid, stanza); | |
401 else | |
402 log("debug", "session %s is changing from occupant %s to %s", real_jid, orig_occupant.nick, dest_occupant.nick); | |
403 local generated_unavail = st.presence {from = orig_occupant.nick, to = real_jid, type = "unavailable"}; | |
404 orig_occupant:set_session(real_jid, generated_unavail); | |
405 dest_nick = select(3, jid_split(dest_occupant.nick)); | |
406 if not is_first_dest_session then -- User is swapping into another pre-existing session | |
407 log("debug", "session %s is swapping into multisession %s, showing it leave.", real_jid, dest_occupant.nick); | |
408 -- Show the other session leaving | |
409 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}) | |
410 :tag("status"):text("you are joining pre-existing session " .. dest_nick):up(); | |
411 add_item(x, self:get_affiliation(bare_jid), "none"); | |
412 local pr = st.presence{from = dest_occupant.nick, to = real_jid, type = "unavailable"} | |
413 :add_child(x); | |
414 self:route_stanza(pr); | |
415 end | |
416 if is_first_dest_session and is_last_orig_session then -- Normal nick change | |
417 log("debug", "no sessions in %s left; publically marking as nick change", orig_occupant.nick); | |
418 orig_x:tag("status", {code = "303";}):up(); | |
419 else -- The session itself always needs to see a nick change | |
420 -- don't want to get our old nick's available presence, | |
421 -- so remove our session from there, and manually generate an unavailable | |
422 orig_occupant:remove_session(real_jid); | |
423 log("debug", "generating nick change for %s", real_jid); | |
424 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); | |
425 -- self:build_item_list(orig_occupant, x, false, dest_nick); -- COMPAT: clients get confused if they see other items besides their own | |
426 add_item(x, self:get_affiliation(bare_jid), orig_occupant.role, real_jid, dest_nick); | |
427 x:tag("status", {code = "303";}):up(); | |
428 x:tag("status", {code = "110";}):up(); | |
429 self:route_stanza(generated_unavail:add_child(x)); | |
430 dest_nick = nil; -- set dest_nick to nil; so general populance doesn't see it for whole orig_occupant | |
431 end | |
432 end | |
433 self:save_occupant(orig_occupant); | |
434 self:publicise_occupant_status(orig_occupant, orig_x, dest_nick); | |
435 | |
436 if is_last_orig_session then | |
437 module:fire_event("muc-occupant-left", {room = self; nick = orig_occupant.nick;}); | |
438 end | |
439 end | |
440 | |
441 if dest_occupant ~= nil then | |
442 dest_occupant:set_session(real_jid, stanza); | |
443 local dest_x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); | |
444 if is_new_room then | |
445 dest_x:tag("status", {code = "201"}):up(); | |
446 end | |
447 if orig_occupant == nil and self:get_whois() == "anyone" then | |
448 dest_x:tag("status", {code = "100"}):up(); | |
449 end | |
450 self:save_occupant(dest_occupant); | |
451 | |
452 if orig_occupant == nil and is_first_dest_session then | |
453 -- Send occupant list to newly joined user | |
454 self:send_occupant_list(real_jid, function(nick, occupant) | |
455 -- Don't include self | |
456 return occupant:get_presence(real_jid) == nil; | |
457 end) | |
458 end | |
459 self:publicise_occupant_status(dest_occupant, dest_x); | |
460 | |
461 if orig_occupant ~= nil and orig_occupant ~= dest_occupant and not is_last_orig_session then -- If user is swapping and wasn't last original session | |
462 log("debug", "session %s split nicks; showing %s rejoining", real_jid, orig_occupant.nick); | |
463 -- Show the original nick joining again | |
464 local pr = st.clone(orig_occupant:get_presence()); | |
465 pr.attr.to = real_jid; | |
466 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user";}); | |
467 self:build_item_list(orig_occupant, x, false); | |
468 -- TODO: new status code to inform client this was the multi-session it left? | |
469 pr:add_child(x); | |
470 self:route_stanza(pr); | |
471 end | |
472 | |
473 if orig_occupant == nil and is_first_dest_session then | |
474 module:fire_event("muc-occupant-joined", {room = self; nick = dest_occupant.nick; stanza = stanza;}); | |
475 end | |
476 end | |
649 elseif type ~= 'result' then -- bad type | 477 elseif type ~= 'result' then -- bad type |
650 if type ~= 'visible' and type ~= 'invisible' then -- COMPAT ejabberd can broadcast or forward XEP-0018 presences | 478 if type ~= 'visible' and type ~= 'invisible' then -- COMPAT ejabberd can broadcast or forward XEP-0018 presences |
651 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error? | 479 origin.send(st.error_reply(stanza, "modify", "bad-request")); -- FIXME correct error? |
652 end | 480 end |
653 end | 481 end |
656 | 484 |
657 function room_mt:handle_iq_to_occupant(origin, stanza) | 485 function room_mt:handle_iq_to_occupant(origin, stanza) |
658 local from, to = stanza.attr.from, stanza.attr.to; | 486 local from, to = stanza.attr.from, stanza.attr.to; |
659 local type = stanza.attr.type; | 487 local type = stanza.attr.type; |
660 local id = stanza.attr.id; | 488 local id = stanza.attr.id; |
661 local current_nick = self:get_occupant_jid(from); | 489 local occupant = self:get_occupant_by_nick(to); |
662 local o_data = self._occupants[to]; | |
663 if (type == "error" or type == "result") then | 490 if (type == "error" or type == "result") then |
664 do -- deconstruct_stanza_id | 491 do -- deconstruct_stanza_id |
665 if not current_nick or not o_data then return nil; end | 492 if not occupant then return nil; end |
666 local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(.+)%z(.*)%z(.+)$"); | 493 local from_jid, id, to_jid_hash = (base64.decode(stanza.attr.id) or ""):match("^(.+)%z(.*)%z(.+)$"); |
667 if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end | 494 if not(from == from_jid or from == jid_bare(from_jid)) then return nil; end |
495 local from_occupant_jid = self:get_occupant_jid(from_jid); | |
496 if from_occupant_jid == nil then return nil; end | |
668 local session_jid | 497 local session_jid |
669 for to_jid in pairs(o_data.sessions) do | 498 for to_jid in occupant:each_session() do |
670 if md5(to_jid) == to_jid_hash then | 499 if md5(to_jid) == to_jid_hash then |
671 session_jid = to_jid; | 500 session_jid = to_jid; |
672 break; | 501 break; |
673 end | 502 end |
674 end | 503 end |
675 if session_jid == nil then return nil; end | 504 if session_jid == nil then return nil; end |
676 stanza.attr.from, stanza.attr.to, stanza.attr.id = current_nick, session_jid, id | 505 stanza.attr.from, stanza.attr.to, stanza.attr.id = from_jid, session_jid, id; |
677 end | 506 end |
678 log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to); | 507 log("debug", "%s sent private iq stanza to %s (%s)", from, to, stanza.attr.to); |
679 self:_route_stanza(stanza); | 508 self:route_stanza(stanza); |
680 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; | 509 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; |
681 return true; | 510 return true; |
682 else -- Type is "get" or "set" | 511 else -- Type is "get" or "set" |
512 local current_nick = self:get_occupant_jid(from); | |
683 if not current_nick then | 513 if not current_nick then |
684 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | 514 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); |
685 return true; | 515 return true; |
686 end | 516 end |
687 if not o_data then -- recipient not in room | 517 if not occupant then -- recipient not in room |
688 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); | 518 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); |
689 return true; | 519 return true; |
690 end | 520 end |
691 do -- construct_stanza_id | 521 do -- construct_stanza_id |
692 stanza.attr.id = base64.encode(o_data.jid.."\0"..stanza.attr.id.."\0"..md5(from)); | 522 stanza.attr.id = base64.encode(occupant.jid.."\0"..stanza.attr.id.."\0"..md5(from)); |
693 end | 523 end |
694 stanza.attr.from, stanza.attr.to = current_nick, o_data.jid; | 524 stanza.attr.from, stanza.attr.to = current_nick, occupant.jid; |
695 log("debug", "%s sent private iq stanza to %s (%s)", from, to, o_data.jid); | 525 log("debug", "%s sent private iq stanza to %s (%s)", from, to, occupant.jid); |
696 if stanza.tags[1].attr.xmlns == 'vcard-temp' then | 526 if stanza.tags[1].attr.xmlns == 'vcard-temp' then |
697 stanza.attr.to = jid_bare(stanza.attr.to); | 527 stanza.attr.to = jid_bare(stanza.attr.to); |
698 end | 528 end |
699 self:_route_stanza(stanza); | 529 self:route_stanza(stanza); |
700 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; | 530 stanza.attr.from, stanza.attr.to, stanza.attr.id = from, to, id; |
701 return true; | 531 return true; |
702 end | 532 end |
703 end | 533 end |
704 | 534 |
718 elseif type == "error" and is_kickable_error(stanza) then | 548 elseif type == "error" and is_kickable_error(stanza) then |
719 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); | 549 log("debug", "%s kicked from %s for sending an error message", current_nick, self.jid); |
720 return self:handle_kickable(origin, stanza); -- send unavailable | 550 return self:handle_kickable(origin, stanza); -- send unavailable |
721 end | 551 end |
722 | 552 |
723 local o_data = self._occupants[to]; | 553 local o_data = self:get_occupant_by_nick(to); |
724 if not o_data then | 554 if not o_data then |
725 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); | 555 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); |
726 return true; | 556 return true; |
727 end | 557 end |
728 log("debug", "%s sent private message stanza to %s (%s)", from, to, o_data.jid); | 558 log("debug", "%s sent private message stanza to %s (%s)", from, to, o_data.jid); |
729 stanza:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }):up(); | 559 stanza:tag("x", { xmlns = "http://jabber.org/protocol/muc#user" }):up(); |
730 stanza.attr.from = current_nick; | 560 stanza.attr.from = current_nick; |
731 self:route_to_occupant(o_data, stanza) | 561 self:route_to_occupant(o_data, stanza) |
562 -- TODO: Remove x tag? | |
732 stanza.attr.from = from; | 563 stanza.attr.from = from; |
733 return true; | 564 return true; |
734 end | 565 end |
735 | 566 |
736 function room_mt:send_form(origin, stanza) | 567 function room_mt:send_form(origin, stanza) |
738 :add_child(self:get_form_layout(stanza.attr.from):form()) | 569 :add_child(self:get_form_layout(stanza.attr.from):form()) |
739 ); | 570 ); |
740 end | 571 end |
741 | 572 |
742 function room_mt:get_form_layout(actor) | 573 function room_mt:get_form_layout(actor) |
743 local whois = self:get_whois() | |
744 local form = dataform.new({ | 574 local form = dataform.new({ |
745 title = "Configuration for "..self.jid, | 575 title = "Configuration for "..self.jid, |
746 instructions = "Complete and submit this form to configure the room.", | 576 instructions = "Complete and submit this form to configure the room.", |
747 { | 577 { |
748 name = 'FORM_TYPE', | 578 name = 'FORM_TYPE', |
749 type = 'hidden', | 579 type = 'hidden', |
750 value = 'http://jabber.org/protocol/muc#roomconfig' | 580 value = 'http://jabber.org/protocol/muc#roomconfig' |
751 }, | |
752 { | |
753 name = 'muc#roomconfig_roomname', | |
754 type = 'text-single', | |
755 label = 'Name', | |
756 value = self:get_name() or "", | |
757 }, | |
758 { | |
759 name = 'muc#roomconfig_roomdesc', | |
760 type = 'text-single', | |
761 label = 'Description', | |
762 value = self:get_description() or "", | |
763 }, | |
764 { | |
765 name = 'muc#roomconfig_persistentroom', | |
766 type = 'boolean', | |
767 label = 'Make Room Persistent?', | |
768 value = self:get_persistent() | |
769 }, | |
770 { | |
771 name = 'muc#roomconfig_publicroom', | |
772 type = 'boolean', | |
773 label = 'Make Room Publicly Searchable?', | |
774 value = not self:get_hidden() | |
775 }, | |
776 { | |
777 name = 'muc#roomconfig_changesubject', | |
778 type = 'boolean', | |
779 label = 'Allow Occupants to Change Subject?', | |
780 value = self:get_changesubject() | |
781 }, | |
782 { | |
783 name = 'muc#roomconfig_whois', | |
784 type = 'list-single', | |
785 label = 'Who May Discover Real JIDs?', | |
786 value = { | |
787 { value = 'moderators', label = 'Moderators Only', default = whois == 'moderators' }, | |
788 { value = 'anyone', label = 'Anyone', default = whois == 'anyone' } | |
789 } | |
790 }, | |
791 { | |
792 name = 'muc#roomconfig_roomsecret', | |
793 type = 'text-private', | |
794 label = 'Password', | |
795 value = self:get_password() or "", | |
796 }, | |
797 { | |
798 name = 'muc#roomconfig_moderatedroom', | |
799 type = 'boolean', | |
800 label = 'Make Room Moderated?', | |
801 value = self:get_moderated() | |
802 }, | |
803 { | |
804 name = 'muc#roomconfig_membersonly', | |
805 type = 'boolean', | |
806 label = 'Make Room Members-Only?', | |
807 value = self:get_members_only() | |
808 }, | |
809 { | |
810 name = 'muc#roomconfig_historylength', | |
811 type = 'text-single', | |
812 label = 'Maximum Number of History Messages Returned by Room', | |
813 value = tostring(self:get_historylength()) | |
814 } | 581 } |
815 }); | 582 }); |
816 return module:fire_event("muc-config-form", { room = self, actor = actor, form = form }) or form; | 583 return module:fire_event("muc-config-form", { room = self, actor = actor, form = form }) or form; |
817 end | 584 end |
818 | 585 |
819 function room_mt:process_form(origin, stanza) | 586 function room_mt:process_form(origin, stanza) |
820 local query = stanza.tags[1]; | 587 local form = stanza.tags[1]:get_child("x", "jabber:x:data"); |
821 local form = query:get_child("x", "jabber:x:data") | 588 if form.attr.type == "cancel" then |
822 if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end | 589 origin.send(st.reply(stanza)); |
823 if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end | 590 elseif form.attr.type == "submit" then |
824 if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Not a submitted form")); return; end | 591 local fields = self:get_form_layout(stanza.attr.from):data(form); |
825 | 592 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then |
826 local fields = self:get_form_layout(stanza.attr.from):data(form); | 593 origin.send(st.error_reply(stanza, "cancel", "bad-request", "Form is not of type room configuration")); |
827 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Form is not of type room configuration")); return; end | 594 return true; |
828 | 595 end |
829 | 596 |
830 local changed = {}; | 597 local event = {room = self; origin = origin; stanza = stanza; fields = fields; status_codes = {};}; |
831 | 598 function event.update_option(name, field, allowed) |
832 local function handle_option(name, field, allowed) | 599 local new = fields[field]; |
833 local new = fields[field]; | 600 if new == nil then return; end |
834 if new == nil then return; end | 601 if allowed and not allowed[new] then return; end |
835 if allowed and not allowed[new] then return; end | 602 if new == self["get_"..name](self) then return; end |
836 if new == self["get_"..name](self) then return; end | 603 event.status_codes["104"] = true; |
837 changed[name] = true; | 604 self["set_"..name](self, new); |
838 self["set_"..name](self, new); | 605 return true; |
839 end | 606 end |
840 | 607 module:fire_event("muc-config-submitted", event); |
841 local event = { room = self, fields = fields, changed = changed, stanza = stanza, origin = origin, update_option = handle_option }; | 608 |
842 module:fire_event("muc-config-submitted", event); | 609 if self.save then self:save(true); end |
843 | 610 origin.send(st.reply(stanza)); |
844 handle_option("name", "muc#roomconfig_roomname"); | 611 |
845 handle_option("description", "muc#roomconfig_roomdesc"); | 612 if next(event.status_codes) then |
846 handle_option("persistent", "muc#roomconfig_persistentroom"); | 613 local msg = st.message({type='groupchat', from=self.jid}) |
847 handle_option("moderated", "muc#roomconfig_moderatedroom"); | 614 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}) |
848 handle_option("members_only", "muc#roomconfig_membersonly"); | 615 for code in pairs(event.status_codes) do |
849 handle_option("public", "muc#roomconfig_publicroom"); | 616 msg:tag("status", {code = code;}):up(); |
850 handle_option("changesubject", "muc#roomconfig_changesubject"); | 617 end |
851 handle_option("historylength", "muc#roomconfig_historylength"); | 618 msg:up(); |
852 handle_option("whois", "muc#roomconfig_whois", valid_whois); | 619 self:broadcast_message(msg); |
853 handle_option("password", "muc#roomconfig_roomsecret"); | 620 end |
854 | 621 else |
855 if self.save then self:save(true); end | 622 origin.send(st.error_reply(stanza, "cancel", "bad-request", "Not a submitted form")); |
856 if self:is_locked() then | 623 end |
857 self:unlock(); | 624 return true; |
858 end | 625 end |
859 origin.send(st.reply(stanza)); | 626 |
860 | 627 -- Removes everyone from the room |
861 if next(changed) then | 628 function room_mt:clear(x) |
862 local msg = st.message({type='groupchat', from=self.jid}) | 629 x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'}); |
863 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}):up() | 630 local occupants_updated = {}; |
864 :tag('status', {code = '104'}):up(); | 631 for nick, occupant in self:each_occupant() do |
865 if changed.whois then | 632 occupant.role = nil; |
866 local code = (self:get_whois() == 'moderators') and "173" or "172"; | 633 self:save_occupant(occupant); |
867 msg.tags[1]:tag('status', {code = code}):up(); | 634 occupants_updated[occupant] = true; |
868 end | 635 end |
869 self:broadcast_message(msg, false) | 636 for occupant in pairs(occupants_updated) do |
637 self:publicise_occupant_status(occupant, x); | |
638 module:fire_event("muc-occupant-left", { room = self; nick = occupant.nick; }); | |
870 end | 639 end |
871 end | 640 end |
872 | 641 |
873 function room_mt:destroy(newjid, reason, password) | 642 function room_mt:destroy(newjid, reason, password) |
874 local pr = st.presence({type = "unavailable"}) | 643 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"}) |
875 :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"}) | 644 :tag("item", { affiliation='none', role='none' }):up() |
876 :tag("item", { affiliation='none', role='none' }):up() | 645 :tag("destroy", {jid=newjid}); |
877 :tag("destroy", {jid=newjid}) | 646 if reason then x:tag("reason"):text(reason):up(); end |
878 if reason then pr:tag("reason"):text(reason):up(); end | 647 if password then x:tag("password"):text(password):up(); end |
879 if password then pr:tag("password"):text(password):up(); end | 648 x:up(); |
880 for nick, occupant in pairs(self._occupants) do | 649 self:clear(x); |
881 pr.attr.from = nick; | |
882 for jid in pairs(occupant.sessions) do | |
883 pr.attr.to = jid; | |
884 self:_route_stanza(pr); | |
885 self._jid_nick[jid] = nil; | |
886 end | |
887 self._occupants[nick] = nil; | |
888 module:fire_event("muc-occupant-left", { room = self; nick = nick; }); | |
889 end | |
890 self:set_persistent(false); | |
891 module:fire_event("muc-room-destroyed", { room = self }); | 650 module:fire_event("muc-room-destroyed", { room = self }); |
892 end | 651 end |
893 | 652 |
894 function room_mt:handle_disco_info_get_query(origin, stanza) | 653 function room_mt:handle_disco_info_get_query(origin, stanza) |
895 origin.send(self:get_disco_info(stanza)); | 654 origin.send(self:get_disco_info(stanza)); |
909 origin.send(st.error_reply(stanza, "modify", "jid-malformed")); | 668 origin.send(st.error_reply(stanza, "modify", "jid-malformed")); |
910 return true; | 669 return true; |
911 end | 670 end |
912 end | 671 end |
913 if not item.attr.jid and item.attr.nick then -- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation | 672 if not item.attr.jid and item.attr.nick then -- COMPAT Workaround for Miranda sending 'nick' instead of 'jid' when changing affiliation |
914 local occupant = self._occupants[self.jid.."/"..item.attr.nick]; | 673 local occupant = self:get_occupant_by_nick(self.jid.."/"..item.attr.nick); |
915 if occupant then item.attr.jid = occupant.jid; end | 674 if occupant then item.attr.jid = occupant.jid; end |
916 elseif not item.attr.nick and item.attr.jid then | 675 elseif not item.attr.nick and item.attr.jid then |
917 local nick = self:get_occupant_jid(item.attr.jid); | 676 local nick = self:get_occupant_jid(item.attr.jid); |
918 if nick then item.attr.nick = select(3, jid_split(nick)); end | 677 if nick then item.attr.nick = select(3, jid_split(nick)); end |
919 end | 678 end |
920 local actor = stanza.attr.from; | 679 local actor = stanza.attr.from; |
921 local callback = function() origin.send(st.reply(stanza)); end | |
922 local reason = item:get_child_text("reason"); | 680 local reason = item:get_child_text("reason"); |
681 local success, errtype, err | |
923 if item.attr.affiliation and item.attr.jid and not item.attr.role then | 682 if item.attr.affiliation and item.attr.jid and not item.attr.role then |
924 local success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, callback, reason); | 683 success, errtype, err = self:set_affiliation(actor, item.attr.jid, item.attr.affiliation, reason); |
925 if not success then origin.send(st.error_reply(stanza, errtype, err)); end | |
926 return true; | |
927 elseif item.attr.role and item.attr.nick and not item.attr.affiliation then | 684 elseif item.attr.role and item.attr.nick and not item.attr.affiliation then |
928 local success, errtype, err = self:set_role(actor, self.jid.."/"..item.attr.nick, item.attr.role, callback, reason); | 685 success, errtype, err = self:set_role(actor, self.jid.."/"..item.attr.nick, item.attr.role, reason); |
929 if not success then origin.send(st.error_reply(stanza, errtype, err)); end | |
930 return true; | |
931 else | 686 else |
932 origin.send(st.error_reply(stanza, "cancel", "bad-request")); | 687 success, errtype, err = nil, "cancel", "bad-request"; |
933 return true; | 688 end |
934 end | 689 if not success then origin.send(st.error_reply(stanza, errtype, err)); end |
690 origin.send(st.reply(stanza)); | |
691 return true; | |
935 end | 692 end |
936 | 693 |
937 function room_mt:handle_admin_query_get_command(origin, stanza) | 694 function room_mt:handle_admin_query_get_command(origin, stanza) |
938 local actor = stanza.attr.from; | 695 local actor = stanza.attr.from; |
939 local affiliation = self:get_affiliation(actor); | 696 local affiliation = self:get_affiliation(actor); |
954 origin.send(st.error_reply(stanza, "auth", "forbidden")); | 711 origin.send(st.error_reply(stanza, "auth", "forbidden")); |
955 return true; | 712 return true; |
956 end | 713 end |
957 elseif _rol and not _aff then | 714 elseif _rol and not _aff then |
958 local role = self:get_role(self:get_occupant_jid(actor)) or self:get_default_role(affiliation); | 715 local role = self:get_role(self:get_occupant_jid(actor)) or self:get_default_role(affiliation); |
959 if role == "moderator" then | 716 if valid_roles[role or "none"] >= valid_roles.moderator then |
960 if _rol == "none" then _rol = nil; end | 717 if _rol == "none" then _rol = nil; end |
961 local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin"); | 718 local reply = st.reply(stanza):query("http://jabber.org/protocol/muc#admin"); |
962 for occupant_jid, occupant in pairs(self._occupants) do | 719 -- TODO: whois check here? (though fully anonymous rooms are not supported) |
720 for occupant_jid, occupant in self:each_occupant() do | |
963 if occupant.role == _rol then | 721 if occupant.role == _rol then |
964 reply:tag("item", { | 722 local nick = select(3,jid_split(occupant_jid)); |
965 nick = select(3, jid_split(occupant_jid)), | 723 self:build_item_list(occupant, reply, false, nick); |
966 role = _rol or "none", | |
967 affiliation = occupant.affiliation or "none", | |
968 jid = occupant.jid | |
969 }):up(); | |
970 end | 724 end |
971 end | 725 end |
972 origin.send(reply); | 726 origin.send(reply:up()); |
973 return true; | 727 return true; |
974 else | 728 else |
975 origin.send(st.error_reply(stanza, "auth", "forbidden")); | 729 origin.send(st.error_reply(stanza, "auth", "forbidden")); |
976 return true; | 730 return true; |
977 end | 731 end |
1005 local reason = child:get_child_text("reason"); | 759 local reason = child:get_child_text("reason"); |
1006 local password = child:get_child_text("password"); | 760 local password = child:get_child_text("password"); |
1007 self:destroy(newjid, reason, password); | 761 self:destroy(newjid, reason, password); |
1008 origin.send(st.reply(stanza)); | 762 origin.send(st.reply(stanza)); |
1009 return true; | 763 return true; |
764 elseif child.name == "x" and child.attr.xmlns == "jabber:x:data" then | |
765 return self:process_form(origin, stanza); | |
1010 else | 766 else |
1011 self:process_form(origin, stanza); | 767 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
1012 return true; | 768 return true; |
1013 end | 769 end |
1014 end | 770 end |
1015 | 771 |
1016 function room_mt:handle_groupchat_to_room(origin, stanza) | 772 function room_mt:handle_groupchat_to_room(origin, stanza) |
773 -- Prosody has made the decision that messages with <subject/> are exclusively subject changes | |
774 -- e.g. body will be ignored; even if the subject change was not allowed | |
775 if stanza:get_child("subject") then | |
776 return module:fire_event("muc-subject-change", {room = self, origin = origin, stanza = stanza}); | |
777 end | |
1017 local from = stanza.attr.from; | 778 local from = stanza.attr.from; |
1018 local current_nick = self:get_occupant_jid(from); | 779 local occupant = self:get_occupant_by_real_jid(from); |
1019 local occupant = self._occupants[current_nick]; | |
1020 if not occupant then -- not in room | 780 if not occupant then -- not in room |
1021 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); | 781 origin.send(st.error_reply(stanza, "cancel", "not-acceptable")); |
1022 return true; | 782 return true; |
1023 elseif occupant.role == "visitor" then | 783 elseif occupant.role == "visitor" then |
1024 origin.send(st.error_reply(stanza, "auth", "forbidden")); | 784 origin.send(st.error_reply(stanza, "auth", "forbidden")); |
1025 return true; | 785 return true; |
1026 else | 786 end |
1027 local from = stanza.attr.from; | 787 stanza.attr.from = occupant.nick; |
1028 stanza.attr.from = current_nick; | 788 self:broadcast_message(stanza); |
1029 local subject = stanza:get_child_text("subject"); | 789 stanza.attr.from = from; |
1030 if subject then | 790 return true; |
1031 if occupant.role == "moderator" or | |
1032 ( self:get_changesubject() and occupant.role == "participant" ) then -- and participant | |
1033 self:set_subject(current_nick, subject); | |
1034 else | |
1035 stanza.attr.from = from; | |
1036 origin.send(st.error_reply(stanza, "auth", "forbidden")); | |
1037 end | |
1038 else | |
1039 self:broadcast_message(stanza, self:get_historylength() > 0 and stanza:get_child("body")); | |
1040 end | |
1041 stanza.attr.from = from; | |
1042 return true; | |
1043 end | |
1044 end | 791 end |
1045 | 792 |
1046 -- hack - some buggy clients send presence updates to the room rather than their nick | 793 -- hack - some buggy clients send presence updates to the room rather than their nick |
1047 function room_mt:handle_presence_to_room(origin, stanza) | 794 function room_mt:handle_presence_to_room(origin, stanza) |
1048 local current_nick = self:get_occupant_jid(stanza.attr.from); | 795 local current_nick = self:get_occupant_jid(stanza.attr.from); |
1054 stanza.attr.to = to; | 801 stanza.attr.to = to; |
1055 end | 802 end |
1056 return handled; | 803 return handled; |
1057 end | 804 end |
1058 | 805 |
806 -- Need visitor role or higher to invite | |
807 module:hook("muc-pre-invite", function(event) | |
808 local room, stanza = event.room, event.stanza; | |
809 local _from, _to = stanza.attr.from, stanza.attr.to; | |
810 local inviter = room:get_occupant_by_real_jid(_from); | |
811 local role = inviter and inviter.role or room:get_default_role(room:get_affiliation(_from)); | |
812 if valid_roles[role or "none"] <= valid_roles.visitor then | |
813 event.origin.send(st.error_reply(stanza, "auth", "forbidden")); | |
814 return true; | |
815 end | |
816 end); | |
817 | |
1059 function room_mt:handle_mediated_invite(origin, stanza) | 818 function room_mt:handle_mediated_invite(origin, stanza) |
1060 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite") | 819 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite"); |
1061 local _from, _to = stanza.attr.from, stanza.attr.to; | 820 local invitee = jid_prep(payload.attr.to); |
1062 local current_nick = self:get_occupant_jid(_from) | 821 if not invitee then |
1063 -- Need visitor role or higher to invite | 822 origin.send(st.error_reply(stanza, "cancel", "jid-malformed")); |
1064 if not self:get_role(current_nick) or not self:get_default_role(self:get_affiliation(_from)) then | 823 return true; |
1065 origin.send(st.error_reply(stanza, "auth", "forbidden")); | 824 elseif module:fire_event("muc-pre-invite", {room = self, origin = origin, stanza = stanza}) then |
1066 return true; | 825 return true; |
1067 end | 826 end |
1068 local _invitee = jid_prep(payload.attr.to); | 827 local invite = st.message({from = self.jid, to = invitee, id = stanza.attr.id}) |
1069 if _invitee then | 828 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}) |
1070 if self:get_whois() == "moderators" then | 829 :tag('invite', {from = stanza.attr.from;}) |
1071 _from = current_nick; | 830 :tag('reason'):text(payload:get_child_text("reason")):up() |
1072 end | |
1073 local _reason = payload:get_child_text("reason") | |
1074 local invite = st.message({from = _to, to = _invitee, id = stanza.attr.id}) | |
1075 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}) | |
1076 :tag('invite', {from=_from}) | |
1077 :tag('reason'):text(_reason or ""):up() | |
1078 :up(); | |
1079 local password = self:get_password() | |
1080 if password then | |
1081 invite:tag("password"):text(password):up(); | |
1082 end | |
1083 invite:up() | |
1084 :tag('x', {xmlns="jabber:x:conference", jid=_to}) -- COMPAT: Some older clients expect this | |
1085 :text(_reason or "") | |
1086 :up() | 831 :up() |
1087 :tag('body') -- Add a plain message for clients which don't support invites | 832 :up(); |
1088 :text(_from..' invited you to the room '.._to..(_reason and (' ('.._reason..')') or "")) | 833 if not module:fire_event("muc-invite", {room = self, stanza = invite, origin = origin, incoming = stanza}) then |
1089 :up(); | 834 self:route_stanza(invite); |
1090 module:fire_event("muc-invite", { room = self, stanza = invite, origin = origin, incoming = stanza }); | 835 end |
1091 return true; | 836 return true; |
1092 else | 837 end |
838 | |
839 -- COMPAT: Some older clients expect this | |
840 module:hook("muc-invite", function(event) | |
841 local room, stanza = event.room, event.stanza; | |
842 local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite"); | |
843 local reason = invite:get_child_text("reason"); | |
844 stanza:tag('x', {xmlns = "jabber:x:conference"; jid = room.jid;}) | |
845 :text(reason or "") | |
846 :up(); | |
847 end); | |
848 | |
849 -- Add a plain message for clients which don't support invites | |
850 module:hook("muc-invite", function(event) | |
851 local room, stanza = event.room, event.stanza; | |
852 local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite"); | |
853 local reason = invite:get_child_text("reason") or ""; | |
854 stanza:tag("body") | |
855 :text(invite.attr.from.." invited you to the room "..room.jid..(reason == "" and (" ("..reason..")") or "")) | |
856 :up(); | |
857 end); | |
858 | |
859 function room_mt:handle_mediated_decline(origin, stanza) | |
860 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline"); | |
861 local declinee = jid_prep(payload.attr.to); | |
862 if not declinee then | |
1093 origin.send(st.error_reply(stanza, "cancel", "jid-malformed")); | 863 origin.send(st.error_reply(stanza, "cancel", "jid-malformed")); |
1094 return true; | 864 return true; |
1095 end | 865 elseif module:fire_event("muc-pre-decline", {room = self, origin = origin, stanza = stanza}) then |
1096 end | 866 return true; |
1097 | 867 end |
1098 module:hook("muc-invite", function(event) | 868 local decline = st.message({from = self.jid, to = declinee, id = stanza.attr.id}) |
1099 event.room:_route_stanza(event.stanza); | 869 :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"}) |
1100 return true; | 870 :tag("decline", {from = stanza.attr.from}) |
1101 end, -1) | 871 :tag("reason"):text(payload:get_child_text("reason")):up() |
1102 | |
1103 -- When an invite is sent; add an affiliation for the invitee | |
1104 module:hook("muc-invite", function(event) | |
1105 local room, stanza = event.room, event.stanza | |
1106 local invitee = stanza.attr.to | |
1107 if room:get_members_only() and not room:get_affiliation(invitee) then | |
1108 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite").attr.from | |
1109 local current_nick = room:get_occupant_jid(from) | |
1110 log("debug", "%s invited %s into members only room %s, granting membership", from, invitee, room.jid); | |
1111 room:set_affiliation(from, invitee, "member", nil, "Invited by " .. current_nick) | |
1112 end | |
1113 end) | |
1114 | |
1115 function room_mt:handle_mediated_decline(origin, stanza) | |
1116 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline") | |
1117 local declinee = jid_prep(payload.attr.to); | |
1118 if declinee then | |
1119 local from, to = stanza.attr.from, stanza.attr.to; | |
1120 -- TODO: Validate declinee | |
1121 local reason = payload:get_child_text("reason") | |
1122 local decline = st.message({from = to, to = declinee, id = stanza.attr.id}) | |
1123 :tag('x', {xmlns='http://jabber.org/protocol/muc#user'}) | |
1124 :tag('decline', {from=from}) | |
1125 :tag('reason'):text(reason or ""):up() | |
1126 :up() | |
1127 :up() | 872 :up() |
1128 :tag('body') -- Add a plain message for clients which don't support declines | 873 :up(); |
1129 :text(from..' declined your invite to the room '..to..(reason and (' ('..reason..')') or "")) | 874 if not module:fire_event("muc-decline", {room = self, stanza = decline, origin = origin, incoming = stanza}) then |
1130 :up(); | 875 local occupant = self:get_occupant_by_real_jid(decline.attr.to); |
1131 module:fire_event("muc-decline", { room = self, stanza = decline, origin = origin, incoming = stanza }); | 876 if occupant then |
1132 return true; | 877 self:route_to_occupant(occupant, decline); |
1133 else | 878 else |
1134 origin.send(st.error_reply(stanza, "cancel", "jid-malformed")); | 879 self:route_stanza(decline); |
1135 return true; | 880 end |
1136 end | 881 end |
1137 end | 882 return true; |
1138 | 883 end |
884 | |
885 -- Add a plain message for clients which don't support declines | |
1139 module:hook("muc-decline", function(event) | 886 module:hook("muc-decline", function(event) |
1140 local room, stanza = event.room, event.stanza | 887 local room, stanza = event.room, event.stanza; |
1141 local occupant = room:get_occupant_by_real_jid(stanza.attr.to); | 888 local decline = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline"); |
1142 if occupant then | 889 local reason = decline:get_child_text("reason") or ""; |
1143 room:route_to_occupant(occupant, stanza) | 890 stanza:tag("body") |
1144 else | 891 :text(decline.attr.from.." declined your invite to the room "..room.jid..(reason == "" and (" ("..reason..")") or "")) |
1145 room:route_stanza(stanza); | 892 :up(); |
1146 end | 893 end); |
1147 return true; | |
1148 end, -1) | |
1149 | 894 |
1150 function room_mt:handle_message_to_room(origin, stanza) | 895 function room_mt:handle_message_to_room(origin, stanza) |
1151 local type = stanza.attr.type; | 896 local type = stanza.attr.type; |
1152 if type == "groupchat" then | 897 if type == "groupchat" then |
1153 return self:handle_groupchat_to_room(origin, stanza) | 898 return self:handle_groupchat_to_room(origin, stanza) |
1165 return self:handle_mediated_decline(origin, stanza) | 910 return self:handle_mediated_decline(origin, stanza) |
1166 end | 911 end |
1167 origin.send(st.error_reply(stanza, "cancel", "bad-request")); | 912 origin.send(st.error_reply(stanza, "cancel", "bad-request")); |
1168 return true; | 913 return true; |
1169 end | 914 end |
1170 else | |
1171 return nil; | |
1172 end | 915 end |
1173 end | 916 end |
1174 | 917 |
1175 function room_mt:route_stanza(stanza) | 918 function room_mt:route_stanza(stanza) |
1176 module:send(stanza) | 919 module:send(stanza); |
1177 end | 920 end |
1178 | 921 |
1179 function room_mt:get_affiliation(jid) | 922 function room_mt:get_affiliation(jid) |
1180 local node, host, resource = jid_split(jid); | 923 local node, host, resource = jid_split(jid); |
1181 local bare = node and node.."@"..host or host; | 924 local bare = node and node.."@"..host or host; |
1182 local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID. | 925 local result = self._affiliations[bare]; -- Affiliations are granted, revoked, and maintained based on the user's bare JID. |
1183 if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned | 926 if not result and self._affiliations[host] == "outcast" then result = "outcast"; end -- host banned |
1184 return result; | 927 return result; |
1185 end | 928 end |
1186 function room_mt:set_affiliation(actor, jid, affiliation, callback, reason) | 929 |
930 function room_mt:set_affiliation(actor, jid, affiliation, reason) | |
931 if not actor then return nil, "modify", "not-acceptable"; end; | |
932 | |
1187 jid = jid_bare(jid); | 933 jid = jid_bare(jid); |
1188 if affiliation == "none" then affiliation = nil; end | 934 |
1189 if affiliation and affiliation ~= "outcast" and affiliation ~= "owner" and affiliation ~= "admin" and affiliation ~= "member" then | 935 if valid_affiliations[affiliation or "none"] == nil then |
1190 return nil, "modify", "not-acceptable"; | 936 return nil, "modify", "not-acceptable"; |
1191 end | 937 end |
938 affiliation = affiliation ~= "none" and affiliation or nil; -- coerces `affiliation == false` to `nil` | |
939 | |
940 local target_affiliation = self._affiliations[jid]; -- Raw; don't want to check against host | |
941 local is_downgrade = valid_affiliations[target_affiliation or "none"] > valid_affiliations[affiliation or "none"]; | |
942 | |
1192 if actor ~= true then | 943 if actor ~= true then |
1193 local actor_affiliation = self:get_affiliation(actor); | 944 local actor_bare = jid_bare(actor); |
1194 local target_affiliation = self:get_affiliation(jid); | 945 local actor_affiliation = self._affiliations[actor_bare]; |
1195 if target_affiliation == affiliation then -- no change, shortcut | 946 if actor_affiliation == "owner" then |
1196 if callback then callback(); end | 947 if actor_bare == jid then -- self change |
1197 return true; | 948 -- need at least one owner |
1198 end | 949 local is_last = true; |
1199 if actor_affiliation ~= "owner" then | 950 for j, aff in pairs(self._affiliations) do if j ~= jid and aff == "owner" then is_last = false; break; end end |
1200 if affiliation == "owner" or affiliation == "admin" or actor_affiliation ~= "admin" or target_affiliation == "owner" or target_affiliation == "admin" then | 951 if is_last then |
1201 return nil, "cancel", "not-allowed"; | 952 return nil, "cancel", "conflict"; |
1202 end | 953 end |
1203 elseif target_affiliation == "owner" and jid_bare(actor) == jid then -- self change | 954 end |
1204 local is_last = true; | 955 -- owners can do anything else |
1205 for j, aff in pairs(self._affiliations) do if j ~= jid and aff == "owner" then is_last = false; break; end end | 956 elseif affiliation == "owner" or affiliation == "admin" |
1206 if is_last then | 957 or actor_affiliation ~= "admin" |
1207 return nil, "cancel", "conflict"; | 958 or target_affiliation == "owner" or target_affiliation == "admin" then |
1208 end | 959 -- Can't demote owners or other admins |
1209 end | 960 return nil, "cancel", "not-allowed"; |
1210 end | 961 end |
962 end | |
963 | |
964 -- Set in 'database' | |
1211 self._affiliations[jid] = affiliation; | 965 self._affiliations[jid] = affiliation; |
966 | |
967 -- Update roles | |
1212 local role = self:get_default_role(affiliation); | 968 local role = self:get_default_role(affiliation); |
1213 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"}) | 969 local role_rank = valid_roles[role or "none"]; |
1214 :tag("item", {affiliation=affiliation or "none", role=role or "none"}) | 970 local occupants_updated = {}; -- Filled with old roles |
1215 :tag("reason"):text(reason or ""):up() | 971 for nick, occupant in self:each_occupant() do |
1216 :up(); | 972 if occupant.bare_jid == jid then |
1217 local presence_type = nil; | 973 -- need to publcize in all cases; as affiliation in <item/> has changed. |
974 occupants_updated[occupant] = occupant.role; | |
975 if occupant.role ~= role and ( | |
976 is_downgrade or | |
977 valid_roles[occupant.role or "none"] < role_rank -- upgrade | |
978 ) then | |
979 occupant.role = role; | |
980 self:save_occupant(occupant); | |
981 end | |
982 end | |
983 end | |
984 | |
985 -- Tell the room of the new occupant affiliations+roles | |
986 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"}); | |
1218 if not role then -- getting kicked | 987 if not role then -- getting kicked |
1219 presence_type = "unavailable"; | |
1220 if affiliation == "outcast" then | 988 if affiliation == "outcast" then |
1221 x:tag("status", {code="301"}):up(); -- banned | 989 x:tag("status", {code="301"}):up(); -- banned |
1222 else | 990 else |
1223 x:tag("status", {code="321"}):up(); -- affiliation change | 991 x:tag("status", {code="321"}):up(); -- affiliation change |
1224 end | 992 end |
1225 end | 993 end |
1226 local modified_nicks = {}; | 994 local is_semi_anonymous = self:get_whois() == "moderators"; |
1227 for nick, occupant in pairs(self._occupants) do | 995 for occupant, old_role in pairs(occupants_updated) do |
1228 if jid_bare(occupant.jid) == jid then | 996 self:publicise_occupant_status(occupant, x, nil, actor, reason); |
1229 if not role then -- getting kicked | 997 if is_semi_anonymous and |
1230 self._occupants[nick] = nil; | 998 (old_role == "moderator" and occupant.role ~= "moderator") or |
1231 else | 999 (old_role ~= "moderator" and occupant.role == "moderator") then -- Has gained or lost moderator status |
1232 occupant.affiliation, occupant.role = affiliation, role; | 1000 -- Send everyone else's presences (as jid visibility has changed) |
1233 end | 1001 for real_jid in occupant:each_session() do |
1234 for jid,pres in pairs(occupant.sessions) do -- remove for all sessions of the nick | 1002 self:send_occupant_list(real_jid, function(occupant_jid, occupant) |
1235 if not role then self._jid_nick[jid] = nil; end | 1003 return occupant.bare_jid ~= jid; |
1236 local p = st.clone(pres); | 1004 end); |
1237 p.attr.from = nick; | 1005 end |
1238 p.attr.type = presence_type; | 1006 end |
1239 p.attr.to = jid; | 1007 end |
1240 p:add_child(x); | 1008 |
1241 self:_route_stanza(p); | |
1242 if occupant.jid == jid then | |
1243 modified_nicks[nick] = p; | |
1244 end | |
1245 end | |
1246 end | |
1247 end | |
1248 if self.save then self:save(); end | 1009 if self.save then self:save(); end |
1249 if callback then callback(); end | |
1250 for nick,p in pairs(modified_nicks) do | |
1251 p.attr.from = nick; | |
1252 self:broadcast_except_nick(p, nick); | |
1253 end | |
1254 return true; | 1010 return true; |
1255 end | 1011 end |
1256 | 1012 |
1257 function room_mt:get_role(nick) | 1013 function room_mt:get_role(nick) |
1258 local session = self._occupants[nick]; | 1014 local occupant = self:get_occupant_by_nick(nick); |
1259 return session and session.role or nil; | 1015 return occupant and occupant.role or nil; |
1260 end | 1016 end |
1261 function room_mt:can_set_role(actor_jid, occupant_jid, role) | 1017 |
1262 local occupant = self._occupants[occupant_jid]; | 1018 function room_mt:set_role(actor, occupant_jid, role, reason) |
1263 if not occupant or not actor_jid then return nil, "modify", "not-acceptable"; end | 1019 if not actor then return nil, "modify", "not-acceptable"; end |
1264 | 1020 |
1265 if actor_jid == true then return true; end | 1021 local occupant = self:get_occupant_by_nick(occupant_jid); |
1266 | 1022 if not occupant then return nil, "modify", "not-acceptable"; end |
1267 local actor = self._occupants[self:get_occupant_jid(actor_jid)]; | 1023 |
1268 if actor and actor.role == "moderator" then | 1024 if valid_roles[role or "none"] == nil then |
1269 if occupant.affiliation ~= "owner" and occupant.affiliation ~= "admin" then | 1025 return nil, "modify", "not-acceptable"; |
1270 if actor.affiliation == "owner" or actor.affiliation == "admin" then | 1026 end |
1271 return true; | 1027 role = role ~= "none" and role or nil; -- coerces `role == false` to `nil` |
1272 elseif occupant.role ~= "moderator" and role ~= "moderator" then | 1028 |
1273 return true; | 1029 if actor ~= true then |
1274 end | 1030 -- Can't do anything to other owners or admins |
1275 end | 1031 local occupant_affiliation = self:get_affiliation(occupant.bare_jid); |
1276 end | 1032 if occupant_affiliation == "owner" and occupant_affiliation == "admin" then |
1277 return nil, "cancel", "not-allowed"; | 1033 return nil, "cancel", "not-allowed"; |
1278 end | 1034 end |
1279 function room_mt:set_role(actor, occupant_jid, role, callback, reason) | 1035 |
1280 if role == "none" then role = nil; end | 1036 -- If you are trying to give or take moderator role you need to be an owner or admin |
1281 if role and role ~= "moderator" and role ~= "participant" and role ~= "visitor" then return nil, "modify", "not-acceptable"; end | 1037 if occupant.role == "moderator" or role == "moderator" then |
1282 local allowed, err_type, err_condition = self:can_set_role(actor, occupant_jid, role); | 1038 local actor_affiliation = self:get_affiliation(actor); |
1283 if not allowed then return allowed, err_type, err_condition; end | 1039 if actor_affiliation ~= "owner" and actor_affiliation ~= "admin" then |
1284 local occupant = self._occupants[occupant_jid]; | 1040 return nil, "cancel", "not-allowed"; |
1285 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"}) | 1041 end |
1286 :tag("item", {affiliation=occupant.affiliation or "none", nick=select(3, jid_split(occupant_jid)), role=role or "none"}) | 1042 end |
1287 :tag("reason"):text(reason or ""):up() | 1043 |
1288 :up(); | 1044 -- Need to be in the room and a moderator |
1289 local presence_type = nil; | 1045 local actor_occupant = self:get_occupant_by_real_jid(actor); |
1290 if not role then -- kick | 1046 if not actor_occupant or actor_occupant.role ~= "moderator" then |
1291 presence_type = "unavailable"; | 1047 return nil, "cancel", "not-allowed"; |
1292 self._occupants[occupant_jid] = nil; | 1048 end |
1293 for jid in pairs(occupant.sessions) do -- remove for all sessions of the nick | 1049 end |
1294 self._jid_nick[jid] = nil; | 1050 |
1295 end | 1051 local x = st.stanza("x", {xmlns = "http://jabber.org/protocol/muc#user"}); |
1052 if not role then | |
1296 x:tag("status", {code = "307"}):up(); | 1053 x:tag("status", {code = "307"}):up(); |
1297 else | 1054 end |
1298 occupant.role = role; | 1055 occupant.role = role; |
1299 end | 1056 self:save_occupant(occupant); |
1300 local bp; | 1057 self:publicise_occupant_status(occupant, x, nil, actor, reason); |
1301 for jid,pres in pairs(occupant.sessions) do -- send to all sessions of the nick | 1058 return true; |
1302 local p = st.clone(pres); | 1059 end |
1303 p.attr.from = occupant_jid; | 1060 |
1304 p.attr.type = presence_type; | 1061 local name = module:require "muc/name"; |
1305 p.attr.to = jid; | 1062 room_mt.get_name = name.get; |
1306 p:add_child(x); | 1063 room_mt.set_name = name.set; |
1307 self:_route_stanza(p); | 1064 |
1308 if occupant.jid == jid then | 1065 local description = module:require "muc/description"; |
1309 bp = p; | 1066 room_mt.get_description = description.get; |
1310 end | 1067 room_mt.set_description = description.set; |
1311 end | 1068 |
1312 if callback then callback(); end | 1069 local hidden = module:require "muc/hidden"; |
1313 if bp then | 1070 room_mt.get_hidden = hidden.get; |
1314 self:broadcast_except_nick(bp, occupant_jid); | 1071 room_mt.set_hidden = hidden.set; |
1315 end | 1072 function room_mt:get_public() |
1316 return true; | 1073 return not self:get_hidden(); |
1317 end | 1074 end |
1318 | 1075 function room_mt:set_public(public) |
1319 function room_mt:_route_stanza(stanza) | 1076 return self:set_hidden(not public); |
1320 local muc_child; | 1077 end |
1321 if stanza.name == "presence" then | 1078 |
1322 local to_occupant = self._occupants[self:get_occupant_jid(stanza.attr.to)]; | 1079 local password = module:require "muc/password"; |
1323 local from_occupant = self._occupants[stanza.attr.from]; | 1080 room_mt.get_password = password.get; |
1324 if to_occupant and from_occupant then | 1081 room_mt.set_password = password.set; |
1325 if self:get_whois() == 'anyone' then | 1082 |
1326 muc_child = stanza:get_child("x", "http://jabber.org/protocol/muc#user"); | 1083 local whois = module:require "muc/whois"; |
1327 else | 1084 room_mt.get_whois = whois.get; |
1328 if to_occupant.role == "moderator" or jid_bare(to_occupant.jid) == jid_bare(from_occupant.jid) then | 1085 room_mt.set_whois = whois.set; |
1329 muc_child = stanza:get_child("x", "http://jabber.org/protocol/muc#user"); | 1086 |
1330 end | 1087 local members_only = module:require "muc/members_only"; |
1331 end | 1088 room_mt.get_members_only = members_only.get; |
1332 end | 1089 room_mt.set_members_only = members_only.set; |
1333 if muc_child then | 1090 |
1334 for item in muc_child:childtags("item") do | 1091 local moderated = module:require "muc/moderated"; |
1335 if from_occupant == to_occupant then | 1092 room_mt.get_moderated = moderated.get; |
1336 item.attr.jid = stanza.attr.to; | 1093 room_mt.set_moderated = moderated.set; |
1337 else | 1094 |
1338 item.attr.jid = from_occupant.jid; | 1095 local persistent = module:require "muc/persistent"; |
1339 end | 1096 room_mt.get_persistent = persistent.get; |
1340 end | 1097 room_mt.set_persistent = persistent.set; |
1341 end | 1098 |
1342 end | 1099 local subject = module:require "muc/subject"; |
1343 self:route_stanza(stanza); | 1100 room_mt.get_changesubject = subject.get_changesubject; |
1344 if muc_child then | 1101 room_mt.set_changesubject = subject.set_changesubject; |
1345 for item in muc_child:childtags("item") do | 1102 room_mt.get_subject = subject.get; |
1346 item.attr.jid = nil; | 1103 room_mt.set_subject = subject.set; |
1347 end | 1104 room_mt.send_subject = subject.send; |
1348 end | 1105 |
1349 end | 1106 local history = module:require "muc/history"; |
1107 room_mt.send_history = history.send; | |
1108 room_mt.get_historylength = history.get_length; | |
1109 room_mt.set_historylength = history.set_length; | |
1350 | 1110 |
1351 local _M = {}; -- module "muc" | 1111 local _M = {}; -- module "muc" |
1112 | |
1113 _M.set_max_history_length = history.set_max_length; | |
1352 | 1114 |
1353 function _M.new_room(jid, config) | 1115 function _M.new_room(jid, config) |
1354 return setmetatable({ | 1116 return setmetatable({ |
1355 jid = jid; | 1117 jid = jid; |
1356 locked = nil; | |
1357 _jid_nick = {}; | 1118 _jid_nick = {}; |
1358 _occupants = {}; | 1119 _occupants = {}; |
1359 _data = { | 1120 _data = { |
1360 whois = 'moderators'; | |
1361 history_length = math.min((config and config.history_length) | |
1362 or default_history_length, max_history_length); | |
1363 }; | 1121 }; |
1364 _affiliations = {}; | 1122 _affiliations = {}; |
1365 }, room_mt); | 1123 }, room_mt); |
1366 end | 1124 end |
1367 | 1125 |
1368 function _M.set_max_history_length(_max_history_length) | |
1369 max_history_length = _max_history_length or math.huge; | |
1370 end | |
1371 | |
1372 _M.room_mt = room_mt; | 1126 _M.room_mt = room_mt; |
1373 | 1127 |
1374 return _M; | 1128 return _M; |