summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--README.md20
-rw-r--r--main.py70
3 files changed, 83 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index 2eea525..a1b9cbb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
-.env
\ No newline at end of file
+.env
+__pycache__/
\ No newline at end of file
diff --git a/README.md b/README.md
index 8549224..6181506 100644
--- a/README.md
+++ b/README.md
@@ -5,19 +5,23 @@ soktdeer rewrite
 
 ## progress
 ### general (from hydrogen)
-- [ ] accounts (creation/getting)
-- [ ] messages (creation/getting)
+- [ ] accounts
+  - [x] account creation
+  - [ ] get account
+  - [ ] login via password
+  - [ ] login via token
+  - [ ] account editing
+  - [ ] account deletion
+- [ ] messages
+  - [ ] message creation
+  - [ ] get message
   - [ ] chat history (v1)
-  - [ ] fetch individual messages
-- [ ] changing profile attributes
-- [ ] account deletion
-- [ ] password changes
-- [ ] inbox (getting)
+- [ ] get inbox
 ### moderation (from hydrogen)
 - [ ] bans
 - [ ] invite codes
 - [ ] kicking
-- [ ] inbox (creation)
+- [ ] post to inbox
 - [ ] support codes
 - [ ] lockdown
 ### new in helium
diff --git a/main.py b/main.py
index 39413c2..9446b71 100644
--- a/main.py
+++ b/main.py
@@ -3,22 +3,44 @@ import json
 import logging

 from websockets.asyncio.server import serve

 from websockets.asyncio.server import broadcast

+import re

+from passlib.hash import scrypt

+import db

+import uuid

+import secrets

 

 addr = "localhost"

 port = 3636

 

 logging.basicConfig(level=logging.INFO)

 

+error_contexts = {

+    "malformedJson": "The JSON data sent to the server could not be parsed.",

+    "lengthInvalid": "A value in the JSON data is longer or shorter than expected.",

+    "invalidUsername": "Username is invalid. It may contain characters that are not permitted in usernames.",

+    "invalidInvite": "The invite code you are trying to use is invalid or has expired.",

+    "usernameTaken": "This username has been taken.",

+    "notExists": "The requested value does not exist."

+}

+

 ulist = {}

-ulist_connection_ids = {}

+user_clients = {}

 clients = []

 

+invite_codes = ["STANLEYYELNATSAB"]

+locked = False

+

 class util:

     def error(code, listener):

+        if code in error_contexts:

+            context = error_contexts[code]

+        else:

+            context = ""

         return json.dumps({

             "error": True,

             "code": code,

             "form": "helium-util",

+            "context": context,

             "listener": listener

         })

     

@@ -50,6 +72,52 @@ async def handler(websocket):
             if fc != True:

                 await websocket.send(util.error(fc, listener))

                 continue

+            r["username"] = r["username"].lower()

+            r["invite_code"] = r["invite_code"].upper()

+            if not re.fullmatch("[a-z0-9-_]{1,20}", r["username"]):

+                await websocket.send(util.error("invalidUsername", listener))

+                continue

+            if r["invite_code"] not in invite_codes:

+                await websocket.send(util.error("invalidInvite", listener))

+                continue

+            if db.acc.get(r["username"]) != "notExists":

+                await websocket.send(util.error("usernameTaken", listener))

+                continue

+            data = {

+                "_id": str(uuid.uuid4()),

+                "username": r["username"],

+                "display_name": r["username"],

+                "avatar": None,

+                "bot": False,

+                "verified": False,

+                "banned_until": 0,

+                "profile": {

+                    "bio": "",

+                    "lastfm": "",

+                    "banner": None,

+                    "links": {}

+                },

+                "secure": {

+                    "password": scrypt.hash(r["password"]),

+                    "token": secrets.token_urlsafe(64),

+                    "ban_reason": "",

+                    "invite_code": r["invite_code"],

+                    "support_code": secrets.token_hex(16)

+                }

+            }

+            result = db.acc.add(data)

+            if result != True:

+                await websocket.send(util.error(result, listener))

+                continue

+            invite_codes.remove(r["invite_code"])

+            await websocket.send(json.dumps({"error": False, "token": data["secure"]["token"], "listener": listener}))

+        elif r["command"] == "login_pswd":

+            fc = util.field_check({"username": range(1,21), "password": range(8,256)}, r)

+            if fc != True:

+                await websocket.send(util.error(fc, listener))

+                continue

+            r["username"] = r["username"].lower()

+

         else:

             await websocket.send(util.error("malformedJson", listener))

     if websocket in clients: