Compare commits

...

2 Commits

Author SHA1 Message Date
d7533a18a9 Actually add IRC Client 2022-06-14 12:21:25 -05:00
014de7c6aa Tweaks for Asterisk dialogs, IRC profile editor dialog functional. 2022-06-14 09:07:40 -05:00
5 changed files with 194 additions and 66 deletions

111
lib/asterisk/irc_client.rb Normal file
View File

@@ -0,0 +1,111 @@
require "socket"
require "openssl"
require "ircparser"
require_relative "irc_profile"
class W3DHub
class Asterisk
class IRCClient
class SSL
def self.default_context
verify_peer_and_hostname
end
def self.verify_peer_and_hostname
verify_peer.tap do |context|
context.verify_hostname = true
end
end
def self.verify_peer
no_verify.tap do |context|
context.verify_mode = OpenSSL::SSL::VERIFY_PEER
context.cert_store = OpenSSL::X509::Store.new.tap(&:set_default_paths)
end
end
def self.verify_hostname_only
no_verify.tap do |context|
context.verify_hostname = true
end
end
def self.no_verify
OpenSSL::SSL::SSLContext.new
end
end
attr_reader :status
def initialize(irc_profile = nil)
@irc_profile = irc_profile
socket = dial(
@irc_profile.server_hostname,
@irc_profile.server_port,
ssl_context: irc_profile.server_ssl ? irc_profile.server_verify_ssl ? SSL.default_context : SSL.no_verify : false
)
authenticate_with_brenbot!(socket)
ensure
socket&.close
end
def dial(hostname, port = 6697, local_host: nil, local_port: nil, ssl_context: SSL.default_context)
Socket.tcp(hostname, port, local_host, local_port).then do |socket|
if ssl_context
ssl_context = SSL.send(ssl_context) if ssl_context.is_a? Symbol
OpenSSL::SSL::SSLSocket.new(socket, ssl_context).tap do |ssl_socket|
ssl_socket.hostname = hostname
ssl_socket.connect
end
else
socket
end
end
end
def authenticate_with_brenbot!(socket)
pp @irc_profile, "#{@irc_profile.nickname}!#{@irc_profile.username.split("/").first}"
# exit
pass = IRCParser::Message.new(command: "PASS", parameters: [@irc_profile.password])
user = IRCParser::Message.new(command: "USER", parameters: [@irc_profile.username, "0", "*", ":#{@irc_profile.nickname}"])
nick = IRCParser::Message.new(command: "NICK", parameters: [@irc_profile.nickname])
socket.puts(pass)
socket.puts(user)
socket.puts(nick)
pp socket
socket.flush
until socket.closed?
raw = socket.gets
next if raw.to_s.empty?
msg = IRCParser::Message.parse(raw)
if msg.command == "PING"
pong = IRCParser::Message.new(command: "PONG", parameters: [msg.parameters.first.sub("\r\n", "")])
socket.puts("#{pong}")
socket.flush
elsif msg.command == "001" && msg.parameters.join.include?("#{@irc_profile.nickname}!#{@irc_profile.username.split("/").first}")
pm = IRCParser::Message.new(command: "PRIVMSG", parameters: [@irc_profile.server_bot, "!auth #{@irc_profile.bot_auth_username} password"])
socket.puts(pm)
quit = IRCParser::Message.new(command: "QUIT", parameters: ["Quiting from an Asterisk"])
socket.puts(quit)
socket.flush
socket.close
end
end
end
end
end
end
W3DHub::Asterisk::IRCClient.new

View File

@@ -21,17 +21,17 @@ class W3DHub
end
stack(width: 1.0, fill: true, padding_left: 8, padding_right: 8) do
stack(width: 1.0, height: 60) do
stack(width: 1.0, height: 66) do
para "Game or Mod Title:"
@game_title = edit_line "#{@game&.title}", width: 1.0
@game_title = edit_line "#{@game&.title}", width: 1.0, fill: true
end
stack(width: 1.0, height: 60) do
stack(width: 1.0, height: 66) do
para "Path to Executable:"
flow(width: 1.0) do
@game_path = edit_line "#{@game&.path}", width: 0.749
button "Browse...", width: 0.25, enabled: W3DHub.unix?, tip: W3DHub.unix? ? "Browse for game executable" : "Not available on Windows" do
flow(width: 1.0, fill: true) do
@game_path = edit_line "#{@game&.path}", fill: true, height: 1.0
button "Browse...", width: 128, height: 1.0, enabled: W3DHub.unix?, tip: W3DHub.unix? ? "Browse for game executable" : "Not available on Windows" do
path = W3DHub.ask_file
@game_path.value = path if !path.empty? && File.exist?(path)
end

View File

@@ -15,70 +15,62 @@ class W3DHub
background 0x88_000000
# tagline "<b>#{I18n.t(:"server_browser.direct_connect")}</b>", fill: true, text_align: :center
tagline @profile ? "Update IRC Profile" : "Add IRC Profile", width: 1.0, text_align: :center
tagline @profile ? "Update IRC Profile" : "Add IRC Profile", width: 1.0, fill: true, text_align: :center
end
stack(width: 1.0, fill: true, padding_left: 8, padding_right: 8) do
stack(width: 1.0, height: 60) do
stack(width: 1.0, height: 66) do
para "IRC Nickname:"
@irc_nickname = edit_line "#{@profile&.nickname}", width: 1.0
@nickname = edit_line "#{@profile&.nickname}", width: 1.0, fill: true
end
stack(width: 1.0, height: 60) do
stack(width: 1.0, height: 66) do
flow(width: 1.0, height: 1.0) do
stack(width: 0.5, height: 1.0) do
para "IRC Username:"
@irc_username = edit_line "#{@profile&.username}", width: 1.0
para "IRC Username (Optional):"
@username = edit_line "#{@profile&.username}", width: 1.0, fill: true
end
stack(width: 0.5, height: 1.0) do
para "IRC Server Password:"
@irc_password = edit_line @profile ? Base64.strict_decode64(@profile.password) : "", width: 1.0, type: :password
para "IRC Server Password (Optional):"
@password = edit_line @profile ? Base64.strict_decode64(@profile.password) : "", width: 1.0, fill: true, type: :password
end
end
end
stack(width: 1.0, height: 60) do
stack(width: 1.0, height: 66, margin_top: 32) do
flow(width: 1.0, height: 1.0) do
stack(width: 0.75, height: 1.0) do
para "IRC Server IP or Hostname:"
@irc_hostname = edit_line "#{@profile&.server_hostname}", width: 1.0
@server_hostname = edit_line "#{@profile&.server_hostname}", width: 1.0, fill: true
end
stack(width: 0.249, height: 1.0) do
para "IRC Server Port:"
@irc_port = edit_line "#{@profile&.server_port || '6667'}", width: 1.0
@server_port = edit_line "#{@profile&.server_port || '6667'}", width: 1.0, fill: true
end
end
end
flow(width: 1.0, height: 60, margin_top: 8) do
flow(width: 0.5, height: 60) do
para "IRC Server Use SSL:", margin_top: 8
@irc_bot = check_box "#{@profile&.server_bot}"
end
flow(width: 0.5, height: 60) do
para "IRC Verify Server SSL Certificate:", margin_top: 8
@irc_bot = check_box "#{@profile&.server_bot}"
end
flow(width: 1.0, height: 66, margin_top: 8) do
@server_ssl = check_box "IRC Server Use SSL", checked: @profile&.server_ssl, text_size: 18, width: 0.5, height: 66
@server_verify_ssl = check_box "IRC Verify Server SSL Certificate", checked: @profile ? @profile.server_verify_ssl : true, text_size: 18, width: 0.5, height: 66
end
stack(width: 1.0, height: 60, margin_top: 32) do
stack(width: 1.0, height: 66) do
para "Brenbot Bot Name:"
@irc_bot = edit_line "#{@profile&.server_bot}", width: 1.0
@bot_username = edit_line "#{@profile&.bot_username}", width: 1.0, fill: true
end
flow(width: 1.0, height: 60) do
stack(width: 0.5, height: 60) do
flow(width: 1.0, height: 66) do
stack(width: 0.5, height: 66) do
para "Brenbot Auth Username:"
@irc_bot = edit_line "#{@profile&.server_bot}", width: 1.0
@bot_auth_username = edit_line "#{@profile&.bot_auth_username}", width: 1.0, fill: true
end
stack(width: 0.5, height: 60) do
stack(width: 0.5, height: 66) do
para "Brenbot Auth Password:"
@irc_password = edit_line @profile ? Base64.strict_decode64(@profile.password) : "", width: 1.0, type: :password
@bot_auth_password = edit_line @profile ? Base64.strict_decode64(@profile.bot_auth_password) : "", width: 1.0, fill: true, type: :password
end
end
@@ -95,11 +87,16 @@ class W3DHub
pop_state
@options[:save_callback].call(
@profile,
@irc_nickname.value,
@irc_password.value,
@irc_hostname.value,
@irc_port.value,
@irc_bot.value
@nickname.value,
@username.value,
@password.value,
@server_hostname.value,
@server_port.value,
@server_ssl.value,
@server_verify_ssl.value,
@bot_username.value,
@bot_auth_username.value,
@bot_auth_password.value
)
end
end
@@ -132,18 +129,19 @@ class W3DHub
def valid?
generated_name = IRCProfileForm.generate_profile_name(
@irc_nickname.value,
@irc_hostname.value,
@irc_port.value,
@irc_bot.value
@nickname.value,
@server_hostname.value,
@server_port.value,
@bot_username.value
)
existing_profile = W3DHub::Store[:asterisk_config].irc_profiles.find { |profile| profile.name == generated_name }
@irc_nickname.value.length.positive? &&
@irc_password.value.length.positive? && # May be optional?
@irc_hostname.value.length.positive? &&
@irc_port.value.length.positive? &&
@irc_bot.value.length.positive?
@nickname.value.length.positive? &&
@server_hostname.value.length.positive? &&
@server_port.value.length.positive? &&
@bot_username.value.length.positive? &&
@bot_auth_username.value.length.positive? &&
@bot_auth_password.value.length.positive?
end
def self.generate_profile_name(nickname, hostname, port, bot)

