moontalk/client/moontalk.rb

129 lines
2.8 KiB
Ruby
Raw Normal View History

2024-02-03 12:27:55 -05:00
#!/usr/bin/ruby
2024-02-03 12:38:35 -05:00
# written by Emil.
2024-02-03 12:55:38 -05:00
# Licensed under the GPLv3 only.
2024-02-03 12:27:55 -05:00
#
2024-02-03 12:55:38 -05:00
# SHORT GUIDE
2024-02-03 12:27:55 -05:00
#
# -serv specifies server,
# -port specifies port,
# -name specifies your display name,
#
# Type "BYE" to stop it after it connects to the moon.
# Type Enter and nothing else to refresh the messages.
# Type messages and send them, hopefully everyone hears you just fine.
#
2024-02-03 12:55:38 -05:00
# Commands:
#
# Commands must be near the beginning of messages, and will preform their respective action.
#
# BYE, QUIT - exits
# SAY - says something verbatim
# CLEAR - clears the terminal
# NICK... - sets the nick
# RECONNECT - reconnects
# DELAY... - sets delays
2024-02-03 12:27:55 -05:00
require 'socket'
require 'readline'
@serv = '7ks473deh6ggtwqsvbqdurepv5i6iblpbkx33b6cydon3ajph73sssad.onion'
@port = 50000
@name = "anonymous"
def read_from(socket)
begin # probably too large of a read size...
puts socket.read_nonblock(2 << 16)
rescue
end
end
def post(socket, msg)
update_prefix
socket.puts(@prefix + msg)
end
def update_prefix
date = Time.now.utc.strftime("%Y/%m/%d %k:%M:%S")
@prefix = "<#{date} #{@name}> "
end
def connect
puts "Connecting to " + @serv + ":" + @port.to_s + " as " + @name
socket = TCPSocket.new(@serv, @port)
if not socket
puts "Failed to connect."
exit 1
end
return socket
end
def main
begin
ARGV.each_with_index do |element,index|
if element == "-name"
@name = ARGV[index + 1]
end
if element == "-serv"
@serv = ARGV[index + 1]
end
if element == "-port"
@port = ARGV[index + 1].to_i
end
end
rescue
end
2024-02-05 16:20:06 -05:00
loop do
begin
socket = connect
delay = 0
2024-02-03 12:27:55 -05:00
2024-02-05 16:20:06 -05:00
h = { "BYE" => 1, "QUIT" => 1, "SAY" => 2, "CLEAR" => 3, "NAME" => 4, "NICK" => 4,
"RC" => 5, "RECONNECT" => 5, "DELAY" => 6 }
2024-02-03 12:27:55 -05:00
2024-02-05 16:20:06 -05:00
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]
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)
2024-02-03 12:27:55 -05:00
end
end
2024-02-05 16:20:06 -05:00
read_from(socket)
sleep delay
update_prefix
end
rescue (Errno::ECONNREFUSED Errno::EPIPE)
puts "\n THE HOUSE IS ON FIRE."
ensure
puts "\nSTOPPING NOW."
exit 1
2024-02-03 12:27:55 -05:00
end
end
end
main