Class: Vetinari::Bot

Inherits:
Object
  • Object
show all
Includes:
Celluloid::IO, IRC
Defined in:
lib/vetinari/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IRC

#away, #back, #join, #message, #quit, #register, #rename

Constructor Details

#initialize(&block) ⇒ Bot

Returns a new instance of Bot.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vetinari/bot.rb', line 10

def initialize(&block)
  @actor     = Actor.current
  @config    = Configuration.new(&block)
  @callbacks = CallbackContainer.new(Actor.current)
  @users     = UserContainer.new
  @channels  = ChannelContainer.new
  @socket    = nil
  @connected = false
  @user      = nil

  setup_channel_and_user_tracking
  setup_default_callbacks
  setup_dcc
end

Instance Attribute Details

#callbacksObject (readonly)

Returns the value of attribute callbacks.



5
6
7
# File 'lib/vetinari/bot.rb', line 5

def callbacks
  @callbacks
end

#channelsObject (readonly)

Returns the value of attribute channels.



5
6
7
# File 'lib/vetinari/bot.rb', line 5

def channels
  @channels
end

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/vetinari/bot.rb', line 5

def config
  @config
end

#server_managerObject (readonly)

Returns the value of attribute server_manager.



5
6
7
# File 'lib/vetinari/bot.rb', line 5

def server_manager
  @server_manager
end

#userObject (readonly)

Returns the value of attribute user.



5
6
7
# File 'lib/vetinari/bot.rb', line 5

def user
  @user
end

#usersObject (readonly)

Returns the value of attribute users.



5
6
7
# File 'lib/vetinari/bot.rb', line 5

def users
  @users
end

Instance Method Details

#connectObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vetinari/bot.rb', line 32

def connect
  @config.loggers.info '-- Starting Vetinari'
  @socket = TCPSocket.open(@config.server, @config.port)
  # port, ip = Socket.unpack_sockaddr_in(@socket.to_io.getpeername)
  # @config.internal_port = port
  # @config.internal_ip   = ip
  register

  while message = @socket.gets do
    parse message
  end

  disconnected
end

#connected?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/vetinari/bot.rb', line 55

def connected?
  @connected ? true : false
end

#dont_die(actor) ⇒ Object



96
97
98
# File 'lib/vetinari/bot.rb', line 96

def dont_die(actor)
  # nothing!
end

#finalizeObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vetinari/bot.rb', line 81

def finalize
  if connected?
    quit
    @socket.close rescue nil
  end

  @callbacks.terminate_callbacks
  @users.terminate
  @channels.terminate

  links.each do |actor|
    actor.terminate if actor.alive?
  end
end

#inspectObject



59
60
61
62
# File 'lib/vetinari/bot.rb', line 59

def inspect
  nick = @user.nick rescue @config.nick
  "#<Bot nick=#{nick}>"
end

#on(event, options = {}, &block) ⇒ Object



25
26
27
# File 'lib/vetinari/bot.rb', line 25

def on(event, options = {}, &block)
  @callbacks.add(event, options, block)
end

#parse(message) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/vetinari/bot.rb', line 64

def parse(message)
  message.chomp!

  if message =~ /^PING \S+$/
    if @config.hide_ping_pongs
      raw message.sub(/PING/, 'PONG'), false
    else
      @config.loggers.info "<< #{message}"
      raw message.sub(/PING/, 'PONG')
    end
  else
    @config.loggers.info "<< #{message}"
    env = MessageParser.parse(message, @config.isupport['CHANTYPES'])
    @callbacks.call(env)
  end
end

#raw(message, logging = true) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/vetinari/bot.rb', line 47

def raw(message, logging = true)
  if @socket
    @socket.puts("#{message}\r\n")
    @config.loggers.info ">> #{message}" if logging
    message
  end
end