X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Falias.rb;h=67f481dc6c02ff1df275128f054220afb1e1e352;hb=3d85327c1dd8c2dd1b7068f04c9076d7fe991988;hp=2fba9324f937d58b80a40565e0c27333983e971a;hpb=98dde0dfe318d01e108acaed2413cc842a907b12;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/alias.rb b/data/rbot/plugins/alias.rb index 2fba9324..67f481dc 100644 --- a/data/rbot/plugins/alias.rb +++ b/data/rbot/plugins/alias.rb @@ -12,10 +12,10 @@ # exisitng rbot command and that is run. # # == Example Session -# < alias googlerbot *terms => google site:linuxbrit.co.uk/rbot/ +# < alias googlerbot *terms => google site:ruby-rbot.org # > 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 @@ -36,31 +36,31 @@ 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" - # hash of alias => command entries - @aliases = if File.exist?(@data_file) - YAML.load_file(@data_file) - else - Hash.new - end - @aliases.each_pair do |a, c| - begin - add_alias(a, c) - rescue AliasDefinitionError - warning _("Invalid alias entry %{alias} : %{command} in %{filename}: %{reason}") % - {:alias => a, :command => c, :filename => @data_file, :reason => $1} + @aliases = @registry[:aliases] + unless @aliases + # attempt to load aliases from data file yaml + filename = File.join(datafile, 'aliases.yaml') + if File.exists? filename + begin + @aliases = {} + YAML.load_file(filename).each_pair do |a, c| + add_alias(a, c) + end + rescue + warning _("Data file is not found or corrupt, reinitializing data") + @aliases = {} + end + else + @aliases = {} end - end - end + end + end - def save - Dir.mkdir(@data_path) unless File.exist?(@data_path) - File.open(@data_file, 'w') {|f| f.write @aliases.to_yaml} + def save + @registry[:aliases] = @aliases + @registry.flush end def cmd_add(m, params) @@ -104,12 +104,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) @@ -118,28 +123,21 @@ 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 - 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[$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}) - # 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 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) @@ -149,9 +147,17 @@ 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/ ') + _('"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:ruby-rbot.org ') when 'commands' _('alias list => list defined aliases | alias whatis => show definition of the alias | alias remove => remove defined alias | see the "create" topic about adding aliases') end