X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Fconfig.rb;h=027b99023443c1b870a9e432343400e7590d5a07;hb=e0ae5e3a02ef7c85e9af8c4f476c72f149ab8b32;hp=834b5a988dac35b899a52898f0a651a6da767049;hpb=5940baa7fb81196925d692d4e4fe6d6cab73d1ed;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/config.rb b/lib/rbot/config.rb index 834b5a98..027b9902 100644 --- a/lib/rbot/config.rb +++ b/lib/rbot/config.rb @@ -3,6 +3,14 @@ module Irc require 'yaml' require 'rbot/messagemapper' + unless YAML.respond_to?(:load_file) + def YAML.load_file( filepath ) + File.open( filepath ) do |f| + YAML::load( f ) + end + end + end + class BotConfigValue # allow the definition order to be preserved so that sorting by # definition order is possible. The BotConfigWizard does this to allow @@ -120,14 +128,21 @@ module Irc super @values = params[:values] end + def values + if @values.instance_of?(Proc) + return @values.call(BotConfig.bot) + else + return @values + end + end def parse(string) - unless @values.include?(string) - raise ArgumentError, "invalid value #{string}, allowed values are: " + @values.join(", ") + unless values.include?(string) + raise ArgumentError, "invalid value #{string}, allowed values are: " + values.join(", ") end string end def desc - "#{@desc} [valid values are: " + @values.join(", ") + "]" + "#{@desc} [valid values are: " + values.join(", ") + "]" end end @@ -296,20 +311,30 @@ module Irc :defaults => {:topic => false} if(File.exist?("#{@@bot.botclass}/conf.yaml")) - newconfig = YAML::load_file("#{@@bot.botclass}/conf.yaml") - @@config.update newconfig - else - # first-run wizard! - BotConfigWizard.new(@@bot).run - # save newly created config - save + begin + newconfig = YAML::load_file("#{@@bot.botclass}/conf.yaml") + @@config.update newconfig + return + rescue + $stderr.puts "failed to read conf.yaml: #{$!}" + end end + # if we got here, we need to run the first-run wizard + BotConfigWizard.new(@@bot).run + # save newly created config + save end # write current configuration to #{botclass}/conf.rbot def save - File.open("#{@@bot.botclass}/conf.yaml", "w") do |file| - file.puts @@config.to_yaml + begin + File.open("#{@@bot.botclass}/conf.yaml.new", "w") do |file| + file.puts @@config.to_yaml + end + File.rename("#{@@bot.botclass}/conf.yaml.new", + "#{@@bot.botclass}/conf.yaml") + rescue + $stderr.puts "failed to write configuration file conf.yaml! #{$!}" end end