@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
+ 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.each_pair do |a, c|
+ @aliases = Hash.new
+ aliases.each_pair do |a, c|
begin
add_alias(a, c)
rescue AliasDefinitionError
end
def save
- Dir.mkdir(@data_path) unless File.exist?(@data_path)
- File.open(@data_file, 'w') {|f| f.write @aliases.to_yaml}
+ FileUtils.mkdir_p(@data_path)
+ Utils.safe_save(@data_file) {|f| f.write @aliases.to_yaml}
end
def cmd_add(m, params)
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 <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'