]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/alias.rb
weather plugin: refactor HTML cleanup code
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / alias.rb
index ca708b5290435ca3eac833d89e60df3b9e11c5de..289ef1ad69513bf48b5f04c082d30de17210e0f9 100644 (file)
@@ -29,6 +29,7 @@
 #   to detect and stop this, but a few recursive calls can still cause spamming
 
 require 'yaml'
+require 'set'
 
 class AliasPlugin < Plugin
   # an exception raised when loading or getting input of invalid alias definitions
@@ -42,12 +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)
-                 YAML.load_file(@data_file)
-               else
-                 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
@@ -58,8 +64,8 @@ class AliasPlugin < Plugin
   end 
 
   def save 
-    Dir.mkdir(@data_path) unless File.exist?(@data_path)
-    File.open(@data_file, 'w') {|f| f.write @aliases.to_yaml}
+    FileUtils.mkdir_p(@data_path)
+    Utils.safe_save(@data_file) {|f| f.write @aliases.to_yaml}
   end
 
   def cmd_add(m, params)
@@ -103,8 +109,9 @@ class AliasPlugin < Plugin
     # each alias is implemented by adding a message map, whose handler creates a message
     # containing the aliased command
 
-    command.grep(/<(\w+)>/) {$1}.all? {|s| text =~ /(?:^|\s)[:*]#{s}(?:\s|$)/ } or
-      raise AliasDefinitionError.new(_('Not all substitutions in command text have matching arguments in alias text'))
+    command.grep(/<(\w+)>/) {$1}.to_set ==
+      text.grep(/(?:^|\s)[:*](\w+)(?:\s|$)/) {$1}.to_set or
+      raise AliasDefinitionError.new(_('The arguments in alias must match the substitutions in command, and vice versa'))
     
     @aliases[text] = command
     map text, :action => :"alias_handle<#{text}>", :auth_path => 'run'
@@ -147,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'
@@ -170,6 +185,9 @@ plugin.map 'alias whatis *text',
 plugin.map 'alias remove *text',
            :action => :cmd_remove,
            :auth_path => 'edit'
+plugin.map 'alias rm *text',
+           :action => :cmd_remove,
+           :auth_path => 'edit'
 plugin.map 'alias *text => *command',
            :action => :cmd_add,
            :auth_path => 'edit'