commit 3efde7cb8c9b4b47b36e0f301ef159f485355d40 from: Aleksey Ryndin date: Thu Oct 10 13:36:35 2024 UTC Add: honkers managment commit - 3601f1e9b8cf6ad43170a5a87b4254efd32ac30d commit + 3efde7cb8c9b4b47b36e0f301ef159f485355d40 blob - 4c889b567270a0ca3d25d70848322816751150e0 blob + f929aab777e2fdd7e2bf4b700cadb24522e0670c --- lonk.py +++ lonk.py @@ -480,12 +480,11 @@ def page_honker(db_con, client_id, lonk_url, honk_url) print_gethonks(gethonks_answer, lonk_url, honk_url) -def print_gethonks(gethonks_answer, lonk_url, honk_url): - - def _menu(): - print(f"=> {honk_url.build_url(path='newhonk')} new honk\r") +def menu(lonk_url, honk_url, gethonks_answer=None): + print(f"=> {honk_url.build_url(path='newhonk')} new honk\r") + print(f"=> {lonk_url.build([])} lonk home\r") - print(f"=> {lonk_url.build([])} lonk home\r") + if gethonks_answer: line = f"=> {lonk_url.build('atme')} @me" if gethonks_answer["mecount"]: line += f' ({gethonks_answer["mecount"]})' @@ -496,12 +495,14 @@ def print_gethonks(gethonks_answer, lonk_url, honk_url line += f' ({gethonks_answer["chatcount"]})' print(line + "\r") - print(f"=> {honk_url.build_url(path='honkers')} honkers\r") - print(f"=> {lonk_url.build('search')} search\r") - print(f"=> {lonk_url.build('longago')} long ago\r") - print(f"=> {lonk_url.build('myhonks')} my honks\r") + print(f"=> {lonk_url.build('search')} search\r") + print(f"=> {lonk_url.build('longago')} long ago\r") + print(f"=> {lonk_url.build('myhonks')} my honks\r") + print(f"=> {lonk_url.build('gethonkers')} honkers\r") + print(f"=> {lonk_url.build('addhonker')} add new honker\r") - _menu() +def print_gethonks(gethonks_answer, lonk_url, honk_url): + menu(lonk_url, honk_url, gethonks_answer) print("\r") print("\r") @@ -537,7 +538,7 @@ def print_gethonks(gethonks_answer, lonk_url, honk_url if gethonks_answer.get("honks"): print("\r") - _menu() + menu(lonk_url, honk_url, gethonks_answer) def new_client_stage_1_ask_server(lonk_url): @@ -592,10 +593,77 @@ def bonk(db_con, client_id, lonk_url, honk_url): print("51 Not found\r") return - bonk_answer = honk_url.do_get("zonkit", wherefore="bonk", what=what, answer_is_json=False) + honk_url.do_get("zonkit", wherefore="bonk", what=what, answer_is_json=False) print(f'30 {lonk_url.build("myhonks")}\r') + + +def gethonkers(db_con, client_id, lonk_url, honk_url): + print("20 text/gemini\r") + print("# 𝓗 onk: honkers\r") + print("\r") + + menu(lonk_url, honk_url) + print("\r") + + honkers = honk_url.do_get("gethonkers").get("honkers") or [] + for honker in honkers: + print(f'## {honker.get("Name") or honker["XID"]}\r') + for field_name, display_name in zip(("Name", "XID", "Flavor"), ("name", "url", "flavor")): + value = honker.get(field_name) + if value: + print(f'{display_name}: {value}\r') + if honker.get("Flavor") == "sub": + print(f'=> {lonk_url.build("unsubscribe", urlencode({"honkerid": honker["ID"]}))} unsubscribe\r') + else: + print(f'=> {lonk_url.build("subscribe", urlencode({"honkerid": honker["ID"]}))} (re)subscribe\r') + print('\r') + + if honkers: + print("\r") + menu(lonk_url, honk_url) + + +def addhonker(db_con, client_id, lonk_url, honk_url): + if not lonk_url.query: + print("10 honker url (XID): \r") + return + url = unquote(lonk_url.query) + try: + honk_url.do_get("savehonker", url=url, answer_is_json=False) + print(f'30 {lonk_url.build("gethonkers")}\r') + except HTTPError as error: + print("20 text/gemini\r") + print("# 𝓗 onk: add new honker\r") + print("\r") + menu(lonk_url, honk_url) + print("\r") + print('## Error\r') + print(f'> {error.fp.read().decode("utf8")}\r') + +def unsubscribe(db_con, client_id, lonk_url, honk_url): + honkerid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("honkerid") + if not honkerid: + print("51 Not found\r") + return + + url = unquote(lonk_url.query) + honk_url.do_get("savehonker", honkerid=honkerid, unsub="unsub", answer_is_json=False) + print(f'30 {lonk_url.build("gethonkers")}\r') + + +def subscribe(db_con, client_id, lonk_url, honk_url): + honkerid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("honkerid") + if not honkerid: + print("51 Not found\r") + return + + url = unquote(lonk_url.query) + honk_url.do_get("savehonker", honkerid=honkerid, sub="sub", answer_is_json=False) + print(f'30 {lonk_url.build("gethonkers")}\r') + + def authenticated(cert_hash, lonk_url, fn_impl): db_con = db_connect() row = db_con.execute("SELECT client_id, honk_url, token FROM client WHERE cert_hash=?", (cert_hash, )).fetchone() @@ -635,6 +703,14 @@ def vgi(cert_hash, raw_url): authenticated(cert_hash, lonk_url, page_honker) elif lonk_url.page == "bonk": authenticated(cert_hash, lonk_url, bonk) + elif lonk_url.page == "gethonkers": + authenticated(cert_hash, lonk_url, gethonkers) + elif lonk_url.page == "addhonker": + authenticated(cert_hash, lonk_url, addhonker) + elif lonk_url.page == "unsubscribe": + authenticated(cert_hash, lonk_url, unsubscribe) + elif lonk_url.page == "subscribe": + authenticated(cert_hash, lonk_url, subscribe) elif lonk_url.page == "ask_server": new_client_stage_1_ask_server(lonk_url) elif lonk_url.page == "ask_username": @@ -662,10 +738,14 @@ if __name__ == '__main__': finally: stderr.write(f"{cert_hash_}|{input_url}|{clock_gettime(CLOCK_MONOTONIC) - start_time:.3f}sec.\n") except HTTPError as error: + stderr.write(f"{error}\n") print(f"43 Remote server return {error.code}: {error.reason}\r") except URLError as error: + stderr.write(f"{error}\n") print(f"43 Error while trying to access remote server: {error.reason}\r") except TimeoutError as error: + stderr.write(f"{error}\n") print(f"43 Error while trying to access remote server: {error}\r") else: + stderr.write("Certificate required\n") print("60 Certificate required\r")