diff --git a/lists b/lists index a207794..1f026eb 100755 --- a/lists +++ b/lists @@ -15,25 +15,18 @@ if not STORAGE_PATH.exists(): ALLOWED_LIST_NAME_CHARS = string.ascii_lowercase + string.digits + "_" -def get_user_dir(): - user_dir = STORAGE_PATH / scgi.tls_client_hash[7:] - if not user_dir.is_dir(): - user_dir.mkdir() - return user_dir - - def show_lists(): - user_dir = get_user_dir() + user_dir = scgi.get_user_dir() scgi.header(20, "text/gemini") print("# Lists") for child in user_dir.iterdir(): if child.is_file(): - print(f"=> {scgi.script_name}/{child.name} 📄 {child.name}") - print(f"=> {scgi.script_name}/_/create ✨ Create a new list") + scgi.link(f"/{child.name}", f"📄 {child.name}") + scgi.link("/_/create", "✨ Create a new list") def show_list(list_name): - list_file = get_user_dir() / list_name + list_file = scgi.get_user_dir() / list_name try: with open(list_file, "rt") as f: content = f.read() @@ -43,19 +36,29 @@ def show_list(list_name): scgi.header(20, "text/gemini") print("# " + list_name) print(content) - print(f"=> {scgi.script_name}/{list_name}/add Append item") - print(f"=> {scgi.script_name}/{list_name}/pop Pop first item") + scgi.link(f"/{list_name}/add", "Append item") + scgi.link(f"/{list_name}/pop", "Pop first item") + scgi.link(f"/{list_name}/clear", "Clear list") + scgi.link(f"/{list_name}/delete", "Delete list") def process_action(list_name, action): - if list_name == "_" and action == "create": - process_creation() - elif action == "add": - process_add(list_name) - elif action == "pop": - process_pop(list_name) + if list_name == "_": + if action == "create": + process_creation() + else: + scgi.not_found() else: - scgi.not_found() + if action == "add": + process_add(list_name) + elif action == "pop": + process_pop(list_name) + elif action == "clear": + process_clear(list_name) + elif action == "delete": + process_delete(list_name) + else: + scgi.not_found() def process_creation(): @@ -66,7 +69,7 @@ def process_creation(): for char in new_list_name: if char not in ALLOWED_LIST_NAME_CHARS: scgi.temp_error(f"Illegal character in list name: {char}") - list_file = get_user_dir() / new_list_name + list_file = scgi.get_user_dir() / new_list_name list_file.touch(mode=0o660) redirect_to_list(new_list_name) @@ -78,7 +81,7 @@ def process_add(list_name): if "\n" in item: scgi.temp_error("No new line allowed in item.") line = f"* {item}\n" - list_file = get_user_dir() / list_name + list_file = scgi.get_user_dir() / list_name try: with open(list_file, "at") as f: f.write(line) @@ -89,11 +92,37 @@ def process_add(list_name): def process_pop(list_name): - list_file = get_user_dir() / list_name + list_file = scgi.get_user_dir() / list_name subprocess.run(["sed", "-i", "1d", str(list_file)]) redirect_to_list(list_name) +def process_clear(list_name): + if scgi.query_string_dec: + if scgi.query_string_dec != list_name: + redirect_to_list(list_name) + list_file = scgi.get_user_dir() / list_name + try: + with open(list_file, "wt") as f: + f.truncate() + except OSError: + scgi.temp_error() + redirect_to_list(list_name) + else: + scgi.require_input("Type the list name to confirm its clearing.") + + +def process_delete(list_name): + if scgi.query_string_dec: + if scgi.query_string_dec != list_name: + redirect_to_list(list_name) + list_file = scgi.get_user_dir() / list_name + list_file.unlink() + scgi.redirect_temp(scgi.script_name) + else: + scgi.require_input("Type the list name to confirm its deletion.") + + def redirect_to_list(list_name): scgi.redirect_temp(scgi.script_name + "/" + list_name)