Software /
code /
prosody
Comparison
plugins/muc/muc.lib.lua @ 2216:dbbb5ed41365
MUC: Slightly refactored form processing.
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Wed, 25 Nov 2009 22:26:06 +0500 |
parent | 2174:13375e6c4ecb |
child | 2217:838f6d546177 |
comparison
equal
deleted
inserted
replaced
2174:13375e6c4ecb | 2216:dbbb5ed41365 |
---|---|
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 | |
437 end | 436 end |
438 | 437 |
439 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc | 438 function room_mt:handle_to_room(origin, stanza) -- presence changes and groupchat messages, along with disco/etc |
440 local type = stanza.attr.type; | 439 local type = stanza.attr.type; |
441 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns; | 440 local xmlns = stanza.tags[1] and stanza.tags[1].attr.xmlns; |
509 end | 508 end |
510 elseif type == "set" or type == "get" then | 509 elseif type == "set" or type == "get" then |
511 origin.send(st.error_reply(stanza, "cancel", "bad-request")); | 510 origin.send(st.error_reply(stanza, "cancel", "bad-request")); |
512 end | 511 end |
513 elseif xmlns == "http://jabber.org/protocol/muc#owner" and (type == "get" or type == "set") and stanza.tags[1].name == "query" then | 512 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); | 513 if self:get_affiliation(stanza.attr.from) ~= "owner" then |
514 origin.send(st.error_reply(stanza, "auth", "forbidden")); | |
515 elseif stanza.attr.type == "get" then | |
516 self:send_form(origin, stanza); | |
517 elseif stanza.attr.type == "set" then | |
518 self:process_form(origin, stanza) | |
519 end | |
515 elseif type == "set" or type == "get" then | 520 elseif type == "set" or type == "get" then |
516 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); | 521 origin.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
517 end | 522 end |
518 elseif stanza.name == "message" and type == "groupchat" then | 523 elseif stanza.name == "message" and type == "groupchat" then |
519 local from, to = stanza.attr.from, stanza.attr.to; | 524 local from, to = stanza.attr.from, stanza.attr.to; |