X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Falias.rb;h=89be0020434b81e1593a40d573aa77db6a80592e;hb=682e7f1708b9534385467b3d707433cb256b6405;hp=bafd85288cda35e721261e5cd7fea6bfcf5ae07d;hpb=3527629fa971d1407d152fecad13fb346c9286da;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/alias.rb b/data/rbot/plugins/alias.rb index bafd8528..89be0020 100644 --- a/data/rbot/plugins/alias.rb +++ b/data/rbot/plugins/alias.rb @@ -36,12 +36,10 @@ class AliasPlugin < Plugin class AliasDefinitionError < ArgumentError end - MAX_RECURSION_DEPTH = 10 - def initialize super - @data_path = "#{@bot.botclass}/alias/" - @data_file = "#{@data_path}/aliases.yaml" + @data_path = datafile + @data_file = File.join(@data_path, 'aliases.yaml') # hash of alias => command entries data = nil aliases = if File.exist?(@data_file) && @@ -60,10 +58,10 @@ class AliasPlugin < Plugin warning _("Invalid alias entry %{alias} : %{command} in %{filename}: %{reason}") % {:alias => a, :command => c, :filename => @data_file, :reason => $1} end - end - end + end + end - def save + def save FileUtils.mkdir_p(@data_path) Utils.safe_save(@data_file) {|f| f.write @aliases.to_yaml} end @@ -112,7 +110,7 @@ class AliasPlugin < Plugin 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 @@ -130,25 +128,16 @@ class AliasPlugin < Plugin 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 - current_depth = if m.respond_to?(:depth) then m.depth else 0 end - if current_depth > MAX_RECURSION_DEPTH - m.reply _('The alias seems to have caused infinite recursion. Please examine your alias definitions') - return - end 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}) - # tag incremented depth on the message - class << new_msg - self - end.send(:define_method, :depth) {current_depth + 1} - - @bot.plugins.privmsg(new_msg) + begin + # create a fake message containing the intended command + @bot.plugins.privmsg fake_message(command.gsub(/<(\w+)>/) {|arg| params[:"#{$1}"].to_s}, :from => m, :delegate => false) + rescue RecurseTooDeep + m.reply _('The alias seems to have caused infinite recursion. Please examine your alias definitions') + return + end else 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})