Class: IrcCat::Bot

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, nick, nick_pass, &block) ⇒ Bot

Returns a new instance of Bot.



10
11
12
13
14
15
16
17
18
19
# File 'lib/irc_cat/bot.rb', line 10

def initialize(host, port, nick, nick_pass, &block)
  @host, @port, @nick, @nick_pass = host, port, nick, nick_pass
  @online, @connect_block = false, block

  @realname = "irccat v#{VERSION::STRING}"
  @refresh_rate = 10
  @channels = {}

  puts "Connecting to IRC #{@host}:#{@port}"
end

Class Method Details

.run(host, port, nick, nick_pass = nil, &block) ⇒ Object



6
7
8
# File 'lib/irc_cat/bot.rb', line 6

def self.run(host, port, nick, nick_pass = nil, &block)
  new(host, port, nick, nick_pass, &block).run
end

Instance Method Details

#announce(msg) ⇒ Object

Announces states



63
64
65
66
67
# File 'lib/irc_cat/bot.rb', line 63

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

#auto_rejoin(channel) ⇒ Object



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

def auto_rejoin(channel)
  join_channel(channel, @channels[channel])
end

#err(exception) ⇒ Object



143
144
145
146
# File 'lib/irc_cat/bot.rb', line 143

def err(exception)
  $stderr.puts "ERROR: #{exception}"
  $stderr.puts "TRACE: #{exception.backtrace}"
end

#handle(line) ⇒ Object

Handle a received message



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/irc_cat/bot.rb', line 96

def handle(line)
  puts "Got: #{line}"
  case line
  when /^:.+\s376/
    puts "We're online"
    @online = true
    @connect_block.call(self) if @connect_block
  when /^:.+KICK (#[^\s]+)/
    auto_rejoin($1)
  end
end

#join_channel(channel, key) ⇒ Object

Automatic events



110
111
112
113
114
115
116
117
118
# File 'lib/irc_cat/bot.rb', line 110

def join_channel(channel, key)
  @channels[channel] = key
  $stdout.flush
  if key
    sendln "JOIN #{channel} #{key}"
  else
    sendln "JOIN #{channel}"
  end
end

#log(str) ⇒ Object

Logging methods



139
140
141
# File 'lib/irc_cat/bot.rb', line 139

def log(str)
  $stdout.puts "DEBUG: #{str}"
end

#loginObject



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/irc_cat/bot.rb', line 124

def 
  begin
    sendln "NICK #{@nick}"
    sendln "USER irc_cat . . :#{@realname}"
    if @nick_pass
      puts "logging in to NickServ"
      sendln "PRIVMSG NICKSERV :identify #{@nick_pass}"
    end
  rescue Exception => e
    err e
  end
end

#online?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/irc_cat/bot.rb', line 21

def online?
  @online
end

#run(&block) ⇒ Object



25
26
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
58
59
60
# File 'lib/irc_cat/bot.rb', line 25

def run(&block)
  @socket = TCPSocket.open(@host, @port)
  

  trap(:INT) {
    puts "Bye bye."
    sexit('God^WConsole killed me')
    sleep 1
    @socket.close
    exit
  }

  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



70
71
72
# File 'lib/irc_cat/bot.rb', line 70

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

#sendln(cmd) ⇒ Object

Sends a message to the server



85
86
87
88
89
90
91
92
# File 'lib/irc_cat/bot.rb', line 85

def sendln(cmd)
  puts "Send: #{cmd}"
  if cmd.size <= 510
    @socket.write("#{cmd}\r\n")
    STDOUT.flush
  else
  end
end

#sexit(message = 'quit') ⇒ Object

Send EXIT



79
80
81
# File 'lib/irc_cat/bot.rb', line 79

def sexit(message = 'quit')
  sendln "QUIT :#{message}"
end

#topic(chan, msg) ⇒ Object



74
75
76
# File 'lib/irc_cat/bot.rb', line 74

def topic(chan, msg)
  sendln "TOPIC #{chan} :#{msg}"
end