From 609ecba1b5baa74ea657c6a5e010c3c1ed0dfb6c Mon Sep 17 00:00:00 2001
From: Yaohan Chen <yaohan.chen@gmail.com>
Date: Tue, 7 Aug 2007 20:42:22 +0000
Subject: * use Util.safe_save in alias plugin * handle corrupt data file in
 alias plugin

---
 data/rbot/plugins/alias.rb | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

(limited to 'data/rbot')

diff --git a/data/rbot/plugins/alias.rb b/data/rbot/plugins/alias.rb
index 2fba9324..218754a6 100644
--- a/data/rbot/plugins/alias.rb
+++ b/data/rbot/plugins/alias.rb
@@ -43,9 +43,12 @@ class AliasPlugin < Plugin
     @data_path = "#{@bot.botclass}/alias/"
     @data_file = "#{@data_path}/aliases.yaml"
     # hash of alias => command entries
-    @aliases = if File.exist?(@data_file)
-                 YAML.load_file(@data_file)
+    @aliases = if File.exist?(@data_file) &&
+                  data = YAML.load_file(@data_file) &&
+                  data.respond_to?(:each_pair)
+                 data
                else
+                 warning _("Data file is not found or corrupt, reinitializing data")
                  Hash.new
                end
     @aliases.each_pair do |a, c|
@@ -60,7 +63,7 @@ class AliasPlugin < Plugin
 
   def save 
     Dir.mkdir(@data_path) unless File.exist?(@data_path)
-    File.open(@data_file, 'w') {|f| f.write @aliases.to_yaml}
+    Utils.safe_save(@data_file) {|f| f.write @aliases.to_yaml}
   end
 
   def cmd_add(m, params)
-- 
cgit v1.2.3