update ruby client

This commit is contained in:
Emil Williams 2024-02-17 18:55:24 +00:00
parent 4fb053fbe9
commit f742b9fc88

View File

@ -14,16 +14,19 @@
# Type Enter and nothing else to refresh the messages.
# Type messages and send them, hopefully everyone hears you just fine.
#
# Commands (which always begin with a forward slash):
# Commands (which always begin with a forward slash, case doesn't matter):
#
# Commands must be near the beginning of messages, and will preform their respective action.
#
# BYE, QUIT - exits
# SAY - says something verbatim
# RAW - send a raw message (no prefix)
# CLEAR - clears the terminal
# NICK... - sets the nick
# RECONNECT - reconnects
# DELAY... - sets delays
#
# /nick anon9999
require 'socket'
require 'readline'
@ -40,13 +43,17 @@ def read_from(socket)
end
end
def raw(socket, msg)
socket.puts(msg)
end
def post(socket, msg)
update_prefix
socket.puts(@prefix + msg)
raw(socket, @prefix + msg)
end
def update_prefix
date = Time.now.utc.strftime("%Y/%m/%d %k:%M:%S")
date = Time.now.utc.strftime("%Y/%m/%d %k:%M")
@prefix = "<#{date} #{@name}> "
end
@ -82,36 +89,47 @@ def main
delay = 0
h = { "BYE" => 1, "QUIT" => 1, "SAY" => 2, "CLEAR" => 3, "NAME" => 4, "NICK" => 4,
"RC" => 5, "RECONNECT" => 5, "DELAY" => 6 }
"RC" => 5, "RECONNECT" => 5, "DELAY" => 6, "RAW" => 7 }
update_prefix
while msg = Readline.readline(@prefix, true)
if not msg.empty?
msg.strip!
word = msg.split(/^\/([\w]*)$|^([\w]*) (.*)$/)[1..]
case h[word[0]]
when h["BYE"]
post(socket, "BYE")
exit 0
when h["CLEAR"]
puts "\e[1;1H\e[2J"
when h["NICK"]
if not word[1].empty?
@name = word[1]
word = msg.split(/^\/([\w]*)\W+?(.*)?$/)[1..]
if not word.empty?
case h[word[0].upcase]
when h["BYE"]
post(socket, "BYE")
exit 0
when h["CLEAR"]
puts "\e[1;1H\e[2J"
when h["NICK"]
if not word[1].empty?
@name = word[1]
end
when h["RC"]
puts "reconnecting..."
socket.close
socket = connect
when h["DELAY"]
delay = word[1].to_i
when h["SAY"]
if not word.length > 1
word.push("")
end
post(socket, word[1])
when h["RAW"]
begin
raw(socket, (msg.match(/\/?raw (.*)/i)[1]))
puts "raw message sent"
rescue
puts "didn't send that"
end
else
post(socket, msg)
end
when h["RC"]
puts "reconnecting..."
socket.close
socket = connect
when h["DELAY"]
delay = word[1].to_i
when h["SAY"]
if not word.length > 1
word.push("")
end
post(socket, word[1])
else
post(socket, msg)
post(socket, msg)
end
end