Blob


1 #!/usr/bin/env python3
2 from json import loads as json_loads
3 from mimetypes import guess_type
4 from os import environ
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
15 class _BaseTag:
16 def __init__(self, tag, attrs):
17 self.tag = tag
19 def on_data(self, data):
20 raise AssertionError("The method must be overridden")
22 def flush(self):
23 raise AssertionError("The method must be overridden")
26 class ParagraphTag(_BaseTag):
27 def __init__(self, tag, attrs):
28 super().__init__(tag, attrs)
29 self.content = []
30 self.footer = []
32 def on_data(self, data):
33 self.content.append(data.strip())
35 def flush(self):
36 result = " ".join(" ".join(data.split()) for data in self.content if data)
37 footer = self.footer
38 self.content = []
39 self.footer = []
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)
46 self.href = href
47 self.paragraph = paragraph
48 self.content = []
50 def on_data(self, data):
51 if not self.content:
52 self.paragraph.on_data("↳")
53 self.paragraph.on_data(data)
54 self.content.append(data.strip())
56 def flush(self):
57 text = " ".join(" ".join(data.split()) for data in self.content if data)
58 self.paragraph.footer.append(f"=> {self.href} {text}")
59 return ""
62 class LiItemTag(ParagraphTag):
63 def flush(self):
64 content = super().flush()
65 return f"* {content}" if content else ""
68 class QuoteTag(ParagraphTag):
69 def flush(self):
70 content = super().flush()
71 return f"> {content}" if content else ""
74 class HeaderTag(ParagraphTag):
75 def flush(self):
76 content = super().flush()
77 if not content:
78 return ""
79 return f"{'#' * int(self.tag[1:])} {content}"
82 class PreformattedTag(_BaseTag):
83 def __init__(self, tag, attrs):
84 super().__init__(tag, attrs)
85 self.content = ""
87 def on_data(self, data):
88 self.content += data
90 def flush(self):
91 result = self.content
92 self.content = ""
93 return f"```\n{result}\n```\n" if result else ""
96 class HtmlToGmi(HTMLParser):
97 def __init__(self, base_url, fn_media_url):
98 super().__init__()
99 self.gmi_text = []
100 self.stack = []
101 self.base_url = base_url
102 self.fn_media_url = fn_media_url
104 def feed(self, data):
105 super().feed(data)
106 while self.stack:
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):
111 def _push(elem):
112 if self.stack:
113 self.gmi_text.append(self.stack[-1].flush())
114 self.stack.append(elem)
116 if tag == "p":
117 _push(ParagraphTag(tag, attrs))
118 elif tag == "pre":
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))
126 elif tag == "a":
127 href = dict(attrs).get("href")
128 if href:
129 self.stack.append(LinkTag(urljoin(self.base_url, href), self._get_current_paragraph(), tag, attrs))
130 elif tag == "img":
131 img = dict(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)
135 else:
136 src = img.get("src")
137 if src:
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}")
142 elif tag == "br":
143 if self.stack:
144 self.gmi_text.append(self.stack[-1].flush())
146 def handle_data(self, data):
147 if not self.stack:
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):
158 return elem
160 self.stack = [ParagraphTag("p", [])] + self.stack
161 return self.stack[0]
164 class LonkUrl:
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]
169 self._base_path = []
170 for path_part in self.splitted_path:
171 self._base_path.append(path_part)
172 if path_part == "lonk":
173 break
175 def build(self, page, query=""):
176 page = page if isinstance(page, list) else [page]
177 return urlunsplit(
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
184 @property
185 def query(self):
186 return self._splitted_url.query
189 class HonkUrl:
190 def __init__(self, raw_url, token):
191 self._splitted_url = urlsplit(raw_url)
192 self._token = token
194 def build(self, scheme=None, netloc=None, path="", query="", fragment=""):
195 return urlunsplit(
197 scheme or self._splitted_url.scheme,
198 netloc or self._splitted_url.netloc,
199 path,
200 query,
201 fragment,
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):
213 db_con.execute(
214 """
215 CREATE TABLE
216 client (
217 client_id INTEGER PRIMARY KEY,
218 cert_hash TEXT UNIQUE,
219 honk_url TEXT NOT NULL,
220 token TEXT NOT NULL
222 """
224 db_con.execute(
225 """
226 CREATE TABLE
227 convoy (
228 convoy_id INTEGER PRIMARY KEY,
229 convoy TEXT,
230 client_id INTEGER,
231 honk_id INTEGER,
232 handle TEXT,
233 oondle TEXT,
234 url TEXT,
235 html TEXT,
236 date TEXT,
237 public INTEGER,
238 handles TEXT,
239 honker_url TEXT,
240 oonker_url TEXT,
241 FOREIGN KEY (client_id) REFERENCES client (client_id),
242 UNIQUE (convoy, client_id)
244 """
246 db_con.execute(
247 """
248 CREATE TABLE
249 donk (
250 donk_id INTEGER PRIMARY KEY,
251 client_id INTEGER,
252 convoy_id INTEGER,
253 url TEXT NOT NULL,
254 mime TEXT,
255 alt_text TEXT,
256 FOREIGN KEY (client_id) REFERENCES client (client_id),
257 FOREIGN KEY (convoy_id) REFERENCES convoy (convoy_id)
259 """
261 db_con.execute(
262 """
263 CREATE TABLE
264 xonker (
265 xonker_id INTEGER PRIMARY KEY,
266 client_id INTEGER,
267 convoy_id INTEGER,
268 url TEXT NOT NULL,
269 FOREIGN KEY (client_id) REFERENCES client (client_id),
270 FOREIGN KEY (convoy_id) REFERENCES convoy (convoy_id)
272 """
274 db_con.execute(
275 """
276 CREATE TABLE
277 meta (
278 db_schema_version INTEGER
280 """
282 db_con.execute("INSERT INTO meta(db_schema_version) VALUES (?)", (1, ))
285 def db_connect():
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)
290 if not db_exist:
291 with db_con:
292 db_create_schema(db_con)
293 return db_con
296 def print_header(page_name):
297 print("20 text/gemini\r")
298 print(f"# 𝓗 onk: {page_name}\r")
299 print("\r")
302 def print_menu(lonk_url, honk_url, gethonks_answer=None):
303 print(f"## 📝 Menu\r")
304 print(f"=> {lonk_url.build('newhonk')} new honk\r")
305 print(f"=> {lonk_url.build([])} lonk home\r")
307 if gethonks_answer:
308 line = f"=> {lonk_url.build('atme')} @me"
309 if gethonks_answer["mecount"]:
310 line += f' ({gethonks_answer["mecount"]})'
311 print(line + "\r")
313 line = f"=> {honk_url.build(path='chatter')} chatter"
314 if gethonks_answer["chatcount"]:
315 line += f' ({gethonks_answer["chatcount"]})'
316 print(line + "\r")
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)
327 print("\r")
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"]}'
334 lines = [
335 f'##{"# ↱" if re_url else ""} From {from_} {honk["Date"]}',
336 f'=> {lonk_url.build("convoy", urlencode({"c": convoy}))} Convoy {convoy}',
337 f'=> {honk["XID"]}',
339 if re_url:
340 lines.append(f'=> {re_url} Re: {re_url}')
341 lines.append("")
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}')
347 if donk_text:
348 lines.append(donk_text)
349 lines.append("")
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=""),
356 "honkback",
359 lines.append(f'=> {honk_back_url} ↱ honk back')
360 for xonker in (honk.get("Honker"), honk.get("Oonker")):
361 if xonker:
362 lines.append(f'=> {lonk_url.build("honker", urlencode({"xid": xonker}))} honks of {xonker}')
363 print("\r\n".join(lines))
364 print("\r")
366 if gethonks_answer.get("honks"):
367 print("\r")
368 print_menu(lonk_url, honk_url, gethonks_answer)
371 class _LonkTreeItem:
372 def __init__(self, convoy_id, convoy, honk_id, handle, oondle, url, html, date, public, handles, honker, oonker):
373 self.convoy_id = convoy_id
374 self.convoy = convoy
375 self.honk_id = honk_id
376 self.handle = handle
377 self.oondle = oondle
378 self.url = url
379 self.html = html
380 self.date = date
381 self.public = bool(public)
382 self.handles = handles
383 self.honker = honker
384 self.oonker = oonker
385 self.donks = []
386 self.thread = []
388 def iterate_honks(self):
389 if self.html is not None:
390 yield {
391 "Convoy": self.convoy,
392 "Handle": self.handle,
393 "Oondle": self.oondle,
394 "ID": self.honk_id,
395 "XID": self.url,
396 "HTML": self.html,
397 "Date": self.date,
398 "Public": self.public,
399 "Handles": self.handles,
400 "Honker": self.honker,
401 "Oonker": self.oonker,
402 "Donks": [
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")
415 lonk_page = {}
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(
421 """
422 SELECT
423 convoy_id, convoy, honk_id, handle, oondle, url, html, date, public, handles, honker_url, oonker_url
424 FROM
425 convoy
426 WHERE
427 client_id=? AND convoy=?
428 """,
429 (client_id, convoy)
430 ).fetchone()
431 if row:
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, )
437 while True:
438 donks = res_donks.fetchmany()
439 if not donks:
440 break
442 for donk in donks:
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(
450 """
451 INSERT INTO
452 convoy(convoy, client_id, honk_id, handle, oondle, url, html, date, public, handles, honker_url, oonker_url)
453 VALUES
454 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
455 RETURNING
456 convoy_id, convoy, honk_id, handle, oondle, url, html, date, public, handles, honker_url, oonker_url
457 """,
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"))
459 ).fetchone()
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
465 db_con.execute(
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))
471 if honk.get("RID"):
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)
475 break
476 else:
477 _save_convoy(convoy, {"ID": None, "Handle": None, "XID": None, "HTML": None, "Date": None, "Handles": None})
478 else:
479 _save_convoy(convoy, honk)
481 if honk.get("RID"):
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)}
494 if "c" not in query:
495 print("51 Not found\r")
496 return
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")
506 return
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")
516 print_header("@me")
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")
534 if not xid:
535 print("51 Not found\r")
536 return
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")
545 if not what:
546 print("51 Not found\r")
547 return
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)
562 if value:
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')
566 else:
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')
569 print('\r')
571 if honkers:
572 print("\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")
579 return
581 url = unquote(lonk_url.query)
582 try:
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)
588 print("\r")
589 print('## Error\r')
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")
595 if not honkerid:
596 print("51 Not found\r")
597 return
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")
606 if not honkerid:
607 print("51 Not found\r")
608 return
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")
618 return
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")
630 return
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()
641 if not row:
642 print(f'30 {lonk_url.build("ask_server")}\r')
643 return
644 client_id, honk_url, token = row
645 with db_con:
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")
652 return
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")
661 return
662 if len(lonk_url.splitted_path) < 3:
663 print('59 Bad request\r')
664 return
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")
673 return
674 if len(lonk_url.splitted_path) < 4:
675 print('59 Bad request\r')
676 return
678 honk_url = unquote(lonk_url.splitted_path[-3])
679 post_data = {
680 "username": unquote(lonk_url.splitted_path[-2]),
681 "password": unquote(lonk_url.query),
682 "gettoken": "1",
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()
687 with db_con:
688 db_con.execute(
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")
698 while True:
699 content = response.read(512 * 1024)
700 if not content:
701 break
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")
745 else:
746 proxy(mime=query["m"], url=query["u"])
747 else:
748 print("51 Not found\r")
751 if __name__ == '__main__':
752 cert_hash_ = environ.get("VGI_CERT_HASH")
753 if cert_hash_:
754 try:
755 start_time = clock_gettime(CLOCK_MONOTONIC)
756 try:
757 input_url = input().strip()
758 vgi(cert_hash_, input_url)
759 finally:
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")
770 else:
771 stderr.write("Certificate required\n")
772 print("60 Certificate required\r")