View File

@@ -21,9 +21,9 @@ class W3DHub
end
stack(width: 1.0, fill: true, padding_left: 8, padding_right: 8) do
stack(width: 1.0, height: 65) do
stack(width: 1.0, height: 66) do
para "Server Profile Name:"
@server_name = edit_line "#{@server_profile&.name}", width: 1.0
@server_name = edit_line "#{@server_profile&.name}", width: 1.0, fill: true
@server_name.subscribe(:changed) do |label|
@save_button.enabled = label.value.length.positive?
end

View File

@@ -9,7 +9,7 @@ class W3DHub
background 0xee_444444
stack(width: 1.0, max_width: 720, height: 1.0, max_height: 512, v_align: :center, h_align: :center, background: 0xee_222222) do
stack(width: 1.0, max_width: 720, height: 1.0, max_height: 540, v_align: :center, h_align: :center, background: 0xee_222222) do
# Title bar
flow(width: 1.0, height: 32, padding: 8) do
background 0x88_000000
@@ -20,7 +20,7 @@ class W3DHub
end
stack(width: 1.0, fill: true, scroll: true) do
stack(width: 1.0, height: 60, margin_left: 8, margin_right: 8) do
stack(width: 1.0, height: 66, margin_left: 8, margin_right: 8) do
para "Server profiles", text_align: :center, width: 1.0
flow(width: 1.0, fill: true) do
@@ -51,7 +51,7 @@ class W3DHub
end
stack(width: 1.0, fill: true, margin_top: 8, padding: 8, border_color: 0xff_111111, border_thickness: 1) do
flow(width: 1.0, height: 60) do
flow(width: 1.0, height: 66) do
stack(width: 0.5, height: 1.0) do
para "Nickname:"
@server_nickname = edit_line "", width: 1.0, fill: true
@@ -73,7 +73,7 @@ class W3DHub
end
end
flow(width: 1.0, height: 60) do
flow(width: 1.0, height: 66) do
stack(width: 0.5, height: 1.0) do
para "Server IP or Hostname:"
@server_hostname = edit_line "", width: 1.0, fill: true
@@ -95,7 +95,7 @@ class W3DHub
end
end
stack(width: 1.0, height: 60) do
stack(width: 1.0, height: 66) do
para "Game or Mod:"
flow(width: 1.0, fill: true) do
@@ -124,7 +124,7 @@ class W3DHub
end
end
stack(width: 1.0, height: 60) do
stack(width: 1.0, height: 66) do
para "Launch arguments (Optional):"
@launch_arguments = edit_line "", width: 1.0, fill: true
@launch_arguments.subscribe(:changed) do |e|
@@ -134,7 +134,7 @@ class W3DHub
end
end
stack(width: 1.0, height: 60) do
stack(width: 1.0, height: 66) do
para "IRC Profile:"
flow(width: 1.0, fill: true) do
@@ -293,24 +293,43 @@ class W3DHub
@games_list.choose = title
end
def save_irc_profile(updated, nickname, password, hostname, port, bot)
generated_name = Asterisk::States::IRCProfileForm.generate_profile_name(nickname, hostname, port, bot)
def save_irc_profile(
updated, nickname, username, password,
server_hostname, server_port, server_ssl, server_verify_ssl,
bot_username, bot_auth_username, bot_auth_password
)
generated_name = Asterisk::States::IRCProfileForm.generate_profile_name(
nickname,
server_hostname,
server_port,
bot_username
)
if updated
updated.name = generated_name
updated.nickname = nickname
updated.username = username
updated.password = Base64.strict_encode64(password)
updated.server_hostname = hostname
updated.server_port = port
updated.server_bot = bot
updated.server_hostname = hserver_hostname
updated.server_port = hserver_port
updated.server_ssl = server_ssl
updated.server_verify_ssl = server_verify_ssl
updated.bot_username = bot_username
updated.bot_auth_username = bot_auth_username
updated.bot_auth_password = Base64.strict_encode64(bot_auth_password)
else
profile = Asterisk::IRCProfile.new({
name: generated_name,
nickname: nickname,
username: username,
password: Base64.strict_encode64(password),
server_hostname: hostname,
server_port: port,
server_bot: bot
server_hostname: server_hostname,
server_port: server_port,
server_ssl: server_ssl,
server_verify_ssl: server_verify_ssl,
bot_username: bot_username,
bot_auth_username: bot_auth_username,
bot_auth_password: Base64.strict_encode64(bot_auth_password)
})
W3DHub::Store[:asterisk_config].irc_profiles << profile