- @@items[key].set_string(value)
- rescue ArgumentError => e
- m.reply "failed to set #{key}: #{e.message}"
- return
- end
- if @@items[key].requires_restart
- m.reply "this config change will take effect on the next restart"
- else
- m.okay
- end
- end
-
- def handle_help(m, params)
- topic = params[:topic]
- case topic
- when false
- m.reply "config module - bot configuration. usage: list, desc, get, set, unset"
- when "list"
- m.reply "config list => list configuration modules, config list <module> => list configuration keys for module <module>"
- when "get"
- m.reply "config get <key> => get configuration value for key <key>"
- when "unset"
- m.reply "reset key <key> to the default"
- when "set"
- m.reply "config set <key> <value> => set configuration value for key <key> to <value>"
- when "desc"
- m.reply "config desc <key> => describe what key <key> configures"
- else
- m.reply "no help for config #{topic}"
- end
- end
- def usage(m,params)
- m.reply "incorrect usage, try '#{@bot.nick}: help config'"
- end
-
- # bot:: parent bot class
- # create a new config hash from #{botclass}/conf.rbot
- def initialize(bot)
- @@bot = bot
-
- # respond to config messages, to provide runtime configuration
- # management
- # messages will be:
- # get
- # set
- # unset
- # desc
- # and for arrays:
- # add TODO
- # remove TODO
- @handler = MessageMapper.new(self)
- @handler.map 'config list :module', :action => 'handle_list',
- :defaults => {:module => false}
- @handler.map 'config get :key', :action => 'handle_get'
- @handler.map 'config desc :key', :action => 'handle_desc'
- @handler.map 'config describe :key', :action => 'handle_desc'
- @handler.map 'config set :key *value', :action => 'handle_set'
- @handler.map 'config unset :key', :action => 'handle_unset'
- @handler.map 'config help :topic', :action => 'handle_help',
- :defaults => {:topic => false}
- @handler.map 'help config :topic', :action => 'handle_help',
- :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
+ debug "Writing new conf.yaml ..."
+ File.open("#{@bot.botclass}/conf.yaml.new", "w") do |file|
+ savehash = {}
+ @config.each { |key, val|
+ savehash[key.to_s] = val
+ }
+ file.puts savehash.to_yaml
+ end
+ debug "Officializing conf.yaml ..."
+ File.rename("#{@bot.botclass}/conf.yaml.new",
+ "#{@bot.botclass}/conf.yaml")
+ @changed = false
+ rescue => e
+ error "failed to write configuration file conf.yaml! #{$!}"
+ error "#{e.class}: #{e}"
+ error e.backtrace.join("\n")