X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Falias.rb;h=a587736ccff862eaeb21873b532ac35ed2c7e952;hb=24bb60775741d3731400f1e430ef6bf3a2e1b933;hp=218754a6eefc887e3a2830f62b17239422578ad1;hpb=609ecba1b5baa74ea657c6a5e010c3c1ed0dfb6c;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/alias.rb b/data/rbot/plugins/alias.rb index 218754a6..a587736c 100644 --- a/data/rbot/plugins/alias.rb +++ b/data/rbot/plugins/alias.rb @@ -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 @@ -107,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) @@ -121,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 @@ -130,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 @@ -142,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) @@ -152,7 +161,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 => " => 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/ ') when 'commands'