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")
270 def print_gethonks(gethonks_answer, lonk_url, honk_url):
271 print_menu(lonk_url, honk_url, gethonks_answer)
274 for honk in gethonks_answer.get("honks") or []:
275 convoy = honk["Convoy"]
276 re_url = honk.get("RID")
277 oondle = honk.get("Oondle")
278 from_ = f'{oondle} (🔁 {honk["Handle"]})' if oondle else f'{honk["Handle"]}'
280 f'##{"# ↱" if re_url else ""} From {from_} {honk["Date"]}',
281 f'=> {lonk_url.build("convoy", urlencode({"c": convoy}))} Convoy {convoy}',
285 lines.append(f'=> {re_url} Re: {re_url}')
287 lines.append(HtmlToGmi(honk_url.build(), lonk_url.media).feed(honk["HTML"]))
288 for donk in honk.get("Donks") or []:
290 donk_url = honk_url.build(path=f'/d/{donk["XID"]}')
292 donk_url = urljoin(honk["XID"], donk["URL"])
293 donk_mime = donk["Media"]
294 lines.append(f'=> {lonk_url.media(donk_mime, donk_url)} {donk_url}')
295 donk_text = donk.get("Desc") or donk.get("Name") or None
297 lines.append(donk_text)
299 if honk.get("Public"):
300 lines.append(f'=> {lonk_url.build("bonk", urlencode({"w": honk["XID"]}))} ↺ bonk')
301 honk_back_url = lonk_url.build(
303 quote(honk["Handles"] or " ", safe=""),
304 quote(honk["XID"], safe=""),
308 lines.append(f'=> {honk_back_url} ↱ honk back')
309 for xonker in (honk.get("Honker"), honk.get("Oonker")):
311 lines.append(f'=> {lonk_url.build("honker", urlencode({"xid": xonker}))} honks of {xonker}')
312 print("\r\n".join(lines))
315 if gethonks_answer.get("honks"):
317 print_menu(lonk_url, honk_url, gethonks_answer)
321 def __init__(self, honk):
325 def iterate_honks(self):
326 if self.honk is not None:
328 yield from self.thread
331 def page_lonk(lonk_url, honk_url):
332 gethonks_answer = honk_url.get("gethonks", page="home")
336 for i, honk in enumerate(reversed(gethonks_answer["honks"])):
337 timeline.setdefault(honk["Convoy"], i)
339 lonk_tree.setdefault(honk["Convoy"], _LonkTreeItem(None))
341 lonk_tree[honk["Convoy"]] = _LonkTreeItem(honk)
343 # fetch first 36 threads (without start honk)
344 sorted_ = sorted(timeline.keys(), key=lambda convoy: timeline[convoy], reverse=True)
346 for convoy in [convoy for convoy in sorted_ if lonk_tree[convoy].honk is None][:36]:
347 for honk in honk_url.get("gethonks", page="convoy", c=convoy)["honks"]:
348 if not honk.get("RID"):
349 if convoy != honk["Convoy"]:
350 correction_map[convoy] = honk["Convoy"]
351 tl_weight_1 = timeline.pop(convoy)
352 convoy = honk["Convoy"]
353 tl_weight_2 = timeline.get(convoy)
354 timeline[convoy] = tl_weight_1 if tl_weight_2 is None else min(tl_weight_1, tl_weight_2)
356 item = lonk_tree.get(convoy)
358 lonk_tree[convoy] = _LonkTreeItem(honk)
359 elif item.honk is None:
363 # no thread start found
366 # link answers to thread
367 for honk in reversed(gethonks_answer.pop("honks")):
369 item = lonk_tree.get(correction_map.get(honk["Convoy"], honk["Convoy"]))
371 if item.honk is not None:
372 item.thread.append(honk)
374 # build honks for page
375 gethonks_answer["honks"] = []
376 for convoy in sorted(timeline.keys(), key=lambda convoy: timeline[convoy], reverse=True):
377 item = lonk_tree[convoy]
378 if item.honk is None:
379 break # first unfetched
380 gethonks_answer["honks"] += list(item.iterate_honks())
382 print_header("lonk home")
383 print_gethonks(gethonks_answer, lonk_url, honk_url)
386 def page_first(lonk_url, honk_url):
387 gethonks_answer = honk_url.get("gethonks", page="home")
388 gethonks_answer["honks"] = [honk for honk in gethonks_answer.pop("honks") if honk["What"] in {"bonked", "honked"}]
389 print_header("first class only")
390 print_gethonks(gethonks_answer, lonk_url, honk_url)
393 def page_convoy(lonk_url, honk_url):
394 query = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}
396 print("51 Not found\r")
399 gethonks_answer = honk_url.get("gethonks", page="convoy", c=query["c"])
400 print_header(f"convoy {query['c']}")
401 print_gethonks(gethonks_answer, lonk_url, honk_url)
404 def page_search(lonk_url, honk_url):
405 if not lonk_url.query:
406 print("10 What are we looking for?\r")
409 q = unquote(lonk_url.query)
410 gethonks_answer = honk_url.get("gethonks", page="search", q=q)
411 print_header(f"search - {q}")
412 print_gethonks(gethonks_answer, lonk_url, honk_url)
415 def page_atme(lonk_url, honk_url):
416 gethonks_answer = honk_url.get("gethonks", page="atme")
418 print_gethonks(gethonks_answer, lonk_url, honk_url)
421 def page_longago(lonk_url, honk_url):
422 gethonks_answer = honk_url.get("gethonks", page="longago")
423 print_header("long ago")
424 print_gethonks(gethonks_answer, lonk_url, honk_url)
427 def page_myhonks(lonk_url, honk_url):
428 gethonks_answer = honk_url.get("gethonks", page="myhonks")
429 print_header("my honks")
430 print_gethonks(gethonks_answer, lonk_url, honk_url)
433 def page_honker(lonk_url, honk_url):
434 xid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("xid")
436 print("51 Not found\r")
439 gethonks_answer = honk_url.get("gethonks", page="honker", xid=xid)
440 print_header(f"honks of {xid}")
441 print_gethonks(gethonks_answer, lonk_url, honk_url)
444 def bonk(lonk_url, honk_url):
445 what = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("w")
447 print("51 Not found\r")
450 honk_url.get("zonkit", wherefore="bonk", what=what, answer_is_json=False)
451 print(f'30 {lonk_url.build("myhonks")}\r')
454 def gethonkers(lonk_url, honk_url):
455 print_header("honkers")
456 print_menu(lonk_url, honk_url)
458 honkers = honk_url.get("gethonkers").get("honkers") or []
459 for honker in honkers:
460 print(f'## {honker.get("Name") or honker["XID"]}\r')
461 for field_name, display_name in zip(("Name", "XID", "Flavor"), ("name", "url", "flavor")):
462 value = honker.get(field_name)
464 print(f'{display_name}: {value}\r')
465 if honker.get("Flavor") == "sub":
466 print(f'=> {lonk_url.build("unsubscribe", urlencode({"honkerid": honker["ID"]}))} unsubscribe\r')
468 print(f'=> {lonk_url.build("subscribe", urlencode({"honkerid": honker["ID"]}))} (re)subscribe\r')
469 print(f'=> {lonk_url.build("honker", urlencode({"xid": honker["XID"]}))} honks of {honker["XID"]}\r')
474 print_menu(lonk_url, honk_url)
477 def addhonker(lonk_url, honk_url):
478 if not lonk_url.query:
479 print("10 honker url: \r")
482 url = unquote(lonk_url.query)
484 honk_url.get("savehonker", url=url, answer_is_json=False)
485 print(f'30 {lonk_url.build("gethonkers")}\r')
486 except HTTPError as error:
487 print_header("add new honker")
488 print_menu(lonk_url, honk_url)
491 print(f'> {error.fp.read().decode("utf8")}\r')
494 def unsubscribe(lonk_url, honk_url):
495 honkerid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("honkerid")
497 print("51 Not found\r")
500 url = unquote(lonk_url.query)
501 honk_url.get("savehonker", honkerid=honkerid, unsub="unsub", answer_is_json=False)
502 print(f'30 {lonk_url.build("gethonkers")}\r')
505 def subscribe(lonk_url, honk_url):
506 honkerid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("honkerid")
508 print("51 Not found\r")
511 url = unquote(lonk_url.query)
512 honk_url.get("savehonker", honkerid=honkerid, sub="sub", answer_is_json=False)
513 print(f'30 {lonk_url.build("gethonkers")}\r')
516 def newhonk(lonk_url, honk_url):
517 if not lonk_url.query:
518 print("10 let's make some noise: \r")
521 noise = unquote(lonk_url.query)
522 honk_url.get("honk", noise=noise, answer_is_json=False)
523 print(f'30 {lonk_url.build("myhonks")}\r')
526 def honkback(lonk_url, honk_url):
527 if not lonk_url.query:
528 handles = unquote(lonk_url.splitted_path[-3]).strip()
529 rid = unquote(lonk_url.splitted_path[-2])
530 print(f"10 Answer to {handles or rid}:\r")
533 noise = unquote(lonk_url.query)
534 rid = unquote(lonk_url.splitted_path[-2])
535 honk_url.get("honk", noise=noise, rid=rid, answer_is_json=False)
536 print(f'30 {lonk_url.build("myhonks")}\r')
539 def authenticated(cert_hash, lonk_url, fn_impl):
540 db_con = db_connect()
541 row = db_con.execute("SELECT honk_url, token FROM client WHERE cert_hash=?", (cert_hash, )).fetchone()
543 print(f'30 {lonk_url.build("ask_server")}\r')
545 honk_url, token = row
547 fn_impl(lonk_url, HonkUrl(honk_url, token))
550 def new_client_stage_1_ask_server(lonk_url):
551 if not lonk_url.query:
552 print("10 Honk server URL\r")
554 splitted = urlsplit(unquote(lonk_url.query))
555 path = [quote(urlunsplit((splitted.scheme, splitted.netloc, "", "", "")), safe=""), "ask_username"]
556 print(f'30 {lonk_url.build(path)}\r')
559 def new_client_stage_2_ask_username(lonk_url):
560 if not lonk_url.query:
561 print("10 Honk user name\r")
563 if len(lonk_url.splitted_path) < 3:
564 print('59 Bad request\r')
566 quoted_server = lonk_url.splitted_path[-2]
567 path = [quoted_server, quote(unquote(lonk_url.query), safe=""), "ask_password"]
568 print(f'30 {lonk_url.build(path)}\r')
571 def new_client_stage_3_ask_password(cert_hash, lonk_url):
572 if not lonk_url.query:
573 print("11 Honk user password\r")
575 if len(lonk_url.splitted_path) < 4:
576 print('59 Bad request\r')
579 honk_url = unquote(lonk_url.splitted_path[-3])
581 "username": unquote(lonk_url.splitted_path[-2]),
582 "password": unquote(lonk_url.query),
585 with urlopen(honk_url + "/dologin", data=urlencode(post_data).encode(), timeout=15) as response:
586 token = response.read().decode("utf8")
587 db_con = db_connect()
590 "INSERT OR REPLACE INTO client (cert_hash, honk_url, token) VALUES (?, ?, ?)",
591 (cert_hash, honk_url, token)
593 print(f'30 {lonk_url.build([])}\r')
596 def proxy(mime, url):
597 with urlopen(url, timeout=10) as response:
598 stdout.buffer.write(b"20 " + mime.encode() + b"\r\n")
600 content = response.read(512 * 1024)
603 stdout.buffer.write(content)
606 def vgi(cert_hash, lonk_url):
607 if lonk_url.page == "lonk":
608 authenticated(cert_hash, lonk_url, page_lonk)
609 elif lonk_url.page == "first":
610 authenticated(cert_hash, lonk_url, page_first)
611 elif lonk_url.page == "convoy":
612 authenticated(cert_hash, lonk_url, page_convoy)
613 elif lonk_url.page == "atme":
614 authenticated(cert_hash, lonk_url, page_atme)
615 elif lonk_url.page == "search":
616 authenticated(cert_hash, lonk_url, page_search)
617 elif lonk_url.page == "longago":
618 authenticated(cert_hash, lonk_url, page_longago)
619 elif lonk_url.page == "myhonks":
620 authenticated(cert_hash, lonk_url, page_myhonks)
621 elif lonk_url.page == "honker":
622 authenticated(cert_hash, lonk_url, page_honker)
623 elif lonk_url.page == "bonk":
624 authenticated(cert_hash, lonk_url, bonk)
625 elif lonk_url.page == "gethonkers":
626 authenticated(cert_hash, lonk_url, gethonkers)
627 elif lonk_url.page == "addhonker":
628 authenticated(cert_hash, lonk_url, addhonker)
629 elif lonk_url.page == "unsubscribe":
630 authenticated(cert_hash, lonk_url, unsubscribe)
631 elif lonk_url.page == "subscribe":
632 authenticated(cert_hash, lonk_url, subscribe)
633 elif lonk_url.page == "newhonk":
634 authenticated(cert_hash, lonk_url, newhonk)
635 elif lonk_url.page == "honkback":
636 authenticated(cert_hash, lonk_url, honkback)
637 elif lonk_url.page == "ask_server":
638 new_client_stage_1_ask_server(lonk_url)
639 elif lonk_url.page == "ask_username":
640 new_client_stage_2_ask_username(lonk_url)
641 elif lonk_url.page == "ask_password":
642 new_client_stage_3_ask_password(cert_hash, lonk_url)
643 elif lonk_url.page == "proxy":
644 query = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}
645 if "m" not in query or "u" not in query:
646 print("51 Not found\r")
648 proxy(mime=query["m"], url=query["u"])
650 print("51 Not found\r")
654 cert_hash_ = environ.get("VGI_CERT_HASH")
656 input_url = input().strip()
657 lonk_url = LonkUrl(input_url)
659 start_time = clock_gettime(CLOCK_MONOTONIC)
661 vgi(cert_hash_, lonk_url)
663 stderr.write(f"{cert_hash_}|{input_url}|{clock_gettime(CLOCK_MONOTONIC) - start_time:.3f}sec.\n")
664 except HTTPError as error:
665 stderr.write(f"{error}\n")
666 if error.code == 403:
667 print("20 text/gemini\r")
670 print(f"Remote server return {error.code}: {error.reason}\r")
672 print("The previously issued token has probably expired. You need to authenticate again:\r")
673 print(f'=> {lonk_url.build("ask_server")}\r')
675 print(f"43 Remote server return {error.code}: {error.reason}\r")
676 except URLError as error:
677 stderr.write(f"{error}\n")
678 print(f"43 Error while trying to access remote server: {error.reason}\r")
679 except TimeoutError as error:
680 stderr.write(f"{error}\n")
681 print(f"43 Error while trying to access remote server: {error}\r")
683 stderr.write("Certificate required\n")
684 print("60 Certificate required\r")
687 if __name__ == '__main__':