moontalk/client/moontalk.rb

123 lines
2.6 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
socket = connect
begin
delay = 0
h = { "BYE" => 1, "QUIT" => 1, "SAY" => 2, "CLEAR" => 3, "NICK" => 4, "RECONNECT" => 5, "DELAY" => 6 }
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["RECONNECT"]
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)
end
end
read_from(socket)
sleep delay
update_prefix
end
ensure
puts "\nSTOPPING NOW."
socket.close
end
end
main