mb2q: Fetch mbox from lore

Email clients suck. Some can't even export emails as an mbox.

When passing the -M option and a MessageID as instead of a filename,
mb2q will fetch the mbox containing this MessgeID and use that
as the input.

Signed-off-by: Marc Zyngier <maz@kernel.org>
diff --git a/mb2q b/mb2q
index 15571f4..174e46a 100755
--- a/mb2q
+++ b/mb2q
@@ -4,6 +4,8 @@
 
 from email.utils import make_msgid, formatdate
 from argparse import ArgumentParser
+from gzip import GzipFile
+import urllib.request
 import subprocess
 import mailbox
 import email.policy
@@ -650,6 +652,9 @@
     parser.add_argument('-S', '--SortSubject', dest='sortsubject',
                         action='store_true',
                         help='Sort input mbox by subject')
+    parser.add_argument('-M', '--MessageID', dest='msgid',
+                        action='store_true',
+                        help='Download mbox from lore')
     args = parser.parse_args()
 
     try:
@@ -678,6 +683,15 @@
     compile_tags()
 
     q = quilter(args)
+
+    if args.msgid:
+        baseurl = urllib.request.urlopen('https://lore.kernel.org/r/' + args.inbox)
+        url = urllib.request.urlopen(baseurl.geturl() + 't.mbox')
+        mboxgz = GzipFile(None, 'rb', 9, url)
+        with open(args.inbox, "wb") as mboxfile:
+            mboxfile.write(mboxgz.read())
+            mboxfile.close()
+
     mbox = mailbox.mbox(args.inbox, create=False)
 
     if args.sortsubject:
@@ -702,3 +716,6 @@
             os.makedirs(pdir)
         q.write_series(pdir)
         q.write_patches(pdir)
+
+    if args.msgid:
+        os.unlink(args.inbox)