From cba92412a82f0737cf2517412fabb825ed133ccb Mon Sep 17 00:00:00 2001 From: desvox Date: Sun, 5 Aug 2018 19:13:41 -0500 Subject: [PATCH] Allow setting server admins from server's config.json --- clients/network_client.py | 5 +++-- clients/urwid/main.py | 12 +++++++----- server.py | 30 +++++++++++++++++++++--------- src/db.py | 15 +++++++++++++++ 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/clients/network_client.py b/clients/network_client.py index c45ef8b..3a2ca3d 100644 --- a/clients/network_client.py +++ b/clients/network_client.py @@ -185,7 +185,8 @@ class BBJ(object): { "instance_name": (string), // a title set by the server owner - "allow_anon": (bool) // whether anonymous participation is allowed + "allow_anon": (bool), // whether anonymous participation is allowed + "admins": (list) // usernames of those who have admin rights on the server } """ response = self("instance_info") @@ -434,7 +435,7 @@ class BBJ(object): If the user element isnt found, ValueError is raised. See also `user_is_registered` """ - response = self("user_get", user=user_id_or_name) + response = self("user_get", target_user=user_id_or_name) return response["data"] diff --git a/clients/urwid/main.py b/clients/urwid/main.py index 665bb41..06bae27 100644 --- a/clients/urwid/main.py +++ b/clients/urwid/main.py @@ -2243,6 +2243,7 @@ class ActionBox(urwid.ListBox): elif keyl == "f12": app.loop.stop() call("clear", shell=True) + motherfucking_rainbows(obnoxious_logo) readline.set_completer(rlcompleter.Completer().complete) readline.parse_and_bind("tab: complete") interact(banner=version + "\n(BBJ Interactive Console)", local=globals()) @@ -2466,11 +2467,12 @@ def bbjrc(mode, **params): values = json.load(_in) # update it with new keys if necessary for key, default_value in default_prefs.items(): - # HACK: checking if they == None should not be necessary, as the program - # should never store a preference value as a NoneType. However ~vilmibm - # encountered the editor being stored as None, so there is a misstep somewhere - # and this will at least keep the program from continuing to crash should - # anyone else ever run into it + # The application will never store a config value + # as the NoneType, so users may set an option as + # null in their file to reset it to default. + # Also covers a previous encounter a user + # had with having a NoneType set in their + # config by accident, crashing the program. if key not in values or values[key] == None: values[key] = default_value # else make one diff --git a/server.py b/server.py index c40640a..f650ecc 100644 --- a/server.py +++ b/server.py @@ -12,7 +12,8 @@ dbname = "data.sqlite" # any values here may be overrided in the config.json. Any values not listed # here will have no effect on the server. -app_config = { +default_config = { + "admins": [], "port": 7099, "host": "127.0.0.1", "instance_name": "BBJ", @@ -20,13 +21,22 @@ app_config = { "debug": False } - try: - with open("config.json") as _conf: - app_config.update(json.load(_conf)) + with open("config.json", "r") as _in: + app_config = json.load(_in) + # update the file with new keys if necessary + for key, default_value in default_config.items(): + # The application will never store a config value + # as the NoneType, so users may set an option as + # null in their file to reset it to default + if key not in app_config or app_config[key] == None: + app_config[key] = default_value +# else just use the defaults except FileNotFoundError: - with open("config.json", "w") as _conf: - json.dump(app_config, _conf, indent=2) + app_config = default_prefs +finally: + with open("config.json", "w") as _out: + json.dump(app_config, _out, indent=2) def api_method(function): @@ -192,7 +202,8 @@ class API(object): """ return { "allow_anon": app_config["allow_anon"], - "instance_name": app_config["instance_name"] + "instance_name": app_config["instance_name"], + "admins": app_config["admins"] } @api_method @@ -667,10 +678,11 @@ API_CONFIG = { def run(): - # user anonymity is achieved in the laziest possible way: a literal user - # named anonymous. may god have mercy on my soul. _c = sqlite3.connect(dbname) try: + db.set_admins(_c, app_config["admins"]) + # user anonymity is achieved in the laziest possible way: a literal user + # named anonymous. may god have mercy on my soul. db.anon = db.user_resolve(_c, "anonymous") if not db.anon: db.anon = db.user_register( diff --git a/src/db.py b/src/db.py index ff4fc3e..a5dcf08 100644 --- a/src/db.py +++ b/src/db.py @@ -411,6 +411,21 @@ def user_update(connection, user_object, parameters): return user_resolve(connection, user_id) +def set_admins(connection, users): + """ + Set the server admins to be the content of `users`. + Any other users that previously had admin rights + not included in `users` will have their privledge + revoked. + """ + connection.execute("UPDATE users SET is_admin = 0") + for user in users: + connection.execute( + "UPDATE users SET is_admin = 1 WHERE user_name = ?", + (user,)) + connection.commit() + + def user_externalize(user_object): """ Cleanse private/internal data from a user object