Comparison

plugins/muc/muc.lib.lua @ 6208:d724289a5226

plugins/muc: Move password functions to seperate module
author daurnimator <quae@daurnimator.com>
date Wed, 02 Apr 2014 16:57:59 -0400
parent 6206:f937bb5c83c3
child 6209:cc00e78e6a31
comparison
equal deleted inserted replaced
6207:a5928fdeaf97 6208:d724289a5226
398 end); 398 end);
399 module:hook("muc-disco#info", function(event) 399 module:hook("muc-disco#info", function(event)
400 event.reply:tag("feature", {var = "http://jabber.org/protocol/muc"}):up(); 400 event.reply:tag("feature", {var = "http://jabber.org/protocol/muc"}):up();
401 end); 401 end);
402 module:hook("muc-disco#info", function(event) 402 module:hook("muc-disco#info", function(event)
403 event.reply:tag("feature", {var = event.room:get_password() and "muc_passwordprotected" or "muc_unsecured"}):up();
404 end);
405 module:hook("muc-disco#info", function(event)
406 event.reply:tag("feature", {var = event.room:get_moderated() and "muc_moderated" or "muc_unmoderated"}):up(); 403 event.reply:tag("feature", {var = event.room:get_moderated() and "muc_moderated" or "muc_unmoderated"}):up();
407 end); 404 end);
408 module:hook("muc-disco#info", function(event) 405 module:hook("muc-disco#info", function(event)
409 event.reply:tag("feature", {var = event.room:get_members_only() and "muc_membersonly" or "muc_open"}):up(); 406 event.reply:tag("feature", {var = event.room:get_members_only() and "muc_membersonly" or "muc_open"}):up();
410 end); 407 end);
478 end 475 end
479 end 476 end
480 function room_mt:get_name() 477 function room_mt:get_name()
481 return self._data.name or jid_split(self.jid); 478 return self._data.name or jid_split(self.jid);
482 end 479 end
483 function room_mt:set_password(password)
484 if password == "" or type(password) ~= "string" then password = nil; end
485 if self._data.password ~= password then
486 self._data.password = password;
487 if self.save then self:save(true); end
488 end
489 end
490 function room_mt:get_password()
491 return self._data.password;
492 end
493 function room_mt:set_moderated(moderated) 480 function room_mt:set_moderated(moderated)
494 moderated = moderated and true or nil; 481 moderated = moderated and true or nil;
495 if self._data.moderated ~= moderated then 482 if self._data.moderated ~= moderated then
496 self._data.moderated = moderated; 483 self._data.moderated = moderated;
497 if self.save then self:save(true); end 484 if self.save then self:save(true); end
573 560
574 -- Give the room creator owner affiliation 561 -- Give the room creator owner affiliation
575 module:hook("muc-room-pre-create", function(event) 562 module:hook("muc-room-pre-create", function(event)
576 event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner"); 563 event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
577 end, -1); 564 end, -1);
578
579 module:hook("muc-occupant-pre-join", function(event)
580 local room, stanza = event.room, event.stanza;
581 local password = stanza:get_child("x", "http://jabber.org/protocol/muc");
582 password = password and password:get_child_text("password", "http://jabber.org/protocol/muc");
583 if not password or password == "" then password = nil; end
584 if room:get_password() ~= password then
585 local from, to = stanza.attr.from, stanza.attr.to;
586 log("debug", "%s couldn't join due to invalid password: %s", from, to);
587 local reply = st.error_reply(stanza, "auth", "not-authorized"):up();
588 reply.tags[1].attr.code = "401";
589 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
590 return true;
591 end
592 end, -20);
593 565
594 -- registration required for entering members-only room 566 -- registration required for entering members-only room
595 module:hook("muc-occupant-pre-join", function(event) 567 module:hook("muc-occupant-pre-join", function(event)
596 local room, stanza = event.room, event.stanza; 568 local room, stanza = event.room, event.stanza;
597 local affiliation = room:get_affiliation(stanza.attr.from); 569 local affiliation = room:get_affiliation(stanza.attr.from);
923 } 895 }
924 }); 896 });
925 end); 897 end);
926 module:hook("muc-config-form", function(event) 898 module:hook("muc-config-form", function(event)
927 table.insert(event.form, { 899 table.insert(event.form, {
928 name = 'muc#roomconfig_roomsecret',
929 type = 'text-private',
930 label = 'Password',
931 value = event.room:get_password() or "",
932 });
933 end);
934 module:hook("muc-config-form", function(event)
935 table.insert(event.form, {
936 name = 'muc#roomconfig_moderatedroom', 900 name = 'muc#roomconfig_moderatedroom',
937 type = 'boolean', 901 type = 'boolean',
938 label = 'Make Room Moderated?', 902 label = 'Make Room Moderated?',
939 value = event.room:get_moderated() 903 value = event.room:get_moderated()
940 }); 904 });
1020 module:hook("muc-config-submitted", function(event) 984 module:hook("muc-config-submitted", function(event)
1021 if event.update_option("whois", "muc#roomconfig_whois", valid_whois) then 985 if event.update_option("whois", "muc#roomconfig_whois", valid_whois) then
1022 local code = (event.room:get_whois() == 'moderators') and "173" or "172"; 986 local code = (event.room:get_whois() == 'moderators') and "173" or "172";
1023 event.status_codes[code] = true; 987 event.status_codes[code] = true;
1024 end 988 end
1025 end);
1026 module:hook("muc-config-submitted", function(event)
1027 event.update_option("password", "muc#roomconfig_roomsecret");
1028 end); 989 end);
1029 990
1030 -- Removes everyone from the room 991 -- Removes everyone from the room
1031 function room_mt:clear(x) 992 function room_mt:clear(x)
1032 x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'}); 993 x = x or st.stanza("x", {xmlns='http://jabber.org/protocol/muc#user'});
1248 if not module:fire_event("muc-invite", {room = self, stanza = invite, origin = origin, incoming = stanza}) then 1209 if not module:fire_event("muc-invite", {room = self, stanza = invite, origin = origin, incoming = stanza}) then
1249 self:route_stanza(invite); 1210 self:route_stanza(invite);
1250 end 1211 end
1251 return true; 1212 return true;
1252 end 1213 end
1253
1254 -- Add password to outgoing invite
1255 module:hook("muc-invite", function(event)
1256 local password = event.room:get_password();
1257 if password then
1258 local x = event.stanza:get_child("x", "http://jabber.org/protocol/muc#user");
1259 x:tag("password"):text(password):up();
1260 end
1261 end);
1262 1214
1263 -- COMPAT: Some older clients expect this 1215 -- COMPAT: Some older clients expect this
1264 module:hook("muc-invite", function(event) 1216 module:hook("muc-invite", function(event)
1265 local room, stanza = event.room, event.stanza; 1217 local room, stanza = event.room, event.stanza;
1266 local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite"); 1218 local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
1507 1459
1508 local description = module:require "muc/description"; 1460 local description = module:require "muc/description";
1509 room_mt.get_description = description.get; 1461 room_mt.get_description = description.get;
1510 room_mt.set_description = description.set; 1462 room_mt.set_description = description.set;
1511 1463
1464 local password = module:require "muc/password";
1465 room_mt.get_password = password.get;
1466 room_mt.set_password = password.set;
1467
1512 local _M = {}; -- module "muc" 1468 local _M = {}; -- module "muc"
1513 1469
1514 function _M.new_room(jid, config) 1470 function _M.new_room(jid, config)
1515 return setmetatable({ 1471 return setmetatable({
1516 jid = jid; 1472 jid = jid;