Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
mimify Namespace Reference

Functions

def unmimify
 
def mime_encode
 
def mime_encode_header
 
def mimify_part
 
def mimify
 

Variables

int MAXLEN = 200
 
string CHARSET = 'ISO-8859-1'
 
string QUOTE = '> '
 
list __all__ = ["mimify","unmimify","mime_encode_header","mime_decode_header"]
 
tuple qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I)
 
tuple base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I)
 
tuple mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)
 
int is_repl = 1
 
tuple mp_res = mp.match(line)
 
string multipart = '--'
 if is_repl and len(line) >= 4 and line[:4] == QUOTE+'–' and line[-3:] != '–
': multipart = line[:-1] More...
 
tuple line = ifile.readline()
 
 pref = prefix
 
tuple nifile = File(ifile, multipart)
 
tuple newline = ifile.readline()
 
tuple mime_char = re.compile('[=\177-\377]')
 
tuple mime_header_char = re.compile('[=?\177-\377]')
 
tuple mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)(?=[ \t)]|\n)')
 
tuple mv = re.compile('^mime-version:', re.I)
 
tuple cte = re.compile('^content-transfer-encoding:', re.I)
 
tuple iso_char = re.compile('[\177-\377]')
 
string usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]'
 
int decode_base64 = 0
 
 encode = mimify
 
tuple encode_args = (sys.stdin, sys.stdout)
 

Function Documentation

def mimify.mime_encode (   line,
  header 
)
Code a single line as quoted-printable.
If header is set, quote some extra characters.

Definition at line 224 of file mimify.py.

References string.upper().

225 def mime_encode(line, header):
226  """Code a single line as quoted-printable.
227  If header is set, quote some extra characters."""
228  if header:
229  reg = mime_header_char
230  else:
231  reg = mime_char
232  newline = ''
233  pos = 0
234  if len(line) >= 5 and line[:5] == 'From ':
235  # quote 'From ' at the start of a line for stupid mailers
236  newline = ('=%02x' % ord('F')).upper()
237  pos = 1
238  while 1:
239  res = reg.search(line, pos)
240  if res is None:
241  break
242  newline = newline + line[pos:res.start(0)] + \
243  ('=%02x' % ord(res.group(0))).upper()
244  pos = res.end(0)
245  line = newline + line[pos:]
246 
247  newline = ''
248  while len(line) >= 75:
249  i = 73
250  while line[i] == '=' or line[i-1] == '=':
251  i = i - 1
252  i = i + 1
253  newline = newline + line[:i] + '=\n'
254  line = line[i:]
255  return newline + line
def mimify.mime_encode_header (   line)
Code a single header line as quoted-printable.

Definition at line 258 of file mimify.py.

References mime_encode().

259 def mime_encode_header(line):
260  """Code a single header line as quoted-printable."""
261  newline = ''
262  pos = 0
263  while 1:
264  res = mime_header.search(line, pos)
265  if res is None:
266  break
267  newline = '%s%s%s=?%s?Q?%s?=' % \
268  (newline, line[pos:res.start(0)], res.group(1),
269  CHARSET, mime_encode(res.group(2), 1))
270  pos = res.end(0)
271  return newline + line[pos:]
def mimify.mimify (   infile,
  outfile 
)
Convert 8bit parts of a MIME mail message to quoted-printable.

Definition at line 411 of file mimify.py.

References mimify_part(), and aifc.open().

412 def mimify(infile, outfile):
413  """Convert 8bit parts of a MIME mail message to quoted-printable."""
414  if type(infile) == type(''):
415  ifile = open(infile)
416  if type(outfile) == type('') and infile == outfile:
417  import os
418  d, f = os.path.split(infile)
419  os.rename(infile, os.path.join(d, ',' + f))
420  else:
421  ifile = infile
422  if type(outfile) == type(''):
423  ofile = open(outfile, 'w')
424  else:
425  ofile = outfile
426  nifile = File(ifile, None)
427  mimify_part(nifile, ofile, 0)
428  ofile.flush()
def mimify.mimify_part (   ifile,
  ofile,
  is_mime 
)
Convert an 8bit part of a MIME mail message to quoted-printable.

