Class: Isaac::IRC

Inherits:
Object
  • Object
show all
Defined in:
lib/isaac/bot.rb

Instance Method Summary collapse

Constructor Details

#initialize(bot, config) ⇒ IRC

Returns a new instance of IRC.



121
122
123
124
125
# File 'lib/isaac/bot.rb', line 121

def initialize(bot, config)
  @bot, @config = bot, config
  @transfered = 0
  @registration = []
end

Instance Method Details

#connectObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/isaac/bot.rb', line 127

def connect
  tcp_socket = TCPSocket.open(@config.server, @config.port)

  if @config.ssl
    begin
      require 'openssl'
    rescue ::LoadError
      raise(RuntimeError,"unable to require 'openssl'",caller)
    end

    ssl_context = OpenSSL::SSL::SSLContext.new
    ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE

    unless @config.environment == :test
      puts "Using SSL with #{@config.server}:#{@config.port}"
    end

    @socket = OpenSSL::SSL::SSLSocket.new(tcp_socket, ssl_context)
    @socket.sync = true
    @socket.connect
  else
    @socket = tcp_socket
  end

  @queue = Queue.new(@socket, @bot.config.server)
  message "PASS #{@config.password}" if @config.password
  message "NICK #{@config.nick}"
  message "USER #{@config.nick} 0 * :#{@config.realname}"
  @queue.lock

  while line = @socket.gets
    parse line
  end
end

#message(msg) ⇒ Object



196
197
198
# File 'lib/isaac/bot.rb', line 196

def message(msg)
  @queue << msg
end

#parse(input) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/isaac/bot.rb', line 162

def parse(input)
  puts "<< #{input}" if @bot.config.verbose
  msg = Message.new(input)

  if ("001".."004").include? msg.command
    @registration << msg.command
    if registered?
      @queue.unlock
      @bot.dispatch(:connect)
    end
  elsif msg.command == "PRIVMSG"
    if msg.params.last == "\001VERSION\001"
      message "NOTICE #{msg.nick} :\001VERSION #{@bot.config.version}\001"
    end

    type = msg.channel? ? :channel : :private
    @bot.dispatch(type, msg)
  elsif msg.error?
    @bot.dispatch(:error, msg)
  elsif msg.command == "PING"
    @queue.unlock
    message "PONG :#{msg.params.first}"
  elsif msg.command == "PONG"
    @queue.unlock
  else
    event = msg.command.downcase.to_sym
    @bot.dispatch(event, msg)
  end
end

#registered?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/isaac/bot.rb', line 192

def registered?
  (("001".."004").to_a - @registration).empty?
end