]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/alias.rb
translator.rb: use "help <translator>" instead of "help translator <translator>"...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / alias.rb
index 218754a6eefc887e3a2830f62b17239422578ad1..289ef1ad69513bf48b5f04c082d30de17210e0f9 100644 (file)
@@ -43,15 +43,17 @@ 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) &&
-                  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
+    data = nil
+    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|
+    @aliases = Hash.new
+    aliases.each_pair do |a, c|
       begin
         add_alias(a, c)
       rescue AliasDefinitionError
@@ -62,7 +64,7 @@ class AliasPlugin < Plugin
   end 
 
   def save 
-    Dir.mkdir(@data_path) unless File.exist?(@data_path)
+    FileUtils.mkdir_p(@data_path)
     Utils.safe_save(@data_file) {|f| f.write @aliases.to_yaml}
   end
 
@@ -152,7 +154,15 @@ class AliasPlugin < Plugin
   def help(plugin, topic='')
     case topic
     when ''
-      _('Create and use aliases for commands. Topics: create, commands')
+      if plugin == 'alias' # FIXME find out plugin name programmatically
+        _('Create and use aliases for commands. Topics: create, commands')
+      else
+        # show definition of all aliases whose first word is the parameter of the help
+        # command
+        @aliases.keys.select {|a| a[/\A(\w+)/, 1] == plugin}.map do |a|
+          "#{a} => #{@aliases[a]}"
+        end.join ' | '
+      end
     when 'create'
       _('"alias <text> => <command>" => add text as an alias of command. Text can contain placeholders marked with : or * for :words and *multiword arguments. The command can contain placeholders enclosed with < > which will be substituded with argument values. For example: alias googlerbot *terms => google site:linuxbrit.co.uk/rbot/ <terms>')
     when 'commands'