added admins and banning
this took more time than i thought it would
This commit is contained in:
+90
-2
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import asyncio
|
||||
import json, random, re, math, time, traceback
|
||||
import json, random, re, math, time, traceback, datetime, humanize
|
||||
from websockets.exceptions import ConnectionClosed, ConnectionClosedOK
|
||||
from websockets.asyncio.server import serve
|
||||
|
||||
@@ -11,6 +11,8 @@ from db import Database
|
||||
userdb = Database("users")
|
||||
userdb.createTable("NAMES",{"id":"int","name":"string","creation_date":"int","last_login":"int","last_namechange":"int"})
|
||||
userdb.createTable("PASSWORDS",{"id":"int","sum":"string"})
|
||||
userdb.createTable("ADMIN",{"id":"int","is_admin":"int"})
|
||||
userdb.createTable("BANNED",{"id":"int","is_banned":"int","motive":"str","ban_date":"int","expire_date":"int"})
|
||||
|
||||
isetdb = Database("isettings")
|
||||
isetdb.createTable("BASIC",{"id":"int","color":"int","encoding":"str"})
|
||||
@@ -65,6 +67,28 @@ def forgetSession(sid):
|
||||
if sid in sessionCreatedAccount:
|
||||
sessionCreatedAccount.remove(sid)
|
||||
|
||||
def isAdmin(uid):
|
||||
try:
|
||||
if uid == None: return False
|
||||
if not userdb.tableExists("ADMIN"): return False
|
||||
row = userdb.rowFromPK("ADMIN","id",uid)
|
||||
if row==None: return False
|
||||
if int(row["is_admin"])!=1: return False
|
||||
return True
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def isBanned(uid):
|
||||
if not userdb.tableExists("BANNED"): return False
|
||||
row = userdb.rowFromPK("BANNED","id",uid)
|
||||
if row==None: return False
|
||||
if int(row["is_banned"])==1:
|
||||
if row["expire_date"]!=0 and time.time()>row["expire_date"]:
|
||||
userdb.updateFromPK("BANNED","id",uid,{"is_banned":0})
|
||||
return False
|
||||
return True
|
||||
return False
|
||||
|
||||
async def handleCommand(ws,cmd):
|
||||
global sessionUsers
|
||||
print(f"recieved command: {cmd} (from {id(ws)})")
|
||||
@@ -117,6 +141,11 @@ async def handleCommand(ws,cmd):
|
||||
dbsum = userdb.rowFromPK("PASSWORDS","id",uid)["sum"]
|
||||
if psum.lower() != dbsum.lower():
|
||||
return await ws.send(clientError("Invalid username or password."))
|
||||
if isBanned(uid):
|
||||
row = userdb.rowFromPK("BANNED","id",uid)
|
||||
expire = row["expire_date"]
|
||||
timemsg = "You are permanently banned." if expire==0 else f"You will be unbanned in {humanize.naturaltime(datetime.datetime.fromtimestamp(expire))}."
|
||||
return await ws.send(clientError(f"Your account has been banned!\nMovite: {row["motive"]}\n\n{timemsg}"))
|
||||
print(f"session {id(ws)} has logged in as {name} ({uid})")
|
||||
sessionUsers[id(ws)] = uid
|
||||
userdb.updateFromPK("NAMES","id",uid,{"last_login":time.time()})
|
||||
@@ -132,7 +161,7 @@ async def handleCommand(ws,cmd):
|
||||
if uid == None:
|
||||
return await ws.send(response({"login":False}))
|
||||
name = userdb.rowFromPK("NAMES","id",uid)["name"]
|
||||
return await ws.send(response({"login":True,"uid":uid,"name":name}))
|
||||
return await ws.send(response({"login":True,"uid":uid,"name":name,"admin":isAdmin(uid)}))
|
||||
|
||||
case "changename":
|
||||
if uid == None:
|
||||
@@ -164,6 +193,35 @@ async def handleCommand(ws,cmd):
|
||||
userdb.updateFromPK("PASSWORDS","id",uid,{"sum":newpass})
|
||||
return await ws.send(response("success"))
|
||||
|
||||
case "getuserinfo":
|
||||
if "name" in cmd:
|
||||
name = cmd["name"]
|
||||
row = userdb.findRow("NAMES","name = ?",(name,))
|
||||
if not row: await ws.send(clientError("Cannot find this user."))
|
||||
auid = row["id"]
|
||||
else:
|
||||
auid = row["id"]
|
||||
row = userdb.rowFromPK("NAMES","id",auid)
|
||||
if not row: await ws.send(clientError("Cannot find this user."))
|
||||
name = row["name"]
|
||||
banned = isBanned(auid)
|
||||
ban_details = {}
|
||||
if banned:
|
||||
brow = userdb.rowFromPK("BANNED","id",auid)
|
||||
ban_details["motive"] = brow["motive"]
|
||||
ban_details["ban_date"] = brow["ban_date"]
|
||||
ban_details["expire_date"] = brow["expire_date"]
|
||||
|
||||
return await ws.send(response({
|
||||
"name": name,
|
||||
"id": auid,
|
||||
"created": row["creation_date"],
|
||||
"last_login": row["last_login"],
|
||||
"last_namechange": row["last_namechange"],
|
||||
"banned": banned,
|
||||
"ban_details": ban_details
|
||||
}))
|
||||
|
||||
# interface settings
|
||||
case "getisettings":
|
||||
if uid == None:
|
||||
@@ -195,6 +253,36 @@ async def handleCommand(ws,cmd):
|
||||
isetdb.updateFromPK(intname,"id",uid,newset)
|
||||
return await ws.send(response("success"))
|
||||
|
||||
# admin-specific
|
||||
case "banuser":
|
||||
if not isAdmin(uid):
|
||||
return await ws.send(clientError("This account does not have admin privileges."))
|
||||
user = cmd["user"]
|
||||
if not userdb.hasPK("NAMES","id",user):
|
||||
return await ws.send(clientError("Cannot find this user."))
|
||||
motive = cmd["motive"]
|
||||
duration = cmd["duration"] # in seconds
|
||||
expire = time.time()+duration if duration>0 else 0
|
||||
if userdb.hasPK("BANNED","id",user):
|
||||
userdb.updateFromPK("BANNED","id",user,{"is_banned":1,"motive":motive,"ban_date": time.time(),"expire_date":expire})
|
||||
else:
|
||||
userdb.insertRow("BANNED",{"id":user,"is_banned":1,"motive":motive,"ban_date": time.time(),"expire_date":expire})
|
||||
return await ws.send(response("success"))
|
||||
|
||||
case "unbanuser":
|
||||
if not isAdmin(uid):
|
||||
return await ws.send(clientError("This account does not have admin privileges."))
|
||||
user = cmd["user"]
|
||||
if not userdb.hasPK("NAMES","id",user):
|
||||
return await ws.send(clientError("Cannot find this user."))
|
||||
if userdb.hasPK("BANNED","id",user):
|
||||
if userdb.rowFromPK("BANNED","id",user)["is_banned"]==0:
|
||||
return await ws.send(clientError("This user is not banned."))
|
||||
userdb.updateFromPK("BANNED","id",user,{"is_banned":0})
|
||||
else:
|
||||
userdb.insertRow("BANNED",{"id":user,"is_banned":0,"motive":""})
|
||||
return await ws.send(response("success"))
|
||||
|
||||
case _:
|
||||
return await ws.send(clientError(f"Command '{cmd["type"]}' does not exist."))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user