Class: IRC::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/wires/ircbot.rb,
lib/wires/ircbot/handlers.rb,
lib/wires/ircbot/overrides.rb

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs, &block) ⇒ Bot

Returns a new instance of Bot.



17
18
19
20
21
22
# File 'lib/wires/ircbot.rb', line 17

def initialize(**kwargs, &block)
  @rescue_exceptions = true
  kwargs.each_pair { |k,v| instance_variable_set("@#{k}",v) }
  instance_eval &block
  init_handlers
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



66
67
68
69
70
# File 'lib/wires/ircbot.rb', line 66

def method_missing(meth, *args)
  @socket and meth!=:to_a ?
    send_command(meth, *args) :
    super
end

Instance Method Details

#connect!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/wires/ircbot.rb', line 36

def connect!
  @socket = TCPSocket.open(@server, @port)
  
  nick @nick
  user @nick, 0, '*', (@realname or @nick)
  
  while (m = @socket.gets.match /^((?::(.+?) )?(\w+) (.*?)\r\n)$/)
    begin
      fire_and_wait [message:[
        *(m[4].split ' '),
        string:  m[1].rstrip,
        prefix:  m[2],
        command: m[3]]]
    rescue Exception => ex;
      raise unless @rescue_exceptions
      puts ex.backtrace
      puts "#{ex.message} (#{ex.class})"
    end
  end
end

#default_handlersObject



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/wires/ircbot/handlers.rb', line 4

def default_handlers
  
  on :ping do |e|
    pong e.target
  end
  
  on :end_of_motd do |event|
    @channels.each { |c| join c }
  end
  
end

#init_handlersObject



25
26
27
28
29
30
31
32
33
# File 'lib/wires/ircbot.rb', line 25

def init_handlers
  on :message do |m| 
    m = IRC.parse_message m
    begin fire_and_wait m 
    rescue ArgumentError
    end
  end
  default_handlers
end

#privmsg(target, *msg) ⇒ Object



5
6
7
8
# File 'lib/wires/ircbot/overrides.rb', line 5

def privmsg(target, *msg)
  return if target==@nick # Don't try to send to self
  send_command :privmsg, "#{target} :#{msg.join ' '}"
end

#send_command(cmd, *args) ⇒ Object



58
59
60
61
62
63
# File 'lib/wires/ircbot.rb', line 58

def send_command(cmd, *args)
  args.map! { |x| x.to_s }
  cmd = [cmd.to_s.upcase, *args].join(' ')
  @socket.puts(cmd+"\r")
  puts "\033[1m<< #{cmd}\033[22m"
end