]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Users can now be imported from exported data files. Existing users are overwritten
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 6 Aug 2006 18:13:28 +0000 (18:13 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Sun, 6 Aug 2006 18:13:28 +0000 (18:13 +0000)
lib/rbot/core/auth.rb

index 2d8b00d0fbc7728bc5aa56e5c876e8e06064abe7..b38dd49aad99c27e5854069b6cba51b2890bee73 100644 (file)
@@ -572,7 +572,7 @@ class AuthModule < CoreBotModule
 \r
     has_to = what[-2] == "to"\r
     if has_to\r
-      exportfile = what[-1]\r
+      exportfile = "#{@bot.botclass}/#{what[-1]}"\r
       what.slice!(-2,2)\r
     end\r
 \r
@@ -624,6 +624,7 @@ class AuthModule < CoreBotModule
 \r
     m.reply "exporting to #{exportfile} ..."\r
     begin\r
+      # m.reply yaml_hash.inspect\r
       File.open(exportfile, "w") do |file|\r
         file.puts YAML::dump(yaml_hash)\r
       end\r
@@ -635,6 +636,84 @@ class AuthModule < CoreBotModule
     m.reply "done"\r
   end\r
 \r
+  def auth_import(m, params)\r
+\r
+    importfile = "#{@bot.botclass}/new-auth.users"\r
+\r
+    what = params[:things]\r
+\r
+    has_from = what[-2] == "from"\r
+    if has_from\r
+      importfile = "#{@bot.botclass}/#{what[-1]}"\r
+      what.slice!(-2,2)\r
+    end\r
+\r
+    what.delete("all")\r
+\r
+    m.reply "reading #{importfile} ..."\r
+    begin\r
+      yaml_hash = YAML::load_file(importfile)\r
+    rescue => e\r
+      m.reply "failed to import from: #{e}"\r
+      debug e.backtrace.dup.unshift(e.inspect).join("\n")\r
+      return\r
+    end\r
+\r
+    # m.reply yaml_hash.inspect\r
+\r
+    m.reply "selecting data to import ..."\r
+\r
+    if what.empty?\r
+      we_want = yaml_hash\r
+    else\r
+      we_want = yaml_hash.delete_if { |key, val|\r
+        not what.include?(key)\r
+      }\r
+    end\r
+\r
+    m.reply "parsing data from import ..."\r
+\r
+    buser_hash = {}\r
+\r
+    begin\r
+      yaml_hash.each { |k, val|\r
+        buser_hash[k] = { :username => k }\r
+        val.each { |kk, v|\r
+          case kk\r
+          when :netmasks\r
+            buser_hash[k][kk] = []\r
+            v.each { |nm|\r
+              buser_hash[k][kk] << nm[:fullform].to_irc_netmask(:casemap => nm[:casemap].to_irc_casemap).to_irc_netmask(:server => @bot.server)\r
+            }\r
+          else\r
+            buser_hash[k][kk] = v\r
+          end\r
+        }\r
+      }\r
+    rescue => e\r
+      m.reply "failed to parse data: #{e}"\r
+      debug e.backtrace.dup.unshift(e.inspect).join("\n")\r
+      return\r
+    end\r
+\r
+    # m.reply buser_hash.inspect\r
+\r
+    org_buser_array = @bot.auth.save_array\r
+    org_buser_hash = org_buser_array.inject({}) { |h, u|\r
+      h[u[:username]] = u\r
+      h\r
+    }\r
+\r
+    # TODO we may want to do a(n optional) key-by-key merge\r
+    #\r
+    org_buser_hash.merge!(buser_hash)\r
+    new_buser_array = org_buser_hash.values\r
+    @bot.auth.load_array(new_buser_array, true)\r
+    @bot.auth.set_changed\r
+\r
+    m.reply "done"\r
+  end\r
+\r
 end\r
 \r
 auth = AuthModule.new\r
@@ -644,9 +723,9 @@ auth.map "user export *things",
   :defaults => { :things => ['all'] },\r
   :auth_path => ':manage:fedex:'\r
 \r
-# auth.map "user import",\r
- :action => 'auth_import',\r
- :auth_path => ':manage:fedex:'\r
+auth.map "user import *things",\r
+ :action => 'auth_import',\r
+ :auth_path => ':manage:fedex:'\r
 \r
 auth.map "user create :name :password",\r
   :action => 'auth_create_user',\r