5 class AuthModule < CoreBotModule
\r
9 load_array(:default, true)
\r
10 debug "Initialized auth. Botusers: #{@bot.auth.save_array.inspect}"
\r
17 def save_array(key=:default)
\r
18 if @bot.auth.changed?
\r
19 @registry[key] = @bot.auth.save_array
\r
20 @bot.auth.reset_changed
\r
21 debug "saved botusers (#{key}): #{@registry[key].inspect}"
\r
25 def load_array(key=:default, forced=false)
\r
26 debug "loading botusers (#{key}): #{@registry[key].inspect}"
\r
27 @bot.auth.load_array(@registry[key], forced) if @registry.has_key?(key)
\r
30 # The permission parameters accept arguments with the following syntax:
\r
31 # cmd_path... [on #chan .... | in here | in private]
\r
32 # This auxiliary method scans the array _ar_ to see if it matches
\r
33 # the given syntax: it expects + or - signs in front of _cmd_path_
\r
34 # elements when _setting_ = true
\r
36 # It returns an array whose first element is the array of cmd_path,
\r
37 # the second element is an array of locations and third an array of
\r
38 # warnings occurred while parsing the strings
\r
40 def parse_args(ar, setting)
\r
45 next_must_be_chan = false
\r
48 ar.each_with_index { |x, i|
\r
49 if doing_cmds # parse cmd_path
\r
50 # check if the list is done
\r
51 if x == "on" or x == "in"
\r
53 next_must_be_chan = true if x == "on"
\r
56 if "+-".include?(x[0])
\r
57 warns << ArgumentError("please do not use + or - in front of command #{x} when resetting") unless setting
\r
59 warns << ArgumentError("+ or - expected in front of #{x}") if setting
\r
62 else # parse locations
\r
68 case next_must_be_chan
\r
70 locs << x.gsub(/^here$/,'_').gsub(/^private$/,'?')
\r
72 warns << ArgumentError("#{x} doesn't look like a channel name") unless @bot.server.supports[:chantypes].include?(x[0])
\r
81 warns << "trailing comma" if wants_more
\r
82 warns << "you probably forgot a comma" unless last_idx == ar.length - 1
\r
83 return cmds, locs, warns
\r
86 def auth_set(m, params)
\r
87 cmds, locs, warns = parse_args(params[:args])
\r
88 errs = warns.select { |w| w.kind_of?(Exception) }
\r
90 m.reply "couldn't satisfy your request: #{errs.join(',')}"
\r
93 user = params[:user].sub(/^all$/,"everyone")
\r
95 bu = @bot.auth.get_botuser(user)
\r
97 m.reply "couldn't find botuser #{user}"
\r
107 ch = "?" if loc == "_"
\r
109 ch = m.target.to_s if loc == "_"
\r
111 cmds.each { |setval|
\r
112 val = setval[0].chr == '+'
\r
113 cmd = setval[1..-1]
\r
114 bu.set_permission(cmd, val, ch)
\r
118 m.reply "Something went wrong while trying to set the permissions"
\r
121 @bot.auth.set_changed
\r
122 debug "User #{user} permissions changed"
\r
123 m.reply "Ok, #{user} now also has permissions #{params[:args].join(' ')}"
\r
126 def auth_login(m, params)
\r
128 case @bot.auth.login(m.source, params[:botuser], params[:password])
\r
130 m.reply "welcome, #{@bot.auth.irc_to_botuser(m.source).username}"
\r
131 @bot.auth.set_changed
\r
133 m.reply "sorry, can't do"
\r
136 m.reply "couldn't login: #{e}"
\r
143 auth = AuthModule.new
\r
145 auth.map "permissions set *args for :user",
\r
146 :action => 'auth_set',
\r
147 :auth_path => ':edit::set:'
\r
149 auth.map "permissions reset *args for :user",
\r
150 :action => 'auth_reset',
\r
151 :auth_path => ':edit::reset:'
\r
153 auth.map "login :botuser :password",
\r
154 :action => 'auth_login',
\r
156 :defaults => { :password => nil },
\r
157 :auth_path => '!login!'
\r
159 auth.map "login :botuser",
\r
160 :action => 'auth_login',
\r
161 :defaults => { :password => nil },
\r
162 :auth_path => '!login!'
\r
164 auth.default_auth('*', false)
\r