4 # :title: Alias plugin for rbot
6 # Author:: Yaohan Chen <yaohan.chen@gmail.com>
7 # Copyright:: (C) 2007 Yaohan Chen
10 # This plugin allows defining aliases for rbot commands. Aliases are like normal rbot
11 # commands and can take parameters. When called, they will be substituted into an
12 # exisitng rbot command and that is run.
15 # < alias googlerbot *terms => google site:linuxbrit.co.uk/rbot/ <terms>
17 # < googlerbot plugins
18 # > Results for site:linuxbrit.co.uk/rbot/ plugins: ....
21 # By default, only the owner can define and remove aliases, while everyone else can
22 # use and view them. When a command is executed with an alias, it's mapped normally with
23 # the alias user appearing to attempt to execute the command. Therefore it should be not
24 # possible to use aliases to circumvent permission sets. Care should be taken when
25 # defining aliases, due to these concerns:
26 # * Defined aliases can potentially override other plugins' maps, if this plugin is
28 # * Aliases can cause infinite recursion of aliases and/or commands. The plugin attempts
29 # to detect and stop this, but a few recursive calls can still cause spamming
34 class AliasPlugin < Plugin
35 # an exception raised when loading or getting input of invalid alias definitions
36 class AliasDefinitionError < ArgumentError
39 MAX_RECURSION_DEPTH = 10
43 @data_path = "#{@bot.botclass}/alias/"
44 @data_file = "#{@data_path}/aliases.yaml"
45 # hash of alias => command entries
46 @aliases = if File.exist?(@data_file)
47 YAML.load_file(@data_file)
51 @aliases.each_pair do |a, c|
54 rescue AliasDefinitionError
55 warning _("Invalid alias entry %{alias} : %{command} in %{filename}: %{reason}") %
56 {:alias => a, :command => c, :filename => @data_file, :reason => $1}
62 Dir.mkdir(@data_path) unless File.exist?(@data_path)
63 File.open(@data_file, 'w') {|f| f.write @aliases.to_yaml}
66 def cmd_add(m, params)
68 add_alias(params[:text].to_s, params[:command].to_s)
70 rescue AliasDefinitionError
71 m.reply _('The definition you provided is invalid: %{reason}') % {:reason => $!}
75 def cmd_remove(m, params)
76 text = params[:text].to_s
77 if @aliases.has_key?(text)
79 # TODO when rbot supports it, remove the mapping corresponding to the alias
82 m.reply _('No such alias is defined')
86 def cmd_list(m, params)
88 m.reply _('No aliases defined')
90 m.reply @aliases.map {|a, c| "#{a} => #{c}"}.join(' | ')
94 def cmd_whatis(m, params)
95 text = params[:text].to_s
96 if @aliases.has_key?(text)
97 m.reply _('Alias of %{command}') % {:command => @aliases[text]}
99 m.reply _('No such alias is defined')
103 def add_alias(text, command)
104 # each alias is implemented by adding a message map, whose handler creates a message
105 # containing the aliased command
107 command.grep(/<(\w+)>/) {$1}.to_set ==
108 text.grep(/(?:^|\s)[:*](\w+)(?:\s|$)/) {$1}.to_set or
109 raise AliasDefinitionError.new(_('The arguments in alias must match the substitutions in command, and vice versa'))
111 @aliases[text] = command
112 map text, :action => :"alias_handle<#{text}>", :auth_path => 'run'
115 def respond_to?(name, include_private=false)
116 name.to_s =~ /\Aalias_handle<.+>\Z/ || super
119 def method_missing(name, *args, &block)
120 if name.to_s =~ /\Aalias_handle<(.+)>\Z/
122 # messages created by alias handler will have a depth method, which returns the
123 # depth of "recursion" caused by the message
124 current_depth = if m.respond_to?(:depth) then m.depth else 0 end
125 if current_depth > MAX_RECURSION_DEPTH
126 m.reply _('The alias seems to have caused infinite recursion. Please examine your alias definitions')
130 command = @aliases[$1]
132 # create a fake message containing the intended command
133 new_msg = PrivMessage.new(@bot, m.server, m.server.user(m.source), m.target,
134 command.gsub(/<(\w+)>/) {|arg| params[:"#{$1}"].to_s})
135 # tag incremented depth on the message
138 end.send(:define_method, :depth) {current_depth + 1}
140 @bot.plugins.privmsg(new_msg)
142 m.reply _("Error handling the alias, the command is not defined")
145 super(name, *args, &block)
149 def help(plugin, topic='')
152 _('Create and use aliases for commands. Topics: create, commands')
154 _('"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>')
156 _('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')
161 plugin = AliasPlugin.new
162 plugin.default_auth('edit', false)
163 plugin.default_auth('run', true)
164 plugin.default_auth('list', true)
166 plugin.map 'alias list',
167 :action => :cmd_list,
169 plugin.map 'alias whatis *text',
170 :action => :cmd_whatis,
172 plugin.map 'alias remove *text',
173 :action => :cmd_remove,
175 plugin.map 'alias rm *text',
176 :action => :cmd_remove,
178 plugin.map 'alias *text => *command',