mwahaha behold the power of python hax
authorJann Horn <jann@thejh.net>
Mon, 2 Sep 2024 00:51:16 +0000 (02:51 +0200)
committerJann Horn <jann@thejh.net>
Mon, 2 Sep 2024 00:51:16 +0000 (02:51 +0200)
main.py

diff --git a/main.py b/main.py
index cb35da4..3481978 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -1,11 +1,39 @@
 #!/usr/bin/env python3
+import builtins
+#import contextlib
+import io
+import os
+_open = builtins.open
+#@contextlib.contextmanager
+def open_override(path, *args, **kwargs):
+    if str(path).startswith('/MBOX/'):
+        data = mbox_data[path[6:]]
+        fd = os.memfd_create('mbox')
+        file = None
+        try:
+            if os.write(fd, data) != len(data):
+                raise Exception('os.write short???')
+            os.lseek(fd, 0, os.SEEK_SET)
+            file = _open(fd, *args, **kwargs)
+        finally:
+            if file == None:
+                os.close(fd)
+        return file
+    return _open(path, *args, **kwargs)
+builtins.open = open_override
+
 import copy
 import difflib
 import email
 import email.policy
+import gzip
 import html
+import http.client
 import mailbox
-from urllib.parse import parse_qs
+from urllib.parse import parse_qs, quote
+
+mbox_data = {}
+mbox_data_index = 0
 
 # for diff preparation:
 # normalize whitespace sequences to single space, for when people reply from
@@ -288,8 +316,8 @@ class ThreadMessage:
             # only do this for diff roots, at the end
             self.blocks.trim_blocks()
 
-def parse_mbox():
-    mbox = mailbox.mbox('t.mbox', factory=lambda x: email.message_from_binary_file(x, policy=email.policy.default))
+def parse_mbox(mail_path):
+    mbox = mailbox.mbox(mail_path, factory=lambda x: email.message_from_binary_file(x, policy=email.policy.default))
     root_message = None
     messages_by_id = {}
     all_messages = []
@@ -346,9 +374,6 @@ def app(environ, start_response):
     out = []
     query = parse_qs(environ.get('QUERY_STRING', ''))
     msgid = query.get('msgid', [''])[0]
-    print(repr(msgid))
-
-    all_topic_roots = parse_mbox()
 
     out.append('<!DOCTYPE html>')
     out.append('<html>')
@@ -379,29 +404,59 @@ def app(environ, start_response):
     out.append('      </form>')
     out.append('    </div>')
     out.append('    <div id="below-header">')
-    out.append('      <div id="topic_panel">')
-    out.append('        <h1>Topics</h1>')
-    for topic_root in all_topic_roots:
-        out.append('        <span class="topic_link'+(' empty-topic' if topic_root.non_diff_root_descendants == 0 else '')+'"><a href="#topic-'+str(topic_root.index)+'">'+html.escape(topic_root.msg.get('subject', '<no subject>'), quote=False)+'</a> ('+str(topic_root.non_diff_root_descendants)+' replies)</span>')
-    out.append('      </div>')
-    out.append('      <div id="main_panel">')
-    for topic_root in all_topic_roots:
-        out.append('        <div class="alt-content" id="topic-'+str(topic_root.index)+'">')
-        printed_top_header = False
-        printed_threads_header = False
-        for block in topic_root.blocks.ranges:
-            if block.start_pos_child < 0 and not printed_top_header:
-                out.append('          <h1>Top-posted messages (sorry this section looks like a mess)</h1>')
-                printed_top_header = True
-            if block.start_pos_child >= 0 and not printed_threads_header:
-                out.append('          <h1>Actual thread</h1>')
-                printed_threads_header = True
-            out.append('          <div class="message-block-container" style="margin-left:'+str(block.message.depth*4)+'em">')
-            out.append('            <div class="message-block-meta">'+html.escape(block.message.from_hdr, quote=False)+'</div>')
-            out.append('            <div class="message-block-content">'+html.escape(block.get_text(), quote=False)+'</div>')
-            out.append('          </div>')
-        out.append('        </div>')
-    out.append('      </div>')
+
+    all_topic_roots = None
+    if msgid != '':
+        http_conn = http.client.HTTPSConnection('lore.kernel.org')
+        try:
+            http_conn.request('GET', '/all/'+quote(msgid, safe='')+'/t.mbox.gz', headers={'Host': 'lore.kernel.org'})
+            http_resp = http_conn.getresponse()
+            if http_resp.status != 200:
+                out.append('lore.kernel.org returned '+str(http_resp.status)+' '+html.escape(http_resp.reason, quote=False))
+            else:
+                response_body = http_resp.read()
+                out.append('lore.kernel.org returned '+str(len(response_body))+' bytes<br>')
+                response_body = gzip.decompress(response_body)
+                out.append('decompresses to '+str(len(response_body))+' bytes<br>')
+
+                # hack...
+                global mbox_data_index
+                mbox_data_index += 1
+                mbox_data_index_str = str(mbox_data_index)
+                #response_body = response_body.decode('utf-8')
+                mbox_data[mbox_data_index_str] = response_body
+                try:
+                    all_topic_roots = parse_mbox('/MBOX/'+mbox_data_index_str)
+                finally:
+                    del mbox_data[mbox_data_index_str]
+
+        finally:
+            http_conn.close()
+
+    if all_topic_roots != None:
+        out.append('      <div id="topic_panel">')
+        out.append('        <h1>Topics</h1>')
+        for topic_root in all_topic_roots:
+            out.append('        <span class="topic_link'+(' empty-topic' if topic_root.non_diff_root_descendants == 0 else '')+'"><a href="#topic-'+str(topic_root.index)+'">'+html.escape(topic_root.msg.get('subject', '<no subject>'), quote=False)+'</a> ('+str(topic_root.non_diff_root_descendants)+' replies)</span>')
+        out.append('      </div>')
+        out.append('      <div id="main_panel">')
+        for topic_root in all_topic_roots:
+            out.append('        <div class="alt-content" id="topic-'+str(topic_root.index)+'">')
+            printed_top_header = False
+            printed_threads_header = False
+            for block in topic_root.blocks.ranges:
+                if block.start_pos_child < 0 and not printed_top_header:
+                    out.append('          <h1>Top-posted messages (sorry this section looks like a mess)</h1>')
+                    printed_top_header = True
+                if block.start_pos_child >= 0 and not printed_threads_header:
+                    out.append('          <h1>Actual thread</h1>')
+                    printed_threads_header = True
+                out.append('          <div class="message-block-container" style="margin-left:'+str(block.message.depth*4)+'em">')
+                out.append('            <div class="message-block-meta">'+html.escape(block.message.from_hdr, quote=False)+'</div>')
+                out.append('            <div class="message-block-content">'+html.escape(block.get_text(), quote=False)+'</div>')
+                out.append('          </div>')
+            out.append('        </div>')
+        out.append('      </div>')
     out.append('    </div>')
     out.append('  </body>')
     out.append('</html>')