Comparison

plugins/muc/muc.lib.lua @ 2221:7ddd24d8260c

Merge with trunk
author Matthew Wild <mwild1@gmail.com>
date Wed, 25 Nov 2009 19:59:16 +0000
parent 2217:838f6d546177
child 2411:c2b6c55201af
comparison
equal deleted inserted replaced
2220:98a880fda813 2221:7ddd24d8260c
387 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room")); 387 origin.send(st.error_reply(stanza, "cancel", "item-not-found", "Recipient not in room"));
388 end 388 end
389 end 389 end
390 end 390 end
391 391
392 function room_mt:handle_form(origin, stanza) 392 function room_mt:send_form(origin, stanza)
393 if self:get_affiliation(stanza.attr.from) ~= "owner" then origin.send(st.error_reply(stanza, "auth", "forbidden")); return; end 393 local title = "Configuration for "..self.jid;
394 if stanza.attr.type == "get" then 394 origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner")
395 local title = "Configuration for "..self.jid; 395 :tag("x", {xmlns='jabber:x:data', type='form'})
396 origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner") 396 :tag("title"):text(title):up()
397 :tag("x", {xmlns='jabber:x:data', type='form'}) 397 :tag("instructions"):text(title):up()
398 :tag("title"):text(title):up() 398 :tag("field", {type='hidden', var='FORM_TYPE'}):tag("value"):text("http://jabber.org/protocol/muc#roomconfig"):up():up()
399 :tag("instructions"):text(title):up() 399 :tag("field", {type='boolean', label='Make Room Persistent?', var='muc#roomconfig_persistentroom'})
400 :tag("field", {type='hidden', var='FORM_TYPE'}):tag("value"):text("http://jabber.org/protocol/muc#roomconfig"):up():up() 400 :tag("value"):text(self._data.persistent and "1" or "0"):up()
401 :tag("field", {type='boolean', label='Make Room Persistent?', var='muc#roomconfig_persistentroom'}) 401 :up()
402 :tag("value"):text(self._data.persistent and "1" or "0"):up() 402 :tag("field", {type='boolean', label='Make Room Publicly Searchable?', var='muc#roomconfig_publicroom'})
403 :up() 403 :tag("value"):text(self._data.hidden and "0" or "1"):up()
404 :tag("field", {type='boolean', label='Make Room Publicly Searchable?', var='muc#roomconfig_publicroom'}) 404 :up()
405 :tag("value"):text(self._data.hidden and "0" or "1"):up() 405 );
406 :up() 406 end
407 ); 407
408 elseif stanza.attr.type == "set" then 408 function room_mt:process_form(origin, stanza)
409 local query = stanza.tags[1]; 409 local query = stanza.tags[1];
410 local form; 410 local form;
411 for _, tag in ipairs(query.tags) do if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then form = tag; break; end end 411 for _, tag in ipairs(query.tags) do if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then form = tag; break; end end
412 if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end 412 if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end
413 if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end 413 if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end
414 if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end 414 if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
415 local fields = {}; 415 local fields = {};
416 for _, field in pairs(form.tags) do 416 for _, field in pairs(form.tags) do
417 if field.name == "field" and field.attr.var and field.tags[1].name == "value" and #field.tags[1].tags == 0 then 417 if field.name == "field" and field.attr.var and field.tags[1].name == "value" and #field.tags[1].tags == 0 then
418 fields[field.attr.var] = field.tags[1][1] or ""; 418 fields[field.attr.var] = field.tags[1][1] or "";
419 end 419 end
420 end 420 end
421 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end 421 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
422 422
423 local persistent = fields['muc#roomconfig_persistentroom']; 423 local persistent = fields['muc#roomconfig_persistentroom'];
424 if persistent == "0" or persistent == "false" then persistent = nil; elseif persistent == "1" or persistent == "true" then persistent = true; 424 if persistent == "0" or persistent == "false" then persistent = nil; elseif persistent == "1" or persistent == "true" then persistent = true;
425 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end 425 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
426 self._data.persistent = persistent; 426 self._data.persistent = persistent;
427 module:log("debug", "persistent=%s", tostring(persistent)); 427 module:log("debug", "persistent=%s", tostring(persistent));
428 428
429 local public = fields['muc#roomconfig_publicroom']; 429 local public = fields['muc#roomconfig_publicroom'];
430 if public == "0" or public == "false" then public = nil; elseif public == "1" or public == "true" then public = true; 430 if public == "0" or public == "false" then public = nil; elseif public == "1" or public == "true" then public = true;
431 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end 431 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end
432 self._data.hidden = not public and true or nil; 432 self._data.hidden = not public and true or nil;
433 433
434 if self.save then self:save(true); end 434 if self.save then self:save(true); end
435 origin.send(st.reply(stanza)); 435 origin.send(st.reply(stanza));
436 end 436 end
437
438 function room_mt:destroy(newjid, reason, password)
439 local pr = st.presence({type = "unavailable"})
440 :tag("x", {xmlns = "http://jabber.org/protocol/muc#user"})
441 :tag("item", { affiliation='none', role='none' }):up()
442 :tag("destroy", {jid=newjid})
443 if reason then pr:tag("reason"):text(reason):up(); end
444 if password then pr:tag("password"):text(password):up(); end
445 for nick, occupant in pairs(self._occupants) do
446 pr.attr.from = nick;
447 for jid in pairs(occupant.sessions) do
448 pr.attr.to = jid;
449 self:_route_stanza(pr);
450 self._jid_nick[jid] = nil;
451 end
452 self._occupants[nick] = nil;
453 end
454 self._data.persistent = nil;
455 if self.save then self:save(true); end
437 end 456 end
438 457
439 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc 458 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc
440 local type = stanza.attr.type; 459 local type = stanza.attr.type;
441 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns; 460 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns;
509 end 528 end
510 elseif type == "set" or type == "get" then 529 elseif type == "set" or type == "get" then
511 origin.send(st.error_reply(stanza, "cancel", "bad-request")); 530 origin.send(st.error_reply(stanza, "cancel", "bad-request"));
512 end 531 end
513 elseif xmlns == "http://jabber.org/protocol/muc#owner" and (type == "get" or type == "set") and stanza.tags[1].name == "query" then 532 elseif xmlns == "http://jabber.org/protocol/muc#owner" and (type == "get" or type == "set") and stanza.tags[1].name == "query" then
514 self:handle_form(origin, stanza); 533 if self:get_affiliation(stanza.attr.from) ~= "owner" then
534 origin.send(st.error_reply(stanza, "auth", "forbidden"));
535 elseif stanza.attr.type == "get" then
536 self:send_form(origin, stanza);
537 elseif stanza.attr.type == "set" then
538 local child = stanza.tags[1].tags[1];
539 if not child then
540 origin.send(st.error_reply(stanza, "auth", "bad-request"));
541 elseif child.name == "destroy" then
542 local newjid = child.attr.jid;
543 local reason, password;
544 for _,tag in ipairs(child.tags) do
545 if tag.name == "reason" then
546 reason = #tag.tags == 0 and tag[1];
547 elseif tag.name == "password" then
548 password = #tag.tags == 0 and tag[1];
549 end
550 end
551 self:destroy(newjid, reason, password);
552 origin.send(st.reply(stanza));
553 else
554 self:process_form(origin, stanza);
555 end
556 end
515 elseif type == "set" or type == "get" then 557 elseif type == "set" or type == "get" then
516 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); 558 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
517 end 559 end
518 elseif stanza.name == "message" and type == "groupchat" then 560 elseif stanza.name == "message" and type == "groupchat" then
519 local from, to = stanza.attr.from, stanza.attr.to; 561 local from, to = stanza.attr.from, stanza.attr.to;