Make permissions handled per server type

master
Marrub 2017-08-10 02:31:12 -04:00
parent 1c25c42790
commit a52e788a3f
3 changed files with 36 additions and 17 deletions

View File

@ -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

View File

@ -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

View File

@ -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