aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/aliaser.rb53
-rw-r--r--core/config.rb2
-rw-r--r--core/irc.rb3
-rw-r--r--dinobot.rb34
-rw-r--r--module/admin.rb2
5 files changed, 69 insertions, 25 deletions
diff --git a/core/aliaser.rb b/core/aliaser.rb
new file mode 100644
index 0000000..b867e10
--- /dev/null
+++ b/core/aliaser.rb
@@ -0,0 +1,53 @@
+require_relative 'store'
+
+module Dinobot
+ module Core
+ class Aliaser
+ attr_accessor :data
+
+ @@instance = nil
+ @@mutex = Mutex.new
+
+ def initialize
+ @store = Dinobot::Core::Store.new('aliaser.db')
+ @data = @store.data
+ end
+
+ def add(from, to, channel=:global)
+ @data[channel] = Hash.new unless @data.key?(channel)
+ @data[channel][from] = to
+
+ save
+ end
+
+ def remove(from, channel=:global)
+ return unless data.key?(channel)
+
+ @data[channel].delete(from)
+ @data.delete(channel) if @data[channel].empty?
+
+ save
+ end
+
+ def aliases
+ @data.dup
+ end
+
+ def save
+ @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/config.rb b/core/config.rb
index 36511ed..236b3d9 100644
--- a/core/config.rb
+++ b/core/config.rb
@@ -9,7 +9,7 @@ module Dinobot
@@mutex = Mutex.new
def initialize
- @store = Dinobot::Core::Store.new('config')
+ @store = Dinobot::Core::Store.new('config.db')
@data = @store.data
if @data.empty?
diff --git a/core/irc.rb b/core/irc.rb
index fdb2ad7..d682299 100644
--- a/core/irc.rb
+++ b/core/irc.rb
@@ -17,7 +17,6 @@ module Dinobot
def connect
@logger.info "Connecting to #{@server}:#{@port}."
-
@socket = TCPSocket.new(@server, @port)
puts "PASS #{@pass}" if @pass
@@ -26,6 +25,7 @@ module Dinobot
end
def disconnect
+ @logger.info 'Disconnecting.'
@socket.close
end
@@ -44,7 +44,6 @@ module Dinobot
def puts(str)
@logger.out str.inspect
-
@socket.puts str
end
diff --git a/dinobot.rb b/dinobot.rb
index 93e3ae8..a99927e 100644
--- a/dinobot.rb
+++ b/dinobot.rb
@@ -1,5 +1,6 @@
require 'timeout'
+require_relative 'core/aliaser'
require_relative 'core/config'
require_relative 'core/irc'
require_relative 'core/logger'
@@ -8,7 +9,6 @@ require_relative 'core/store'
module Dinobot
class Bot
- attr_accessor :trigger
attr_reader :server, :port, :nick, :pass, :modules, :channels
def initialize(server, port, nick, pass=nil, &block)
@@ -17,13 +17,14 @@ module Dinobot
@nick = nick
@pass = pass
+ @modules = Hash.new
+ @channels = Array.new
+
@irc = Dinobot::Core::IRC.new(@server, @port, @nick, @pass)
+
+ @aliaser = Dinobot::Core::Aliaser.instance
@config = Dinobot::Core::Config.instance
@logger = Dinobot::Core::Logger.instance
- @aliases = Dinobot::Core::Store.new('data/aliases')
-
- @modules = Hash.new
- @channels = Array.new
instance_eval(&block) if block_given?
end
@@ -32,7 +33,7 @@ module Dinobot
@irc.connect unless @irc.connected?
@channels.each do |channel|
- @irc.join channel
+ @irc.join(channel)
end
while line = @irc.gets
@@ -40,7 +41,6 @@ module Dinobot
end
@irc.disconnect
- @logger.info 'Disconnected.'
end
def say(channel, message)
@@ -65,7 +65,6 @@ module Dinobot
def load_module(mod)
mod = mod.downcase.intern
- @logger.info "Loading module: #{mod}"
begin
load "module/#{mod}.rb"
@@ -82,10 +81,9 @@ module Dinobot
def unload_module(mod)
mod = mod.downcase.intern
- @logger.info "Unloading module: #{mod}"
begin
- raise 'module not loaded' unless @modules.has_key?(mod)
+ raise 'module not loaded' unless @modules.key?(mod)
@modules.delete(mod)
m = Dinobot::Module.send(:remove_const,
@@ -98,17 +96,11 @@ module Dinobot
end
def add_alias(from, to)
- @aliases[:global] = Hash.new unless @aliases[:global]
-
- @aliases[:global][from] = to
- @aliases.save
+ @aliaser.add(from, to)
end
def remove_alias(from)
- return unless @aliases[:global]
-
- @aliases[:global].delete(from)
- @aliases.save
+ @aliaser.remove(from)
end
private
@@ -127,7 +119,7 @@ module Dinobot
end
def process_line_thread(line)
- @irc.pong line[5..-1] if line =~ /\APING /
+ @irc.pong(line[5..-1]) if line =~ /\APING /
if line =~ /\A(\S+) PRIVMSG (\S+) :(.*)/
user, channel, message = line.scan(/\A(\S+) PRIVMSG (\S+) :(.*)/).first
@@ -146,8 +138,8 @@ module Dinobot
def exec_command(m, command)
# FIXME: Improve and add debug output.
- if @aliases[:global]
- @aliases[:global].each do |k, v|
+ if @aliaser.aliases.key?(:global)
+ @aliaser.aliases[:global].each do |k, v|
command.sub!(/\A#{Regexp.escape(k)}\b/, v)
end
end
diff --git a/module/admin.rb b/module/admin.rb
index 957be7d..9b5b64e 100644
--- a/module/admin.rb
+++ b/module/admin.rb
@@ -7,7 +7,7 @@ module Dinobot
def initialize(bot)
super
- @store = Dinobot::Core::Store.new('data/admin')
+ @store = Dinobot::Core::Store.new('data/admin.db')
@commands << :join << :part << :quit << :load << :unload
@commands << :listadmins << :listmodules << :listchannels