From 607d7ae62d59bbc422da2211286fca96caa4f5e7 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 21 Apr 2014 06:23:47 -0700 Subject: Add config system for non-module data. --- core/config.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ core/store.rb | 2 +- dinobot.rb | 6 +++--- 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 core/config.rb diff --git a/core/config.rb b/core/config.rb new file mode 100644 index 0000000..fec2e18 --- /dev/null +++ b/core/config.rb @@ -0,0 +1,43 @@ +require_relative 'store' + +module Dinobot + module Core + class Config + attr_accessor :data + + @@instance = nil + @@mutex = Mutex.new + + def initialize + @store = Dinobot::Core::Store.new('config') + @data = @store.data[:data] + + if @data.nil? + @data = Hash.new + + @data[:trigger] = Hash.new + @data[:trigger][:global] = '!' + + save + end + end + + def save + @store.data[:data] = @data + @store.save + end + + class << self + def instance + return @@instance if @@instance + + @@mutex.synchronize do + @@instance ||= new + end + end + end + + private_class_method :allocate, :new + end + end +end diff --git a/core/store.rb b/core/store.rb index 856acee..ba49733 100644 --- a/core/store.rb +++ b/core/store.rb @@ -11,7 +11,7 @@ module Dinobot read - @data ||= {} + @data ||= Hash.new end def read diff --git a/dinobot.rb b/dinobot.rb index a00517f..9b33894 100644 --- a/dinobot.rb +++ b/dinobot.rb @@ -1,5 +1,6 @@ require 'timeout' +require_relative 'core/config' require_relative 'core/irc' require_relative 'core/logger' @@ -14,9 +15,8 @@ module Dinobot @nick = nick @pass = pass - @trigger = '!' - @irc = Dinobot::Core::IRC.new(@server, @port, @nick, @pass) + @config = Dinobot::Core::Config.instance @logger = Dinobot::Core::Logger.instance @modules = Hash.new @@ -115,7 +115,7 @@ module Dinobot if str =~ /(\S+) PRIVMSG (\S+) :(.*)/ user, channel, message = str.scan(/(\S+) PRIVMSG (\S+) :(.*)/).first - return unless message.sub!(/^#{Regexp.escape(@trigger)}/, '') + return unless message.sub!(/^#{Regexp.escape(@config.data[:trigger][:global])}/, '') if methods = exec_command(user, channel, message) ensure_valid_methods(methods) -- cgit v1.2.3-70-g09d2