X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fcore%2Fauth.rb;h=eee500456c241e135fb919018c572d98bc359cd8;hb=12906a51f698aebe1c9a5e19b15555ebbbcc4368;hp=2d8b00d0fbc7728bc5aa56e5c876e8e06064abe7;hpb=8d566aa8ef469c09f147ec1532e79b3c8cacbdca;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/core/auth.rb b/lib/rbot/core/auth.rb index 2d8b00d0..eee50045 100644 --- a/lib/rbot/core/auth.rb +++ b/lib/rbot/core/auth.rb @@ -1,6 +1,11 @@ #-- vim:sw=2:et #++ - +# +# :title: rbot auth management from IRC +# +# Author:: Giuseppe "Oblomov" Bilotta +# Copyright:: (C) 2006,2007 Giuseppe Bilotta +# License:: GPL v2 class AuthModule < CoreBotModule @@ -89,7 +94,7 @@ class AuthModule < CoreBotModule splits = params[:args] has_for = splits[-2] == "for" - return usage unless has_for + return usage(m) unless has_for begin user = @bot.auth.get_botuser(splits[-1].sub(/^all$/,"everyone")) @@ -140,7 +145,13 @@ class AuthModule < CoreBotModule def auth_view_perm(m, params) begin - user = @bot.auth.get_botuser(params[:user].sub(/^all$/,"everyone")) + if params[:user].nil? + user = get_botusername_for(m.source) + return m.reply("you are owner, you can do anything") if user == @bot.auth.botwoner + else + user = @bot.auth.get_botuser(params[:user].sub(/^all$/,"everyone")) + return m.reply("owner can do anything") if user.username == "owner" + end rescue return m.reply("couldn't find botuser #{params[:user]}") end @@ -182,6 +193,11 @@ class AuthModule < CoreBotModule "welcome, #{get_botusername_for(user)}" end + def auth_auth(m, params) + params[:botuser] = 'owner' + auth_login(m,params) + end + def auth_login(m, params) begin case @bot.auth.login(m.source, params[:botuser], params[:password]) @@ -221,38 +237,48 @@ class AuthModule < CoreBotModule m.reply rep end - def help(plugin, topic="") - case topic - when /^login/ + def help(cmd, topic="") + case cmd + when "login" return "login [] []: logs in to the bot as botuser with password . When using the full form, you must contact the bot in private. 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/ + 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)" - when /^user show/ - return "user show : shows info about the user; can be any of autologin, login-by-mask, netmasks" - when /^user (en|dis)able/ - return "user enable|disable : turns on or off (autologin, login-by-mask)" - when /^user set/ - return "user set password : sets the user password to ; passwords can only contain upper and lowercase letters and numbers, and must be at least 4 characters long" - when /^user (add|rm)/ - return "user add|rm netmask : adds/removes netmask from the list of netmasks known to the botuser you're linked to" - when /^user reset/ - return "user reset : resets to the default values. can be +netmasks+ (the list will be emptied), +autologin+ or +login-by-mask+ (will be reset to the default value) or +password+ (a new one will be generated and you'll be told in private)" - when /^user tell/ - return "user tell the password for : contacts in private to tell him/her the password for " - when /^user create/ - return "user create : create botuser named with password . The password can be omitted, in which case a random one will be generated. The should only contain alphanumeric characters and the underscore (_)" - when /^user list/ - return "user list : lists all the botusers" - when /^user destroy/ - return "user destroy : destroys ; this function #{Bold}must#{Bold} be called in two steps. On the first call, no password must be specified: is then queued for destruction. On the second call, you must specify the correct password for , and it will be destroyed. If you want to cancel the destruction, issue the command +user cancel destroy +" - when /^user/ - return "user show, enable|disable, add|rm netmask, set, reset, tell, create, list, destroy" + case topic + when "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 "set", "reset", "[re]set", "(re)set" + return "permissions [re]set [in ] for : sets or resets the permissions for botuser in channel (use ? to change the permissions for private addressing)" + when "view" + return "permissions view [for ]: display the permissions for user " + else + return "topics: syntax, (re)set, view" + end + when "user" + case topic + when "show" + return "user show : shows info about the user; can be any of autologin, login-by-mask, netmasks" + when /^(en|dis)able/ + return "user enable|disable : turns on or off (autologin, login-by-mask)" + when "set" + return "user set password : sets the user password to ; passwords can only contain upper and lowercase letters and numbers, and must be at least 4 characters long" + when "add", "rm" + return "user add|rm netmask : adds/removes netmask from the list of netmasks known to the botuser you're linked to" + when "reset" + return "user reset : resets to the default values. can be +netmasks+ (the list will be emptied), +autologin+ or +login-by-mask+ (will be reset to the default value) or +password+ (a new one will be generated and you'll be told in private)" + when "tell" + return "user tell the password for : contacts in private to tell him/her the password for " + when "create" + return "user create : create botuser named with password . The password can be omitted, in which case a random one will be generated. The should only contain alphanumeric characters and the underscore (_)" + when "list" + return "user list : lists all the botusers" + when "destroy" + return "user destroy : destroys ; this function #{Bold}must#{Bold} be called in two steps. On the first call, no password must be specified: is then queued for destruction. On the second call, you must specify the correct password for , and it will be destroyed. If you want to cancel the destruction, issue the command +user cancel destroy +" + else + return "user show, enable|disable, add|rm netmask, set, reset, tell, create, list, destroy" + end else - return "#{name}: login, whoami, permission syntax, permissions, user" + return "#{name}: login, whoami, permission syntax, permissions [re]set, permissions view, user" end end @@ -572,7 +598,7 @@ class AuthModule < CoreBotModule has_to = what[-2] == "to" if has_to - exportfile = what[-1] + exportfile = "#{@bot.botclass}/#{what[-1]}" what.slice!(-2,2) end @@ -624,6 +650,7 @@ class AuthModule < CoreBotModule m.reply "exporting to #{exportfile} ..." begin + # m.reply yaml_hash.inspect File.open(exportfile, "w") do |file| file.puts YAML::dump(yaml_hash) end @@ -635,6 +662,84 @@ class AuthModule < CoreBotModule m.reply "done" end + def auth_import(m, params) + + importfile = "#{@bot.botclass}/new-auth.users" + + what = params[:things] + + has_from = what[-2] == "from" + if has_from + importfile = "#{@bot.botclass}/#{what[-1]}" + what.slice!(-2,2) + end + + what.delete("all") + + m.reply "reading #{importfile} ..." + begin + yaml_hash = YAML::load_file(importfile) + rescue => e + m.reply "failed to import from: #{e}" + debug e.backtrace.dup.unshift(e.inspect).join("\n") + return + end + + # m.reply yaml_hash.inspect + + m.reply "selecting data to import ..." + + if what.empty? + we_want = yaml_hash + else + we_want = yaml_hash.delete_if { |key, val| + not what.include?(key) + } + end + + m.reply "parsing data from import ..." + + buser_hash = {} + + begin + yaml_hash.each { |k, val| + buser_hash[k] = { :username => k } + val.each { |kk, v| + case kk + when :netmasks + buser_hash[k][kk] = [] + v.each { |nm| + buser_hash[k][kk] << nm[:fullform].to_irc_netmask(:casemap => nm[:casemap].to_irc_casemap).to_irc_netmask(:server => @bot.server) + } + else + buser_hash[k][kk] = v + end + } + } + rescue => e + m.reply "failed to parse data: #{e}" + debug e.backtrace.dup.unshift(e.inspect).join("\n") + return + end + + # m.reply buser_hash.inspect + + org_buser_array = @bot.auth.save_array + org_buser_hash = org_buser_array.inject({}) { |h, u| + h[u[:username]] = u + h + } + + # TODO we may want to do a(n optional) key-by-key merge + # + org_buser_hash.merge!(buser_hash) + new_buser_array = org_buser_hash.values + @bot.auth.load_array(new_buser_array, true) + @bot.auth.set_changed + + m.reply "done" + end + end auth = AuthModule.new @@ -644,30 +749,25 @@ auth.map "user export *things", :defaults => { :things => ['all'] }, :auth_path => ':manage:fedex:' -# auth.map "user import", -# :action => 'auth_import', -# :auth_path => ':manage:fedex:' +auth.map "user import *things", + :action => 'auth_import', + :auth_path => ':manage:fedex:' auth.map "user create :name :password", :action => 'auth_create_user', :defaults => {:password => nil}, :auth_path => ':manage:' -auth.map "user cancel destroy :name :password", +auth.map "user [cancel] destroy :name :password", :action => 'auth_destroy_user', :defaults => { :password => nil }, :auth_path => ':manage::destroy:' -auth.map "user destroy :name :password", - :action => 'auth_destroy_user', - :defaults => { :password => nil }, - :auth_path => ':manage:' - -auth.map "user copy :source :dest", +auth.map "user copy :source [to] :dest", :action => 'auth_copy_ren_user', :auth_path => ':manage:' -auth.map "user rename :source :dest", +auth.map "user rename :source [to] :dest", :action => 'auth_copy_ren_user', :auth_path => ':manage:' @@ -691,6 +791,11 @@ auth.map "whoami", :action => 'auth_whoami', :auth_path => '!*!' +auth.map "auth :password", + :action => 'auth_auth', + :public => false, + :auth_path => '!login!' + auth.map "login :botuser :password", :action => 'auth_login', :public => false, @@ -713,7 +818,7 @@ auth.map "permissions reset *args", :action => 'auth_edit_perm', :auth_path => ':edit::reset:' -auth.map "permissions view for :user", +auth.map "permissions view [for :user]", :action => 'auth_view_perm', :auth_path => '::'