2 from json import loads as json_loads
3 from mimetypes import guess_type
5 from pathlib import Path
6 from sys import stdout, stderr
7 from sqlite3 import connect as sqlite3_connect
8 from time import clock_gettime, CLOCK_MONOTONIC
9 from urllib.error import HTTPError, URLError
10 from urllib.parse import urlencode, urlunsplit, urljoin, urlsplit, parse_qsl, unquote, quote
11 from urllib.request import urlopen
12 from html.parser import HTMLParser
16 def __init__(self, tag, attrs):
19 def on_data(self, data):
20 raise AssertionError("The method must be overridden")
23 raise AssertionError("The method must be overridden")
26 class ParagraphTag(_BaseTag):
27 def __init__(self, tag, attrs):
28 super().__init__(tag, attrs)
32 def on_data(self, data):
33 self.content.append(data.strip())
36 result = " ".join(" ".join(data.split()) for data in self.content if data)
40 return "\n".join([result] + footer) + "\n" if result else ""
43 class LinkTag(_BaseTag):
44 def __init__(self, href, paragraph, tag, attrs):
45 super().__init__(tag, attrs)
47 self.paragraph = paragraph
50 def on_data(self, data):
52 self.paragraph.on_data("↳")
53 self.paragraph.on_data(data)
54 self.content.append(data.strip())
57 text = " ".join(" ".join(data.split()) for data in self.content if data)
58 self.paragraph.footer.append(f"=> {self.href} {text}")
62 class LiItemTag(ParagraphTag):
64 content = super().flush()
65 return f"* {content}" if content else ""
68 class QuoteTag(ParagraphTag):
70 content = super().flush()
71 return f"> {content}" if content else ""
74 class HeaderTag(ParagraphTag):
76 content = super().flush()
79 return f"{'#' * int(self.tag[1:])} {content}"
82 class PreformattedTag(_BaseTag):
83 def __init__(self, tag, attrs):
84 super().__init__(tag, attrs)
87 def on_data(self, data):
93 return f"```\n{result}\n```\n" if result else ""
96 class HtmlToGmi(HTMLParser):
97 def __init__(self, base_url, fn_media_url):
101 self.base_url = base_url
102 self.fn_media_url = fn_media_url
104 def feed(self, data):
107 self.gmi_text.append(self.stack.pop().flush())
108 return "\n".join(gmi_text for gmi_text in self.gmi_text if gmi_text)
110 def handle_starttag(self, tag, attrs):
113 self.gmi_text.append(self.stack[-1].flush())
114 self.stack.append(elem)
117 _push(ParagraphTag(tag, attrs))
119 _push(PreformattedTag(tag, attrs))
120 elif tag in {"h1", "h2", "h3", "h4", "h5", "h6"}:
121 _push(HeaderTag(tag, attrs))
122 elif tag in {"li", "dt"}:
123 _push(LiItemTag(tag, attrs))
124 elif tag in {"blockquote", "q"}:
125 _push(QuoteTag(tag, attrs))
127 href = dict(attrs).get("href")
129 self.stack.append(LinkTag(urljoin(self.base_url, href), self._get_current_paragraph(), tag, attrs))
132 title = img.get("title") or ""
133 if img.get("class") == "emu" and title and self.stack:
134 self.stack[-1].on_data(title)
138 http_img_url = urljoin(self.base_url, src)
139 mime, _ = guess_type(http_img_url)
140 img_url = self.fn_media_url(mime, http_img_url)
141 self.gmi_text.append(f"=> {img_url} {title or http_img_url}")
144 self.gmi_text.append(self.stack[-1].flush())
146 def handle_data(self, data):
148 self.stack.append(ParagraphTag("p", []))
149 self.stack[-1].on_data(data)
151 def handle_endtag(self, tag):
152 if self.stack and tag == self.stack[-1].tag:
153 self.gmi_text.append(self.stack.pop().flush())
155 def _get_current_paragraph(self):
156 for elem in reversed(self.stack):
157 if isinstance(elem, ParagraphTag):
160 self.stack = [ParagraphTag("p", [])] + self.stack
165 def __init__(self, raw_url):
166 self._splitted_url = urlsplit(raw_url)
167 self.splitted_path = [part for part in self._splitted_url.path.split("/") if part]
168 self.page = self.splitted_path[-1]
170 for path_part in self.splitted_path:
171 self._base_path.append(path_part)
172 if path_part == "lonk":
175 def build(self, page, query=""):
176 page = page if isinstance(page, list) else [page]
178 ("gemini", self._splitted_url.netloc, "/".join(self._base_path + page), query, "")
181 def media(self, mime, url):
182 return self.build("proxy", urlencode({"m": mime, "u": url})) if mime else url
186 return self._splitted_url.query
190 def __init__(self, raw_url, token):
191 self._splitted_url = urlsplit(raw_url)
194 def build(self, scheme=None, netloc=None, path="", query="", fragment=""):
197 scheme or self._splitted_url.scheme,
198 netloc or self._splitted_url.netloc,
205 def get(self, action, answer_is_json=True, **kwargs):
206 query = {**{"action": action, "token": self._token}, **kwargs}
207 with urlopen(self.build(path="api", query=urlencode(query)), timeout=45) as response:
208 answer = response.read().decode("utf8")
209 return json_loads(answer) if answer_is_json else answer
212 def db_create_schema(db_con):
217 client_id INTEGER PRIMARY KEY,
218 cert_hash TEXT UNIQUE,
219 honk_url TEXT NOT NULL,
228 convoy_id INTEGER PRIMARY KEY,
241 FOREIGN KEY (client_id) REFERENCES client (client_id),
242 UNIQUE (convoy, client_id)
250 donk_id INTEGER PRIMARY KEY,
256 FOREIGN KEY (client_id) REFERENCES client (client_id),
257 FOREIGN KEY (convoy_id) REFERENCES convoy (convoy_id)
265 xonker_id INTEGER PRIMARY KEY,
269 FOREIGN KEY (client_id) REFERENCES client (client_id),
270 FOREIGN KEY (convoy_id) REFERENCES convoy (convoy_id)
278 db_schema_version INTEGER
282 db_con.execute("INSERT INTO meta(db_schema_version) VALUES (?)", (1, ))
286 db_file_path = Path(__file__).parent / ".local" / "db"
287 db_file_path.parent.mkdir(parents=True, exist_ok=True)
288 db_exist = db_file_path.exists()
289 db_con = sqlite3_connect(db_file_path)
292 db_create_schema(db_con)
296 def print_header(page_name):
297 print("20 text/gemini\r")
298 print(f"# 𝓗 onk: {page_name}\r")
302 def print_menu(lonk_url, honk_url, gethonks_answer=None):
304 print(f"=> {lonk_url.build('newhonk')} new honk\r")
305 print(f"=> {lonk_url.build([])} lonk home\r")
308 line = f"=> {lonk_url.build('atme')} @me"
309 if gethonks_answer["mecount"]:
310 line += f' ({gethonks_answer["mecount"]})'
313 line = f"=> {honk_url.build(path='chatter')} chatter"
314 if gethonks_answer["chatcount"]:
315 line += f' ({gethonks_answer["chatcount"]})'
318 print(f"=> {lonk_url.build('search')} search\r")
319 print(f"=> {lonk_url.build('longago')} long ago\r")
320 print(f"=> {lonk_url.build('myhonks')} my honks\r")
321 print(f"=> {lonk_url.build('gethonkers')} honkers\r")
322 print(f"=> {lonk_url.build('addhonker')} add new honker\r")
325 def print_gethonks(gethonks_answer, lonk_url, honk_url):
326 print_menu(lonk_url, honk_url, gethonks_answer)
329 for honk in gethonks_answer.get("honks") or []:
330 convoy = honk["Convoy"]
331 re_url = honk.get("RID")
332 oondle = honk.get("Oondle")
333 from_ = f'{oondle} (🔁 {honk["Handle"]})' if oondle else f'{honk["Handle"]}'
335 f'##{"# ↱" if re_url else ""} From {from_} {honk["Date"]}',
336 f'=> {lonk_url.build("convoy", urlencode({"c": convoy}))} Convoy {convoy}',
340 lines.append(f'=> {re_url} Re: {re_url}')
342 lines.append(HtmlToGmi(honk_url.build(), lonk_url.media).feed(honk["HTML"]))
343 for donk in honk.get("Donks") or []:
344 donk_url = honk_url.build(path=f'/d/{donk["XID"]}') if donk.get("XID") else donk["URL"]
345 donk_mime, donk_text = donk["Media"], donk.get("Desc") or donk.get("Name") or None
346 lines.append(f'=> {lonk_url.media(donk_mime, donk_url)} {donk_url}')
348 lines.append(donk_text)
350 if honk.get("Public"):
351 lines.append(f'=> {lonk_url.build("bonk", urlencode({"w": honk["XID"]}))} ↺ bonk')
352 honk_back_url = lonk_url.build(
354 quote(honk["Handles"] or " ", safe=""),
355 quote(honk["XID"], safe=""),
359 lines.append(f'=> {honk_back_url} ↱ honk back')
360 for xonker in (honk.get("Honker"), honk.get("Oonker")):
362 lines.append(f'=> {lonk_url.build("honker", urlencode({"xid": xonker}))} honks of {xonker}')
363 print("\r\n".join(lines))
366 if gethonks_answer.get("honks"):
368 print_menu(lonk_url, honk_url, gethonks_answer)
372 def __init__(self, convoy_id, convoy, honk_id, handle, oondle, url, html, date, public, handles, honker, oonker):
373 self.convoy_id = convoy_id
375 self.honk_id = honk_id
381 self.public = bool(public)
382 self.handles = handles
388 def iterate_honks(self):
389 if self.html is not None:
391 "Convoy": self.convoy,
392 "Handle": self.handle,
393 "Oondle": self.oondle,
398 "Public": self.public,
399 "Handles": self.handles,
400 "Honker": self.honker,
401 "Oonker": self.oonker,
403 {"URL": donk[0], "Media": donk[1], "Desc": donk[2]}
404 for donk in self.donks
407 child_honks = self.thread[:]
408 child_honks.reverse()
409 yield from reversed(child_honks)
412 def page_lonk(db_con, client_id, lonk_url, honk_url):
413 gethonks_answer = honk_url.get("gethonks", page="home")
416 for honk in reversed(gethonks_answer.pop("honks", None) or []):
417 convoy = honk["Convoy"]
419 if convoy not in lonk_page:
420 row = db_con.execute(
423 convoy_id, convoy, honk_id, handle, oondle, url, html, date, public, handles, honker_url, oonker_url
427 client_id=? AND convoy=?
432 lonk_page[convoy] = _LonkTreeItem(*row)
433 res_donks = db_con.execute(
434 "SELECT url, mime, alt_text FROM donk WHERE client_id=? AND convoy_id=?",
435 (client_id, lonk_page[convoy].convoy_id, )
438 donks = res_donks.fetchmany()
443 donk_url, donk_mime, donk_text = donk
444 lonk_page[convoy].donks.append((donk_url, donk_mime, donk_text))
446 if convoy not in lonk_page:
447 def _save_convoy(convoy, honk):
448 is_public = 1 if honk.get("Public") else 0
449 row = db_con.execute(
452 convoy(convoy, client_id, honk_id, handle, oondle, url, html, date, public, handles, honker_url, oonker_url)
454 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
456 convoy_id, convoy, honk_id, handle, oondle, url, html, date, public, handles, honker_url, oonker_url
458 (convoy, client_id, honk["ID"], honk["Handle"], honk.get("Oondle"), honk["XID"], honk["HTML"], honk["Date"], is_public, honk["Handles"], honk.get("Honker"), honk.get("Oonker"))
460 lonk_page[convoy] = _LonkTreeItem(*row)
462 for donk in (honk.get("Donks") or []):
463 donk_url = honk_url.build(path=f'/d/{donk["XID"]}') if donk.get("XID") else donk["URL"]
464 donk_mime, donk_text = donk["Media"], donk.get("Desc") or donk.get("Name") or None
466 "INSERT INTO donk (client_id, convoy_id, url, mime, alt_text) VALUES (?, ?, ?, ?, ?)",
467 (client_id, lonk_page[convoy].convoy_id, donk_url, donk_mime, donk_text, )
469 lonk_page[convoy].donks.append((donk_url, donk_mime, donk_text))
472 for honk_in_convoy in honk_url.get("gethonks", page="convoy", c=convoy)["honks"]:
473 if not honk_in_convoy.get("RID"):
474 _save_convoy(convoy, honk_in_convoy)
477 _save_convoy(convoy, {"ID": None, "Handle": None, "XID": None, "HTML": None, "Date": None, "Handles": None})
479 _save_convoy(convoy, honk)
482 lonk_page[convoy].thread.append(honk)
484 gethonks_answer["honks"] = []
485 for tree_item in reversed(lonk_page.values()):
486 gethonks_answer["honks"] += list(tree_item.iterate_honks())
488 print_header("lonk home")
489 print_gethonks(gethonks_answer, lonk_url, honk_url)
492 def page_convoy(db_con, client_id, lonk_url, honk_url):
493 query = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}
495 print("51 Not found\r")
498 gethonks_answer = honk_url.get("gethonks", page="convoy", c=query["c"])
499 print_header(f"convoy {query['c']}")
500 print_gethonks(gethonks_answer, lonk_url, honk_url)
503 def page_search(db_con, client_id, lonk_url, honk_url):
504 if not lonk_url.query:
505 print("10 What are we looking for?\r")
508 q = unquote(lonk_url.query)
509 gethonks_answer = honk_url.get("gethonks", page="search", q=q)
510 print_header(f"search - {q}")
511 print_gethonks(gethonks_answer, lonk_url, honk_url)
514 def page_atme(db_con, client_id, lonk_url, honk_url):
515 gethonks_answer = honk_url.get("gethonks", page="atme")
517 print_gethonks(gethonks_answer, lonk_url, honk_url)
520 def page_longago(db_con, client_id, lonk_url, honk_url):
521 gethonks_answer = honk_url.get("gethonks", page="longago")
522 print_header("long ago")
523 print_gethonks(gethonks_answer, lonk_url, honk_url)
526 def page_myhonks(db_con, client_id, lonk_url, honk_url):
527 gethonks_answer = honk_url.get("gethonks", page="myhonks")
528 print_header("my honks")
529 print_gethonks(gethonks_answer, lonk_url, honk_url)
532 def page_honker(db_con, client_id, lonk_url, honk_url):
533 xid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("xid")
535 print("51 Not found\r")
538 gethonks_answer = honk_url.get("gethonks", page="honker", xid=xid)
539 print_header(f"honks of {xid}")
540 print_gethonks(gethonks_answer, lonk_url, honk_url)
543 def bonk(db_con, client_id, lonk_url, honk_url):
544 what = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("w")
546 print("51 Not found\r")
549 honk_url.get("zonkit", wherefore="bonk", what=what, answer_is_json=False)
550 print(f'30 {lonk_url.build("myhonks")}\r')
553 def gethonkers(db_con, client_id, lonk_url, honk_url):
554 print_header("honkers")
555 print_menu(lonk_url, honk_url)
557 honkers = honk_url.get("gethonkers").get("honkers") or []
558 for honker in honkers:
559 print(f'## {honker.get("Name") or honker["XID"]}\r')
560 for field_name, display_name in zip(("Name", "XID", "Flavor"), ("name", "url", "flavor")):
561 value = honker.get(field_name)
563 print(f'{display_name}: {value}\r')
564 if honker.get("Flavor") == "sub":
565 print(f'=> {lonk_url.build("unsubscribe", urlencode({"honkerid": honker["ID"]}))} unsubscribe\r')
567 print(f'=> {lonk_url.build("subscribe", urlencode({"honkerid": honker["ID"]}))} (re)subscribe\r')
568 print(f'=> {lonk_url.build("honker", urlencode({"xid": honker["XID"]}))} honks of {honker["XID"]}\r')
573 print_menu(lonk_url, honk_url)
576 def addhonker(db_con, client_id, lonk_url, honk_url):
577 if not lonk_url.query:
578 print("10 honker url: \r")
581 url = unquote(lonk_url.query)
583 honk_url.get("savehonker", url=url, answer_is_json=False)
584 print(f'30 {lonk_url.build("gethonkers")}\r')
585 except HTTPError as error:
586 print_header("add new honker")
587 print_menu(lonk_url, honk_url)
590 print(f'> {error.fp.read().decode("utf8")}\r')
593 def unsubscribe(db_con, client_id, lonk_url, honk_url):
594 honkerid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("honkerid")
596 print("51 Not found\r")
599 url = unquote(lonk_url.query)
600 honk_url.get("savehonker", honkerid=honkerid, unsub="unsub", answer_is_json=False)
601 print(f'30 {lonk_url.build("gethonkers")}\r')
604 def subscribe(db_con, client_id, lonk_url, honk_url):
605 honkerid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("honkerid")
607 print("51 Not found\r")
610 url = unquote(lonk_url.query)
611 honk_url.get("savehonker", honkerid=honkerid, sub="sub", answer_is_json=False)
612 print(f'30 {lonk_url.build("gethonkers")}\r')
615 def newhonk(db_con, client_id, lonk_url, honk_url):
616 if not lonk_url.query:
617 print("10 let's make some noise: \r")
620 noise = unquote(lonk_url.query)
621 honk_url.get("honk", noise=noise, answer_is_json=False)
622 print(f'30 {lonk_url.build("myhonks")}\r')
625 def honkback(db_con, client_id, lonk_url, honk_url):
626 if not lonk_url.query:
627 handles = unquote(lonk_url.splitted_path[-3]).strip()
628 rid = unquote(lonk_url.splitted_path[-2])
629 print(f"10 Answer to {handles or rid}:\r")
632 noise = unquote(lonk_url.query)
633 rid = unquote(lonk_url.splitted_path[-2])
634 honk_url.get("honk", noise=noise, rid=rid, answer_is_json=False)
635 print(f'30 {lonk_url.build("myhonks")}\r')
638 def authenticated(cert_hash, lonk_url, fn_impl):
639 db_con = db_connect()
640 row = db_con.execute("SELECT client_id, honk_url, token FROM client WHERE cert_hash=?", (cert_hash, )).fetchone()
642 print(f'30 {lonk_url.build("ask_server")}\r')
644 client_id, honk_url, token = row
646 fn_impl(db_con, client_id, lonk_url, HonkUrl(honk_url, token))
649 def new_client_stage_1_ask_server(lonk_url):
650 if not lonk_url.query:
651 print("10 Honk server URL\r")
653 splitted = urlsplit(unquote(lonk_url.query))
654 path = [quote(urlunsplit((splitted.scheme, splitted.netloc, "", "", "")), safe=""), "ask_username"]
655 print(f'30 {lonk_url.build(path)}\r')
658 def new_client_stage_2_ask_username(lonk_url):
659 if not lonk_url.query:
660 print("10 Honk user name\r")
662 if len(lonk_url.splitted_path) < 3:
663 print('59 Bad request\r')
665 quoted_server = lonk_url.splitted_path[-2]
666 path = [quoted_server, quote(unquote(lonk_url.query), safe=""), "ask_password"]
667 print(f'30 {lonk_url.build(path)}\r')
670 def new_client_stage_3_ask_password(cert_hash, lonk_url):
671 if not lonk_url.query:
672 print("11 Honk user password\r")
674 if len(lonk_url.splitted_path) < 4:
675 print('59 Bad request\r')
678 honk_url = unquote(lonk_url.splitted_path[-3])
680 "username": unquote(lonk_url.splitted_path[-2]),
681 "password": unquote(lonk_url.query),
684 with urlopen(honk_url + "/dologin", data=urlencode(post_data).encode(), timeout=15) as response:
685 token = response.read().decode("utf8")
686 db_con = db_connect()
689 "INSERT INTO client (cert_hash, honk_url, token) VALUES (?, ?, ?)",
690 (cert_hash, honk_url, token)
692 print(f'30 {lonk_url.build([])}\r')
695 def proxy(mime, url):
696 with urlopen(url, timeout=10) as response:
697 stdout.buffer.write(b"20 " + mime.encode() + b"\r\n")
699 content = response.read(512 * 1024)
702 stdout.buffer.write(content)
705 def vgi(cert_hash, raw_url):
706 lonk_url = LonkUrl(raw_url)
707 if lonk_url.page == "lonk":
708 authenticated(cert_hash, lonk_url, page_lonk)
709 elif lonk_url.page == "convoy":
710 authenticated(cert_hash, lonk_url, page_convoy)
711 elif lonk_url.page == "atme":
712 authenticated(cert_hash, lonk_url, page_atme)
713 elif lonk_url.page == "search":
714 authenticated(cert_hash, lonk_url, page_search)
715 elif lonk_url.page == "longago":
716 authenticated(cert_hash, lonk_url, page_longago)
717 elif lonk_url.page == "myhonks":
718 authenticated(cert_hash, lonk_url, page_myhonks)
719 elif lonk_url.page == "honker":
720 authenticated(cert_hash, lonk_url, page_honker)
721 elif lonk_url.page == "bonk":
722 authenticated(cert_hash, lonk_url, bonk)
723 elif lonk_url.page == "gethonkers":
724 authenticated(cert_hash, lonk_url, gethonkers)
725 elif lonk_url.page == "addhonker":
726 authenticated(cert_hash, lonk_url, addhonker)
727 elif lonk_url.page == "unsubscribe":
728 authenticated(cert_hash, lonk_url, unsubscribe)
729 elif lonk_url.page == "subscribe":
730 authenticated(cert_hash, lonk_url, subscribe)
731 elif lonk_url.page == "newhonk":
732 authenticated(cert_hash, lonk_url, newhonk)
733 elif lonk_url.page == "honkback":
734 authenticated(cert_hash, lonk_url, honkback)
735 elif lonk_url.page == "ask_server":
736 new_client_stage_1_ask_server(lonk_url)
737 elif lonk_url.page == "ask_username":
738 new_client_stage_2_ask_username(lonk_url)
739 elif lonk_url.page == "ask_password":
740 new_client_stage_3_ask_password(cert_hash, lonk_url)
741 elif lonk_url.page == "proxy":
742 query = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}
743 if "m" not in query or "u" not in query:
744 print("51 Not found\r")
746 proxy(mime=query["m"], url=query["u"])
748 print("51 Not found\r")
751 if __name__ == '__main__':
752 cert_hash_ = environ.get("VGI_CERT_HASH")
755 start_time = clock_gettime(CLOCK_MONOTONIC)
757 input_url = input().strip()
758 vgi(cert_hash_, input_url)
760 stderr.write(f"{cert_hash_}|{input_url}|{clock_gettime(CLOCK_MONOTONIC) - start_time:.3f}sec.\n")
761 except HTTPError as error:
762 stderr.write(f"{error}\n")
763 print(f"43 Remote server return {error.code}: {error.reason}\r")
764 except URLError as error:
765 stderr.write(f"{error}\n")
766 print(f"43 Error while trying to access remote server: {error.reason}\r")
767 except TimeoutError as error:
768 stderr.write(f"{error}\n")
769 print(f"43 Error while trying to access remote server: {error}\r")
771 stderr.write("Certificate required\n")
772 print("60 Certificate required\r")