diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-14 04:36:52 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-14 04:36:52 -0700 |
commit | e8908d71464ab36937ed4a95e8a32c4b3cb4726c (patch) | |
tree | 2e883fc962bf776edb7df4b5a1083cb92a30d8c7 /dinobot.rb | |
parent | d920b5654e30ed10caf56bab6198fbd99fecd12d (diff) | |
download | dinobot-e8908d71464ab36937ed4a95e8a32c4b3cb4726c.tar.gz dinobot-e8908d71464ab36937ed4a95e8a32c4b3cb4726c.tar.xz |
Minor cleanup and improvements; remove run loop.
Diffstat (limited to 'dinobot.rb')
-rw-r--r-- | dinobot.rb | 45 |
1 files changed, 23 insertions, 22 deletions
@@ -2,6 +2,9 @@ require 'socket' module Dinobot class Bot + attr_accessor :trigger + attr_reader :server, :port, :nick, :pass, :modules, :channels + def initialize(server, port, nick, pass=nil, &block) @server = server @port = port @@ -34,23 +37,21 @@ module Dinobot 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 "== Connecting to #{@server}:#{@port}." + connect + + while str = @socket.gets + str.chomp! + puts "<< #{str.inspect}" - puts "== Disconnected." - @socket.close + begin + parse_line(str) + rescue + end end + + puts '== Disconnected.' + @socket.close end def parse_line(str) @@ -59,14 +60,14 @@ module Dinobot if str =~ /(\S+) PRIVMSG (\S+) :(.*)/ user, channel, message = str.scan(/(\S+) PRIVMSG (\S+) :(.*)/).first - if message =~ /^#{Regexp.escape(@trigger)}/ - message.sub!(@trigger, '') + return unless message =~ /^#{Regexp.escape(@trigger)}/ + + message.sub!(@trigger, '') - mod = message.split.first.downcase.intern + mod = message.split.first.downcase.intern - if @modules.has_key?(mod) - exec_commands(@modules[mod].call(user, channel, message)) - end + if @modules.has_key?(mod) + exec_commands(@modules[mod].call(user, channel, message)) end end end @@ -85,7 +86,7 @@ module Dinobot def out(str) return unless connected? - puts ">> " + str.inspect + puts ">> #{str.inspect}" @socket.puts str end |