Software /
code /
prosody
Comparison
plugins/muc/muc.lib.lua @ 1754:67b66eec9777
MUC: Added support for room configuration forms, persistence and hidden rooms.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Mon, 07 Sep 2009 20:51:59 +0500 |
parent | 1753:a84901db4085 |
child | 1755:1614e8e62ad5 |
comparison
equal
deleted
inserted
replaced
1753:a84901db4085 | 1754:67b66eec9777 |
---|---|
188 local function room_get_disco_items(self, stanza) end | 188 local function room_get_disco_items(self, stanza) end |
189 function room_mt:set_subject(current_nick, subject) | 189 function room_mt:set_subject(current_nick, subject) |
190 -- TODO check nick's authority | 190 -- TODO check nick's authority |
191 if subject == "" then subject = nil; end | 191 if subject == "" then subject = nil; end |
192 self._data['subject'] = subject; | 192 self._data['subject'] = subject; |
193 if self.save then self:save(); end | |
193 local msg = st.message({type='groupchat', from=current_nick}) | 194 local msg = st.message({type='groupchat', from=current_nick}) |
194 :tag('subject'):text(subject):up(); | 195 :tag('subject'):text(subject):up(); |
195 self:broadcast_message(msg, false); | 196 self:broadcast_message(msg, false); |
196 return true; | 197 return true; |
197 end | 198 end |
302 stanza.attr.to, stanza.attr.from = jid, current_nick; | 303 stanza.attr.to, stanza.attr.from = jid, current_nick; |
303 self:route_stanza(stanza); | 304 self:route_stanza(stanza); |
304 elseif type ~= "error" and type ~= "result" then -- recipient not in room | 305 elseif type ~= "error" and type ~= "result" then -- recipient not in room |
305 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); | 306 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); |
306 end | 307 end |
308 end | |
309 end | |
310 | |
311 function room_mt:handle_form(origin, stanza) | |
312 if self:get_affiliation(stanza.attr.from) ~= "owner" then origin.send(st.error_reply(nil, "auth", "forbidden")); return; end | |
313 if stanza.attr.type == "get" then | |
314 local title = "Configuration for "..self.jid; | |
315 origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner") | |
316 :tag("x", {xmlns='jabber:x:data', type='form'}) | |
317 :tag("title"):text(title):up() | |
318 :tag("instructions"):text(title):up() | |
319 :tag("field", {type='hidden', var='FORM_TYPE'}):tag("value"):text("http://jabber.org/protocol/muc#roomconfig"):up():up() | |
320 :tag("field", {type='boolean', label='Make Room Persistent?', var='muc#roomconfig_persistentroom'}) | |
321 :tag("value"):text(self._data.persistent and "1" or "0"):up() | |
322 :up() | |
323 :tag("field", {type='boolean', label='Make Room Publicly Searchable?', var='muc#roomconfig_publicroom'}) | |
324 :tag("value"):text(self._data.hidden and "0" or "1"):up() | |
325 :up() | |
326 ); | |
327 elseif stanza.attr.type == "set" then | |
328 local query = stanza.tags[1]; | |
329 local form; | |
330 for _, tag in ipairs(query.tags) do if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then form = tag; break; end end | |
331 if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end | |
332 if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end | |
333 if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end | |
334 local fields = {}; | |
335 for _, field in pairs(form.tags) do | |
336 if field.name == "field" and field.attr.var and field.tags[1].name == "value" and #field.tags[1].tags == 0 then | |
337 fields[field.attr.var] = field.tags[1][1] or ""; | |
338 end | |
339 end | |
340 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end | |
341 | |
342 local persistent = fields['muc#roomconfig_persistentroom']; | |
343 if persistent == "0" or persistent == "false" then persistent = nil; elseif persistent == "1" or persistent == "true" then persistent = true; | |
344 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end | |
345 self._data.persistent = persistent; | |
346 module:log("debug", "persistent=%s", tostring(persistent)); | |
347 | |
348 local public = fields['muc#roomconfig_publicroom']; | |
349 if public == "0" or public == "false" then public = nil; elseif public == "1" or public == "true" then public = true; | |
350 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end | |
351 self._data.hidden = not public and true or nil; | |
352 | |
353 if self.save then self:save(true); end | |
354 origin.send(st.reply(stanza)); | |
307 end | 355 end |
308 end | 356 end |
309 | 357 |
310 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc | 358 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc |
311 local type = stanza.attr.type; | 359 local type = stanza.attr.type; |
371 end | 419 end |
372 end | 420 end |
373 elseif type == "set" or type == "get" then | 421 elseif type == "set" or type == "get" then |
374 origin.send(st.error_reply(stanza, "cancel", "bad-request")); | 422 origin.send(st.error_reply(stanza, "cancel", "bad-request")); |
375 end | 423 end |
424 elseif xmlns == "http://jabber.org/protocol/muc#owner" and (type == "get" or type == "set") and stanza.tags[1].name == "query" then | |
425 self:handle_form(origin, stanza); | |
426 elseif type == "set" or type == "get" then | |
427 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | |
376 end | 428 end |
377 elseif stanza.name == "message" and type == "groupchat" then | 429 elseif stanza.name == "message" and type == "groupchat" then |
378 local from, to = stanza.attr.from, stanza.attr.to; | 430 local from, to = stanza.attr.from, stanza.attr.to; |
379 local room = jid_bare(to); | 431 local room = jid_bare(to); |
380 local current_nick = self._jid_nick[from]; | 432 local current_nick = self._jid_nick[from]; |
472 p.attr.to = jid; | 524 p.attr.to = jid; |
473 self:route_stanza(p); | 525 self:route_stanza(p); |
474 end | 526 end |
475 end | 527 end |
476 end | 528 end |
529 if room.save then room:save(); end | |
477 if callback then callback(); end | 530 if callback then callback(); end |
478 for _, nick in ipairs(modified_nicks) do | 531 for _, nick in ipairs(modified_nicks) do |
479 p.attr.from = nick; | 532 p.attr.from = nick; |
480 self:broadcast_except_nick(p, nick); | 533 self:broadcast_except_nick(p, nick); |
481 end | 534 end |