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.

98 lines
2.0 KiB

# Prototype of the heimdall server
$LOAD_PATH << "."
$VERSION = "0.1"
$PORT = 9128
require "proto"
require "socket"
PROTO_CODES = {
:SUCCESS => 0,
:BADJSON => 1,
:NOMETHOD => 2,
:INVPARAM => 3,
:NOTIMPL => 4
}
PROTO_MESSAGES = {
:SUCCESS => "Done",
:BADJSON => "Invalid JSON object",
:NOMETHOD => "Invalid method",
:INVPARAM => "Invalid parameters",
:NOTIMPL => "Not implemented"
}
class Server
@channelPool
@channelListeners
@methods
def initialize()
@channelPool = {}
@channelListeners = {}
@methods = [
"join",
"useradd",
"userdel",
"userinfo",
"lusers",
"lchannels",
"chantop",
"chansend",
"chanedit"
]
end
def send(client,message)
client.puts JSON.dump(message)
end
def err(client,code)
client.puts JSON.dump({
:error=>PROTO_MESSAGES[code],
:code=>PROTO_CODES[code]
})
client.close
end
def serve(client)
# basic validation steps
begin
# 1. JSON validation
data = client.gets
data = JSON.load(data)
rescue JSON::ParserError
err(client,:BADJSON)
end
# 2. Method validation
method = data[:method]
err(client,:NOMETHOD) if not @methods.include?(method)
# last but not least: calling the given method
if not self.methods.include?(method) then
err(client,:NOTIMPL)
return
end
send(client,self.method(method).call(client,data))
end
def join(client,data)
chaind = data["chanid"]
chanid = rand(89999999)+10000000 if not chanid
if not @channelPool[chanid] then
@channelPool[chanid] = Heimdall::Channel.new
@channelListeners
end
return {
:code=>PROTO_CODE[:SUCCESS],
:method=>"join",
:chanid=>chanid,
}
end
end
puts "Starting heimdall v#{$VERSION} server on port #{$PORT}..."
server = TCPServer.new 9128
loop do
Thread.start(server.accept) do |client|
puts "Received client connection on #{client.addr}"
end
end