Class: Isaac::IRC

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

Instance Method Summary collapse

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

#connectObject



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)
  message "PASS #{@config.password}" if @config.password
  message "NICK #{@config.nick}"
  message "USER #{@config.nick} 0 * :#{@config.realname}"
  @lock = true

  while line = @socket.gets
    parse line
  end
end

#continue_queueObject



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 message(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/
    message "NOTICE #{$1} :\001VERSION #{@bot.config.version}\001"
  when /^PING (\S+)/
    @transfered, @lock = 0, false
    message "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

Returns:

  • (Boolean)


132
133
134
# File 'lib/isaac.rb', line 132

def registered?
  ([1,2,3,4] - @registration).empty?
end