BDServer/modules/Account.py
2024-11-24 23:28:19 +02:00

109 lines
2.7 KiB
Python

# AGPL 3 or any later version
# (C) J.Y.Amihud ( Blender Dumbass )
import os
import sys
import json
import random
import hashlib
import urllib.parse
from datetime import datetime
from getpass import getpass
from modules import Set
from modules import markdown
from modules.Common import *
def Load(name):
folder = Set.Folder()+"/accounts/"
try:
with open(folder+name+".json") as o:
return json.load(o)
except:
return {}
def Save(account):
name = account.get("username", "")
folder = Set.Folder()+"/accounts/"
try:
with open(folder+name+".json", "w") as save:
json.dump(account, save, indent=4)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Cannot save account!", e)
def Account():
if len(sys.argv) < 3:
from modules import Help
Help.Accounts()
if "--list" in sys.argv:
List()
elif "--add" in sys.argv:
try:
account = sys.argv[ sys.argv.index("--add") + 1]
if "--" in account: 1/0 # Failing this for the error message.
Add(account)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Account name wasn't specified!")
print('Use: $ python3 run.py --account --add blenderdumbass')
elif "--edit" in sys.argv:
try:
account = sys.argv[ sys.argv.index("--edit") + 1]
if "--" in account: 1/0 # Failing this for the error message.
Edit(account)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Account name wasn't specified!")
print('Use: $ python3 run.py --account --edit blenderdumbass')
def List():
folder = Set.Folder()+"/accounts"
for account in os.listdir(folder):
if account.endswith(".json"):
print(account.replace(".json", ""))
def Add(name):
# Adds a new account.
account = Load(name)
if account:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Account already exists.")
account = {
"username":name,
"bio":"",
"invite_codes":{},
"invited":[],
"invited_by":"",
"password":hashlib.sha512(getpass("Account's Password: ").encode("utf-8")).hexdigest(),
"title":name,
"email":"",
"website":"",
"mastodon":"",
"matrix":"",
"sessions":{}
}
Save(account)
print(clr["bold"]+clr["tbyl"]+name+clr["norm"]+" is added."+clr["norm"])
def Edit(name):
folder = Set.Folder()+"/accounts/"
os.system("nano "+folder+name+".json")