Class: Vbot::BotController

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

Overview

BotController

Author

Richard Davis

Copyright

Copyright 2018 Richard Davis

License

GNU Public License 3

This class establishes, maintains, and closes the connection to the IRC server.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ BotController

Initializes a VbotController object



49
50
51
52
53
54
55
56
57
58
# File 'lib/vbot/bot_controller.rb', line 49

def initialize config
  @server = config['server']
  @port = config['port']
  @nick = config['nick']
  @pass = config['pass']
  @ident = config['ident']
  @gecos = config['gecos']
  @chan = config['chan']
  start_connection
end

Instance Attribute Details

#chanObject (readonly)

The channel on the IRC server to join.



45
46
47
# File 'lib/vbot/bot_controller.rb', line 45

def chan
  @chan
end

#gecosObject (readonly)

The gecos for the bot.



43
44
45
# File 'lib/vbot/bot_controller.rb', line 43

def gecos
  @gecos
end

#identObject (readonly)

The name to identify to the server with.



41
42
43
# File 'lib/vbot/bot_controller.rb', line 41

def ident
  @ident
end

#nickObject (readonly)

The nick for the bot.



37
38
39
# File 'lib/vbot/bot_controller.rb', line 37

def nick
  @nick
end

#passObject (readonly)

The pass for the bot.



39
40
41
# File 'lib/vbot/bot_controller.rb', line 39

def pass
  @pass
end

#portObject (readonly)

The port to connect over.



35
36
37
# File 'lib/vbot/bot_controller.rb', line 35

def port
  @port
end

#serverObject (readonly)

The IRC server to connect to.



33
34
35
# File 'lib/vbot/bot_controller.rb', line 33

def server
  @server
end

Instance Method Details

#close_connectionObject

Closes the connection to the server.



92
93
94
95
# File 'lib/vbot/bot_controller.rb', line 92

def close_connection
  send_message quit_from_server
  @socket.close
end

#handle_connectionObject

Handles the connection to the server.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vbot/bot_controller.rb', line 71

def handle_connection
  begin
    until @socket.eof? do
      buffer = @socket.gets
      puts buffer
      msg = buffer.split
      response = parse_message msg
      unless response.nil?
        send_message response
        puts "#{@nick.upcase} => #{response}"
      end
    end
  rescue SocketError => e
    puts e
    close_connection
    puts "\nERROR IN CONNECTION. Terminating...\n"
  end
end

#start_connectionObject

Starts the connection to the server and provides identification.



62
63
64
65
66
67
# File 'lib/vbot/bot_controller.rb', line 62

def start_connection
  @socket = TCPSocket.open @server, @port
  send_message nick_to_server
  send_message ident_with_server
  send_message identify_with_nickserv
end