]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/alias.rb
* replace references to old website to references to new site
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / alias.rb
index 0a6e58691f0fe622296370a1ea705d7d645cfd74..bafd85288cda35e721261e5cd7fea6bfcf5ae07d 100644 (file)
 # exisitng rbot command and that is run.
 #
 # == Example Session
-#   < alias googlerbot *terms => google site:linuxbrit.co.uk/rbot/ <terms>
+#   < alias googlerbot *terms => google site:ruby-rbot.org <terms>
 #   > okay
 #   < googlerbot plugins
-#   > Results for site:linuxbrit.co.uk/rbot/ plugins: ....
+#   > Results for site:ruby-rbot.org plugins: ....
 #
 # == Security
 # By default, only the owner can define and remove aliases, while everyone else can
@@ -64,7 +64,7 @@ class AliasPlugin < Plugin
   end 
 
   def save 
-    FileUtils.mkdir_p(@data_path) unless FileTest.directory?(@data_path)
+    FileUtils.mkdir_p(@data_path)
     Utils.safe_save(@data_file) {|f| f.write @aliases.to_yaml}
   end
 
@@ -109,12 +109,17 @@ 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}.to_set ==
-      text.grep(/(?:^|\s)[:*](\w+)(?:\s|$)/) {$1}.to_set or
+    command.scan(/<(\w+)>/).flatten.to_set ==
+      text.split.grep(/\A[:*](\w+)\Z/) {$1}.to_set or
       raise AliasDefinitionError.new(_('The arguments in alias must match the substitutions in command, and vice versa'))
     
+    begin
+      map text, :action => :"alias_handle<#{text}>", :auth_path => 'run'
+    rescue
+      raise AliasDefinitionError.new(_('Error mapping %{text} as command: %{error}') %
+                                     {:text => text, :error => $!})
+    end
     @aliases[text] = command
-    map text, :action => :"alias_handle<#{text}>", :auth_path => 'run'
   end
 
   def respond_to?(name, include_private=false)
@@ -123,6 +128,7 @@ class AliasPlugin < Plugin
 
   def method_missing(name, *args, &block)
     if name.to_s =~ /\Aalias_handle<(.+)>\Z/
+      text = $1
       m, params = args
       # messages created by alias handler will have a depth method, which returns the 
       # depth of "recursion" caused by the message
@@ -132,11 +138,11 @@ class AliasPlugin < Plugin
         return
       end
 
-      command = @aliases[$1]
+      command = @aliases[text]
       if command
         # create a fake message containing the intended command
         new_msg = PrivMessage.new(@bot, m.server, m.server.user(m.source), m.target,
-                                    command.gsub(/<(\w+)>/) {|arg| params[:"#{$1}"].to_s})
+                  command.gsub(/<(\w+)>/) {|arg| params[:"#{$1}"].to_s})
         # tag incremented depth on the message
         class << new_msg
           self
@@ -144,7 +150,8 @@ class AliasPlugin < Plugin
 
         @bot.plugins.privmsg(new_msg)
       else
-        m.reply _("Error handling the alias, the command is not defined")
+        m.reply(_("Error handling the alias, The alias %{text} is not defined or has beeen removed. I will stop responding to it after rescan,") %
+                {:text => text})
       end
     else
       super(name, *args, &block)
@@ -164,7 +171,7 @@ class AliasPlugin < Plugin
         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>')
+      _('"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:ruby-rbot.org <terms>')
     when 'commands'
       _('alias list => list defined aliases | alias whatis <alias> => show definition of the alias | alias remove <alias> => remove defined alias | see the "create" topic about adding aliases')
     end