From a52e788a3f8c2c253cf79f4e29f6874f7a624109 Mon Sep 17 00:00:00 2001 From: Marrub Date: Thu, 10 Aug 2017 02:31:12 -0400 Subject: [PATCH] Make permissions handled per server type --- source/module.rb | 5 ++--- source/server.rb | 16 ++-------------- source/sv_discord.rb | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/source/module.rb b/source/module.rb index 594899f..b226f6e 100644 --- a/source/module.rb +++ b/source/module.rb @@ -9,16 +9,15 @@ module Vrobot4::Module end def usable_in? m, type - chan = m.chan.name role = m.user.roles mprm = m.serv.mprm retm = true retc = false retr = false retm = mprm[:glob][type] if mprm[:glob].key? type - retc = mprm[:chan][type].key? chan if mprm[:chan].key? type + retc = mprm[:chan][type][m.chan] if mprm[:chan].key? type retr = mprm[:role][type].scan /[#{role}]/ if mprm[:role].key? type - role.scan(@roles).any? && (retm || retc || retr) + role.scan(@roles).any? and (retm or retc or retr) end def run m, argv diff --git a/source/server.rb b/source/server.rb index 2bbf701..10d8fc8 100644 --- a/source/server.rb +++ b/source/server.rb @@ -75,21 +75,9 @@ module Vrobot4::Server "" end - private + protected def load_permissions pinf - @mprm = {chan: {}, role: {}, glob: {}} - pinf.each do |perm| - mod = Vrobot4::Module.get_module_type(perm["module"])[:type] - if perm.key? "channel" - chan = perm["channel"] - @mprm[:chan][mod] = {} unless @mprm[:chan].key? chan - @mprm[:chan][mod][chan] = true - elsif perm.key? "roles" - @mprm[:role][mod] = perm["roles"] - else - @mprm[:glob][mod] = perm["enable"] - end - end + raise NotImplementedError, "Server::#load_permissions not implemented" end end diff --git a/source/sv_discord.rb b/source/sv_discord.rb index f37f49b..4320ace 100644 --- a/source/sv_discord.rb +++ b/source/sv_discord.rb @@ -98,6 +98,38 @@ class Sv_Discord < Vrobot4::Server::AudioServer def flags "AD" end + + private + class ChannelPerms + def initialize + @cprm = {} + end + + def [] chan + (@cprm.key? chan.name and @cprm[chan.name]) or + (@cprm.key? chan.real.id and @cprm[chan.real.id]) + end + + def []= chan, set + @cprm[chan] = set + end + end + + protected + def load_permissions pinf + @mprm = {chan: {}, role: {}, glob: {}} + pinf.each do |perm| + mod = Vrobot4::Module.get_module_type(perm["module"])[:type] + if perm.key? "channel" + @mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan].key? mod + @mprm[:chan][mod][perm["channel"]] = true + elsif perm.key? "roles" + @mprm[:role][mod] = perm["roles"] + else + @mprm[:glob][mod] = perm["enable"] + end + end + end end ## EOF