Class: Isaac::IRC
- Inherits:
-
Object
- Object
- Isaac::IRC
- Defined in:
- lib/isaac.rb
Instance Method Summary collapse
- #connect ⇒ Object
- #continue_queue ⇒ Object
-
#initialize(bot, config) ⇒ IRC
constructor
A new instance of IRC.
- #message(msg) ⇒ Object
- #parse(input) ⇒ Object
- #registered? ⇒ Boolean
Constructor Details
#initialize(bot, config) ⇒ IRC
Returns a new instance of IRC.
84 85 86 87 88 89 90 |
# File 'lib/isaac.rb', line 84 def initialize(bot, config) @bot, @config = bot, config @transfered = 0 @registration = [] @lock = false @queue = [] end |
Instance Method Details
#connect ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/isaac.rb', line 92 def connect @socket = TCPSocket.open(@config.server, @config.port) "PASS #{@config.password}" if @config.password "NICK #{@config.nick}" "USER #{@config.nick} 0 * :#{@config.realname}" @lock = true while line = @socket.gets parse line end end |
#continue_queue ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/isaac.rb', line 141 def continue_queue # <= 1472 allows for \n while !@lock && msg = @queue.shift if (@transfered + msg.size) < 1472 @socket.puts msg puts ">> #{msg}" if @bot.config.verbose @transfered += msg.size + 1 else @queue.unshift(msg) @lock = true @socket.puts "PING :#{@bot.config.server}" break end end end |
#message(msg) ⇒ Object
136 137 138 139 |
# File 'lib/isaac.rb', line 136 def (msg) @queue << msg continue_queue end |
#parse(input) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/isaac.rb', line 104 def parse(input) puts "<< #{input}" if @bot.config.verbose case input when /^:\S+ 00([1-4])/ @registration << $1.to_i if registered? @lock = false @bot.dispatch(:connect) continue_queue end when /^:(\S+)!\S+ PRIVMSG \S+ :?\001VERSION\001/ "NOTICE #{$1} :\001VERSION #{@bot.config.version}\001" when /^PING (\S+)/ @transfered, @lock = 0, false "PONG #{$1}" when /^:(\S+)!(\S+) PRIVMSG (\S+) :?(.*)/ env = { :nick => $1, :userhost => $2, :channel => $3, :message => $4 } type = env[:channel].match(/^#/) ? :channel : :private @bot.dispatch(type, env) when /^:\S+ ([4-5]\d\d) \S+ (\S+)/ env = {:error => $1.to_i, :message => $1, :nick => $2, :channel => $2} @bot.dispatch(:error, env) when /^:\S+ PONG/ @transfered, @lock = 0, false continue_queue end end |
#registered? ⇒ Boolean
132 133 134 |
# File 'lib/isaac.rb', line 132 def registered? ([1,2,3,4] - @registration).empty? end |