Handle non-text files using mailcap-specified programs.
This commit is contained in:
parent
1668ed1468
commit
9d61d4a4aa
|
@ -1,10 +1,14 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import cgi
|
import cgi
|
||||||
|
import mailcap
|
||||||
|
import os
|
||||||
import socket
|
import socket
|
||||||
import ssl
|
import ssl
|
||||||
|
import tempfile
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
|
caps = mailcap.getcaps()
|
||||||
menu = []
|
menu = []
|
||||||
hist = []
|
hist = []
|
||||||
|
|
||||||
|
@ -66,26 +70,33 @@ while True:
|
||||||
if not status.startswith("2"):
|
if not status.startswith("2"):
|
||||||
print("Error %s: %s" % (status, mime))
|
print("Error %s: %s" % (status, mime))
|
||||||
continue
|
continue
|
||||||
# Text only!
|
# Handle text
|
||||||
if not mime.startswith("text/"):
|
if mime.startswith("text/"):
|
||||||
print("Sorry, plain text only.")
|
# Decode according to declared charset
|
||||||
continue
|
|
||||||
else:
|
|
||||||
mime, mime_opts = cgi.parse_header(mime)
|
mime, mime_opts = cgi.parse_header(mime)
|
||||||
body = fp.read()
|
body = fp.read()
|
||||||
body = body.decode(mime_opts.get("charset","UTF-8"))
|
body = body.decode(mime_opts.get("charset","UTF-8"))
|
||||||
# Handle a Gemini map
|
# Handle a Gemini map
|
||||||
if mime == "text/gemini":
|
if mime == "text/gemini":
|
||||||
menu = []
|
menu = []
|
||||||
for line in body.splitlines():
|
for line in body.splitlines():
|
||||||
if line.startswith("\t") and line.count("\t") == 2:
|
if line.startswith("\t") and line.count("\t") == 2:
|
||||||
_, text, link_url = line.split("\t")
|
_, text, link_url = line.split("\t")
|
||||||
link_url = absolutise_url(url, link_url)
|
link_url = absolutise_url(url, link_url)
|
||||||
menu.append(link_url)
|
menu.append(link_url)
|
||||||
print("[%d] %s" % (len(menu), text))
|
print("[%d] %s" % (len(menu), text))
|
||||||
else:
|
else:
|
||||||
print(line)
|
print(line)
|
||||||
# Handle any other plain text
|
# Handle any other plain text
|
||||||
|
else:
|
||||||
|
print(body)
|
||||||
|
# Handle non-text
|
||||||
else:
|
else:
|
||||||
print(body)
|
tmpfp = tempfile.NamedTemporaryFile("wb", delete=False)
|
||||||
|
tmpfp.write(fp.read())
|
||||||
|
tmpfp.close()
|
||||||
|
cmd_str, _ = mailcap.findmatch(caps, mime, filename=tmpfp.name)
|
||||||
|
os.system(cmd_str)
|
||||||
|
os.unlink(tmpfp.name)
|
||||||
|
# Update history
|
||||||
hist.append(url)
|
hist.append(url)
|
||||||
|
|
Loading…
Reference in New Issue