Simple bridge server for various bots of mine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.8 KiB

#!/usr/bin/ruby
require 'readline'
require 'net/http'
require 'json'
require 'websocket-client-simple'
require 'uri'
puts "Connecting to server #{ARGV[0]} on port #{ARGV[1]}"
def get(path)
where = URI("http://#{ARGV[0]}:#{ARGV[1]}/#{path}")
JSON.parse(Net::HTTP.get(where))
end
def post(path,data)
where = URI("http://#{ARGV[0]}:#{ARGV[1]}/#{path}")
Net::HTTP.post(where,data.to_json)
end
version = get("version")["version"]
puts "Server reported version: #{version}"
print "Nickname> "
nickname = $stdin.gets.strip
puts "Trying to log in..."
res = get("/user/exists?protocol_id=#{"heimdall-"+nickname}")
puts "Account exists! exiting" if res["exists"]
return if res["exists"]
puts "Creating account..."
test = post("/user/new",{username: nickname, protocol_id: "heimdall-"+nickname})
unless test.kind_of? Net::HTTPOK then
puts "Something went wrong! exiting"
exit
end
puts "Your id is: heimdall-#{nickname}"
puts "Establishing websocket connection..."
ws = WebSocket::Client::Simple.connect "ws://#{ARGV[0]}:#{ARGV[2]}"
ws.on :message do |msg|
data = JSON.parse(msg.data)
if data.has_key? "websocket" then
WEBSOCKET_UID = uid = data["websocket"]
response = post("/user/listen",{
websocket: uid,
protocol_id: "heimdall-"+nickname
})
unless response.kind_of? Net::HTTPOK then
puts "Something went wrong when initiating listening to user! Check server logs for info."
end
elsif data.has_key? "error" then
puts "ERROR: #{data["error"]}"
elsif data.has_key? "room" then
puts "[#{data["room"]["name"]}] #{data["user"]["username"]}: #{data["content"]}"
else
puts "#{data["user"]["username"]}: #{data["content"]}"
end
end
ws.on :open do |msg|
puts "Websocket connection established"
end
at_exit do
ws.close
end
target = nil
target_type = nil
at_exit do
post("/user/delete",{
"protocol_id"=> "heimdall-"+nickname
})
end
while buf = Readline.readline("", true)
if buf == "/help" then
puts "Commands:"
puts "/help - this message"
puts "/send <protcol_id> - direct messages to somebody"
puts "/exit - quit program"
puts "/find <username> - find a username by pattern"
puts "/find-protoid <protoid> - find by a protocol id"
puts "/username <username> - set your username (does not change your protocol id"
puts "/create-room <roomname> - create a room"
puts "/join <room id> - join a room"
puts "/leave <room> - leave a room"
puts "/send-room <room> - send messages to a room"
next
end
if buf == "/exit" then
post("/user/delete",{
"protocol_id"=> "heimdall-"+nickname
})
exit
end
if buf.match(/^\/send .*$/) then
target = buf.match(/^\/send ([^\s]*)$/)[1]
target_type = "user"
next
end
if buf.match(/^\/send-room .*$/) then
target = buf.match(/^\/send-room ([^\s]*)$/)[1]
target_type = "room"
next
end
if buf.match(/^\/join .*$/) and defined? WEBSOCKET_UID then
target = buf.match(/^\/join ([^\s]*)$/)[1]
if get("/room/exists?protocol_id=#{target}")["exists"] then
target_type = "room"
post("/room/listen", {
websocket: WEBSOCKET_UID,
protocol_id: target
})
else
target = nil
end
next
end
if buf.match(/^\/leave .*$/) and defined? WEBSOCKET_UID then
target = buf.match(/^\/leave ([^\s]*)$/)[1]
if get("/room/exists?protocol_id=#{target}")["exists"] then
post("/room/unlisten",{
websocket: WEBSOCKET_UID,
protocol_id: target
})
if target_type == "room" then
target = nil
end
end
end
if buf.match(/^\/find .*$/) then
uname = (buf.match /^\/find (.*)$/)[1]
users = get("/user/find/by-name?username=#{uname}")["results"]
puts "Found #{users.length} results: "
users.each { |x| puts x[0] }
next
end
if buf.match(/^\/find-protoid .*$/) then
pid = (buf.match /^\/find-protoid (.*)$/)[1]
users = get("/user/find/by-protoid?protocol_id=#{pid}")["results"]
puts "Found #{users.length} results: "
users.each { |x| puts x[0] }
next
end
if buf.match(/^\/username .*$/) then
uname = (buf.match /^\/username (.*)$/)[1]
post("/user/modify",{
data: {
username: uname
},
protocol_id: "heimdall-"+nickname
})
next
end
if buf.match(/^\/create-room \S+$/) then
name = (buf.match /^\/create-room (\S+)$/)[1]
post("/room/new",{
protocol_id: "heimdall-room-"+name,
name: name
})
puts("Room id: heimdall-room-#{name}")
end
if target and target_type then
if target_type == "user" then
post("/user/send", {
"to" => target,
"content" => buf,
"from" => "heimdall-"+nickname
})
elsif target_type == "room" then
post("/room/send", {
"to" => target,
"content" => buf,
"from" => "heimdall-"+nickname
})
end
end
end