summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/alias.rb
blob: 1496755d099dd8db3d51005afb14940d737d2a8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#-- 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'
require 'set'

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
    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 = Hash.new
    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 
    FileUtils.mkdir_p(@data_path)
    Utils.safe_save(@data_file) {|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}.to_set ==
      text.grep(/(?:^|\s)[:*](\w+)(?:\s|$)/) {$1}.to_set or
      raise AliasDefinitionError.new(_('The arguments in alias must match the substitutions in command, and vice versa'))
    
    @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 rm *text',
           :action => :cmd_remove,
           :auth_path => 'edit'
plugin.map 'alias *text => *command',
           :action => :cmd_add,
           :auth_path => 'edit'