Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/python27/lib64/python2....
File: mimetools.py
"""Various tools used by MIME-reading or MIME-writing programs."""
[0] Fix | Delete
[1] Fix | Delete
[2] Fix | Delete
import os
[3] Fix | Delete
import sys
[4] Fix | Delete
import tempfile
[5] Fix | Delete
from warnings import filterwarnings, catch_warnings
[6] Fix | Delete
with catch_warnings():
[7] Fix | Delete
if sys.py3kwarning:
[8] Fix | Delete
filterwarnings("ignore", ".*rfc822 has been removed", DeprecationWarning)
[9] Fix | Delete
import rfc822
[10] Fix | Delete
[11] Fix | Delete
from warnings import warnpy3k
[12] Fix | Delete
warnpy3k("in 3.x, mimetools has been removed in favor of the email package",
[13] Fix | Delete
stacklevel=2)
[14] Fix | Delete
[15] Fix | Delete
__all__ = ["Message","choose_boundary","encode","decode","copyliteral",
[16] Fix | Delete
"copybinary"]
[17] Fix | Delete
[18] Fix | Delete
class Message(rfc822.Message):
[19] Fix | Delete
"""A derived class of rfc822.Message that knows about MIME headers and
[20] Fix | Delete
contains some hooks for decoding encoded and multipart messages."""
[21] Fix | Delete
[22] Fix | Delete
def __init__(self, fp, seekable = 1):
[23] Fix | Delete
rfc822.Message.__init__(self, fp, seekable)
[24] Fix | Delete
self.encodingheader = \
[25] Fix | Delete
self.getheader('content-transfer-encoding')
[26] Fix | Delete
self.typeheader = \
[27] Fix | Delete
self.getheader('content-type')
[28] Fix | Delete
self.parsetype()
[29] Fix | Delete
self.parseplist()
[30] Fix | Delete
[31] Fix | Delete
def parsetype(self):
[32] Fix | Delete
str = self.typeheader
[33] Fix | Delete
if str is None:
[34] Fix | Delete
str = 'text/plain'
[35] Fix | Delete
if ';' in str:
[36] Fix | Delete
i = str.index(';')
[37] Fix | Delete
self.plisttext = str[i:]
[38] Fix | Delete
str = str[:i]
[39] Fix | Delete
else:
[40] Fix | Delete
self.plisttext = ''
[41] Fix | Delete
fields = str.split('/')
[42] Fix | Delete
for i in range(len(fields)):
[43] Fix | Delete
fields[i] = fields[i].strip().lower()
[44] Fix | Delete
self.type = '/'.join(fields)
[45] Fix | Delete
self.maintype = fields[0]
[46] Fix | Delete
self.subtype = '/'.join(fields[1:])
[47] Fix | Delete
[48] Fix | Delete
def parseplist(self):
[49] Fix | Delete
str = self.plisttext
[50] Fix | Delete
self.plist = []
[51] Fix | Delete
while str[:1] == ';':
[52] Fix | Delete
str = str[1:]
[53] Fix | Delete
if ';' in str:
[54] Fix | Delete
# XXX Should parse quotes!
[55] Fix | Delete
end = str.index(';')
[56] Fix | Delete
else:
[57] Fix | Delete
end = len(str)
[58] Fix | Delete
f = str[:end]
[59] Fix | Delete
if '=' in f:
[60] Fix | Delete
i = f.index('=')
[61] Fix | Delete
f = f[:i].strip().lower() + \
[62] Fix | Delete
'=' + f[i+1:].strip()
[63] Fix | Delete
self.plist.append(f.strip())
[64] Fix | Delete
str = str[end:]
[65] Fix | Delete
[66] Fix | Delete
def getplist(self):
[67] Fix | Delete
return self.plist
[68] Fix | Delete
[69] Fix | Delete
def getparam(self, name):
[70] Fix | Delete
name = name.lower() + '='
[71] Fix | Delete
n = len(name)
[72] Fix | Delete
for p in self.plist:
[73] Fix | Delete
if p[:n] == name:
[74] Fix | Delete
return rfc822.unquote(p[n:])
[75] Fix | Delete
return None
[76] Fix | Delete
[77] Fix | Delete
def getparamnames(self):
[78] Fix | Delete
result = []
[79] Fix | Delete
for p in self.plist:
[80] Fix | Delete
i = p.find('=')
[81] Fix | Delete
if i >= 0:
[82] Fix | Delete
result.append(p[:i].lower())
[83] Fix | Delete
return result
[84] Fix | Delete
[85] Fix | Delete
def getencoding(self):
[86] Fix | Delete
if self.encodingheader is None:
[87] Fix | Delete
return '7bit'
[88] Fix | Delete
return self.encodingheader.lower()
[89] Fix | Delete
[90] Fix | Delete
def gettype(self):
[91] Fix | Delete
return self.type
[92] Fix | Delete
[93] Fix | Delete
def getmaintype(self):
[94] Fix | Delete
return self.maintype
[95] Fix | Delete
[96] Fix | Delete
def getsubtype(self):
[97] Fix | Delete
return self.subtype
[98] Fix | Delete
[99] Fix | Delete
[100] Fix | Delete
[101] Fix | Delete
[102] Fix | Delete
# Utility functions
[103] Fix | Delete
# -----------------
[104] Fix | Delete
[105] Fix | Delete
try:
[106] Fix | Delete
import thread
[107] Fix | Delete
except ImportError:
[108] Fix | Delete
import dummy_thread as thread
[109] Fix | Delete
_counter_lock = thread.allocate_lock()
[110] Fix | Delete
del thread
[111] Fix | Delete
[112] Fix | Delete
_counter = 0
[113] Fix | Delete
def _get_next_counter():
[114] Fix | Delete
global _counter
[115] Fix | Delete
_counter_lock.acquire()
[116] Fix | Delete
_counter += 1
[117] Fix | Delete
result = _counter
[118] Fix | Delete
_counter_lock.release()
[119] Fix | Delete
return result
[120] Fix | Delete
[121] Fix | Delete
_prefix = None
[122] Fix | Delete
[123] Fix | Delete
def choose_boundary():
[124] Fix | Delete
"""Return a string usable as a multipart boundary.
[125] Fix | Delete
[126] Fix | Delete
The string chosen is unique within a single program run, and
[127] Fix | Delete
incorporates the user id (if available), process id (if available),
[128] Fix | Delete
and current time. So it's very unlikely the returned string appears
[129] Fix | Delete
in message text, but there's no guarantee.
[130] Fix | Delete
[131] Fix | Delete
The boundary contains dots so you have to quote it in the header."""
[132] Fix | Delete
[133] Fix | Delete
global _prefix
[134] Fix | Delete
import time
[135] Fix | Delete
if _prefix is None:
[136] Fix | Delete
import socket
[137] Fix | Delete
try:
[138] Fix | Delete
hostid = socket.gethostbyname(socket.gethostname())
[139] Fix | Delete
except socket.gaierror:
[140] Fix | Delete
hostid = '127.0.0.1'
[141] Fix | Delete
try:
[142] Fix | Delete
uid = repr(os.getuid())
[143] Fix | Delete
except AttributeError:
[144] Fix | Delete
uid = '1'
[145] Fix | Delete
try:
[146] Fix | Delete
pid = repr(os.getpid())
[147] Fix | Delete
except AttributeError:
[148] Fix | Delete
pid = '1'
[149] Fix | Delete
_prefix = hostid + '.' + uid + '.' + pid
[150] Fix | Delete
return "%s.%.3f.%d" % (_prefix, time.time(), _get_next_counter())
[151] Fix | Delete
[152] Fix | Delete
[153] Fix | Delete
# Subroutines for decoding some common content-transfer-types
[154] Fix | Delete
[155] Fix | Delete
def decode(input, output, encoding):
[156] Fix | Delete
"""Decode common content-transfer-encodings (base64, quopri, uuencode)."""
[157] Fix | Delete
if encoding == 'base64':
[158] Fix | Delete
import base64
[159] Fix | Delete
return base64.decode(input, output)
[160] Fix | Delete
if encoding == 'quoted-printable':
[161] Fix | Delete
import quopri
[162] Fix | Delete
return quopri.decode(input, output)
[163] Fix | Delete
if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
[164] Fix | Delete
import uu
[165] Fix | Delete
return uu.decode(input, output)
[166] Fix | Delete
if encoding in ('7bit', '8bit'):
[167] Fix | Delete
return output.write(input.read())
[168] Fix | Delete
if encoding in decodetab:
[169] Fix | Delete
pipethrough(input, decodetab[encoding], output)
[170] Fix | Delete
else:
[171] Fix | Delete
raise ValueError, \
[172] Fix | Delete
'unknown Content-Transfer-Encoding: %s' % encoding
[173] Fix | Delete
[174] Fix | Delete
def encode(input, output, encoding):
[175] Fix | Delete
"""Encode common content-transfer-encodings (base64, quopri, uuencode)."""
[176] Fix | Delete
if encoding == 'base64':
[177] Fix | Delete
import base64
[178] Fix | Delete
return base64.encode(input, output)
[179] Fix | Delete
if encoding == 'quoted-printable':
[180] Fix | Delete
import quopri
[181] Fix | Delete
return quopri.encode(input, output, 0)
[182] Fix | Delete
if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
[183] Fix | Delete
import uu
[184] Fix | Delete
return uu.encode(input, output)
[185] Fix | Delete
if encoding in ('7bit', '8bit'):
[186] Fix | Delete
return output.write(input.read())
[187] Fix | Delete
if encoding in encodetab:
[188] Fix | Delete
pipethrough(input, encodetab[encoding], output)
[189] Fix | Delete
else:
[190] Fix | Delete
raise ValueError, \
[191] Fix | Delete
'unknown Content-Transfer-Encoding: %s' % encoding
[192] Fix | Delete
[193] Fix | Delete
# The following is no longer used for standard encodings
[194] Fix | Delete
[195] Fix | Delete
# XXX This requires that uudecode and mmencode are in $PATH
[196] Fix | Delete
[197] Fix | Delete
uudecode_pipe = '''(
[198] Fix | Delete
TEMP=/tmp/@uu.$$
[199] Fix | Delete
sed "s%^begin [0-7][0-7]* .*%begin 600 $TEMP%" | uudecode
[200] Fix | Delete
cat $TEMP
[201] Fix | Delete
rm $TEMP
[202] Fix | Delete
)'''
[203] Fix | Delete
[204] Fix | Delete
decodetab = {
[205] Fix | Delete
'uuencode': uudecode_pipe,
[206] Fix | Delete
'x-uuencode': uudecode_pipe,
[207] Fix | Delete
'uue': uudecode_pipe,
[208] Fix | Delete
'x-uue': uudecode_pipe,
[209] Fix | Delete
'quoted-printable': 'mmencode -u -q',
[210] Fix | Delete
'base64': 'mmencode -u -b',
[211] Fix | Delete
}
[212] Fix | Delete
[213] Fix | Delete
encodetab = {
[214] Fix | Delete
'x-uuencode': 'uuencode tempfile',
[215] Fix | Delete
'uuencode': 'uuencode tempfile',
[216] Fix | Delete
'x-uue': 'uuencode tempfile',
[217] Fix | Delete
'uue': 'uuencode tempfile',
[218] Fix | Delete
'quoted-printable': 'mmencode -q',
[219] Fix | Delete
'base64': 'mmencode -b',
[220] Fix | Delete
}
[221] Fix | Delete
[222] Fix | Delete
def pipeto(input, command):
[223] Fix | Delete
pipe = os.popen(command, 'w')
[224] Fix | Delete
copyliteral(input, pipe)
[225] Fix | Delete
pipe.close()
[226] Fix | Delete
[227] Fix | Delete
def pipethrough(input, command, output):
[228] Fix | Delete
(fd, tempname) = tempfile.mkstemp()
[229] Fix | Delete
temp = os.fdopen(fd, 'w')
[230] Fix | Delete
copyliteral(input, temp)
[231] Fix | Delete
temp.close()
[232] Fix | Delete
pipe = os.popen(command + ' <' + tempname, 'r')
[233] Fix | Delete
copybinary(pipe, output)
[234] Fix | Delete
pipe.close()
[235] Fix | Delete
os.unlink(tempname)
[236] Fix | Delete
[237] Fix | Delete
def copyliteral(input, output):
[238] Fix | Delete
while 1:
[239] Fix | Delete
line = input.readline()
[240] Fix | Delete
if not line: break
[241] Fix | Delete
output.write(line)
[242] Fix | Delete
[243] Fix | Delete
def copybinary(input, output):
[244] Fix | Delete
BUFSIZE = 8192
[245] Fix | Delete
while 1:
[246] Fix | Delete
line = input.read(BUFSIZE)
[247] Fix | Delete
if not line: break
[248] Fix | Delete
output.write(line)
[249] Fix | Delete
[250] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function