Definition at line 276 of file mimify.py.

References string.lower(), mime_encode(), and mime_encode_header().

277 def mimify_part(ifile, ofile, is_mime):
278  """Convert an 8bit part of a MIME mail message to quoted-printable."""
279  has_cte = is_qp = is_base64 = 0
280  multipart = None
281  must_quote_body = must_quote_header = has_iso_chars = 0
282 
283  header = []
284  header_end = ''
285  message = []
286  message_end = ''
287  # read header
288  hfile = HeaderFile(ifile)
289  while 1:
290  line = hfile.readline()
291  if not line:
292  break
293  if not must_quote_header and iso_char.search(line):
294  must_quote_header = 1
295  if mv.match(line):
296  is_mime = 1
297  if cte.match(line):
298  has_cte = 1
299  if qp.match(line):
300  is_qp = 1
301  elif base64_re.match(line):
302  is_base64 = 1
303  mp_res = mp.match(line)
304  if mp_res:
305  multipart = '--' + mp_res.group(1)
306  if he.match(line):
307  header_end = line
308  break
309  header.append(line)
310 
311  # read body
312  while 1:
313  line = ifile.readline()
314  if not line:
315  break
316  if multipart:
317  if line == multipart + '--\n':
318  message_end = line
319  break
320  if line == multipart + '\n':
321  message_end = line
322  break
323  if is_base64:
324  message.append(line)
325  continue
326  if is_qp:
327  while line[-2:] == '=\n':
328  line = line[:-2]
329  newline = ifile.readline()
330  if newline[:len(QUOTE)] == QUOTE:
331  newline = newline[len(QUOTE):]
332  line = line + newline
333  line = mime_decode(line)
334  message.append(line)
335  if not has_iso_chars:
336  if iso_char.search(line):
337  has_iso_chars = must_quote_body = 1
338  if not must_quote_body:
339  if len(line) > MAXLEN:
340  must_quote_body = 1
341 
342  # convert and output header and body
343  for line in header:
344  if must_quote_header:
345  line = mime_encode_header(line)
346  chrset_res = chrset.match(line)
347  if chrset_res:
348  if has_iso_chars:
349  # change us-ascii into iso-8859-1
350  if chrset_res.group(2).lower() == 'us-ascii':
351  line = '%s%s%s' % (chrset_res.group(1),
352  CHARSET,
353  chrset_res.group(3))
354  else:
355  # change iso-8859-* into us-ascii
356  line = '%sus-ascii%s' % chrset_res.group(1, 3)
357  if has_cte and cte.match(line):
358  line = 'Content-Transfer-Encoding: '
359  if is_base64:
360  line = line + 'base64\n'
361  elif must_quote_body:
362  line = line + 'quoted-printable\n'
363  else:
364  line = line + '7bit\n'
365  ofile.write(line)
366  if (must_quote_header or must_quote_body) and not is_mime:
367  ofile.write('Mime-Version: 1.0\n')
368  ofile.write('Content-Type: text/plain; ')
369  if has_iso_chars:
370  ofile.write('charset="%s"\n' % CHARSET)
371  else:
372  ofile.write('charset="us-ascii"\n')
373  if must_quote_body and not has_cte:
374  ofile.write('Content-Transfer-Encoding: quoted-printable\n')
375  ofile.write(header_end)
376 
377  for line in message:
378  if must_quote_body:
379  line = mime_encode(line, 0)
380  ofile.write(line)
381  ofile.write(message_end)
382 
383  line = message_end
384  while multipart:
385  if line == multipart + '--\n':
386  # read bit after the end of the last part
387  while 1:
388  line = ifile.readline()
389  if not line:
390  return
391  if must_quote_body:
392  line = mime_encode(line, 0)
393  ofile.write(line)
394  if line == multipart + '\n':
395  nifile = File(ifile, multipart)
396  mimify_part(nifile, ofile, 1)
397  line = nifile.peek
398  if not line:
399  # premature end of file
400  break
401  ofile.write(line)
402  continue
403  # unexpectedly no multipart separator--copy rest of file
404  while 1:
405  line = ifile.readline()
406  if not line:
407  return
408  if must_quote_body:
409  line = mime_encode(line, 0)
410  ofile.write(line)
def mimify.unmimify (   infile,
  outfile,
  decode_base64 = 0 
)
Convert quoted-printable parts of a MIME mail message to 8bit.

