class Twitch::Bot::Adapter::Irc

Handle the bot's IRC connection

Attributes

channel[R]
client[R]
socket[R]

Public Class Methods

new(client:) click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 10
def initialize(client:)
  @client = client

  open_socket
end

Public Instance Methods

connect() click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 16
def connect
  enable_twitch_capabilities
  authenticate
end
join_channel(channel) click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 40
def join_channel(channel)
  @channel = channel
  send_data "JOIN ##{channel.name}"
end
part_channel() click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 45
def part_channel
  send_data "PART ##{channel.name}"
  @channel = nil
end
read_data() click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 25
def read_data
  raw_message = read_data_from_socket
  irc_message = IrcMessage.new(raw_message)
  Twitch::Bot::MessageParser.new(irc_message).message
end
send_data(data) click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 36
def send_data(data)
  send_data_to_socket(data)
end
send_message(text) click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 31
def send_message(text)
  privmsg = "PRIVMSG ##{channel.name} :#{text}"
  send_data(privmsg)
end
shutdown() click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 21
def shutdown
  close_socket
end

Private Instance Methods

authenticate() click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 96
def authenticate
  config = client.config
  send_data "PASS #{config.setting('irc_password')}"
  send_data "NICK #{config.setting('irc_nickname')}"
end
close_socket() click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 63
def close_socket
  socket.close
  Twitch::Bot::Logger.debug "Socket closed"
end
enable_twitch_capabilities() click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 90
        def enable_twitch_capabilities
          send_data <<~DATA
            CAP REQ :twitch.tv/tags twitch.tv/commands twitch.tv/membership
          DATA
        end
open_socket() click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 54
def open_socket
  @socket = ::TCPSocket.new(
    client.config.setting("irc_hostname") || "irc.chat.twitch.tv",
    client.config.setting("irc_port") || 6667,
  )

  Twitch::Bot::Logger.debug "Socket open"
end
read_data_from_socket() click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 68
def read_data_from_socket
  Twitch::Bot::Logger.debug "Reading socket..."
  data = socket_read_next
  Twitch::Bot::Logger.debug "< #{data}"
  data
end
sanitize_data(data) click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 102
def sanitize_data(data)
  data.gsub(/(PASS oauth:)(\w+)/) do
    "#{Regexp.last_match(1)}#{'*' * Regexp.last_match(2).size}"
  end
end
send_data_to_socket(data) click to toggle source
# File lib/twitch/bot/adapter/irc.rb, line 85
def send_data_to_socket(data)
  Twitch::Bot::Logger.debug "> #{sanitize_data(data)}"
  socket.puts(data)
end
socket_read_next() click to toggle source

Acceptable :reek:NilCheck

# File lib/twitch/bot/adapter/irc.rb, line 76
def socket_read_next
  line = ""
  loop do
    line = socket.gets&.chomp
    break unless line.nil? || line.empty?
  end
  line
end