Class: Isaac::IRC

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (IRC) initialize(bot, config)

A new instance of IRC



124
125
126
127
128
# File 'lib/isaac/bot.rb', line 124

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

Class Method Details

+ (Object) connect(bot, config)



120
121
122
# File 'lib/isaac/bot.rb', line 120

def self.connect(bot, config)
  EventMachine.connect(config.server, config.port, self, bot, config)
end

Instance Method Details

- (Object) message(msg)



183
184
185
# File 'lib/isaac/bot.rb', line 183

def message(msg)
  @queue << msg
end

- (Object) parse(input)



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/isaac/bot.rb', line 149

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

- (Object) post_init



130
131
132
133
134
135
136
137
# File 'lib/isaac/bot.rb', line 130

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

- (Object) receive_data(data)



139
140
141
142
143
144
145
146
147
# File 'lib/isaac/bot.rb', line 139

def receive_data(data)
  @data << data
  loop do
    line, rest = @data.split("\n", 2)
    return unless rest
    @data = rest
    parse line
  end
end

- (Boolean) registered?

Returns:

  • (Boolean)


179
180
181
# File 'lib/isaac/bot.rb', line 179

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