Definition at line 203 of file mimify.py.

References aifc.open().

204 def unmimify(infile, outfile, decode_base64 = 0):
205  """Convert quoted-printable parts of a MIME mail message to 8bit."""
206  if type(infile) == type(''):
207  ifile = open(infile)
208  if type(outfile) == type('') and infile == outfile:
209  import os
210  d, f = os.path.split(infile)
211  os.rename(infile, os.path.join(d, ',' + f))
212  else:
213  ifile = infile
214  if type(outfile) == type(''):
215  ofile = open(outfile, 'w')
216  else:
217  ofile = outfile
218  nifile = File(ifile, None)
219  unmimify_part(nifile, ofile, decode_base64)
220  ofile.flush()

Variable Documentation

list __all__ = ["mimify","unmimify","mime_encode_header","mime_decode_header"]

Definition at line 32 of file mimify.py.

tuple base64_re = re.compile('^content-transfer-encoding:\\s*base64', re.I)

Definition at line 35 of file mimify.py.

string CHARSET = 'ISO-8859-1'

Definition at line 26 of file mimify.py.

tuple cte = re.compile('^content-transfer-encoding:', re.I)

Definition at line 273 of file mimify.py.

int decode_base64 = 0

Definition at line 434 of file mimify.py.

encode = mimify

Definition at line 445 of file mimify.py.

tuple encode_args = (sys.stdin, sys.stdout)

Definition at line 457 of file mimify.py.

int is_repl = 1

Definition at line 150 of file mimify.py.

tuple iso_char = re.compile('[\177-\377]')

Definition at line 274 of file mimify.py.

tuple line = ifile.readline()

Definition at line 161 of file mimify.py.

tuple MAXLEN = 200

Definition at line 25 of file mimify.py.

tuple mime_char = re.compile('[=\177-\377]')

Definition at line 221 of file mimify.py.

tuple mime_header = re.compile('([ \t(]|^)([-a-zA-Z0-9_+]*[\177-\377][-a-zA-Z0-9_+\177-\377]*)(?=[ \t)]|\n)')

Definition at line 256 of file mimify.py.

tuple mime_header_char = re.compile('[=?\177-\377]')

Definition at line 222 of file mimify.py.

tuple mp = re.compile('^content-type:.*multipart/.*boundary="?([^;"\n]*)

Definition at line 36 of file mimify.py.

tuple mp_res = mp.match(line)

Definition at line 151 of file mimify.py.

multipart = '--'

if is_repl and len(line) >= 4 and line[:4] == QUOTE+'–' and line[-3:] != '–
': multipart = line[:-1]

Definition at line 153 of file mimify.py.

tuple mv = re.compile('^mime-version:', re.I)

Definition at line 272 of file mimify.py.

list newline = ifile.readline()

Definition at line 192 of file mimify.py.

tuple nifile = File(ifile, multipart)

Definition at line 180 of file mimify.py.

string pref = prefix

Definition at line 167 of file mimify.py.

tuple qp = re.compile('^content-transfer-encoding:\\s*quoted-printable', re.I)

Definition at line 34 of file mimify.py.

string QUOTE = '> '

Definition at line 27 of file mimify.py.

string usage = 'Usage: mimify [-l len] -[ed] [infile [outfile]]'

Definition at line 432 of file mimify.py.