Foundation for command system
This commit is contained in:
parent
df36769978
commit
c5def15e4e
1 changed files with 38 additions and 7 deletions
|
@ -2,17 +2,45 @@ import Logging
|
||||||
import DataTypes
|
import DataTypes
|
||||||
|
|
||||||
from .AsyncServer import Server
|
from .AsyncServer import Server
|
||||||
|
from .Broadcast import Broadcast
|
||||||
|
|
||||||
class vHandler(Server.Handler):
|
class Command:
|
||||||
|
def __init__(self, prefix, auth_required=True):
|
||||||
|
self.prefix = prefix
|
||||||
|
self.auth_required = auth_required
|
||||||
|
|
||||||
|
def run(self, handler):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.prefix == other.prefix
|
||||||
|
|
||||||
|
|
||||||
|
class Info(Command):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("info", auth_required=False)
|
||||||
|
|
||||||
|
def run(self, handler):
|
||||||
|
return ["Link Clients connected: %d | Sublime Clients connected: %d" %
|
||||||
|
(len(handler.link_broadcast), len(handler.sublime_broadcast)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class fl0wHandler(Server.Handler):
|
||||||
def setup(self):
|
def setup(self):
|
||||||
Logging.info("Handler for '%s' initalised." % self.info[0])
|
Logging.info("Handler for '%s' initalised." % self.info[0])
|
||||||
self.authed = False
|
|
||||||
self.users = self.kwargs.pop("users")
|
self.users = self.kwargs.pop("users")
|
||||||
|
self.commands = self.kwargs.pop("commands")
|
||||||
|
self.sublime_broadcast = self.kwargs.pop("sublime_broadcast")
|
||||||
|
self.link_broadcast = self.kwargs.pop("link_broadcast")
|
||||||
|
self.client_type = None
|
||||||
|
self.authed = False
|
||||||
|
|
||||||
|
|
||||||
def handle(self, data, type):
|
def handle(self, data, type):
|
||||||
if not self.authed:
|
if self.client_type == None:
|
||||||
if type == DataTypes.json:
|
temp = self.sock.recv()
|
||||||
self.auth(data)
|
if
|
||||||
|
|
||||||
|
|
||||||
def finish(self):
|
def finish(self):
|
||||||
|
@ -24,9 +52,10 @@ class vHandler(Server.Handler):
|
||||||
if "user" and "pw" in collection["auth"]:
|
if "user" and "pw" in collection["auth"]:
|
||||||
temp_user = User(collection["auth"]["user"], collection["auth"]["pw"])
|
temp_user = User(collection["auth"]["user"], collection["auth"]["pw"])
|
||||||
if temp_user in self.users:
|
if temp_user in self.users:
|
||||||
self.sock.send({"auth" : "success"})
|
self.sock.send({"auth" : 1})
|
||||||
self.authed = True
|
self.authed = True
|
||||||
return
|
return
|
||||||
|
self.sock.send({"auth" : 0})
|
||||||
self.sock.close()
|
self.sock.close()
|
||||||
|
|
||||||
|
|
||||||
|
@ -44,4 +73,6 @@ class User:
|
||||||
return self.username
|
return self.username
|
||||||
|
|
||||||
|
|
||||||
server = Server("127.0.0.1", 1337, vHandler, {"users" : [User("test", "123")]})
|
server = Server(("127.0.0.1", 3077), fl0wHandler,
|
||||||
|
{"users" : [User("test", "123")], "commands" : [Info()],
|
||||||
|
"sublime_broadcast" : Broadcast(), "link_broadcast" : Broadcast()})
|
Reference in a new issue