1 ecdd11be 2024-09-11 continue #!/usr/bin/env python3
2 1719b036 2024-09-03 continue from json import loads as json_loads
3 29500f51 2024-09-20 continue from mimetypes import guess_type
4 d76a52db 2024-09-17 continue from os import environ
5 1719b036 2024-09-03 continue from pathlib import Path
6 4acbf994 2024-09-25 continue from sys import stdout, stderr
7 577b12da 2024-09-24 continue from sqlite3 import connect as sqlite3_connect
8 4acbf994 2024-09-25 continue from time import clock_gettime, CLOCK_MONOTONIC
9 15efabff 2024-09-13 continue from urllib.error import HTTPError, URLError
10 e03b8dd3 2024-09-25 continue from urllib.parse import urlencode, urlunsplit, urljoin, urlsplit, parse_qsl, unquote, quote
11 1719b036 2024-09-03 continue from urllib.request import urlopen
12 a7cabe95 2024-09-04 continue from html.parser import HTMLParser
15 a7cabe95 2024-09-04 continue class _BaseTag:
16 a7cabe95 2024-09-04 continue def __init__(self, tag, attrs):
17 a7cabe95 2024-09-04 continue self.tag = tag
19 a7cabe95 2024-09-04 continue def on_data(self, data):
20 e03b8dd3 2024-09-25 continue raise AssertionError("The method must be overridden")
22 a7cabe95 2024-09-04 continue def flush(self):
23 e03b8dd3 2024-09-25 continue raise AssertionError("The method must be overridden")
26 a7cabe95 2024-09-04 continue class ParagraphTag(_BaseTag):
27 a7cabe95 2024-09-04 continue def __init__(self, tag, attrs):
28 a7cabe95 2024-09-04 continue super().__init__(tag, attrs)
29 a7cabe95 2024-09-04 continue self.content = []
30 1be7bb9a 2024-09-05 continue self.footer = []
32 a7cabe95 2024-09-04 continue def on_data(self, data):
33 a7cabe95 2024-09-04 continue self.content.append(data.strip())
35 a7cabe95 2024-09-04 continue def flush(self):
36 f6854b29 2024-10-05 continue result = " ".join(" ".join(data.split()) for data in self.content if data)
37 1be7bb9a 2024-09-05 continue footer = self.footer
38 a7cabe95 2024-09-04 continue self.content = []
39 1be7bb9a 2024-09-05 continue self.footer = []
40 f6854b29 2024-10-05 continue return "\n".join([result] + footer) + "\n" if result else ""
43 a7cabe95 2024-09-04 continue class LinkTag(_BaseTag):
44 1be7bb9a 2024-09-05 continue def __init__(self, href, paragraph, tag, attrs):
45 a7cabe95 2024-09-04 continue super().__init__(tag, attrs)
46 1be7bb9a 2024-09-05 continue self.href = href
47 1be7bb9a 2024-09-05 continue self.paragraph = paragraph
48 a7cabe95 2024-09-04 continue self.content = []
50 a7cabe95 2024-09-04 continue def on_data(self, data):
51 1be7bb9a 2024-09-05 continue if not self.content:
52 1f653a98 2024-09-05 continue self.paragraph.on_data("↳")
53 1be7bb9a 2024-09-05 continue self.paragraph.on_data(data)
54 a7cabe95 2024-09-04 continue self.content.append(data.strip())
56 a7cabe95 2024-09-04 continue def flush(self):
57 1be7bb9a 2024-09-05 continue text = " ".join(" ".join(data.split()) for data in self.content if data)
58 1be7bb9a 2024-09-05 continue self.paragraph.footer.append(f"=> {self.href} {text}")
59 1be7bb9a 2024-09-05 continue return ""
62 280fbeb1 2024-10-11 continue class LiItemTag(ParagraphTag):
63 a7cabe95 2024-09-04 continue def flush(self):
64 a7cabe95 2024-09-04 continue content = super().flush()
65 a7cabe95 2024-09-04 continue return f"* {content}" if content else ""
68 a7cabe95 2024-09-04 continue class QuoteTag(ParagraphTag):
69 a7cabe95 2024-09-04 continue def flush(self):
70 a7cabe95 2024-09-04 continue content = super().flush()
71 a7cabe95 2024-09-04 continue return f"> {content}" if content else ""
74 ae087ef0 2024-09-04 continue class HeaderTag(ParagraphTag):
75 ae087ef0 2024-09-04 continue def flush(self):
76 ae087ef0 2024-09-04 continue content = super().flush()
77 ae087ef0 2024-09-04 continue if not content:
78 ae087ef0 2024-09-04 continue return ""
79 ae087ef0 2024-09-04 continue return f"{'#' * int(self.tag[1:])} {content}"
82 ae087ef0 2024-09-04 continue class PreformattedTag(_BaseTag):
83 ae087ef0 2024-09-04 continue def __init__(self, tag, attrs):
84 ae087ef0 2024-09-04 continue super().__init__(tag, attrs)
85 ae087ef0 2024-09-04 continue self.content = ""
87 ae087ef0 2024-09-04 continue def on_data(self, data):
88 ae087ef0 2024-09-04 continue self.content += data
90 ae087ef0 2024-09-04 continue def flush(self):
91 f6854b29 2024-10-05 continue result = self.content
92 ae087ef0 2024-09-04 continue self.content = ""
93 f6854b29 2024-10-05 continue return f"```\n{result}\n```\n" if result else ""
96 a7cabe95 2024-09-04 continue class HtmlToGmi(HTMLParser):
97 29500f51 2024-09-20 continue def __init__(self, base_url, fn_media_url):
98 a7cabe95 2024-09-04 continue super().__init__()
99 a7cabe95 2024-09-04 continue self.gmi_text = []
100 a7cabe95 2024-09-04 continue self.stack = []
101 1f653a98 2024-09-05 continue self.base_url = base_url
102 29500f51 2024-09-20 continue self.fn_media_url = fn_media_url
104 f6854b29 2024-10-05 continue def feed(self, data):
105 f6854b29 2024-10-05 continue super().feed(data)
106 a7cabe95 2024-09-04 continue while self.stack:
107 a7cabe95 2024-09-04 continue self.gmi_text.append(self.stack.pop().flush())
108 a7cabe95 2024-09-04 continue return "\n".join(gmi_text for gmi_text in self.gmi_text if gmi_text)
110 a7cabe95 2024-09-04 continue def handle_starttag(self, tag, attrs):
111 1b9c1500 2024-09-05 continue def _push(elem):
112 1b9c1500 2024-09-05 continue if self.stack:
113 1b9c1500 2024-09-05 continue self.gmi_text.append(self.stack[-1].flush())
114 1b9c1500 2024-09-05 continue self.stack.append(elem)
116 1b9c1500 2024-09-05 continue if tag == "p":
117 1b9c1500 2024-09-05 continue _push(ParagraphTag(tag, attrs))
118 1b9c1500 2024-09-05 continue elif tag == "pre":
119 1b9c1500 2024-09-05 continue _push(PreformattedTag(tag, attrs))
120 ae087ef0 2024-09-04 continue elif tag in {"h1", "h2", "h3", "h4", "h5", "h6"}:
121 1b9c1500 2024-09-05 continue _push(HeaderTag(tag, attrs))
122 ae087ef0 2024-09-04 continue elif tag in {"li", "dt"}:
123 280fbeb1 2024-10-11 continue _push(LiItemTag(tag, attrs))
124 ae087ef0 2024-09-04 continue elif tag in {"blockquote", "q"}:
125 1b9c1500 2024-09-05 continue _push(QuoteTag(tag, attrs))
126 a7cabe95 2024-09-04 continue elif tag == "a":
127 4e7950e1 2024-09-30 continue href = dict(attrs).get("href")
128 1be7bb9a 2024-09-05 continue if href:
129 1f653a98 2024-09-05 continue self.stack.append(LinkTag(urljoin(self.base_url, href), self._get_current_paragraph(), tag, attrs))
130 a7cabe95 2024-09-04 continue elif tag == "img":
131 4e7950e1 2024-09-30 continue img = dict(attrs)
132 1b9c1500 2024-09-05 continue title = img.get("title") or ""
133 1b9c1500 2024-09-05 continue if img.get("class") == "emu" and title and self.stack:
134 1b9c1500 2024-09-05 continue self.stack[-1].on_data(title)
136 1b9c1500 2024-09-05 continue src = img.get("src")
137 1b9c1500 2024-09-05 continue if src:
138 280fbeb1 2024-10-11 continue http_img_url = urljoin(self.base_url, src)
139 280fbeb1 2024-10-11 continue mime, _ = guess_type(http_img_url)
140 280fbeb1 2024-10-11 continue img_url = self.fn_media_url(mime, http_img_url)
141 280fbeb1 2024-10-11 continue self.gmi_text.append(f"=> {img_url} {title or http_img_url}")
142 5c749e57 2024-09-05 continue elif tag == "br":
143 5c749e57 2024-09-05 continue if self.stack:
144 5c749e57 2024-09-05 continue self.gmi_text.append(self.stack[-1].flush())
146 a7cabe95 2024-09-04 continue def handle_data(self, data):
147 a7cabe95 2024-09-04 continue if not self.stack:
148 1b9c1500 2024-09-05 continue self.stack.append(ParagraphTag("p", []))
149 a7cabe95 2024-09-04 continue self.stack[-1].on_data(data)
151 a7cabe95 2024-09-04 continue def handle_endtag(self, tag):
152 1b9c1500 2024-09-05 continue if self.stack and tag == self.stack[-1].tag:
153 1b9c1500 2024-09-05 continue self.gmi_text.append(self.stack.pop().flush())
155 1be7bb9a 2024-09-05 continue def _get_current_paragraph(self):
156 1be7bb9a 2024-09-05 continue for elem in reversed(self.stack):
157 1be7bb9a 2024-09-05 continue if isinstance(elem, ParagraphTag):
158 1be7bb9a 2024-09-05 continue return elem
160 1be7bb9a 2024-09-05 continue self.stack = [ParagraphTag("p", [])] + self.stack
161 1be7bb9a 2024-09-05 continue return self.stack[0]
164 e03b8dd3 2024-09-25 continue class LonkUrl:
165 e03b8dd3 2024-09-25 continue def __init__(self, raw_url):
166 e03b8dd3 2024-09-25 continue self._splitted_url = urlsplit(raw_url)
167 e03b8dd3 2024-09-25 continue self.splitted_path = [part for part in self._splitted_url.path.split("/") if part]
168 e03b8dd3 2024-09-25 continue self.page = self.splitted_path[-1]
169 e03b8dd3 2024-09-25 continue self._base_path = []
170 e03b8dd3 2024-09-25 continue for path_part in self.splitted_path:
171 e03b8dd3 2024-09-25 continue self._base_path.append(path_part)
172 e03b8dd3 2024-09-25 continue if path_part == "lonk":
175 e03b8dd3 2024-09-25 continue def build(self, page, query=""):
176 e03b8dd3 2024-09-25 continue page = page if isinstance(page, list) else [page]
177 e03b8dd3 2024-09-25 continue return urlunsplit(
178 e03b8dd3 2024-09-25 continue ("gemini", self._splitted_url.netloc, "/".join(self._base_path + page), query, "")
181 fafb2ce7 2024-10-02 continue def media(self, mime, url):
182 fafb2ce7 2024-10-02 continue return self.build("proxy", urlencode({"m": mime, "u": url})) if mime else url
184 e03b8dd3 2024-09-25 continue @property
185 e03b8dd3 2024-09-25 continue def query(self):
186 e03b8dd3 2024-09-25 continue return self._splitted_url.query
189 09255143 2024-09-25 continue class HonkUrl:
190 09255143 2024-09-25 continue def __init__(self, raw_url, token):
191 09255143 2024-09-25 continue self._splitted_url = urlsplit(raw_url)
192 09255143 2024-09-25 continue self._token = token
194 056d3709 2024-10-10 continue def build(self, scheme=None, netloc=None, path="", query="", fragment=""):
195 09255143 2024-09-25 continue return urlunsplit(
197 09255143 2024-09-25 continue scheme or self._splitted_url.scheme,
198 09255143 2024-09-25 continue netloc or self._splitted_url.netloc,
201 09255143 2024-09-25 continue fragment,
205 056d3709 2024-10-10 continue def get(self, action, answer_is_json=True, **kwargs):
206 f6854b29 2024-10-05 continue query = {**{"action": action, "token": self._token}, **kwargs}
207 056d3709 2024-10-10 continue with urlopen(self.build(path="api", query=urlencode(query)), timeout=45) as response:
208 f6854b29 2024-10-05 continue answer = response.read().decode("utf8")
209 f6854b29 2024-10-05 continue return json_loads(answer) if answer_is_json else answer
212 280fbeb1 2024-10-11 continue def db_create_schema(db_con):
213 4acbf994 2024-09-25 continue db_con.execute(
215 577b12da 2024-09-24 continue CREATE TABLE
216 577b12da 2024-09-24 continue client (
217 9f761a0c 2024-09-25 continue client_id INTEGER PRIMARY KEY,
218 577b12da 2024-09-24 continue cert_hash TEXT UNIQUE,
219 577b12da 2024-09-24 continue honk_url TEXT NOT NULL,
220 577b12da 2024-09-24 continue token TEXT NOT NULL
224 da7270e1 2024-10-01 continue db_con.execute(
226 da7270e1 2024-10-01 continue CREATE TABLE
227 4acbf994 2024-09-25 continue convoy (
228 9f761a0c 2024-09-25 continue convoy_id INTEGER PRIMARY KEY,
229 9f761a0c 2024-09-25 continue convoy TEXT,
230 4acbf994 2024-09-25 continue client_id INTEGER,
231 9f761a0c 2024-09-25 continue honk_id INTEGER,
232 3601f1e9 2024-10-10 continue handle TEXT,
233 3601f1e9 2024-10-10 continue oondle TEXT,
234 9f761a0c 2024-09-25 continue url TEXT,
235 9f761a0c 2024-09-25 continue html TEXT,
236 9f761a0c 2024-09-25 continue date TEXT,
237 7c41d255 2024-10-09 continue public INTEGER,
238 3e30fc1f 2024-10-09 continue handles TEXT,
239 3601f1e9 2024-10-10 continue honker_url TEXT,
240 3601f1e9 2024-10-10 continue oonker_url TEXT,
241 9f761a0c 2024-09-25 continue FOREIGN KEY (client_id) REFERENCES client (client_id),
242 280fbeb1 2024-10-11 continue UNIQUE (convoy, client_id)
246 4acbf994 2024-09-25 continue db_con.execute(
248 4acbf994 2024-09-25 continue CREATE TABLE
250 9f761a0c 2024-09-25 continue donk_id INTEGER PRIMARY KEY,
251 8b4f10af 2024-10-03 continue client_id INTEGER,
252 9f761a0c 2024-09-25 continue convoy_id INTEGER,
253 4acbf994 2024-09-25 continue url TEXT NOT NULL,
254 9f761a0c 2024-09-25 continue mime TEXT,
255 9f761a0c 2024-09-25 continue alt_text TEXT,
256 8b4f10af 2024-10-03 continue FOREIGN KEY (client_id) REFERENCES client (client_id),
257 9f761a0c 2024-09-25 continue FOREIGN KEY (convoy_id) REFERENCES convoy (convoy_id)
261 da7270e1 2024-10-01 continue db_con.execute(
263 da7270e1 2024-10-01 continue CREATE TABLE
264 3601f1e9 2024-10-10 continue xonker (
265 3601f1e9 2024-10-10 continue xonker_id INTEGER PRIMARY KEY,
266 3601f1e9 2024-10-10 continue client_id INTEGER,
267 3601f1e9 2024-10-10 continue convoy_id INTEGER,
268 3601f1e9 2024-10-10 continue url TEXT NOT NULL,
269 3601f1e9 2024-10-10 continue FOREIGN KEY (client_id) REFERENCES client (client_id),
270 3601f1e9 2024-10-10 continue FOREIGN KEY (convoy_id) REFERENCES convoy (convoy_id)
274 3601f1e9 2024-10-10 continue db_con.execute(
276 3601f1e9 2024-10-10 continue CREATE TABLE
278 da7270e1 2024-10-01 continue db_schema_version INTEGER
282 da7270e1 2024-10-01 continue db_con.execute("INSERT INTO meta(db_schema_version) VALUES (?)", (1, ))
285 4e7950e1 2024-09-30 continue def db_connect():
286 577b12da 2024-09-24 continue db_file_path = Path(__file__).parent / ".local" / "db"
287 577b12da 2024-09-24 continue db_file_path.parent.mkdir(parents=True, exist_ok=True)
288 577b12da 2024-09-24 continue db_exist = db_file_path.exists()
289 577b12da 2024-09-24 continue db_con = sqlite3_connect(db_file_path)
290 577b12da 2024-09-24 continue if not db_exist:
291 e03b8dd3 2024-09-25 continue with db_con:
292 280fbeb1 2024-10-11 continue db_create_schema(db_con)
293 577b12da 2024-09-24 continue return db_con
296 280fbeb1 2024-10-11 continue def print_header(page_name):
297 280fbeb1 2024-10-11 continue print("20 text/gemini\r")
298 280fbeb1 2024-10-11 continue print(f"# 𝓗 onk: {page_name}\r")
299 280fbeb1 2024-10-11 continue print("\r")
302 280fbeb1 2024-10-11 continue def print_menu(lonk_url, honk_url, gethonks_answer=None):
303 c8840e58 2024-10-13 continue print("## 📝 Menu\r")
304 280fbeb1 2024-10-11 continue print(f"=> {lonk_url.build('newhonk')} new honk\r")
305 280fbeb1 2024-10-11 continue print(f"=> {lonk_url.build([])} lonk home\r")
307 280fbeb1 2024-10-11 continue if gethonks_answer:
308 280fbeb1 2024-10-11 continue line = f"=> {lonk_url.build('atme')} @me"
309 280fbeb1 2024-10-11 continue if gethonks_answer["mecount"]:
310 280fbeb1 2024-10-11 continue line += f' ({gethonks_answer["mecount"]})'
311 280fbeb1 2024-10-11 continue print(line + "\r")
313 280fbeb1 2024-10-11 continue line = f"=> {honk_url.build(path='chatter')} chatter"
314 280fbeb1 2024-10-11 continue if gethonks_answer["chatcount"]:
315 280fbeb1 2024-10-11 continue line += f' ({gethonks_answer["chatcount"]})'
316 280fbeb1 2024-10-11 continue print(line + "\r")
318 280fbeb1 2024-10-11 continue print(f"=> {lonk_url.build('search')} search\r")
319 280fbeb1 2024-10-11 continue print(f"=> {lonk_url.build('longago')} long ago\r")
320 280fbeb1 2024-10-11 continue print(f"=> {lonk_url.build('myhonks')} my honks\r")
321 280fbeb1 2024-10-11 continue print(f"=> {lonk_url.build('gethonkers')} honkers\r")
322 280fbeb1 2024-10-11 continue print(f"=> {lonk_url.build('addhonker')} add new honker\r")
325 280fbeb1 2024-10-11 continue def print_gethonks(gethonks_answer, lonk_url, honk_url):
326 280fbeb1 2024-10-11 continue print_menu(lonk_url, honk_url, gethonks_answer)
327 280fbeb1 2024-10-11 continue print("\r")
329 280fbeb1 2024-10-11 continue for honk in gethonks_answer.get("honks") or []:
330 280fbeb1 2024-10-11 continue convoy = honk["Convoy"]
331 280fbeb1 2024-10-11 continue re_url = honk.get("RID")
332 280fbeb1 2024-10-11 continue oondle = honk.get("Oondle")
333 280fbeb1 2024-10-11 continue from_ = f'{oondle} (🔁 {honk["Handle"]})' if oondle else f'{honk["Handle"]}'
334 280fbeb1 2024-10-11 continue lines = [
335 280fbeb1 2024-10-11 continue f'##{"# ↱" if re_url else ""} From {from_} {honk["Date"]}',
336 280fbeb1 2024-10-11 continue f'=> {lonk_url.build("convoy", urlencode({"c": convoy}))} Convoy {convoy}',
337 280fbeb1 2024-10-11 continue f'=> {honk["XID"]}',
339 280fbeb1 2024-10-11 continue if re_url:
340 280fbeb1 2024-10-11 continue lines.append(f'=> {re_url} Re: {re_url}')
341 280fbeb1 2024-10-11 continue lines.append("")
342 280fbeb1 2024-10-11 continue lines.append(HtmlToGmi(honk_url.build(), lonk_url.media).feed(honk["HTML"]))
343 280fbeb1 2024-10-11 continue for donk in honk.get("Donks") or []:
344 280fbeb1 2024-10-11 continue donk_url = honk_url.build(path=f'/d/{donk["XID"]}') if donk.get("XID") else donk["URL"]
345 280fbeb1 2024-10-11 continue donk_mime, donk_text = donk["Media"], donk.get("Desc") or donk.get("Name") or None
346 280fbeb1 2024-10-11 continue lines.append(f'=> {lonk_url.media(donk_mime, donk_url)} {donk_url}')
347 280fbeb1 2024-10-11 continue if donk_text:
348 280fbeb1 2024-10-11 continue lines.append(donk_text)
349 280fbeb1 2024-10-11 continue lines.append("")
350 280fbeb1 2024-10-11 continue if honk.get("Public"):
351 280fbeb1 2024-10-11 continue lines.append(f'=> {lonk_url.build("bonk", urlencode({"w": honk["XID"]}))} ↺ bonk')
352 280fbeb1 2024-10-11 continue honk_back_url = lonk_url.build(
354 280fbeb1 2024-10-11 continue quote(honk["Handles"] or " ", safe=""),
355 280fbeb1 2024-10-11 continue quote(honk["XID"], safe=""),
356 280fbeb1 2024-10-11 continue "honkback",
359 280fbeb1 2024-10-11 continue lines.append(f'=> {honk_back_url} ↱ honk back')
360 280fbeb1 2024-10-11 continue for xonker in (honk.get("Honker"), honk.get("Oonker")):
361 280fbeb1 2024-10-11 continue if xonker:
362 280fbeb1 2024-10-11 continue lines.append(f'=> {lonk_url.build("honker", urlencode({"xid": xonker}))} honks of {xonker}')
363 280fbeb1 2024-10-11 continue print("\r\n".join(lines))
364 280fbeb1 2024-10-11 continue print("\r")
366 280fbeb1 2024-10-11 continue if gethonks_answer.get("honks"):
367 280fbeb1 2024-10-11 continue print("\r")
368 280fbeb1 2024-10-11 continue print_menu(lonk_url, honk_url, gethonks_answer)
371 8b4f10af 2024-10-03 continue class _LonkTreeItem:
372 3601f1e9 2024-10-10 continue def __init__(self, convoy_id, convoy, honk_id, handle, oondle, url, html, date, public, handles, honker, oonker):
373 4e7950e1 2024-09-30 continue self.convoy_id = convoy_id
374 4e7950e1 2024-09-30 continue self.convoy = convoy
375 4e7950e1 2024-09-30 continue self.honk_id = honk_id
376 3601f1e9 2024-10-10 continue self.handle = handle
377 3601f1e9 2024-10-10 continue self.oondle = oondle
378 4e7950e1 2024-09-30 continue self.url = url
379 4e7950e1 2024-09-30 continue self.html = html
380 4e7950e1 2024-09-30 continue self.date = date
381 7c41d255 2024-10-09 continue self.public = bool(public)
382 3e30fc1f 2024-10-09 continue self.handles = handles
383 3601f1e9 2024-10-10 continue self.honker = honker
384 3601f1e9 2024-10-10 continue self.oonker = oonker
385 8b4f10af 2024-10-03 continue self.donks = []
386 8b4f10af 2024-10-03 continue self.thread = []
388 8b4f10af 2024-10-03 continue def iterate_honks(self):
389 8b4f10af 2024-10-03 continue if self.html is not None:
390 8b4f10af 2024-10-03 continue yield {
391 8b4f10af 2024-10-03 continue "Convoy": self.convoy,
392 3601f1e9 2024-10-10 continue "Handle": self.handle,
393 3601f1e9 2024-10-10 continue "Oondle": self.oondle,
394 8b4f10af 2024-10-03 continue "ID": self.honk_id,
395 8b4f10af 2024-10-03 continue "XID": self.url,
396 8b4f10af 2024-10-03 continue "HTML": self.html,
397 8b4f10af 2024-10-03 continue "Date": self.date,
398 7c41d255 2024-10-09 continue "Public": self.public,
399 3e30fc1f 2024-10-09 continue "Handles": self.handles,
400 3601f1e9 2024-10-10 continue "Honker": self.honker,
401 3601f1e9 2024-10-10 continue "Oonker": self.oonker,
402 8b4f10af 2024-10-03 continue "Donks": [
403 8b4f10af 2024-10-03 continue {"URL": donk[0], "Media": donk[1], "Desc": donk[2]}
404 8b4f10af 2024-10-03 continue for donk in self.donks
407 8b4f10af 2024-10-03 continue child_honks = self.thread[:]
408 8b4f10af 2024-10-03 continue child_honks.reverse()
409 8b4f10af 2024-10-03 continue yield from reversed(child_honks)
412 8b4f10af 2024-10-03 continue def page_lonk(db_con, client_id, lonk_url, honk_url):
413 056d3709 2024-10-10 continue gethonks_answer = honk_url.get("gethonks", page="home")
415 8b4f10af 2024-10-03 continue lonk_page = {}
416 5910837c 2024-10-03 continue for honk in reversed(gethonks_answer.pop("honks", None) or []):
417 9f761a0c 2024-09-25 continue convoy = honk["Convoy"]
419 8b4f10af 2024-10-03 continue if convoy not in lonk_page:
420 9f761a0c 2024-09-25 continue row = db_con.execute(
423 3601f1e9 2024-10-10 continue convoy_id, convoy, honk_id, handle, oondle, url, html, date, public, handles, honker_url, oonker_url
427 3601f1e9 2024-10-10 continue client_id=? AND convoy=?
429 8b4f10af 2024-10-03 continue (client_id, convoy)
430 9f761a0c 2024-09-25 continue ).fetchone()
431 8b4f10af 2024-10-03 continue if row:
432 8b4f10af 2024-10-03 continue lonk_page[convoy] = _LonkTreeItem(*row)
433 8b4f10af 2024-10-03 continue res_donks = db_con.execute(
434 8b4f10af 2024-10-03 continue "SELECT url, mime, alt_text FROM donk WHERE client_id=? AND convoy_id=?",
435 8b4f10af 2024-10-03 continue (client_id, lonk_page[convoy].convoy_id, )
437 8b4f10af 2024-10-03 continue while True:
438 8b4f10af 2024-10-03 continue donks = res_donks.fetchmany()
439 8b4f10af 2024-10-03 continue if not donks:
442 8b4f10af 2024-10-03 continue for donk in donks:
443 8b4f10af 2024-10-03 continue donk_url, donk_mime, donk_text = donk
444 8b4f10af 2024-10-03 continue lonk_page[convoy].donks.append((donk_url, donk_mime, donk_text))
446 8b4f10af 2024-10-03 continue if convoy not in lonk_page:
447 3601f1e9 2024-10-10 continue def _save_convoy(convoy, honk):
448 7c41d255 2024-10-09 continue is_public = 1 if honk.get("Public") else 0
449 4e7950e1 2024-09-30 continue row = db_con.execute(
451 4acbf994 2024-09-25 continue INSERT INTO
452 3601f1e9 2024-10-10 continue convoy(convoy, client_id, honk_id, handle, oondle, url, html, date, public, handles, honker_url, oonker_url)
454 3601f1e9 2024-10-10 continue (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
455 8b4f10af 2024-10-03 continue RETURNING
456 3601f1e9 2024-10-10 continue convoy_id, convoy, honk_id, handle, oondle, url, html, date, public, handles, honker_url, oonker_url
458 3601f1e9 2024-10-10 continue (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 4e7950e1 2024-09-30 continue ).fetchone()
460 3601f1e9 2024-10-10 continue lonk_page[convoy] = _LonkTreeItem(*row)
462 8b4f10af 2024-10-03 continue for donk in (honk.get("Donks") or []):
463 056d3709 2024-10-10 continue donk_url = honk_url.build(path=f'/d/{donk["XID"]}') if donk.get("XID") else donk["URL"]
464 8b4f10af 2024-10-03 continue donk_mime, donk_text = donk["Media"], donk.get("Desc") or donk.get("Name") or None
465 8b4f10af 2024-10-03 continue db_con.execute(
466 8b4f10af 2024-10-03 continue "INSERT INTO donk (client_id, convoy_id, url, mime, alt_text) VALUES (?, ?, ?, ?, ?)",
467 8b4f10af 2024-10-03 continue (client_id, lonk_page[convoy].convoy_id, donk_url, donk_mime, donk_text, )
469 8b4f10af 2024-10-03 continue lonk_page[convoy].donks.append((donk_url, donk_mime, donk_text))
471 8b4f10af 2024-10-03 continue if honk.get("RID"):
472 056d3709 2024-10-10 continue for honk_in_convoy in honk_url.get("gethonks", page="convoy", c=convoy)["honks"]:
473 8b4f10af 2024-10-03 continue if not honk_in_convoy.get("RID"):
474 3601f1e9 2024-10-10 continue _save_convoy(convoy, honk_in_convoy)
477 3601f1e9 2024-10-10 continue _save_convoy(convoy, {"ID": None, "Handle": None, "XID": None, "HTML": None, "Date": None, "Handles": None})
479 3601f1e9 2024-10-10 continue _save_convoy(convoy, honk)
481 8b4f10af 2024-10-03 continue if honk.get("RID"):
482 8b4f10af 2024-10-03 continue lonk_page[convoy].thread.append(honk)
484 8b4f10af 2024-10-03 continue gethonks_answer["honks"] = []
485 5910837c 2024-10-03 continue for tree_item in reversed(lonk_page.values()):
486 8b4f10af 2024-10-03 continue gethonks_answer["honks"] += list(tree_item.iterate_honks())
488 280fbeb1 2024-10-11 continue print_header("lonk home")
489 8b4f10af 2024-10-03 continue print_gethonks(gethonks_answer, lonk_url, honk_url)
492 8b4f10af 2024-10-03 continue def page_convoy(db_con, client_id, lonk_url, honk_url):
493 895356d0 2024-10-01 continue query = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}
494 895356d0 2024-10-01 continue if "c" not in query:
495 895356d0 2024-10-01 continue print("51 Not found\r")
498 056d3709 2024-10-10 continue gethonks_answer = honk_url.get("gethonks", page="convoy", c=query["c"])
499 280fbeb1 2024-10-11 continue print_header(f"convoy {query['c']}")
500 8b4f10af 2024-10-03 continue print_gethonks(gethonks_answer, lonk_url, honk_url)
503 2ff52a39 2024-10-09 continue def page_search(db_con, client_id, lonk_url, honk_url):
504 2ff52a39 2024-10-09 continue if not lonk_url.query:
505 29cd802f 2024-10-09 continue print("10 What are we looking for?\r")
508 2ff52a39 2024-10-09 continue q = unquote(lonk_url.query)
509 056d3709 2024-10-10 continue gethonks_answer = honk_url.get("gethonks", page="search", q=q)
510 280fbeb1 2024-10-11 continue print_header(f"search - {q}")
511 2ff52a39 2024-10-09 continue print_gethonks(gethonks_answer, lonk_url, honk_url)
514 8b4f10af 2024-10-03 continue def page_atme(db_con, client_id, lonk_url, honk_url):
515 056d3709 2024-10-10 continue gethonks_answer = honk_url.get("gethonks", page="atme")
516 280fbeb1 2024-10-11 continue print_header("@me")
517 8b4f10af 2024-10-03 continue print_gethonks(gethonks_answer, lonk_url, honk_url)
520 b5ebf4c7 2024-10-09 continue def page_longago(db_con, client_id, lonk_url, honk_url):
521 056d3709 2024-10-10 continue gethonks_answer = honk_url.get("gethonks", page="longago")
522 280fbeb1 2024-10-11 continue print_header("long ago")
523 b5ebf4c7 2024-10-09 continue print_gethonks(gethonks_answer, lonk_url, honk_url)
526 3601f1e9 2024-10-10 continue def page_myhonks(db_con, client_id, lonk_url, honk_url):
527 056d3709 2024-10-10 continue gethonks_answer = honk_url.get("gethonks", page="myhonks")
528 280fbeb1 2024-10-11 continue print_header("my honks")
529 3601f1e9 2024-10-10 continue print_gethonks(gethonks_answer, lonk_url, honk_url)
532 3601f1e9 2024-10-10 continue def page_honker(db_con, client_id, lonk_url, honk_url):
533 3601f1e9 2024-10-10 continue xid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("xid")
534 3601f1e9 2024-10-10 continue if not xid:
535 3601f1e9 2024-10-10 continue print("51 Not found\r")
538 056d3709 2024-10-10 continue gethonks_answer = honk_url.get("gethonks", page="honker", xid=xid)
539 280fbeb1 2024-10-11 continue print_header(f"honks of {xid}")
540 3601f1e9 2024-10-10 continue print_gethonks(gethonks_answer, lonk_url, honk_url)
543 f6854b29 2024-10-05 continue def bonk(db_con, client_id, lonk_url, honk_url):
544 f6854b29 2024-10-05 continue what = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("w")
545 f6854b29 2024-10-05 continue if not what:
546 f6854b29 2024-10-05 continue print("51 Not found\r")
549 056d3709 2024-10-10 continue honk_url.get("zonkit", wherefore="bonk", what=what, answer_is_json=False)
550 3601f1e9 2024-10-10 continue print(f'30 {lonk_url.build("myhonks")}\r')
553 3efde7cb 2024-10-10 continue def gethonkers(db_con, client_id, lonk_url, honk_url):
554 280fbeb1 2024-10-11 continue print_header("honkers")
555 280fbeb1 2024-10-11 continue print_menu(lonk_url, honk_url)
557 056d3709 2024-10-10 continue honkers = honk_url.get("gethonkers").get("honkers") or []
558 3efde7cb 2024-10-10 continue for honker in honkers:
559 3efde7cb 2024-10-10 continue print(f'## {honker.get("Name") or honker["XID"]}\r')
560 3efde7cb 2024-10-10 continue for field_name, display_name in zip(("Name", "XID", "Flavor"), ("name", "url", "flavor")):
561 3efde7cb 2024-10-10 continue value = honker.get(field_name)
562 3efde7cb 2024-10-10 continue if value:
563 3efde7cb 2024-10-10 continue print(f'{display_name}: {value}\r')
564 3efde7cb 2024-10-10 continue if honker.get("Flavor") == "sub":
565 3efde7cb 2024-10-10 continue print(f'=> {lonk_url.build("unsubscribe", urlencode({"honkerid": honker["ID"]}))} unsubscribe\r')
567 3efde7cb 2024-10-10 continue print(f'=> {lonk_url.build("subscribe", urlencode({"honkerid": honker["ID"]}))} (re)subscribe\r')
568 280fbeb1 2024-10-11 continue print(f'=> {lonk_url.build("honker", urlencode({"xid": honker["XID"]}))} honks of {honker["XID"]}\r')
569 3efde7cb 2024-10-10 continue print('\r')
571 3efde7cb 2024-10-10 continue if honkers:
572 3efde7cb 2024-10-10 continue print("\r")
573 280fbeb1 2024-10-11 continue print_menu(lonk_url, honk_url)
576 3efde7cb 2024-10-10 continue def addhonker(db_con, client_id, lonk_url, honk_url):
577 3efde7cb 2024-10-10 continue if not lonk_url.query:
578 056d3709 2024-10-10 continue print("10 honker url: \r")
581 3efde7cb 2024-10-10 continue url = unquote(lonk_url.query)
583 056d3709 2024-10-10 continue honk_url.get("savehonker", url=url, answer_is_json=False)
584 3efde7cb 2024-10-10 continue print(f'30 {lonk_url.build("gethonkers")}\r')
585 3efde7cb 2024-10-10 continue except HTTPError as error:
586 280fbeb1 2024-10-11 continue print_header("add new honker")
587 280fbeb1 2024-10-11 continue print_menu(lonk_url, honk_url)
588 3efde7cb 2024-10-10 continue print("\r")
589 3efde7cb 2024-10-10 continue print('## Error\r')
590 3efde7cb 2024-10-10 continue print(f'> {error.fp.read().decode("utf8")}\r')
593 3efde7cb 2024-10-10 continue def unsubscribe(db_con, client_id, lonk_url, honk_url):
594 3efde7cb 2024-10-10 continue honkerid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("honkerid")
595 3efde7cb 2024-10-10 continue if not honkerid:
596 3efde7cb 2024-10-10 continue print("51 Not found\r")
599 3efde7cb 2024-10-10 continue url = unquote(lonk_url.query)
600 056d3709 2024-10-10 continue honk_url.get("savehonker", honkerid=honkerid, unsub="unsub", answer_is_json=False)
601 3efde7cb 2024-10-10 continue print(f'30 {lonk_url.build("gethonkers")}\r')
604 3efde7cb 2024-10-10 continue def subscribe(db_con, client_id, lonk_url, honk_url):
605 3efde7cb 2024-10-10 continue honkerid = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}.get("honkerid")
606 3efde7cb 2024-10-10 continue if not honkerid:
607 3efde7cb 2024-10-10 continue print("51 Not found\r")
610 3efde7cb 2024-10-10 continue url = unquote(lonk_url.query)
611 056d3709 2024-10-10 continue honk_url.get("savehonker", honkerid=honkerid, sub="sub", answer_is_json=False)
612 3efde7cb 2024-10-10 continue print(f'30 {lonk_url.build("gethonkers")}\r')
615 056d3709 2024-10-10 continue def newhonk(db_con, client_id, lonk_url, honk_url):
616 056d3709 2024-10-10 continue if not lonk_url.query:
617 056d3709 2024-10-10 continue print("10 let's make some noise: \r")
620 056d3709 2024-10-10 continue noise = unquote(lonk_url.query)
621 056d3709 2024-10-10 continue honk_url.get("honk", noise=noise, answer_is_json=False)
622 056d3709 2024-10-10 continue print(f'30 {lonk_url.build("myhonks")}\r')
625 056d3709 2024-10-10 continue def honkback(db_con, client_id, lonk_url, honk_url):
626 056d3709 2024-10-10 continue if not lonk_url.query:
627 056d3709 2024-10-10 continue handles = unquote(lonk_url.splitted_path[-3]).strip()
628 056d3709 2024-10-10 continue rid = unquote(lonk_url.splitted_path[-2])
629 056d3709 2024-10-10 continue print(f"10 Answer to {handles or rid}:\r")
632 056d3709 2024-10-10 continue noise = unquote(lonk_url.query)
633 056d3709 2024-10-10 continue rid = unquote(lonk_url.splitted_path[-2])
634 056d3709 2024-10-10 continue honk_url.get("honk", noise=noise, rid=rid, answer_is_json=False)
635 056d3709 2024-10-10 continue print(f'30 {lonk_url.build("myhonks")}\r')
638 895356d0 2024-10-01 continue def authenticated(cert_hash, lonk_url, fn_impl):
639 895356d0 2024-10-01 continue db_con = db_connect()
640 895356d0 2024-10-01 continue row = db_con.execute("SELECT client_id, honk_url, token FROM client WHERE cert_hash=?", (cert_hash, )).fetchone()
641 895356d0 2024-10-01 continue if not row:
642 895356d0 2024-10-01 continue print(f'30 {lonk_url.build("ask_server")}\r')
644 895356d0 2024-10-01 continue client_id, honk_url, token = row
645 895356d0 2024-10-01 continue with db_con:
646 fafb2ce7 2024-10-02 continue fn_impl(db_con, client_id, lonk_url, HonkUrl(honk_url, token))
649 280fbeb1 2024-10-11 continue def new_client_stage_1_ask_server(lonk_url):
650 280fbeb1 2024-10-11 continue if not lonk_url.query:
651 280fbeb1 2024-10-11 continue print("10 Honk server URL\r")
653 280fbeb1 2024-10-11 continue splitted = urlsplit(unquote(lonk_url.query))
654 280fbeb1 2024-10-11 continue path = [quote(urlunsplit((splitted.scheme, splitted.netloc, "", "", "")), safe=""), "ask_username"]
655 280fbeb1 2024-10-11 continue print(f'30 {lonk_url.build(path)}\r')
658 280fbeb1 2024-10-11 continue def new_client_stage_2_ask_username(lonk_url):
659 280fbeb1 2024-10-11 continue if not lonk_url.query:
660 280fbeb1 2024-10-11 continue print("10 Honk user name\r")
662 280fbeb1 2024-10-11 continue if len(lonk_url.splitted_path) < 3:
663 280fbeb1 2024-10-11 continue print('59 Bad request\r')
665 280fbeb1 2024-10-11 continue quoted_server = lonk_url.splitted_path[-2]
666 280fbeb1 2024-10-11 continue path = [quoted_server, quote(unquote(lonk_url.query), safe=""), "ask_password"]
667 280fbeb1 2024-10-11 continue print(f'30 {lonk_url.build(path)}\r')
670 280fbeb1 2024-10-11 continue def new_client_stage_3_ask_password(cert_hash, lonk_url):
671 280fbeb1 2024-10-11 continue if not lonk_url.query:
672 280fbeb1 2024-10-11 continue print("11 Honk user password\r")
674 280fbeb1 2024-10-11 continue if len(lonk_url.splitted_path) < 4:
675 280fbeb1 2024-10-11 continue print('59 Bad request\r')
678 280fbeb1 2024-10-11 continue honk_url = unquote(lonk_url.splitted_path[-3])
679 280fbeb1 2024-10-11 continue post_data = {
680 280fbeb1 2024-10-11 continue "username": unquote(lonk_url.splitted_path[-2]),
681 280fbeb1 2024-10-11 continue "password": unquote(lonk_url.query),
682 280fbeb1 2024-10-11 continue "gettoken": "1",
684 280fbeb1 2024-10-11 continue with urlopen(honk_url + "/dologin", data=urlencode(post_data).encode(), timeout=15) as response:
685 280fbeb1 2024-10-11 continue token = response.read().decode("utf8")
686 280fbeb1 2024-10-11 continue db_con = db_connect()
687 280fbeb1 2024-10-11 continue with db_con:
688 280fbeb1 2024-10-11 continue db_con.execute(
689 280fbeb1 2024-10-11 continue "INSERT INTO client (cert_hash, honk_url, token) VALUES (?, ?, ?)",
690 280fbeb1 2024-10-11 continue (cert_hash, honk_url, token)
692 280fbeb1 2024-10-11 continue print(f'30 {lonk_url.build([])}\r')
695 b7194e03 2024-09-12 continue def proxy(mime, url):
696 f6854b29 2024-10-05 continue with urlopen(url, timeout=10) as response:
697 15efabff 2024-09-13 continue stdout.buffer.write(b"20 " + mime.encode() + b"\r\n")
698 15efabff 2024-09-13 continue while True:
699 f6854b29 2024-10-05 continue content = response.read(512 * 1024)
700 15efabff 2024-09-13 continue if not content:
702 15efabff 2024-09-13 continue stdout.buffer.write(content)
705 4acbf994 2024-09-25 continue def vgi(cert_hash, raw_url):
706 4acbf994 2024-09-25 continue lonk_url = LonkUrl(raw_url)
707 e03b8dd3 2024-09-25 continue if lonk_url.page == "lonk":
708 8b4f10af 2024-10-03 continue authenticated(cert_hash, lonk_url, page_lonk)
709 895356d0 2024-10-01 continue elif lonk_url.page == "convoy":
710 8b4f10af 2024-10-03 continue authenticated(cert_hash, lonk_url, page_convoy)
711 895356d0 2024-10-01 continue elif lonk_url.page == "atme":
712 8b4f10af 2024-10-03 continue authenticated(cert_hash, lonk_url, page_atme)
713 2ff52a39 2024-10-09 continue elif lonk_url.page == "search":
714 2ff52a39 2024-10-09 continue authenticated(cert_hash, lonk_url, page_search)
715 b5ebf4c7 2024-10-09 continue elif lonk_url.page == "longago":
716 b5ebf4c7 2024-10-09 continue authenticated(cert_hash, lonk_url, page_longago)
717 3601f1e9 2024-10-10 continue elif lonk_url.page == "myhonks":
718 3601f1e9 2024-10-10 continue authenticated(cert_hash, lonk_url, page_myhonks)
719 3601f1e9 2024-10-10 continue elif lonk_url.page == "honker":
720 3601f1e9 2024-10-10 continue authenticated(cert_hash, lonk_url, page_honker)
721 f6854b29 2024-10-05 continue elif lonk_url.page == "bonk":
722 f6854b29 2024-10-05 continue authenticated(cert_hash, lonk_url, bonk)
723 3efde7cb 2024-10-10 continue elif lonk_url.page == "gethonkers":
724 3efde7cb 2024-10-10 continue authenticated(cert_hash, lonk_url, gethonkers)
725 3efde7cb 2024-10-10 continue elif lonk_url.page == "addhonker":
726 3efde7cb 2024-10-10 continue authenticated(cert_hash, lonk_url, addhonker)
727 3efde7cb 2024-10-10 continue elif lonk_url.page == "unsubscribe":
728 3efde7cb 2024-10-10 continue authenticated(cert_hash, lonk_url, unsubscribe)
729 3efde7cb 2024-10-10 continue elif lonk_url.page == "subscribe":
730 3efde7cb 2024-10-10 continue authenticated(cert_hash, lonk_url, subscribe)
731 056d3709 2024-10-10 continue elif lonk_url.page == "newhonk":
732 056d3709 2024-10-10 continue authenticated(cert_hash, lonk_url, newhonk)
733 056d3709 2024-10-10 continue elif lonk_url.page == "honkback":
734 056d3709 2024-10-10 continue authenticated(cert_hash, lonk_url, honkback)
735 e03b8dd3 2024-09-25 continue elif lonk_url.page == "ask_server":
736 e03b8dd3 2024-09-25 continue new_client_stage_1_ask_server(lonk_url)
737 e03b8dd3 2024-09-25 continue elif lonk_url.page == "ask_username":
738 e03b8dd3 2024-09-25 continue new_client_stage_2_ask_username(lonk_url)
739 e03b8dd3 2024-09-25 continue elif lonk_url.page == "ask_password":
740 e03b8dd3 2024-09-25 continue new_client_stage_3_ask_password(cert_hash, lonk_url)
741 e03b8dd3 2024-09-25 continue elif lonk_url.page == "proxy":
742 e03b8dd3 2024-09-25 continue query = {pair[0]: pair[1] for pair in parse_qsl(lonk_url.query)}
743 b7194e03 2024-09-12 continue if "m" not in query or "u" not in query:
744 b7194e03 2024-09-12 continue print("51 Not found\r")
746 b7194e03 2024-09-12 continue proxy(mime=query["m"], url=query["u"])
748 b7194e03 2024-09-12 continue print("51 Not found\r")
751 383d16ea 2024-09-04 continue if __name__ == '__main__':
752 4e7950e1 2024-09-30 continue cert_hash_ = environ.get("VGI_CERT_HASH")
753 4e7950e1 2024-09-30 continue if cert_hash_:
755 4acbf994 2024-09-25 continue start_time = clock_gettime(CLOCK_MONOTONIC)
757 4e7950e1 2024-09-30 continue input_url = input().strip()
758 4e7950e1 2024-09-30 continue vgi(cert_hash_, input_url)
759 4acbf994 2024-09-25 continue finally:
760 4e7950e1 2024-09-30 continue stderr.write(f"{cert_hash_}|{input_url}|{clock_gettime(CLOCK_MONOTONIC) - start_time:.3f}sec.\n")
761 15efabff 2024-09-13 continue except HTTPError as error:
762 3efde7cb 2024-10-10 continue stderr.write(f"{error}\n")
763 15efabff 2024-09-13 continue print(f"43 Remote server return {error.code}: {error.reason}\r")
764 15efabff 2024-09-13 continue except URLError as error:
765 3efde7cb 2024-10-10 continue stderr.write(f"{error}\n")
766 15efabff 2024-09-13 continue print(f"43 Error while trying to access remote server: {error.reason}\r")
767 dd301323 2024-09-17 continue except TimeoutError as error:
768 3efde7cb 2024-10-10 continue stderr.write(f"{error}\n")
769 dd301323 2024-09-17 continue print(f"43 Error while trying to access remote server: {error}\r")
771 3efde7cb 2024-10-10 continue stderr.write("Certificate required\n")
772 b7194e03 2024-09-12 continue print("60 Certificate required\r")