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
The channel on the IRC server to join.
The gecos for the bot.
The name to identify to the server with.
The nick for the bot.
The pass for the bot.
The port to connect over.
The IRC server to connect to.
Public Class Methods
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
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
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
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
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
Handles ping message from server.
# File lib/vbot/bot_controller.rb, line 125 def handle_ping token ["PONG #{token}"] end
Identifies with server.
# File lib/vbot/bot_controller.rb, line 113 def ident_with_server ["USER #{@ident} * 8 :#{@gecos}\r\n"] end
Identifies with server.
# File lib/vbot/bot_controller.rb, line 107 def identify_with_nickserv ["PRIVMSG NickServ :IDENTIFY #{@pass}\r\n"] end
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
Determines if private message.
# File lib/vbot/bot_controller.rb, line 147 def is_private_message?(window) return false if window == @chan true end
Joins to channel on IRC server.
# File lib/vbot/bot_controller.rb, line 119 def join_to_channel ["JOIN #{@chan}\r\n"] end
Gives nick to server.
# File lib/vbot/bot_controller.rb, line 101 def nick_to_server ["NICK #{@nick}\r\n"] end
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
Quits IRC server.
# File lib/vbot/bot_controller.rb, line 131 def quit_from_server ['QUIT'] end
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