Comparison

plugins/muc/muc.lib.lua @ 6221:f321536afeec

plugins/muc/muc.lib: Move members_only into seperate file
author daurnimator <quae@daurnimator.com>
date Fri, 04 Apr 2014 11:20:20 -0400
parent 6220:4e7d205f49f7
child 6222:355b29881117
comparison
equal deleted inserted replaced
6220:4e7d205f49f7 6221:f321536afeec
49 module:hook("muc-get-default-role", function(event) 49 module:hook("muc-get-default-role", function(event)
50 if event.affiliation_rank >= valid_affiliations.admin then 50 if event.affiliation_rank >= valid_affiliations.admin then
51 return "moderator"; 51 return "moderator";
52 elseif event.affiliation_rank >= valid_affiliations.member then 52 elseif event.affiliation_rank >= valid_affiliations.member then
53 return "participant"; 53 return "participant";
54 end
55 end);
56 module:hook("muc-get-default-role", function(event)
57 if not event.affiliation and event.room:get_members_only() then
58 return false;
59 end 54 end
60 end); 55 end);
61 module:hook("muc-get-default-role", function(event) 56 module:hook("muc-get-default-role", function(event)
62 if not event.affiliation then 57 if not event.affiliation then
63 return event.room:get_moderated() and "visitor" or "participant"; 58 return event.room:get_moderated() and "visitor" or "participant";
284 end); 279 end);
285 module:hook("muc-disco#info", function(event) 280 module:hook("muc-disco#info", function(event)
286 event.reply:tag("feature", {var = event.room:get_moderated() and "muc_moderated" or "muc_unmoderated"}):up(); 281 event.reply:tag("feature", {var = event.room:get_moderated() and "muc_moderated" or "muc_unmoderated"}):up();
287 end); 282 end);
288 module:hook("muc-disco#info", function(event) 283 module:hook("muc-disco#info", function(event)
289 event.reply:tag("feature", {var = event.room:get_members_only() and "muc_membersonly" or "muc_open"}):up();
290 end);
291 module:hook("muc-disco#info", function(event)
292 event.reply:tag("feature", {var = event.room:get_persistent() and "muc_persistent" or "muc_temporary"}):up(); 284 event.reply:tag("feature", {var = event.room:get_persistent() and "muc_persistent" or "muc_temporary"}):up();
293 end); 285 end);
294 module:hook("muc-disco#info", function(event) 286 module:hook("muc-disco#info", function(event)
295 event.reply:tag("feature", {var = event.room:get_hidden() and "muc_hidden" or "muc_public"}):up(); 287 event.reply:tag("feature", {var = event.room:get_hidden() and "muc_hidden" or "muc_public"}):up();
296 end); 288 end);
355 end 347 end
356 end 348 end
357 function room_mt:get_moderated() 349 function room_mt:get_moderated()
358 return self._data.moderated; 350 return self._data.moderated;
359 end 351 end
360 function room_mt:set_members_only(members_only)
361 members_only = members_only and true or nil;
362 if self._data.members_only ~= members_only then
363 self._data.members_only = members_only;
364 if self.save then self:save(true); end
365 end
366 end
367 function room_mt:get_members_only()
368 return self._data.members_only;
369 end
370 function room_mt:set_persistent(persistent) 352 function room_mt:set_persistent(persistent)
371 persistent = persistent and true or nil; 353 persistent = persistent and true or nil;
372 if self._data.persistent ~= persistent then 354 if self._data.persistent ~= persistent then
373 self._data.persistent = persistent; 355 self._data.persistent = persistent;
374 if self.save then self:save(true); end 356 if self.save then self:save(true); end
407 -- Give the room creator owner affiliation 389 -- Give the room creator owner affiliation
408 module:hook("muc-room-pre-create", function(event) 390 module:hook("muc-room-pre-create", function(event)
409 event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner"); 391 event.room:set_affiliation(true, jid_bare(event.stanza.attr.from), "owner");
410 end, -1); 392 end, -1);
411 393
412 -- registration required for entering members-only room
413 module:hook("muc-occupant-pre-join", function(event)
414 local room, stanza = event.room, event.stanza;
415 local affiliation = room:get_affiliation(stanza.attr.from);
416 if affiliation == nil and event.room:get_members_only() then
417 local reply = st.error_reply(stanza, "auth", "registration-required"):up();
418 reply.tags[1].attr.code = "407";
419 event.origin.send(reply:tag("x", {xmlns = "http://jabber.org/protocol/muc"}));
420 return true;
421 end
422 end, -5);
423 394
424 -- check if user is banned 395 -- check if user is banned
425 module:hook("muc-occupant-pre-join", function(event) 396 module:hook("muc-occupant-pre-join", function(event)
426 local room, stanza = event.room, event.stanza; 397 local room, stanza = event.room, event.stanza;
427 local affiliation = room:get_affiliation(stanza.attr.from); 398 local affiliation = room:get_affiliation(stanza.attr.from);
730 type = 'boolean', 701 type = 'boolean',
731 label = 'Make Room Moderated?', 702 label = 'Make Room Moderated?',
732 value = event.room:get_moderated() 703 value = event.room:get_moderated()
733 }); 704 });
734 end); 705 end);
735 module:hook("muc-config-form", function(event)
736 table.insert(event.form, {
737 name = 'muc#roomconfig_membersonly',
738 type = 'boolean',
739 label = 'Make Room Members-Only?',
740 value = event.room:get_members_only()
741 });
742 end);
743 706
744 function room_mt:process_form(origin, stanza) 707 function room_mt:process_form(origin, stanza)
745 local form = stanza.tags[1]:get_child("x", "jabber:x:data"); 708 local form = stanza.tags[1]:get_child("x", "jabber:x:data");
746 if form.attr.type == "cancel" then 709 if form.attr.type == "cancel" then
747 origin.send(st.reply(stanza)); 710 origin.send(st.reply(stanza));
784 module:hook("muc-config-submitted", function(event) 747 module:hook("muc-config-submitted", function(event)
785 event.update_option("persistent", "muc#roomconfig_persistentroom"); 748 event.update_option("persistent", "muc#roomconfig_persistentroom");
786 end); 749 end);
787 module:hook("muc-config-submitted", function(event) 750 module:hook("muc-config-submitted", function(event)
788 event.update_option("moderated", "muc#roomconfig_moderatedroom"); 751 event.update_option("moderated", "muc#roomconfig_moderatedroom");
789 end);
790 module:hook("muc-config-submitted", function(event)
791 event.update_option("members_only", "muc#roomconfig_membersonly");
792 end); 752 end);
793 module:hook("muc-config-submitted", function(event) 753 module:hook("muc-config-submitted", function(event)
794 event.update_option("public", "muc#roomconfig_publicroom"); 754 event.update_option("public", "muc#roomconfig_publicroom");
795 end); 755 end);
796 module:hook("muc-config-submitted", function(event) 756 module:hook("muc-config-submitted", function(event)
995 event.origin.send(st.error_reply(stanza, "auth", "forbidden")); 955 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
996 return true; 956 return true;
997 end 957 end
998 end); 958 end);
999 959
1000 -- Invitation privileges in members-only rooms SHOULD be restricted to room admins;
1001 -- if a member without privileges to edit the member list attempts to invite another user
1002 -- the service SHOULD return a <forbidden/> error to the occupant
1003 module:hook("muc-pre-invite", function(event)
1004 local room, stanza = event.room, event.stanza;
1005 if room:get_members_only() and valid_affiliations[room:get_affiliation(stanza.attr.from) or "none"] < valid_affiliations.admin then
1006 event.origin.send(st.error_reply(stanza, "auth", "forbidden"));
1007 return true;
1008 end
1009 end);
1010
1011 function room_mt:handle_mediated_invite(origin, stanza) 960 function room_mt:handle_mediated_invite(origin, stanza)
1012 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite"); 961 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
1013 local invitee = jid_prep(payload.attr.to); 962 local invitee = jid_prep(payload.attr.to);
1014 if not invitee then 963 if not invitee then
1015 origin.send(st.error_reply(stanza, "cancel", "jid-malformed")); 964 origin.send(st.error_reply(stanza, "cancel", "jid-malformed"));
1045 local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite"); 994 local invite = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite");
1046 local reason = invite:get_child_text("reason") or ""; 995 local reason = invite:get_child_text("reason") or "";
1047 stanza:tag("body") 996 stanza:tag("body")
1048 :text(invite.attr.from.." invited you to the room "..room.jid..(reason == "" and (" ("..reason..")") or "")) 997 :text(invite.attr.from.." invited you to the room "..room.jid..(reason == "" and (" ("..reason..")") or ""))
1049 :up(); 998 :up();
1050 end);
1051
1052 -- When an invite is sent; add an affiliation for the invitee
1053 module:hook("muc-invite", function(event)
1054 local room, stanza = event.room, event.stanza;
1055 local invitee = stanza.attr.to
1056 if room:get_members_only() and not room:get_affiliation(invitee) then
1057 local from = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("invite").attr.from
1058 log("debug", "%s invited %s into members only room %s, granting membership", from, invitee, room.jid);
1059 room:set_affiliation(from, invitee, "member", "Invited by " .. from); -- This might fail; ignore for now
1060 end
1061 end); 999 end);
1062 1000
1063 function room_mt:handle_mediated_decline(origin, stanza) 1001 function room_mt:handle_mediated_decline(origin, stanza)
1064 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline"); 1002 local payload = stanza:get_child("x", "http://jabber.org/protocol/muc#user"):get_child("decline");
1065 local declinee = jid_prep(payload.attr.to); 1003 local declinee = jid_prep(payload.attr.to);
1276 1214
1277 local whois = module:require "muc/whois"; 1215 local whois = module:require "muc/whois";
1278 room_mt.get_whois = whois.get; 1216 room_mt.get_whois = whois.get;
1279 room_mt.set_whois = whois.set; 1217 room_mt.set_whois = whois.set;
1280 1218
1219 local members_only = module:require "muc/members_only";
1220 room_mt.get_members_only = members_only.get;
1221 room_mt.set_members_only = members_only.set;
1222
1281 local history = module:require "muc/history"; 1223 local history = module:require "muc/history";
1282 room_mt.send_history = history.send; 1224 room_mt.send_history = history.send;
1283 room_mt.get_historylength = history.get_length; 1225 room_mt.get_historylength = history.get_length;
1284 room_mt.set_historylength = history.set_length; 1226 room_mt.set_historylength = history.set_length;
1285 1227