Software / code / prosody
Comparison
plugins/muc/muc.lib.lua @ 3591:dff4a77ee285
MUC: Parse submitted form with util.dataforms
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 10 Nov 2010 01:34:57 +0100 |
| parent | 3590:dcc5f3402f5b |
| child | 3592:3adac5780c5a |
comparison
equal
deleted
inserted
replaced
| 3590:dcc5f3402f5b | 3591:dff4a77ee285 |
|---|---|
| 520 end | 520 end |
| 521 end | 521 end |
| 522 end | 522 end |
| 523 | 523 |
| 524 function room_mt:send_form(origin, stanza) | 524 function room_mt:send_form(origin, stanza) |
| 525 origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner") | |
| 526 :add_child(self:get_form_layout():form()) | |
| 527 ); | |
| 528 end | |
| 529 | |
| 530 function room_mt:get_form_layout() | |
| 525 local title = "Configuration for "..self.jid; | 531 local title = "Configuration for "..self.jid; |
| 526 origin.send(st.reply(stanza):query("http://jabber.org/protocol/muc#owner") | 532 return dataform.new({ |
| 527 :add_child(dataform.new({ | |
| 528 title = title, | 533 title = title, |
| 529 instructions = title, | 534 instructions = title, |
| 530 { | 535 { |
| 531 name = 'FORM_TYPE', | 536 name = 'FORM_TYPE', |
| 532 type = 'hidden', | 537 type = 'hidden', |
| 581 name = 'muc#roomconfig_membersonly', | 586 name = 'muc#roomconfig_membersonly', |
| 582 type = 'boolean', | 587 type = 'boolean', |
| 583 label = 'Make Room Members-Only?', | 588 label = 'Make Room Members-Only?', |
| 584 value = self:is_members_only() | 589 value = self:is_members_only() |
| 585 } | 590 } |
| 586 }):form()) | 591 }); |
| 587 ); | |
| 588 end | 592 end |
| 589 | 593 |
| 590 local valid_whois = { | 594 local valid_whois = { |
| 591 moderators = true, | 595 moderators = true, |
| 592 anyone = true, | 596 anyone = true, |
| 596 local query = stanza.tags[1]; | 600 local query = stanza.tags[1]; |
| 597 local form; | 601 local form; |
| 598 for _, tag in ipairs(query.tags) do if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then form = tag; break; end end | 602 for _, tag in ipairs(query.tags) do if tag.name == "x" and tag.attr.xmlns == "jabber:x:data" then form = tag; break; end end |
| 599 if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end | 603 if not form then origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); return; end |
| 600 if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end | 604 if form.attr.type == "cancel" then origin.send(st.reply(stanza)); return; end |
| 601 if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end | 605 if form.attr.type ~= "submit" then origin.send(st.error_reply(stanza, "cancel", "bad-request", "Not a submitted form")); return; end |
| 602 local fields = {}; | 606 |
| 603 for _, field in pairs(form.tags) do | 607 local fields = self:get_form_layout():data(form); |
| 604 if field.name == "field" and field.attr.var then | 608 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 |
| 605 if field.tags[1] and field.tags[1].name == "value" and #field.tags[1].tags == 0 then | |
| 606 fields[field.attr.var] = field.tags[1][1] or ""; | |
| 607 elseif field.attr.type == "boolean" then | |
| 608 fields[field.attr.var] = "false"; | |
| 609 end | |
| 610 end | |
| 611 end | |
| 612 if fields.FORM_TYPE ~= "http://jabber.org/protocol/muc#roomconfig" then origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end | |
| 613 | 609 |
| 614 local dirty = false | 610 local dirty = false |
| 615 | 611 |
| 616 local name = fields['muc#roomconfig_roomname']; | 612 local name = fields['muc#roomconfig_roomname']; |
| 617 if name then | 613 if name then |
| 622 if description then | 618 if description then |
| 623 self:set_description(description); | 619 self:set_description(description); |
| 624 end | 620 end |
| 625 | 621 |
| 626 local persistent = fields['muc#roomconfig_persistentroom']; | 622 local persistent = fields['muc#roomconfig_persistentroom']; |
| 627 if persistent == "0" or persistent == "false" then persistent = nil; elseif persistent == "1" or persistent == "true" then persistent = true; | |
| 628 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end | |
| 629 dirty = dirty or (self:is_persistent() ~= persistent) | 623 dirty = dirty or (self:is_persistent() ~= persistent) |
| 630 module:log("debug", "persistent=%s", tostring(persistent)); | 624 module:log("debug", "persistent=%s", tostring(persistent)); |
| 631 | 625 |
| 632 local moderated = fields['muc#roomconfig_moderatedroom']; | 626 local moderated = fields['muc#roomconfig_moderatedroom']; |
| 633 if moderated == "0" or moderated == "false" then moderated = nil; elseif moderated == "1" or moderated == "true" then moderated = true; | |
| 634 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end | |
| 635 dirty = dirty or (self:is_moderated() ~= moderated) | 627 dirty = dirty or (self:is_moderated() ~= moderated) |
| 636 module:log("debug", "moderated=%s", tostring(moderated)); | 628 module:log("debug", "moderated=%s", tostring(moderated)); |
| 637 | 629 |
| 638 local membersonly = fields['muc#roomconfig_membersonly']; | 630 local membersonly = fields['muc#roomconfig_membersonly']; |
| 639 if membersonly == "0" or membersonly == "false" then membersonly = nil; elseif membersonly == "1" or membersonly == "true" then membersonly = true; | |
| 640 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end | |
| 641 dirty = dirty or (self:is_members_only() ~= membersonly) | 631 dirty = dirty or (self:is_members_only() ~= membersonly) |
| 642 module:log("debug", "membersonly=%s", tostring(membersonly)); | 632 module:log("debug", "membersonly=%s", tostring(membersonly)); |
| 643 | 633 |
| 644 local public = fields['muc#roomconfig_publicroom']; | 634 local public = fields['muc#roomconfig_publicroom']; |
| 645 if public == "0" or public == "false" then public = nil; elseif public == "1" or public == "true" then public = true; | |
| 646 else origin.send(st.error_reply(stanza, "cancel", "bad-request")); return; end | |
| 647 dirty = dirty or (self:is_hidden() ~= (not public and true or nil)) | 635 dirty = dirty or (self:is_hidden() ~= (not public and true or nil)) |
| 648 | 636 |
| 649 local whois = fields['muc#roomconfig_whois']; | 637 local whois = fields['muc#roomconfig_whois']; |
| 650 if not valid_whois[whois] then | 638 if not valid_whois[whois] then |
| 651 origin.send(st.error_reply(stanza, 'cancel', 'bad-request')); | 639 origin.send(st.error_reply(stanza, 'cancel', 'bad-request', "Invalid value for 'whois'")); |
| 652 return; | 640 return; |
| 653 end | 641 end |
| 654 local whois_changed = self._data.whois ~= whois | 642 local whois_changed = self._data.whois ~= whois |
| 655 self._data.whois = whois | 643 self._data.whois = whois |
| 656 module:log('debug', 'whois=%s', whois) | 644 module:log('debug', 'whois=%s', whois) |
| 657 | 645 |
| 658 local password = fields['muc#roomconfig_roomsecret']; | 646 local password = fields['muc#roomconfig_roomsecret']; |
| 659 if password then | 647 if self:get_password() ~= password then |
| 660 self:set_password(password); | 648 self:set_password(password); |
| 661 end | 649 end |
| 662 self:set_moderated(moderated); | 650 self:set_moderated(moderated); |
| 663 self:set_members_only(membersonly); | 651 self:set_members_only(membersonly); |
| 664 self:set_persistent(persistent); | 652 self:set_persistent(persistent); |