class Vbot::BotController

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.

Attributes

chan[R]

The channel on the IRC server to join.

gecos[R]

The gecos for the bot.

ident[R]

The name to identify to the server with.

nick[R]

The nick for the bot.

pass[R]

The pass for the bot.

port[R]

The port to connect over.

server[R]

The IRC server to connect to.

Public Class Methods

new(config) click to toggle source

Initializes a VbotController object

# 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

Public Instance Methods

close_connection() click to toggle source

Closes the connection to the server.

# File lib/vbot/bot_controller.rb, line 92
def close_connection
  send_message quit_from_server
  @socket.close
end
handle_connection() click to toggle source

Handles the connection to the server.

# 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_connection() click to toggle source

Starts the connection to the server and provides identification.

# 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

Private Instance Methods

get_nick_rt(raw_rt) click to toggle source

Gets the nick to reply to.

# File lib/vbot/bot_controller.rb, line 137
def get_nick_rt(raw_rt)
  last = 1
  raw_rt.each_char.with_index do |char, i|
    last = i-1 if char == '!'
  end
  raw_rt.slice(1, last)
end
handle_ping(token) click to toggle source

Handles ping message from server.

# File lib/vbot/bot_controller.rb, line 125
def handle_ping token
  ["PONG #{token}"]
end
ident_with_server() click to toggle source

Identifies with server.

# File lib/vbot/bot_controller.rb, line 113
def ident_with_server
  ["USER #{@ident} * 8 :#{@gecos}\r\n"]
end
identify_with_nickserv() click to toggle source

Identifies with server.

# File lib/vbot/bot_controller.rb, line 107
def identify_with_nickserv
  ["PRIVMSG NickServ :IDENTIFY #{@pass}\r\n"]
end
interpret_command(data) click to toggle source

Parses the message from the server into a hash.

# File lib/vbot/bot_controller.rb, line 154
def interpret_command(data)
  {
    reply_to: get_nick_rt(data[0]),
    pm: is_private_message?(data[2]),
    command: data[4],
    arguments: data.slice(5..-1)
  }
end
is_private_message?(window) click to toggle source

Determines if private message.

# File lib/vbot/bot_controller.rb, line 147
def is_private_message?(window)
  return false if window == @chan
  true
end
join_to_channel() click to toggle source

Joins to channel on IRC server.

# File lib/vbot/bot_controller.rb, line 119
def join_to_channel
  ["JOIN #{@chan}\r\n"]
end
nick_to_server() click to toggle source

Gives nick to server.

# File lib/vbot/bot_controller.rb, line 101
def nick_to_server
  ["NICK #{@nick}\r\n"]
end
parse_message(data) click to toggle source

Performs logic on message to determine response.

# File lib/vbot/bot_controller.rb, line 165
def parse_message(data)
  return handle_ping data[1] if data[0] == 'PING'
  return join_to_channel if data[1] == '376' || data[1] == '422'
end
quit_from_server() click to toggle source

Quits IRC server.

# File lib/vbot/bot_controller.rb, line 131
def quit_from_server
  ['QUIT']
end
send_message(messages) click to toggle source

Writes to socket output.

# File lib/vbot/bot_controller.rb, line 172
def send_message(messages)
  messages.each do |msg|
    @socket.puts msg
  end
end