Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python3....
File: mailcap.py
"""Mailcap file handling. See RFC 1524."""
[0] Fix | Delete
[1] Fix | Delete
import os
[2] Fix | Delete
import warnings
[3] Fix | Delete
import re
[4] Fix | Delete
[5] Fix | Delete
__all__ = ["getcaps","findmatch"]
[6] Fix | Delete
[7] Fix | Delete
[8] Fix | Delete
def lineno_sort_key(entry):
[9] Fix | Delete
# Sort in ascending order, with unspecified entries at the end
[10] Fix | Delete
if 'lineno' in entry:
[11] Fix | Delete
return 0, entry['lineno']
[12] Fix | Delete
else:
[13] Fix | Delete
return 1, 0
[14] Fix | Delete
[15] Fix | Delete
_find_unsafe = re.compile(r'[^\xa1-\U0010FFFF\w@+=:,./-]').search
[16] Fix | Delete
[17] Fix | Delete
class UnsafeMailcapInput(Warning):
[18] Fix | Delete
"""Warning raised when refusing unsafe input"""
[19] Fix | Delete
[20] Fix | Delete
[21] Fix | Delete
# Part 1: top-level interface.
[22] Fix | Delete
[23] Fix | Delete
def getcaps():
[24] Fix | Delete
"""Return a dictionary containing the mailcap database.
[25] Fix | Delete
[26] Fix | Delete
The dictionary maps a MIME type (in all lowercase, e.g. 'text/plain')
[27] Fix | Delete
to a list of dictionaries corresponding to mailcap entries. The list
[28] Fix | Delete
collects all the entries for that MIME type from all available mailcap
[29] Fix | Delete
files. Each dictionary contains key-value pairs for that MIME type,
[30] Fix | Delete
where the viewing command is stored with the key "view".
[31] Fix | Delete
[32] Fix | Delete
"""
[33] Fix | Delete
caps = {}
[34] Fix | Delete
lineno = 0
[35] Fix | Delete
for mailcap in listmailcapfiles():
[36] Fix | Delete
try:
[37] Fix | Delete
fp = open(mailcap, 'r')
[38] Fix | Delete
except OSError:
[39] Fix | Delete
continue
[40] Fix | Delete
with fp:
[41] Fix | Delete
morecaps, lineno = _readmailcapfile(fp, lineno)
[42] Fix | Delete
for key, value in morecaps.items():
[43] Fix | Delete
if not key in caps:
[44] Fix | Delete
caps[key] = value
[45] Fix | Delete
else:
[46] Fix | Delete
caps[key] = caps[key] + value
[47] Fix | Delete
return caps
[48] Fix | Delete
[49] Fix | Delete
def listmailcapfiles():
[50] Fix | Delete
"""Return a list of all mailcap files found on the system."""
[51] Fix | Delete
# This is mostly a Unix thing, but we use the OS path separator anyway
[52] Fix | Delete
if 'MAILCAPS' in os.environ:
[53] Fix | Delete
pathstr = os.environ['MAILCAPS']
[54] Fix | Delete
mailcaps = pathstr.split(os.pathsep)
[55] Fix | Delete
else:
[56] Fix | Delete
if 'HOME' in os.environ:
[57] Fix | Delete
home = os.environ['HOME']
[58] Fix | Delete
else:
[59] Fix | Delete
# Don't bother with getpwuid()
[60] Fix | Delete
home = '.' # Last resort
[61] Fix | Delete
mailcaps = [home + '/.mailcap', '/etc/mailcap',
[62] Fix | Delete
'/usr/etc/mailcap', '/usr/local/etc/mailcap']
[63] Fix | Delete
return mailcaps
[64] Fix | Delete
[65] Fix | Delete
[66] Fix | Delete
# Part 2: the parser.
[67] Fix | Delete
def readmailcapfile(fp):
[68] Fix | Delete
"""Read a mailcap file and return a dictionary keyed by MIME type."""
[69] Fix | Delete
warnings.warn('readmailcapfile is deprecated, use getcaps instead',
[70] Fix | Delete
DeprecationWarning, 2)
[71] Fix | Delete
caps, _ = _readmailcapfile(fp, None)
[72] Fix | Delete
return caps
[73] Fix | Delete
[74] Fix | Delete
[75] Fix | Delete
def _readmailcapfile(fp, lineno):
[76] Fix | Delete
"""Read a mailcap file and return a dictionary keyed by MIME type.
[77] Fix | Delete
[78] Fix | Delete
Each MIME type is mapped to an entry consisting of a list of
[79] Fix | Delete
dictionaries; the list will contain more than one such dictionary
[80] Fix | Delete
if a given MIME type appears more than once in the mailcap file.
[81] Fix | Delete
Each dictionary contains key-value pairs for that MIME type, where
[82] Fix | Delete
the viewing command is stored with the key "view".
[83] Fix | Delete
"""
[84] Fix | Delete
caps = {}
[85] Fix | Delete
while 1:
[86] Fix | Delete
line = fp.readline()
[87] Fix | Delete
if not line: break
[88] Fix | Delete
# Ignore comments and blank lines
[89] Fix | Delete
if line[0] == '#' or line.strip() == '':
[90] Fix | Delete
continue
[91] Fix | Delete
nextline = line
[92] Fix | Delete
# Join continuation lines
[93] Fix | Delete
while nextline[-2:] == '\\\n':
[94] Fix | Delete
nextline = fp.readline()
[95] Fix | Delete
if not nextline: nextline = '\n'
[96] Fix | Delete
line = line[:-2] + nextline
[97] Fix | Delete
# Parse the line
[98] Fix | Delete
key, fields = parseline(line)
[99] Fix | Delete
if not (key and fields):
[100] Fix | Delete
continue
[101] Fix | Delete
if lineno is not None:
[102] Fix | Delete
fields['lineno'] = lineno
[103] Fix | Delete
lineno += 1
[104] Fix | Delete
# Normalize the key
[105] Fix | Delete
types = key.split('/')
[106] Fix | Delete
for j in range(len(types)):
[107] Fix | Delete
types[j] = types[j].strip()
[108] Fix | Delete
key = '/'.join(types).lower()
[109] Fix | Delete
# Update the database
[110] Fix | Delete
if key in caps:
[111] Fix | Delete
caps[key].append(fields)
[112] Fix | Delete
else:
[113] Fix | Delete
caps[key] = [fields]
[114] Fix | Delete
return caps, lineno
[115] Fix | Delete
[116] Fix | Delete
def parseline(line):
[117] Fix | Delete
"""Parse one entry in a mailcap file and return a dictionary.
[118] Fix | Delete
[119] Fix | Delete
The viewing command is stored as the value with the key "view",
[120] Fix | Delete
and the rest of the fields produce key-value pairs in the dict.
[121] Fix | Delete
"""
[122] Fix | Delete
fields = []
[123] Fix | Delete
i, n = 0, len(line)
[124] Fix | Delete
while i < n:
[125] Fix | Delete
field, i = parsefield(line, i, n)
[126] Fix | Delete
fields.append(field)
[127] Fix | Delete
i = i+1 # Skip semicolon
[128] Fix | Delete
if len(fields) < 2:
[129] Fix | Delete
return None, None
[130] Fix | Delete
key, view, rest = fields[0], fields[1], fields[2:]
[131] Fix | Delete
fields = {'view': view}
[132] Fix | Delete
for field in rest:
[133] Fix | Delete
i = field.find('=')
[134] Fix | Delete
if i < 0:
[135] Fix | Delete
fkey = field
[136] Fix | Delete
fvalue = ""
[137] Fix | Delete
else:
[138] Fix | Delete
fkey = field[:i].strip()
[139] Fix | Delete
fvalue = field[i+1:].strip()
[140] Fix | Delete
if fkey in fields:
[141] Fix | Delete
# Ignore it
[142] Fix | Delete
pass
[143] Fix | Delete
else:
[144] Fix | Delete
fields[fkey] = fvalue
[145] Fix | Delete
return key, fields
[146] Fix | Delete
[147] Fix | Delete
def parsefield(line, i, n):
[148] Fix | Delete
"""Separate one key-value pair in a mailcap entry."""
[149] Fix | Delete
start = i
[150] Fix | Delete
while i < n:
[151] Fix | Delete
c = line[i]
[152] Fix | Delete
if c == ';':
[153] Fix | Delete
break
[154] Fix | Delete
elif c == '\\':
[155] Fix | Delete
i = i+2
[156] Fix | Delete
else:
[157] Fix | Delete
i = i+1
[158] Fix | Delete
return line[start:i].strip(), i
[159] Fix | Delete
[160] Fix | Delete
[161] Fix | Delete
# Part 3: using the database.
[162] Fix | Delete
[163] Fix | Delete
def findmatch(caps, MIMEtype, key='view', filename="/dev/null", plist=[]):
[164] Fix | Delete
"""Find a match for a mailcap entry.
[165] Fix | Delete
[166] Fix | Delete
Return a tuple containing the command line, and the mailcap entry
[167] Fix | Delete
used; (None, None) if no match is found. This may invoke the
[168] Fix | Delete
'test' command of several matching entries before deciding which
[169] Fix | Delete
entry to use.
[170] Fix | Delete
[171] Fix | Delete
"""
[172] Fix | Delete
if _find_unsafe(filename):
[173] Fix | Delete
msg = "Refusing to use mailcap with filename %r. Use a safe temporary filename." % (filename,)
[174] Fix | Delete
warnings.warn(msg, UnsafeMailcapInput)
[175] Fix | Delete
return None, None
[176] Fix | Delete
entries = lookup(caps, MIMEtype, key)
[177] Fix | Delete
# XXX This code should somehow check for the needsterminal flag.
[178] Fix | Delete
for e in entries:
[179] Fix | Delete
if 'test' in e:
[180] Fix | Delete
test = subst(e['test'], filename, plist)
[181] Fix | Delete
if test is None:
[182] Fix | Delete
continue
[183] Fix | Delete
if test and os.system(test) != 0:
[184] Fix | Delete
continue
[185] Fix | Delete
command = subst(e[key], MIMEtype, filename, plist)
[186] Fix | Delete
if command is not None:
[187] Fix | Delete
return command, e
[188] Fix | Delete
return None, None
[189] Fix | Delete
[190] Fix | Delete
def lookup(caps, MIMEtype, key=None):
[191] Fix | Delete
entries = []
[192] Fix | Delete
if MIMEtype in caps:
[193] Fix | Delete
entries = entries + caps[MIMEtype]
[194] Fix | Delete
MIMEtypes = MIMEtype.split('/')
[195] Fix | Delete
MIMEtype = MIMEtypes[0] + '/*'
[196] Fix | Delete
if MIMEtype in caps:
[197] Fix | Delete
entries = entries + caps[MIMEtype]
[198] Fix | Delete
if key is not None:
[199] Fix | Delete
entries = [e for e in entries if key in e]
[200] Fix | Delete
entries = sorted(entries, key=lineno_sort_key)
[201] Fix | Delete
return entries
[202] Fix | Delete
[203] Fix | Delete
def subst(field, MIMEtype, filename, plist=[]):
[204] Fix | Delete
# XXX Actually, this is Unix-specific
[205] Fix | Delete
res = ''
[206] Fix | Delete
i, n = 0, len(field)
[207] Fix | Delete
while i < n:
[208] Fix | Delete
c = field[i]; i = i+1
[209] Fix | Delete
if c != '%':
[210] Fix | Delete
if c == '\\':
[211] Fix | Delete
c = field[i:i+1]; i = i+1
[212] Fix | Delete
res = res + c
[213] Fix | Delete
else:
[214] Fix | Delete
c = field[i]; i = i+1
[215] Fix | Delete
if c == '%':
[216] Fix | Delete
res = res + c
[217] Fix | Delete
elif c == 's':
[218] Fix | Delete
res = res + filename
[219] Fix | Delete
elif c == 't':
[220] Fix | Delete
if _find_unsafe(MIMEtype):
[221] Fix | Delete
msg = "Refusing to substitute MIME type %r into a shell command." % (MIMEtype,)
[222] Fix | Delete
warnings.warn(msg, UnsafeMailcapInput)
[223] Fix | Delete
return None
[224] Fix | Delete
res = res + MIMEtype
[225] Fix | Delete
elif c == '{':
[226] Fix | Delete
start = i
[227] Fix | Delete
while i < n and field[i] != '}':
[228] Fix | Delete
i = i+1
[229] Fix | Delete
name = field[start:i]
[230] Fix | Delete
i = i+1
[231] Fix | Delete
param = findparam(name, plist)
[232] Fix | Delete
if _find_unsafe(param):
[233] Fix | Delete
msg = "Refusing to substitute parameter %r (%s) into a shell command" % (param, name)
[234] Fix | Delete
warnings.warn(msg, UnsafeMailcapInput)
[235] Fix | Delete
return None
[236] Fix | Delete
res = res + param
[237] Fix | Delete
# XXX To do:
[238] Fix | Delete
# %n == number of parts if type is multipart/*
[239] Fix | Delete
# %F == list of alternating type and filename for parts
[240] Fix | Delete
else:
[241] Fix | Delete
res = res + '%' + c
[242] Fix | Delete
return res
[243] Fix | Delete
[244] Fix | Delete
def findparam(name, plist):
[245] Fix | Delete
name = name.lower() + '='
[246] Fix | Delete
n = len(name)
[247] Fix | Delete
for p in plist:
[248] Fix | Delete
if p[:n].lower() == name:
[249] Fix | Delete
return p[n:]
[250] Fix | Delete
return ''
[251] Fix | Delete
[252] Fix | Delete
[253] Fix | Delete
# Part 4: test program.
[254] Fix | Delete
[255] Fix | Delete
def test():
[256] Fix | Delete
import sys
[257] Fix | Delete
caps = getcaps()
[258] Fix | Delete
if not sys.argv[1:]:
[259] Fix | Delete
show(caps)
[260] Fix | Delete
return
[261] Fix | Delete
for i in range(1, len(sys.argv), 2):
[262] Fix | Delete
args = sys.argv[i:i+2]
[263] Fix | Delete
if len(args) < 2:
[264] Fix | Delete
print("usage: mailcap [MIMEtype file] ...")
[265] Fix | Delete
return
[266] Fix | Delete
MIMEtype = args[0]
[267] Fix | Delete
file = args[1]
[268] Fix | Delete
command, e = findmatch(caps, MIMEtype, 'view', file)
[269] Fix | Delete
if not command:
[270] Fix | Delete
print("No viewer found for", type)
[271] Fix | Delete
else:
[272] Fix | Delete
print("Executing:", command)
[273] Fix | Delete
sts = os.system(command)
[274] Fix | Delete
if sts:
[275] Fix | Delete
print("Exit status:", sts)
[276] Fix | Delete
[277] Fix | Delete
def show(caps):
[278] Fix | Delete
print("Mailcap files:")
[279] Fix | Delete
for fn in listmailcapfiles(): print("\t" + fn)
[280] Fix | Delete
print()
[281] Fix | Delete
if not caps: caps = getcaps()
[282] Fix | Delete
print("Mailcap entries:")
[283] Fix | Delete
print()
[284] Fix | Delete
ckeys = sorted(caps)
[285] Fix | Delete
for type in ckeys:
[286] Fix | Delete
print(type)
[287] Fix | Delete
entries = caps[type]
[288] Fix | Delete
for e in entries:
[289] Fix | Delete
keys = sorted(e)
[290] Fix | Delete
for k in keys:
[291] Fix | Delete
print(" %-15s" % k, e[k])
[292] Fix | Delete
print()
[293] Fix | Delete
[294] Fix | Delete
if __name__ == '__main__':
[295] Fix | Delete
test()
[296] Fix | Delete
[297] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function