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 start_time = clock_gettime(CLOCK_MONOTONIC)
208 query = {**{"action": action, "token": self._token}, **kwargs}
209 with urlopen(self.build(path="api", query=urlencode(query)), timeout=45) as response:
210 answer = response.read().decode("utf8")
211 return json_loads(answer) if answer_is_json else answer
213 stderr.write(f"GET {action} {kwargs}|{clock_gettime(CLOCK_MONOTONIC) - start_time:.3f}sec.\n")
216 def db_create_schema(db_con):
221 cert_hash TEXT PRIMARY KEY,
222 honk_url TEXT NOT NULL,
230 db_file_path = Path(__file__).parent / ".local" / "db"
231 db_file_path.parent.mkdir(parents=True, exist_ok=True)
232 db_exist = db_file_path.exists()
233 db_con = sqlite3_connect(db_file_path)
236 db_create_schema(db_con)
240 def print_header(page_name):
241 print("20 text/gemini\r")
242 print(f"# 𝓗 onk: {page_name}\r")
246 def print_menu(lonk_url, honk_url, gethonks_answer=None):
248 print(f"=> {lonk_url.build('newhonk')} new honk\r")
249 print(f"=> {lonk_url.build([])} lonk home\r")
250 print(f"=> {lonk_url.build('first')} first class only\r")
253 line = f"=> {lonk_url.build('atme')} @me"
254 if gethonks_answer["mecount"]:
255 line += f' ({gethonks_answer["mecount"]})'
258 line = f"=> {honk_url.build(path='chatter')} chatter"
259 if gethonks_answer["chatcount"]:
260 line += f' ({gethonks_answer["chatcount"]})'
263 print(f"=> {lonk_url.build('search')} search\r")
264 print(f"=> {lonk_url.build('longago')} long ago\r")
265 print(f"=> {lonk_url.build('myhonks')} my honks\r")
266 print(f"=> {lonk_url.build('gethonkers')} honkers\r")
267 print(f"=> {lonk_url.build('addhonker')} add new honker\r")
268 print(f"=> {lonk_url.build('digest')} digest\r")
271 def print_gethonks(gethonks_answer, lonk_url, honk_url):
272 print_menu(lonk_url, honk_url, gethonks_answer)
275 for honk in gethonks_answer.get("honks") or []:
276 convoy = honk["Convoy"]
277 re_url = honk.get("RID")
278 oondle = honk.get("Oondle")
279 from_ = f'{oondle} (🔁 {honk["Handle"]})' if oondle else f'{honk["Handle"]}'
281 f'##{"# ↱" if re_url else ""} From {from_} {honk["Date"]}',
282 f'=> {lonk_url.build("convoy", urlencode({"c": convoy}))} Convoy {convoy}',
286 lines.append(f'=> {re_url} Re: {re_url}')
288 lines.append(HtmlToGmi(honk_url.build(), lonk_url.media).feed(honk["HTML"]))
289 for donk in honk.get("Donks") or []:
291 donk_url = honk_url.build(path=f'/d/{donk["XID"]}')
293 donk_url = urljoin(honk["XID"], donk["URL"])
294 donk_mime = donk["Media"]
295 lines.append(f'=> {lonk_url.media(donk_mime, donk_url)} {donk_url}')
296 donk_text = donk.get("Desc") or donk.get("Name") or None
298 lines.append(donk_text)
300 if honk.get("Public"):
301 lines.append(f'=> {lonk_url.build("bonk", urlencode({"w": honk["XID"]}))} ↺ bonk')
302 honk_back_url = lonk_url.build(
304 quote(honk["Handles"] or " ", safe=""),
305 quote(honk["XID"], safe=""),
309 lines.append(f'=> {honk_back_url} ↱ honk back')
310 for xonker in (honk.get("Honker"), honk.get("Oonker")):
312 lines.append(f'=> {lonk_url.build("honker", urlencode({"xid": xonker}))} honks of {xonker}')
313 print("\r\n".join(lines))
316 if gethonks_answer.get("honks"):
318 print_menu(lonk_url, honk_url, gethonks_answer)
322 def __init__(self, honk):
326 def iterate_honks(self):
327 if self.honk is not None:
329 yield from self.thread
332 def page_lonk(lonk_url, honk_url):
333 gethonks_answer = honk_url.get("gethonks", page="home")
337 for i, honk in enumerate(reversed(gethonks_answer["honks"])):
338 timeline.setdefault(honk["Convoy"], i)
340 lonk_tree.setdefault(honk["Convoy"], _LonkTreeItem(None))
342 lonk_tree[honk["Convoy"]] = _LonkTreeItem(honk)
344 # fetch first 36 threads (without start honk)
345 sorted_ = sorted(timeline.keys(), key=lambda convoy: timeline[convoy], reverse=True)
347 for convoy in [convoy for convoy in sorted_ if lonk_tree[convoy].honk is None][:36]:
348 for honk in honk_url.get("gethonks", page="convoy", c=convoy)["honks"]:
349 if not honk.get("RID"):
350 if convoy != honk["Convoy"]:
351 correction_map[convoy] = honk["Convoy"]
352 tl_weight_1 = timeline.pop(convoy)
353 convoy = honk["Convoy"]
354 tl_weight_2 = timeline.get(convoy)
355 timeline[convoy] = tl_weight_1 if tl_weight_2 is None else min(tl_weight_1, tl_weight_2)
357 item = lonk_tree.get(convoy)
359 lonk_tree[convoy] = _LonkTreeItem(honk)
360 elif item.honk is None:
364 # no thread start found
367 # link answers to thread
368 for honk in reversed(gethonks_answer.pop("honks")):
370 item = lonk_tree.get(correction_map.get(honk["Convoy"], honk["Convoy"]))
372 if item.honk is not None:
373 item.thread.append(honk)
375 # build honks for page
376 gethonks_answer["honks"] = []
377 for convoy in sorted(timeline.keys(), key=lambda convoy: timeline[convoy], reverse=True):
378 item = lonk_tree[convoy]
379 if item.honk is None:
380 break # first unfetched
381 gethonks_answer["honks"] += list(item.iterate_honks())
383 print_header("lonk home")
384 print_gethonks(gethonks_answer, lonk_url, honk_url)
387 def page_first(lonk_url, honk_url):
388 gethonks_answer = honk_url.get("gethonks", page="home")
389 gethonks_answer["honks"] = [honk for honk in gethonks_answer.pop("honks") if honk["What"] in {"bonked", "honked"}]
390 print_header("first class only")
391 print_gethonks(gethonks_answer, lonk_url, honk_url)
394 def page_convoy(lonk_url, honk_url):
395 query = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}
397 print("51 Not found\r")
400 gethonks_answer = honk_url.get("gethonks", page="convoy", c=query["c"])
401 print_header(f"convoy {query['c']}")
402 print_gethonks(gethonks_answer, lonk_url, honk_url)
405 def page_search(lonk_url, honk_url):
406 if not lonk_url.query:
407 print("10 What are we looking for?\r")
410 q = unquote(lonk_url.query)
411 gethonks_answer = honk_url.get("gethonks", page="search", q=q)
412 print_header(f"search - {q}")
413 print_gethonks(gethonks_answer, lonk_url, honk_url)
416 def page_atme(lonk_url, honk_url):
417 gethonks_answer = honk_url.get("gethonks", page="atme")
419 print_gethonks(gethonks_answer, lonk_url, honk_url)
422 def page_longago(lonk_url, honk_url):
423 gethonks_answer = honk_url.get("gethonks", page="longago")
424 print_header("long ago")
425 print_gethonks(gethonks_answer, lonk_url, honk_url)
428 def page_myhonks(lonk_url, honk_url):
429 gethonks_answer = honk_url.get("gethonks", page="myhonks")
430 print_header("my honks")
431 print_gethonks(gethonks_answer, lonk_url, honk_url)
434 def page_honker(lonk_url, honk_url):
435 xid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("xid")
437 print("51 Not found\r")
440 gethonks_answer = honk_url.get("gethonks", page="honker", xid=xid)
441 print_header(f"honks of {xid}")
442 print_gethonks(gethonks_answer, lonk_url, honk_url)
445 def page_digest(lonk_url, honk_url):
446 gethonks_answer = honk_url.get("gethonks", page="home")
449 for honk in reversed(gethonks_answer["honks"]):
450 honks[honk["Convoy"]] = True
452 print("20 text/gemini\r")
453 print("# 𝓗 onk: digest\r")
456 print("AV-98+cert> tour *\r")
460 for convoy in reversed(honks.keys()):
461 print(f'=> {lonk_url.build("convoy", urlencode({"c": convoy}))} Convoy {convoy}\r')
465 print("AV-98+cert> tour *\r")
468 print_menu(lonk_url, honk_url)
471 def bonk(lonk_url, honk_url):
472 what = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("w")
474 print("51 Not found\r")
477 honk_url.get("zonkit", wherefore="bonk", what=what, answer_is_json=False)
478 print(f'30 {lonk_url.build("myhonks")}\r')
481 def gethonkers(lonk_url, honk_url):
482 print_header("honkers")
483 print_menu(lonk_url, honk_url)
485 honkers = honk_url.get("gethonkers").get("honkers") or []
486 for honker in honkers:
487 print(f'## {honker.get("Name") or honker["XID"]}\r')
488 for field_name, display_name in zip(("Name", "XID", "Flavor"), ("name", "url", "flavor")):
489 value = honker.get(field_name)
491 print(f'{display_name}: {value}\r')
492 if honker.get("Flavor") == "sub":
493 print(f'=> {lonk_url.build("unsubscribe", urlencode({"honkerid": honker["ID"]}))} unsubscribe\r')
495 print(f'=> {lonk_url.build("subscribe", urlencode({"honkerid": honker["ID"]}))} (re)subscribe\r')
496 print(f'=> {lonk_url.build("honker", urlencode({"xid": honker["XID"]}))} honks of {honker["XID"]}\r')
501 print_menu(lonk_url, honk_url)
504 def addhonker(lonk_url, honk_url):
505 if not lonk_url.query:
506 print("10 honker url: \r")
509 url = unquote(lonk_url.query)
511 honk_url.get("savehonker", url=url, answer_is_json=False)
512 print(f'30 {lonk_url.build("gethonkers")}\r')
513 except HTTPError as error:
514 print_header("add new honker")
515 print_menu(lonk_url, honk_url)
518 print(f'> {error.fp.read().decode("utf8")}\r')
521 def unsubscribe(lonk_url, honk_url):
522 honkerid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("honkerid")
524 print("51 Not found\r")
527 url = unquote(lonk_url.query)
528 honk_url.get("savehonker", honkerid=honkerid, unsub="unsub", answer_is_json=False)
529 print(f'30 {lonk_url.build("gethonkers")}\r')
532 def subscribe(lonk_url, honk_url):
533 honkerid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("honkerid")
535 print("51 Not found\r")
538 url = unquote(lonk_url.query)
539 honk_url.get("savehonker", honkerid=honkerid, sub="sub", answer_is_json=False)
540 print(f'30 {lonk_url.build("gethonkers")}\r')
543 def newhonk(lonk_url, honk_url):
544 if not lonk_url.query:
545 print("10 let's make some noise: \r")
548 noise = unquote(lonk_url.query)
549 honk_url.get("honk", noise=noise, answer_is_json=False)
550 print(f'30 {lonk_url.build("myhonks")}\r')
553 def honkback(lonk_url, honk_url):
554 if not lonk_url.query:
555 handles = unquote(lonk_url.splitted_path[-3]).strip()
556 rid = unquote(lonk_url.splitted_path[-2])
557 print(f"10 Answer to {handles or rid}:\r")
560 noise = unquote(lonk_url.query)
561 rid = unquote(lonk_url.splitted_path[-2])
562 honk_url.get("honk", noise=noise, rid=rid, answer_is_json=False)
563 print(f'30 {lonk_url.build("myhonks")}\r')
566 def authenticated(cert_hash, lonk_url, fn_impl):
567 db_con = db_connect()
568 row = db_con.execute("SELECT honk_url, token FROM client WHERE cert_hash=?", (cert_hash, )).fetchone()
570 print(f'30 {lonk_url.build("ask_server")}\r')
572 honk_url, token = row
574 fn_impl(lonk_url, HonkUrl(honk_url, token))
577 def new_client_stage_1_ask_server(lonk_url):
578 if not lonk_url.query:
579 print("10 Honk server URL\r")
581 splitted = urlsplit(unquote(lonk_url.query))
582 path = [quote(urlunsplit((splitted.scheme, splitted.netloc, "", "", "")), safe=""), "ask_username"]
583 print(f'30 {lonk_url.build(path)}\r')
586 def new_client_stage_2_ask_username(lonk_url):
587 if not lonk_url.query:
588 print("10 Honk user name\r")
590 if len(lonk_url.splitted_path) < 3:
591 print('59 Bad request\r')
593 quoted_server = lonk_url.splitted_path[-2]
594 path = [quoted_server, quote(unquote(lonk_url.query), safe=""), "ask_password"]
595 print(f'30 {lonk_url.build(path)}\r')
598 def new_client_stage_3_ask_password(cert_hash, lonk_url):
599 if not lonk_url.query:
600 print("11 Honk user password\r")
602 if len(lonk_url.splitted_path) < 4:
603 print('59 Bad request\r')
606 honk_url = unquote(lonk_url.splitted_path[-3])
608 "username": unquote(lonk_url.splitted_path[-2]),
609 "password": unquote(lonk_url.query),
612 with urlopen(honk_url + "/dologin", data=urlencode(post_data).encode(), timeout=15) as response:
613 token = response.read().decode("utf8")
614 db_con = db_connect()
617 "INSERT OR REPLACE INTO client (cert_hash, honk_url, token) VALUES (?, ?, ?)",
618 (cert_hash, honk_url, token)
620 print(f'30 {lonk_url.build([])}\r')
623 def proxy(mime, url):
624 with urlopen(url, timeout=10) as response:
625 stdout.buffer.write(b"20 " + mime.encode() + b"\r\n")
627 content = response.read(512 * 1024)
630 stdout.buffer.write(content)
633 def vgi(cert_hash, lonk_url):
634 if lonk_url.page == "lonk":
635 authenticated(cert_hash, lonk_url, page_lonk)
636 elif lonk_url.page == "first":
637 authenticated(cert_hash, lonk_url, page_first)
638 elif lonk_url.page == "convoy":
639 authenticated(cert_hash, lonk_url, page_convoy)
640 elif lonk_url.page == "atme":
641 authenticated(cert_hash, lonk_url, page_atme)
642 elif lonk_url.page == "search":
643 authenticated(cert_hash, lonk_url, page_search)
644 elif lonk_url.page == "longago":
645 authenticated(cert_hash, lonk_url, page_longago)
646 elif lonk_url.page == "myhonks":
647 authenticated(cert_hash, lonk_url, page_myhonks)
648 elif lonk_url.page == "honker":
649 authenticated(cert_hash, lonk_url, page_honker)
650 elif lonk_url.page == "digest":
651 authenticated(cert_hash, lonk_url, page_digest)
652 elif lonk_url.page == "bonk":
653 authenticated(cert_hash, lonk_url, bonk)
654 elif lonk_url.page == "gethonkers":
655 authenticated(cert_hash, lonk_url, gethonkers)
656 elif lonk_url.page == "addhonker":
657 authenticated(cert_hash, lonk_url, addhonker)
658 elif lonk_url.page == "unsubscribe":
659 authenticated(cert_hash, lonk_url, unsubscribe)
660 elif lonk_url.page == "subscribe":
661 authenticated(cert_hash, lonk_url, subscribe)
662 elif lonk_url.page == "newhonk":
663 authenticated(cert_hash, lonk_url, newhonk)
664 elif lonk_url.page == "honkback":
665 authenticated(cert_hash, lonk_url, honkback)
666 elif lonk_url.page == "ask_server":
667 new_client_stage_1_ask_server(lonk_url)
668 elif lonk_url.page == "ask_username":
669 new_client_stage_2_ask_username(lonk_url)
670 elif lonk_url.page == "ask_password":
671 new_client_stage_3_ask_password(cert_hash, lonk_url)
672 elif lonk_url.page == "proxy":
673 query = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}
674 if "m" not in query or "u" not in query:
675 print("51 Not found\r")
677 proxy(mime=query["m"], url=query["u"])
679 print("51 Not found\r")
683 cert_hash_ = environ.get("VGI_CERT_HASH")
685 input_url = input().strip()
686 lonk_url = LonkUrl(input_url)
688 start_time = clock_gettime(CLOCK_MONOTONIC)
690 vgi(cert_hash_, lonk_url)
692 stderr.write(f"{cert_hash_}|{input_url}|{clock_gettime(CLOCK_MONOTONIC) - start_time:.3f}sec.\n")
693 except HTTPError as error:
694 stderr.write(f"{error}\n")
695 if error.code == 403 and lonk_url.page != "proxy":
696 print("20 text/gemini\r")
699 print(f"Remote server return {error.code}: {error.reason}\r")
701 print("The previously issued token has probably expired. You need to authenticate again:\r")
702 print(f'=> {lonk_url.build("ask_server")}\r')
704 print(f"43 Remote server return {error.code}: {error.reason}\r")
705 except URLError as error:
706 stderr.write(f"{error}\n")
707 print(f"43 Error while trying to access remote server: {error.reason}\r")
708 except TimeoutError as error:
709 stderr.write(f"{error}\n")
710 print(f"43 Error while trying to access remote server: {error}\r")
712 stderr.write("Certificate required\n")
713 print("60 Certificate required\r")
716 if __name__ == '__main__':