]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
added alias plugin
authorYaohan Chen <yaohan.chen@gmail.com>
Sat, 21 Jul 2007 00:12:47 +0000 (00:12 +0000)
committerYaohan Chen <yaohan.chen@gmail.com>
Sat, 21 Jul 2007 00:12:47 +0000 (00:12 +0000)
data/rbot/plugins/alias.rb [new file with mode: 0644]
po/en_US/rbot.po
po/ja/rbot.po
po/rbot.pot
po/zh_CN/rbot.po

diff --git a/data/rbot/plugins/alias.rb b/data/rbot/plugins/alias.rb
new file mode 100644 (file)
index 0000000..ca708b5
--- /dev/null
@@ -0,0 +1,178 @@
+#-- vim:sw=2:et
+#++
+#
+# :title: Alias plugin for rbot
+#
+# Author:: Yaohan Chen <yaohan.chen@gmail.com>
+# Copyright:: (C) 2007 Yaohan Chen
+# License:: GPLv2
+#
+# This plugin allows defining aliases for rbot commands. Aliases are like normal rbot
+# commands and can take parameters. When called, they will be substituted into an
+# exisitng rbot command and that is run.
+#
+# == Example Session
+#   < alias googlerbot *terms => google site:linuxbrit.co.uk/rbot/ <terms>
+#   > okay
+#   < googlerbot plugins
+#   > Results for site:linuxbrit.co.uk/rbot/ plugins: ....
+#
+# == Security
+# By default, only the owner can define and remove aliases, while everyone else can
+# use and view them. When a command is executed with an alias, it's mapped normally with
+# the alias user appearing to attempt to execute the command. Therefore it should be not
+# possible to use aliases to circumvent permission sets. Care should be taken when
+# defining aliases, due to these concerns:
+# * Defined aliases can potentially override other plugins' maps, if this plugin is
+#   loaded first
+# * Aliases can cause infinite recursion of aliases and/or commands. The plugin attempts
+#   to detect and stop this, but a few recursive calls can still cause spamming
+
+require 'yaml'
+
+class AliasPlugin < Plugin
+  # an exception raised when loading or getting input of invalid alias definitions
+  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}
+      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}
+  end
+
+  def cmd_add(m, params)
+    begin
+      add_alias(params[:text].to_s, params[:command].to_s)
+      m.okay
+    rescue AliasDefinitionError
+      m.reply _('The definition you provided is invalid: %{reason}') % {:reason => $!}
+    end
+  end
+
+  def cmd_remove(m, params)
+    text = params[:text].to_s
+    if @aliases.has_key?(text)
+      @aliases.delete(text)
+      # TODO when rbot supports it, remove the mapping corresponding to the alias
+      m.okay
+    else
+      m.reply _('No such alias is defined')
+    end
+  end
+
+  def cmd_list(m, params)
+    if @aliases.empty?
+      m.reply _('No aliases defined')
+    else
+      m.reply @aliases.map {|a, c| "#{a} => #{c}"}.join(' | ')
+    end
+  end
+
+  def cmd_whatis(m, params)
+    text = params[:text].to_s
+    if @aliases.has_key?(text)
+      m.reply _('Alias of %{command}') % {:command => @aliases[text]}
+    else
+      m.reply _('No such alias is defined')
+    end
+  end
+
+  def add_alias(text, command)
+    # 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'))
+    
+    @aliases[text] = command
+    map text, :action => :"alias_handle<#{text}>", :auth_path => 'run'
+  end
+
+  def respond_to?(name, include_private=false)
+    name.to_s =~ /\Aalias_handle<.+>\Z/ || super
+  end
+
+  def method_missing(name, *args, &block)
+    if name.to_s =~ /\Aalias_handle<(.+)>\Z/
+      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]
+      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)
+      else
+        m.reply _("Error handling the alias, the command is not defined")
+      end
+    else
+      super(name, *args, &block)
+    end
+  end
+
+  def help(plugin, topic='')
+    case topic
+    when ''
+      _('Create and use aliases for commands. Topics: create, commands')
+    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'
+      _('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
+  end
+end
+
+plugin = AliasPlugin.new
+plugin.default_auth('edit', false)
+plugin.default_auth('run', true)
+plugin.default_auth('list', true)
+
+plugin.map 'alias list',
+           :action => :cmd_list,
+           :auth_path => 'view'
+plugin.map 'alias whatis *text',
+           :action => :cmd_whatis,
+           :auth_path => 'view'
+plugin.map 'alias remove *text',
+           :action => :cmd_remove,
+           :auth_path => 'edit'
+plugin.map 'alias *text => *command',
+           :action => :cmd_add,
+           :auth_path => 'edit'
+
+
+
index 353cc479ae5dadec7e9031b3d49281aab4bf9706..70a0576d8a7310796e5b97205c5b41c5c2aa5a08 100644 (file)
@@ -6,7 +6,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: rbot\n"
-"POT-Creation-Date: 2007-07-14 03:25-0400\n"
+"POT-Creation-Date: 2007-07-20 20:10-0400\n"
 "PO-Revision-Date: 2007-07-14 00:06-0400\n"
 "Last-Translator: Yaohan Chen <yaohan.chen@gmail.com>\n"
 "Language-Team: English\n"
@@ -945,6 +945,61 @@ msgstr ""
 "dictclient databases => List databases; dictclient strategies => List "
 "strategies"
 
+#: data/rbot/plugins/alias.rb:54
+msgid "Invalid alias entry %{alias} : %{command} in %{filename}: %{reason}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:70
+msgid "The definition you provided is invalid: %{reason}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:81 data/rbot/plugins/alias.rb:98
+msgid "No such alias is defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:87
+msgid "No aliases defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:96
+msgid "Alias of %{command}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:107
+msgid ""
+"Not all substitutions in command text have matching arguments in alias text"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:124
+msgid ""
+"The alias seems to have caused infinite recursion. Please examine your alias "
+"definitions"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:140
+msgid "Error handling the alias, the command is not defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:150
+msgid "Create and use aliases for commands. Topics: create, commands"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:152
+msgid ""
+"\"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>"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:154
+msgid ""
+"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"
+msgstr ""
+
 #: data/rbot/plugins/games/shiritori.rb:203
 msgid "%{current_player}, it's your turn. %{previous_word} -> %{current_word}"
 msgstr "%{current_player}, it's your turn. %{previous_word} -> %{current_word}"
index 33b5b65c0e56c1e6c57de06895dddc476fa2291b..3f0337a82ac6cc20052821e591a1cfa2ee403376 100644 (file)
@@ -6,7 +6,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: rbot\n"
-"POT-Creation-Date: 2007-07-14 03:25-0400\n"
+"POT-Creation-Date: 2007-07-20 20:10-0400\n"
 "PO-Revision-Date: 2007-07-09 01:36-0400\n"
 "Last-Translator: Yaohan Chen <yaohan.chen@gmail.com>\n"
 "Language-Team: Japanese\n"
@@ -829,6 +829,61 @@ msgid ""
 "strategies"
 msgstr ""
 
+#: data/rbot/plugins/alias.rb:54
+msgid "Invalid alias entry %{alias} : %{command} in %{filename}: %{reason}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:70
+msgid "The definition you provided is invalid: %{reason}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:81 data/rbot/plugins/alias.rb:98
+msgid "No such alias is defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:87
+msgid "No aliases defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:96
+msgid "Alias of %{command}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:107
+msgid ""
+"Not all substitutions in command text have matching arguments in alias text"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:124
+msgid ""
+"The alias seems to have caused infinite recursion. Please examine your alias "
+"definitions"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:140
+msgid "Error handling the alias, the command is not defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:150
+msgid "Create and use aliases for commands. Topics: create, commands"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:152
+msgid ""
+"\"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>"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:154
+msgid ""
+"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"
+msgstr ""
+
 #: data/rbot/plugins/games/shiritori.rb:203
 msgid "%{current_player}, it's your turn. %{previous_word} -> %{current_word}"
 msgstr "%{current_player} さんの番です。%{previous_word} -> %{current_word}"
@@ -889,7 +944,8 @@ msgstr ""
 #: data/rbot/plugins/games/shiritori.rb:306
 msgid ""
 "It's impossible to continue the chain from %{word}. Start with another word."
-msgstr "「%{word}」ではしりごりを始められないのです。他の言葉で始めてください。"
+msgstr ""
+"「%{word}」ではしりごりを始められないのです。他の言葉で始めてください。"
 
 #: data/rbot/plugins/games/shiritori.rb:327
 msgid ""
@@ -941,13 +997,15 @@ msgid ""
 "%{bold}BINGO!%{bold} the word was %{underline}%{word}%{underline}. Congrats, "
 "%{bold}%{player}%{bold}!"
 msgstr ""
-"%{bold}ビンゴー!%{bold} 秘密の言葉は %{underline}%{word}%{underline} でした。"
-"めでとうございます、%{bold}%{player}%{bold} さん!"
+"%{bold}ビンゴー!%{bold} 秘密の言葉は %{underline}%{word}%{underline} でした。"
+"ã\81\8aã\82\81ã\81§ã\81¨ã\81\86ã\81\94ã\81\96ã\81\84ã\81¾ã\81\99ã\80\81%{bold}%{player}%{bold} ã\81\95ã\82\93ï¼\81"
 
 #: data/rbot/plugins/games/azgame.rb:160
 msgid ""
 "The game was won after %{tries} tries. Scores for this game:    %{scores}"
-msgstr "このゲームが%{tries}つのトライの後勝ちました。スコアは:    %{scores} でした。"
+msgstr ""
+"このゲームが%{tries}つのトライの後勝ちました。スコアは:    %{scores} でし"
+"た。"
 
 #: data/rbot/plugins/games/azgame.rb:163
 msgid "%{word} is not in the range %{bold}%{range}%{bold}"
@@ -955,7 +1013,8 @@ msgstr "%{word}は%{bold}%{range}%{bold}のレンジ外です。"
 
 #: data/rbot/plugins/games/azgame.rb:165
 msgid "%{word} doesn't exist or is not acceptable for the game"
-msgstr "「%{word}」って言葉は存在しませんか、このゲームに入れることができません。"
+msgstr ""
+"「%{word}」って言葉は存在しませんか、このゲームに入れることができません。"
 
 #: data/rbot/plugins/games/azgame.rb:169
 msgid "close, but no cigar. New range: %{bold}%{range}%{bold}"
@@ -1080,7 +1139,8 @@ msgstr "az cancel => いま遊んでいるゲームを終了します"
 
 #: data/rbot/plugins/games/azgame.rb:534
 msgid "az check <word> => checks <word> against current game"
-msgstr "az check <word> => いま遊んでいるゲームのルールで、<word> をチェックします"
+msgstr ""
+"az check <word> => いま遊んでいるゲームのルールで、<word> をチェックします"
 
 #: data/rbot/plugins/games/azgame.rb:536
 msgid ""
index 78a96679bcf28754f9af85b3daaa0f9c85f26986..78d25cc8cfd4a8508df1f96d4f2865e7d9092844 100644 (file)
@@ -7,7 +7,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: rbot\n"
-"POT-Creation-Date: 2007-07-14 03:25-0400\n"
+"POT-Creation-Date: 2007-07-20 20:10-0400\n"
 "PO-Revision-Date: 2007-07-09 01:24-0400\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -839,6 +839,61 @@ msgid ""
 "strategies"
 msgstr ""
 
+#: data/rbot/plugins/alias.rb:54
+msgid "Invalid alias entry %{alias} : %{command} in %{filename}: %{reason}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:70
+msgid "The definition you provided is invalid: %{reason}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:81 data/rbot/plugins/alias.rb:98
+msgid "No such alias is defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:87
+msgid "No aliases defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:96
+msgid "Alias of %{command}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:107
+msgid ""
+"Not all substitutions in command text have matching arguments in alias text"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:124
+msgid ""
+"The alias seems to have caused infinite recursion. Please examine your alias "
+"definitions"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:140
+msgid "Error handling the alias, the command is not defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:150
+msgid "Create and use aliases for commands. Topics: create, commands"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:152
+msgid ""
+"\"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>"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:154
+msgid ""
+"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"
+msgstr ""
+
 #: data/rbot/plugins/games/shiritori.rb:203
 msgid "%{current_player}, it's your turn. %{previous_word} -> %{current_word}"
 msgstr ""
index 3a370dccbc176bc4fd90d6fbd426932fe18a8f4d..45254bcfda015c2489924b08730aaa2df5c218a0 100644 (file)
@@ -6,7 +6,7 @@
 msgid ""
 msgstr ""
 "Project-Id-Version: rbot\n"
-"POT-Creation-Date: 2007-07-14 03:25-0400\n"
+"POT-Creation-Date: 2007-07-20 20:10-0400\n"
 "PO-Revision-Date: 2007-07-09 01:39-0400\n"
 "Last-Translator: Yaohan Chen <yaohan.chen@gmail.com>\n"
 "Language-Team: Chinese\n"
@@ -838,6 +838,61 @@ msgid ""
 "strategies"
 msgstr ""
 
+#: data/rbot/plugins/alias.rb:54
+msgid "Invalid alias entry %{alias} : %{command} in %{filename}: %{reason}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:70
+msgid "The definition you provided is invalid: %{reason}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:81 data/rbot/plugins/alias.rb:98
+msgid "No such alias is defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:87
+msgid "No aliases defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:96
+msgid "Alias of %{command}"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:107
+msgid ""
+"Not all substitutions in command text have matching arguments in alias text"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:124
+msgid ""
+"The alias seems to have caused infinite recursion. Please examine your alias "
+"definitions"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:140
+msgid "Error handling the alias, the command is not defined"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:150
+msgid "Create and use aliases for commands. Topics: create, commands"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:152
+msgid ""
+"\"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>"
+msgstr ""
+
+#: data/rbot/plugins/alias.rb:154
+msgid ""
+"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"
+msgstr ""
+
 #: data/rbot/plugins/games/shiritori.rb:203
 msgid "%{current_player}, it's your turn. %{previous_word} -> %{current_word}"
 msgstr ""