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
|
||||
|
||||
import cgi
|
||||
import mailcap
|
||||
import os
|
||||
import socket
|
||||
import ssl
|
||||
import tempfile
|
||||
import urllib.parse
|
||||
|
||||
caps = mailcap.getcaps()
|
||||
menu = []
|
||||
hist = []
|
||||
|
||||
|
@ -66,11 +70,9 @@ while True:
|
|||
if not status.startswith("2"):
|
||||
print("Error %s: %s" % (status, mime))
|
||||
continue
|
||||
# Text only!
|
||||
if not mime.startswith("text/"):
|
||||
print("Sorry, plain text only.")
|
||||
continue
|
||||
else:
|
||||
# Handle text
|
||||
if mime.startswith("text/"):
|
||||
# Decode according to declared charset
|
||||
mime, mime_opts = cgi.parse_header(mime)
|
||||
body = fp.read()
|
||||
body = body.decode(mime_opts.get("charset","UTF-8"))
|
||||
|
@ -88,4 +90,13 @@ while True:
|
|||
# Handle any other plain text
|
||||
else:
|
||||
print(body)
|
||||
# Handle non-text
|
||||
else:
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue