aboutsummaryrefslogtreecommitdiff
path: root/dinobot.rb
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2014-04-14 01:26:17 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2014-04-14 01:26:17 -0700
commit3446d5f2ec4263fc097d4d5c7996d88daf220693 (patch)
treef1a6398a84af9be58f821da38f95e0b2e14378a3 /dinobot.rb
downloaddinobot-3446d5f2ec4263fc097d4d5c7996d88daf220693.tar.gz
dinobot-3446d5f2ec4263fc097d4d5c7996d88daf220693.tar.xz
Initial commit.
Diffstat (limited to 'dinobot.rb')
-rw-r--r--dinobot.rb84
1 files changed, 84 insertions, 0 deletions
diff --git a/dinobot.rb b/dinobot.rb
new file mode 100644
index 0000000..f88b81b
--- /dev/null
+++ b/dinobot.rb
@@ -0,0 +1,84 @@
+require 'socket'
+
+module Dinobot
+ class Bot
+ def initialize(server, port, nick, pass=nil, &block)
+ @server = server
+ @port = port
+ @nick = nick
+ @pass = pass
+
+ @trigger = '!'
+
+ @socket = nil
+ @modules = Hash.new
+ @channels = Array.new
+
+ instance_eval(&block) if block_given?
+ end
+
+ def connect
+ @socket = TCPSocket.new(@server, @port)
+
+ out "PASS #{@pass}" if @pass
+ out "NICK #{@nick}"
+ out "USER #{@nick} 0 * :#{@nick}"
+
+ @channels.each do |channel|
+ join channel
+ end
+ end
+
+ def connected?
+ !(@socket.nil? || @socket.closed?)
+ end
+
+ def run
+ while true
+ puts "== Connecting to #{@server}:#{@port}."
+ connect
+
+ while str = @socket.gets
+ str.chomp!
+ puts "<< " + str.inspect
+
+ begin
+ parse_line(str)
+ rescue
+ end
+ end
+
+ puts "== Disconnected."
+ @socket.close
+ end
+ end
+
+ def parse_line(str)
+ out str.sub('PING', 'PONG') if str =~ /^PING /
+ end
+
+ def out(str)
+ return unless connected?
+
+ puts ">> " + str.inspect
+
+ @socket.puts str
+ end
+
+ def say(channel, message)
+ out "PRIVMSG #{channel} :#{message}"
+ end
+
+ def join(channel)
+ @channels << channel unless @channels.include?(channel)
+
+ out "JOIN #{channel}"
+ end
+
+ def part(channel)
+ @channels.delete(channel)
+
+ out "PART #{channel}"
+ end
+ end
+end