From 68c44e9b91544f26b6d3c82cc645175adae4c848 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Sat, 5 Aug 2006 18:01:46 +0000 Subject: auth botmodule now allows showing all user settings and enable/disable boolean ones. still needs work for setting and resetting. no user creation yet --- lib/rbot/core/auth.rb | 196 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 188 insertions(+), 8 deletions(-) (limited to 'lib/rbot/core') diff --git a/lib/rbot/core/auth.rb b/lib/rbot/core/auth.rb index 53d88996..b108577d 100644 --- a/lib/rbot/core/auth.rb +++ b/lib/rbot/core/auth.rb @@ -123,11 +123,23 @@ class AuthModule < CoreBotModule m.reply "Ok, #{user} now also has permissions #{params[:args].join(' ')}" end + def get_botuser_for(user) + @bot.auth.irc_to_botuser(user) + end + + def get_botusername_for(user) + get_botuser_for(user).username + end + + def welcome(user) + "welcome, #{get_botusername_for(user)}" + end + def auth_login(m, params) begin case @bot.auth.login(m.source, params[:botuser], params[:password]) when true - m.reply "welcome, #{@bot.auth.irc_to_botuser(m.source).username}" + m.reply welcome(m.source) @bot.auth.set_changed else m.reply "sorry, can't do" @@ -138,17 +150,174 @@ class AuthModule < CoreBotModule end end + def auth_autologin(m, params) + u = do_autologin(m.source) + case u.username + when 'everyone' + m.reply "I couldn't find anything to let you login automatically" + else + m.reply welcome(m.source) + end + end + + def do_autologin(user) + @bot.auth.autologin(user) + end + + def auth_whoami(m, params) + rep = "" + # if m.public? + # rep << m.source.nick << ", " + # end + rep << "you are " + rep << get_botusername_for(m.source).gsub(/^everyone$/, "no one that I know").gsub(/^owner$/, "my boss") + m.reply rep + end + + def help(plugin, topic="") + case topic + when /^login/ + return "login [] []: logs in to the bot as botuser with password . can be omitted if allows login-by-mask and your netmask is among the known ones. if is omitted too autologin will be attempted" + when /^whoami/ + return "whoami: names the botuser you're linked to" + when /^permission syntax/ + return "A permission is specified as module::path::to::cmd; when you want to enable it, prefix it with +; when you want to disable it, prefix it with -; when using the +reset+ command, do not use any prefix" + when /^permission/ + return "permissions (re)set [in ] for : sets or resets the permissions for botuser in channel (use ? to change the permissions for private addressing)" + else + return "#{name}: login, whoami, permission syntax, permissions" + end + end + + def need_args(cmd) + "sorry, I need more arguments to #{cmd}" + end + + def not_args(cmd, *stuff) + "I can only #{cmd} these: #{stuff.join(', ')}" + end + + def set_bool_prop(botuser, prop, val) + k = prop.to_s.gsub("-","_") + botuser.send( (k + "=").to_sym, val) + end + + def reset_bool_prop(botuser, prop) + k = prop.to_s.gsub("-","_") + botuser.send( (k + "=").to_sym, @bot.config['auth.' + k]) + end + + def ask_bool_prop(botuser, prop) + k = prop.to_s.gsub("-","_") + botuser.send( (k + "?").to_sym) + end + + def auth_manage_user(m, params) + splits = params[:data] + + cmd = splits.first + return auth_whoami(m, params) if cmd.nil? + + botuser = get_botuser_for(m.source) + # By default, we do stuff on the botuser the irc user is bound to + butarget = botuser + + has_for = splits[-2] == "for" + butarget = @bot.auth.get_botuser(splits[-1]) if has_for + return m.reply "you can't mess with #{butarget.username}" if butarget == @bot.auth.botowner && botuser != butarget + splits.slice!(-2,2) if has_for + + bools = [:autologin, :"login-by-mask"] + can_set = [:password] + bools + can_reset = can_set + [:netmasks] + + case cmd.to_sym + + when :show, :list + return "you can't see the properties of #{butarget.username}" if botuser != butarget and !botuser.permit?("auth::show::other") + + case splits[1] + when nil, "all" + props = can_reset + when "password" + return m.reply "you can't ask for someone else's password" if botuser != butarget and !botuser.permit?("auth::show::other::password") + return m.reply "c'mon, you can't be asking me seriously to tell you the password in public!" if m.public? + return m.reply "the password for #{butarget.username} is #{butarget.password}" + else + props = splits[1..-1] + end + + str = [] + + props.each { |arg| + k = arg.to_sym + next if k == :password + case k + when *bools + str << "can" + str.last << "not" unless ask_bool_prop(butarget, k) + str.last << " #{k}" + when :netmasks + str << "knows " + if butarget.netmasks.empty? + str.last << "no netmasks" + else + str.last << butarget.netmasks.join(", ") + end + end + } + return m.reply "#{butarget.username} #{str.join('; ')}" + + when :enable, :disable + return m.reply "you can't change the default user" if butarget == @bot.auth.everyone and !botuser.permit?("auth::edit::default") + return m.reply "you can't edit #{butarget.username}" if butarget != botuser and !botuser.permit?("auth::edit::other") + + return m.reply need_args(cmd) unless splits[1] + things = [] + splits[1..-1].each { |a| + arg = a.to_sym + if bools.include?(arg) + set_bool_prop(butarget, arg, cmd.to_sym == :enable) + else + m.reply not_args(cmd, *bools) + end + things << a + } + return auth_manage_user(m, {:data => ["show"] + things }) + + when :set + return m.reply "you can't change the default user" if butarget == @bot.auth.everyone and !botuser.permit?("auth::edit::default") + return m.reply "you can't edit #{butarget.username}" if butarget != botuser and !botuser.permit?("auth::edit::other") + + return need_args(cmd) unless splits[1] + things = [] + # TODO + #return not_args(cmd, *can_set) unless bools.include?(arg) + + when :reset + return m.reply "you can't change the default user" if butarget == @bot.auth.everyone and !botuser.permit?("auth::edit::default") + return m.reply "you can't edit #{butarget.username}" if butarget != botuser and !botuser.permit?("auth::edit::other") + + return need_args(cmd) unless splits[1] + things = [] + # TODO + else + m.reply "sorry, I don't know how to #{m.message}" + end + end + end auth = AuthModule.new -auth.map "permissions set *args for :user", - :action => 'auth_set', - :auth_path => ':edit::set:' +auth.map "user *data", + :action => 'auth_manage_user' -auth.map "permissions reset *args for :user", - :action => 'auth_reset', - :auth_path => ':edit::reset:' +auth.default_auth("user", true) + +auth.map "whoami", + :action => 'auth_whoami', + :auth_path => '!*!' auth.map "login :botuser :password", :action => 'auth_login', @@ -158,8 +327,19 @@ auth.map "login :botuser :password", auth.map "login :botuser", :action => 'auth_login', - :defaults => { :password => nil }, :auth_path => '!login!' +auth.map "login", + :action => 'auth_autologin', + :auth_path => '!login!' + +auth.map "permissions set *args for :user", + :action => 'auth_set', + :auth_path => ':edit::set:' + +auth.map "permissions reset *args for :user", + :action => 'auth_reset', + :auth_path => ':edit::reset:' + auth.default_auth('*', false) -- cgit v1.2.3