Compare commits
No commits in common. "25647e3e95c016b4efb8646d1c3c8e7585c0cdd6" and "cb11018eba089a9f85c1fbcd8d34263822165c8d" have entirely different histories.
25647e3e95
...
cb11018eba
|
@ -1,3 +0,0 @@
|
||||||
[submodule "hyde"]
|
|
||||||
path = hyde
|
|
||||||
url = /hyde
|
|
14
.rubocop.yml
14
.rubocop.yml
|
@ -1,14 +0,0 @@
|
||||||
# The behavior of RuboCop can be controlled via the .rubocop.yml
|
|
||||||
# configuration file. It makes it possible to enable/disable
|
|
||||||
# certain cops (checks) and to alter their behavior if they accept
|
|
||||||
# any parameters. The file can be placed either in your home
|
|
||||||
# directory or in some project directory.
|
|
||||||
#
|
|
||||||
# RuboCop will start looking for the configuration file in the directory
|
|
||||||
# where the inspected file is and continue its way up to the root directory.
|
|
||||||
#
|
|
||||||
# See https://docs.rubocop.org/rubocop/configuration
|
|
||||||
require: standard
|
|
||||||
|
|
||||||
inherit_gem:
|
|
||||||
standard: config/base.yml
|
|
|
@ -1,22 +0,0 @@
|
||||||
---
|
|
||||||
include:
|
|
||||||
- "**/*.rb"
|
|
||||||
exclude:
|
|
||||||
- spec/**/*
|
|
||||||
- test/**/*
|
|
||||||
- vendor/**/*
|
|
||||||
- ".bundle/**/*"
|
|
||||||
require: []
|
|
||||||
domains: []
|
|
||||||
reporters:
|
|
||||||
- rubocop
|
|
||||||
- require_not_found
|
|
||||||
formatter:
|
|
||||||
rubocop:
|
|
||||||
cops: safe
|
|
||||||
except: []
|
|
||||||
only: []
|
|
||||||
extra_args: []
|
|
||||||
require_paths: []
|
|
||||||
plugins: []
|
|
||||||
max_files: 5000
|
|
1
hyde
1
hyde
|
@ -1 +0,0 @@
|
||||||
Subproject commit fd76422b760e6c045581bcb11842ee42221d06e0
|
|
178
proto.rb
178
proto.rb
|
@ -1,83 +1,101 @@
|
||||||
UIDS = {}
|
require "date"
|
||||||
|
def validate(*a)
|
||||||
module Heimdall
|
# Check arguments against either a list of acceptable classes or just a class
|
||||||
class UID
|
raise ArgumentError, "expected an even number of arguments" if a.count%2 != 0
|
||||||
def initialize
|
for i in (0..a.count-1).step(2) do
|
||||||
@UID_prefix = "abstract" if not @UID_prefix
|
if a[i+1].kind_of?(Class) then
|
||||||
id = (1..32).map { |x| (rand()*10).floor }.join
|
raise TypeError, "expected argument #{i/2} of type #{a[i+1].to_s}" unless a[i].kind_of?(a[i+1])
|
||||||
while UIDS.has_key? id do
|
elsif a[i+1].kind_of?(Array) then
|
||||||
id = (1..32).map { |x| (rand()*10).floor }.join
|
raise TypeError, "expected argument #{i/2} of any of the types #{a[i+1].to_s}" unless a[i+1].include?(a[i].class)
|
||||||
end
|
|
||||||
UIDS[@UID_prefix+id] = self
|
|
||||||
@UID = id
|
|
||||||
end
|
|
||||||
attr_reader :UID
|
|
||||||
end
|
|
||||||
|
|
||||||
class UserCache
|
|
||||||
def initialize
|
|
||||||
@users = {}
|
|
||||||
end
|
|
||||||
def add(user)
|
|
||||||
@users[user.protoid] = user
|
|
||||||
end
|
|
||||||
def get(protoid)
|
|
||||||
return @users[user.protoid]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class User < UID
|
|
||||||
def initialize(username, protoid)
|
|
||||||
@username = username
|
|
||||||
@protoid = protoid
|
|
||||||
@UID_prefix = "user"
|
|
||||||
super()
|
|
||||||
end
|
|
||||||
attr_reader :username
|
|
||||||
attr_reader :protoid
|
|
||||||
end
|
|
||||||
|
|
||||||
class Server < UID
|
|
||||||
def initialize
|
|
||||||
@UID_prefix = "server"
|
|
||||||
super()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Channel < UID
|
|
||||||
def initialize
|
|
||||||
@UID_prefix = "channel"
|
|
||||||
super()
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
class DirectChannel < Channel
|
|
||||||
def initialize
|
|
||||||
@UID_prefix = "dchannel"
|
|
||||||
super()
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
class ServerChannel < Channel
|
|
||||||
def initialize
|
|
||||||
@UID_prefix = "schannel"
|
|
||||||
super()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Message < UID
|
|
||||||
def initialize
|
|
||||||
@UID_prefix = "message"
|
|
||||||
super()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class MsgStack < UID
|
|
||||||
def initialize
|
|
||||||
@UID_prefix = "msgstack"
|
|
||||||
super()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
module Heimdall
|
||||||
|
# Core protocol
|
||||||
|
class NetEntity
|
||||||
|
# Abstraction that stores basic object properties
|
||||||
|
@createdAt
|
||||||
|
def initialize()
|
||||||
|
@createdAt = DateTime.new
|
||||||
|
end
|
||||||
|
attr_reader :createdAt
|
||||||
|
end
|
||||||
|
|
||||||
|
class User < NetEntity
|
||||||
|
# User abstraction (not bound to a group chat)
|
||||||
|
@username
|
||||||
|
@nickname
|
||||||
|
def initialize(username,nickname = nil)
|
||||||
|
validate(
|
||||||
|
username, String,
|
||||||
|
nickname, [String,NilClass]
|
||||||
|
)
|
||||||
|
super()
|
||||||
|
nickname = username if not nickname
|
||||||
|
@username = username
|
||||||
|
@nickname = nickname
|
||||||
|
end
|
||||||
|
attr_reader :username
|
||||||
|
attr_accessor :nickname
|
||||||
|
end
|
||||||
|
|
||||||
|
class Channel < NetEntity
|
||||||
|
# Channel abstraction (not bound to a group)
|
||||||
|
# Practically acts as a message stack
|
||||||
|
# Read access to all elements
|
||||||
|
# Write access only to top
|
||||||
|
@messages
|
||||||
|
def initialize()
|
||||||
|
validate(
|
||||||
|
id, String
|
||||||
|
)
|
||||||
|
super()
|
||||||
|
@messages = []
|
||||||
|
end
|
||||||
|
def [](index)
|
||||||
|
@messages[id]
|
||||||
|
end
|
||||||
|
def <<(message)
|
||||||
|
@messages.append(message)
|
||||||
|
end
|
||||||
|
def top(count = 1)
|
||||||
|
return @messages[-1] if count == 1
|
||||||
|
@messages[(-1*count)..-1]
|
||||||
|
end
|
||||||
|
def filter(&block)
|
||||||
|
@messages.filter(block)
|
||||||
|
end
|
||||||
|
def snapshot()
|
||||||
|
@messages.clone
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Message < NetEntity
|
||||||
|
# Message abstraction with edits and content history (not bound to a group)
|
||||||
|
@content
|
||||||
|
@author
|
||||||
|
@editedAt
|
||||||
|
@contentHist
|
||||||
|
def initialize(content,author,channel)
|
||||||
|
validate(
|
||||||
|
content, String,
|
||||||
|
author, Heimdall::User,
|
||||||
|
channel, Heimdall::Channel
|
||||||
|
)
|
||||||
|
super()
|
||||||
|
@editedAt = DateTime.new
|
||||||
|
@contentHist = [content]
|
||||||
|
@content = content
|
||||||
|
@author = author
|
||||||
|
end
|
||||||
|
def edit(content)
|
||||||
|
@content = content
|
||||||
|
@editedAt = DateTime.new
|
||||||
|
@contentHist.append(content)
|
||||||
|
end
|
||||||
|
attr_reader :content
|
||||||
|
attr_reader :author
|
||||||
|
attr_reader :contentHist
|
||||||
|
attr_reader :editedAt
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
122
server.rb
122
server.rb
|
@ -1,44 +1,98 @@
|
||||||
require_relative "proto"
|
# Prototype of the heimdall server
|
||||||
require_relative "hyde/hyde"
|
$LOAD_PATH << "."
|
||||||
require "json"
|
$VERSION = "0.1"
|
||||||
|
$PORT = 9128
|
||||||
|
require "proto"
|
||||||
|
require "socket"
|
||||||
|
|
||||||
Users = Heimdall::UserCache.new
|
PROTO_CODES = {
|
||||||
|
:SUCCESS => 0,
|
||||||
|
:BADJSON => 1,
|
||||||
|
:NOMETHOD => 2,
|
||||||
|
:INVPARAM => 3,
|
||||||
|
:NOTIMPL => 4
|
||||||
|
}
|
||||||
|
|
||||||
def require_keys(dict,key_dict)
|
PROTO_MESSAGES = {
|
||||||
raise KeyError, "not a dict" unless dict.kind_of? Hash
|
:SUCCESS => "Done",
|
||||||
key_dict.each_pair { |k,v|
|
:BADJSON => "Invalid JSON object",
|
||||||
unless (dict.has_key? k) and (dict[k].kind_of? v) then
|
:NOMETHOD => "Invalid method",
|
||||||
raise KeyError, "key #{k} of type #{v} required"
|
: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
|
end
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
server = Hyde::Server.new Port: 8000 do
|
def send(client,message)
|
||||||
path "user" do
|
client.puts JSON.dump(message)
|
||||||
post "new" do |ctx|
|
end
|
||||||
req,res = ctx.request,ctx.response
|
|
||||||
|
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
|
begin
|
||||||
data = JSON::Parser.new(req.body).parse
|
# 1. JSON validation
|
||||||
require_keys(data,{
|
data = client.gets
|
||||||
username: String,
|
data = JSON.load(data)
|
||||||
protocol_id: String
|
rescue JSON::ParserError
|
||||||
})
|
err(client,:BADJSON)
|
||||||
Users.add(Heimdall::User.new data[:username], data[:protocol_id])
|
|
||||||
rescue JSON::ParserError => jsonerror
|
|
||||||
res.body = JSON::fast_generate({
|
|
||||||
error: "#{jsonerror}"
|
|
||||||
})
|
|
||||||
res['Content-Type'] = "application/json"
|
|
||||||
res.status = 400
|
|
||||||
rescue KeyError => keyerror
|
|
||||||
res.body = JSON::fast_generate({
|
|
||||||
error: "#{keyerror}"
|
|
||||||
})
|
|
||||||
res['Content-Type'] = "application/json"
|
|
||||||
res.status = 400
|
|
||||||
end
|
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
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
server.start
|
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
|
||||||
|
|
Loading…
Reference in New Issue