Class: IrcCat::Bot

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

Overview

The IRC bot

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constructor = H.new) ⇒ Bot

Initialize the bot with default values



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/irc_cat/bot.rb', line 12

def initialize(constructor = H.new)
  @realname = "irc_cat #{IrcCat::VERSION::STRING} - http://irccat.rubyforge.org/"
  @refresh_rate = 10
  
  constructor.each do |key, val|
    if val.is_a?(String)
      instance_eval("@#{key} = '#{val}'")
    elsif val.is_a?(Array) 
      instance_eval("@#{key} = []")
      val.each do |v| instance_eval("@#{key}.push('#{v}')") end
    end
  end
  puts "Connecting to IRC #{@host}:#{@port} #{@channel}" 
end

Instance Attribute Details

#channelObject

Returns the value of attribute channel.



9
10
11
# File 'lib/irc_cat/bot.rb', line 9

def channel
  @channel
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/irc_cat/bot.rb', line 9

def host
  @host
end

#nickObject

Returns the value of attribute nick.



9
10
11
# File 'lib/irc_cat/bot.rb', line 9

def nick
  @nick
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/irc_cat/bot.rb', line 9

def port
  @port
end

#socketObject

Returns the value of attribute socket.



9
10
11
# File 'lib/irc_cat/bot.rb', line 9

def socket
  @socket
end

Instance Method Details

#announce(msg) ⇒ Object

Announces states



60
61
62
63
64
# File 'lib/irc_cat/bot.rb', line 60

def announce(msg)
  @channels.each do |channel|
    say(channel, msg)
  end
end

#runObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/irc_cat/bot.rb', line 27

def run
  @socket = TCPSocket.open(@host, @port)    
  
  
  
  threads=[]
  
  threads << Thread.new {
    begin
      while line = @socket.gets do
        # Remove all formatting
        line.gsub!(/[\x02\x1f\x16\x0f]/,'')
        # Remove CTCP ASCII
        line.gsub!(/\001/,'')
        # Send to event handler
        handle line
        # Handle Pings from Server
        sendln "PONG #{$1}" if /^PING\s(.*)/ =~ line                  
      end        
    rescue EOFError
      err 'Server Reset Connection'     
    rescue Exception => e
      err e
    end
  }
  threads << Thread.new {
  }
  threads.each { |th| th.join }
  
  
end

#say(chan, msg) ⇒ Object

Say something funkeh



67
68
69
# File 'lib/irc_cat/bot.rb', line 67

def say(chan,msg)
  sendln "PRIVMSG #{chan} :#{msg}"
end