Exemple #1
0
    def decode(self):
		if self.encoding in ['7bit', '8bit']:
			return self.body
		self.rewindbody()
		out = StringIO()
		mimetools.decode(StringIO(self.body), out, self.encoding)
		return out.getvalue() # XXX look if there an error occurs
Exemple #2
0
def process_mime_body(current, file, submsg):
    data = StringIO.StringIO()
    try:
        mimetools.decode(file, data, submsg.getencoding())
        current['description'] = data.getvalue()
    except:
        return
    def decode_content(message):
        """Decode the content of a message. This method do not checks if the message is
        multipart or if the message mime type is plain text or html.

        Parameters
        ----------
        message: email.message.Message
            Message to decode.

        Returns
        -------
        content: str
            Decoded content of the message

        Raises
        ------
        TypeError
            If the parameter is not an instance of :class:`email.message.Message`.
        """
        if not isinstance(message, email.message.Message):
            raise TypeError("Expected a message object.")
        encoding = message['Content-Transfer-Encoding']
        if encoding and encoding.strip() == 'quoted-printable':
            result = message.get_payload()
            stream = cStringIO.StringIO(result)
            output = cStringIO.StringIO()
            mimetools.decode(stream, output, 'quoted-printable')
            return output.getvalue()
        return message.get_payload(decode=True)
Exemple #4
0
    def __init__(self, ct, f, next=None, uribase='thismessage:/',
    seekable=0, **kw):
        # Get the boundary.  It's too bad I have to write this myself,
        # but no way am I going to import cgi for 10 lines of code!
        for param in ct.split(';'):
            a = param.strip()
            if a.startswith('boundary='):
                if a[9] in [ '"', "'" ]:
                    boundary = a[10:-1]
                else:
                    boundary = a[9:]
                break
        else:
            raise ValueError('boundary parameter not found')

        self.id_dict, self.loc_dict, self.parts = {}, {}, []
        self.next = next
        self.base = uribase

        mf = multifile.MultiFile(f, seekable)
        mf.push(boundary)
        while next(mf):
            head = mimetools.Message(mf)
            body = StringIO.StringIO()
            mimetools.decode(mf, body, head.getencoding())
            body.seek(0)
            part = (head, body)
            self.parts.append(part)
            key = head.get('content-id')
            if key:
                if key[0] == '<' and key[-1] == '>': key = key[1:-1]
                self.id_dict[key] = part
            key = head.get('content-location')
            if key: self.loc_dict[key] = part
        mf.pop()
Exemple #5
0
    def __init__(self, ct, f, next=None, uribase='thismessage:/',
    seekable=0, **kw):
        # Get the boundary.  It's too bad I have to write this myself,
        # but no way am I going to import cgi for 10 lines of code!
        for param in ct.split(';'):
            a = param.strip()
            if a.startswith('boundary='):
                if a[9] in [ '"', "'" ]:
                    boundary = a[10:-1]
                else:
                    boundary = a[9:]
                break
        else:
            raise ValueError('boundary parameter not found')

        self.id_dict, self.loc_dict, self.parts = {}, {}, []
        self.next = next
        self.base = uribase

        mf = multifile.MultiFile(f, seekable)
        mf.push(boundary)
        while next(mf):
            head = mimetools.Message(mf)
            body = StringIO.StringIO()
            mimetools.decode(mf, body, head.getencoding())
            body.seek(0)
            part = (head, body)
            self.parts.append(part)
            key = head.get('content-id')
            if key:
                if key[0] == '<' and key[-1] == '>': key = key[1:-1]
                self.id_dict[key] = part
            key = head.get('content-location')
            if key: self.loc_dict[key] = part
        mf.pop()
Exemple #6
0
    def msgRetrieve(self, message, part="RFC822", encoding=None):

        """Retrieve a full message or a message part for this UID

        You can get a message part if you specify the part id
        by default it returns the whole RFC822 part of it
        Although theoretically it could fetch multiple parts, imaplib
        massacrates the result so it's difficult to parse.
        Specify encoding type to get the data back decoded
        """

        # Convert part number to IMAP-style "BODY[part]"
        if '0' < part[0] <= '9':
            part = "BODY[%s]" % part

        result = self.imap.uid("FETCH", message, "(" + part + ")")
        result = self._checkStatus(result)

        if not result[0]:
            return None

        data = result[0][1]  # imaplib split the returned literal
        # see if we need to decode it.
        if encoding in ('base64', 'quoted-printable', 'uuencode'):
            output = cStringIO.StringIO()
            input = cStringIO.StringIO(data)
            mimetools.decode(input, output, encoding)
            input.close()
            data = output.getvalue()
            output.close()

        return data
Exemple #7
0
    def _make_tempfile(self):
        transfer_encoding = self.headers.get('content-transfer-encoding',
            '').lower()
        tf = NamedTemporaryFile()
        start_pos = self._pos + self._headers_length + 2
        file_length = (self._endpos - 2) - start_pos
        bytes_read = 0

        self._data.seek(start_pos)

        while bytes_read < file_length:
            remaining_bytes = (self._endpos - 2) - self._data.tell()
            chunk_size = min(8196, remaining_bytes)
            tf.write(self._data.read(chunk_size))
            bytes_read += chunk_size

        tf.seek(0)

        if transfer_encoding not in ('', '7bit', '8bit', 'binary'):
            decoded_tf = NamedTemporaryFile()
            mimetools.decode(tf, decoded_tf, transfer_encoding)
            try:
                return codecs.getreader(self.charset)(decoded_tf)
            except (TypeError, LookupError):
                return decoded_tf
        else:
            try:
                return codecs.getreader(self.charset)(tf)
            except (TypeError, LookupError):
                return tf
Exemple #8
0
def main():
    fname_src = sys.argv[1]
    fname_dst = sys.argv[2]

    with open(fname_src) as src:
        with open(fname_dst, 'w') as dst:
            mimetools.decode(src, dst, 'base64')
Exemple #9
0
    def decode_content(message):
        """Decode the content of a message. This method do not checks if the message is
        multipart or if the message mime type is plain text or html.

        Parameters
        ----------
        message: email.message.Message
            Message to decode.

        Returns
        -------
        content: str
            Decoded content of the message

        Raises
        ------
        TypeError
            If the parameter is not an instance of :class:`email.message.Message`.
        """
        if not isinstance(message, email.message.Message):
            raise TypeError("Expected a message object.")
        encoding = message['Content-Transfer-Encoding']
        if encoding and encoding.strip() == 'quoted-printable':
            result = message.get_payload()
            stream = cStringIO.StringIO(result)
            output = cStringIO.StringIO()
            mimetools.decode(stream, output, 'quoted-printable')
            return output.getvalue()
        return message.get_payload(decode=True)
Exemple #10
0
	def getbodytext(self, decode = 1):
		self.fp.seek(self.startofbody)
		encoding = self.getencoding()
		if not decode or encoding in ('7bit', '8bit', 'binary'):
			return self.fp.read()
		from StringIO import StringIO
		output = StringIO()
		mimetools.decode(self.fp, output, encoding)
		return output.getvalue()
Exemple #11
0
	def getbodytext(self, decode = 1):
		self.fp.seek(self.startofbody)
		encoding = self.getencoding()
		if not decode or encoding in ('7bit', '8bit', 'binary'):
			return self.fp.read()
		from StringIO import StringIO
		output = StringIO()
		mimetools.decode(self.fp, output, encoding)
		return output.getvalue()
Exemple #12
0
def Opaque(uri, tc, ps, **keywords):
    '''Resolve a URI and return its content as a string.
    '''
    source = urllib.request.urlopen(uri, **keywords)
    enc = source.info().getencoding()
    if enc in ['7bit', '8bit', 'binary']: return source.read()

    data = StringIO.StringIO()
    mimetools.decode(source, data, enc)
    return data.getvalue()
Exemple #13
0
def Opaque(uri, tc, ps, **keywords):
    '''Resolve a URI and return its content as a string.
    '''
    source = urllib.urlopen(uri, **keywords)
    enc = source.info().getencoding()
    if enc in ['7bit', '8bit', 'binary']: return source.read()

    data = StringIO.StringIO()
    mimetools.decode(source, data, enc)
    return data.getvalue()
 def test_decodeencode(self):
     start = string.ascii_letters + "=" + string.digits + "\n"
     for enc in ["7bit", "8bit", "base64", "quoted-printable", "uuencode", "x-uuencode", "uue", "x-uue"]:
         i = StringIO.StringIO(start)
         o = StringIO.StringIO()
         mimetools.encode(i, o, enc)
         i = StringIO.StringIO(o.getvalue())
         o = StringIO.StringIO()
         mimetools.decode(i, o, enc)
         self.assertEqual(o.getvalue(), start)
Exemple #15
0
def Opaque(uri, tc, ps, **keywords):
    """Resolve a URI and return its content as a string.
    """
    source = urllib.urlopen(uri, **keywords)
    enc = source.info().getencoding()
    if enc in ["7bit", "8bit", "binary"]:
        return source.read()

    data = StringIO.StringIO()
    mimetools.decode(source, data, enc)
    return data.getvalue()
Exemple #16
0
 def test_decodeencode(self):
     start = string.ascii_letters + "=" + string.digits + "\n"
     for enc in ['7bit','8bit','base64','quoted-printable',
                 'uuencode', 'x-uuencode', 'uue', 'x-uue']:
         i = StringIO.StringIO(start)
         o = StringIO.StringIO()
         mimetools.encode(i, o, enc)
         i = StringIO.StringIO(o.getvalue())
         o = StringIO.StringIO()
         mimetools.decode(i, o, enc)
         self.assertEqual(o.getvalue(), start)
 def getbodytext(self, decode = 1):
     """Return the message's body text as string.  This undoes a
     Content-Transfer-Encoding, but does not interpret other MIME
     features (e.g. multipart messages).  To suppress decoding,
     pass 0 as an argument."""
     self.fp.seek(self.startofbody)
     encoding = self.getencoding()
     if not decode or encoding in ('', '7bit', '8bit', 'binary'):
         return self.fp.read()
     from StringIO import StringIO
     output = StringIO()
     mimetools.decode(self.fp, output, encoding)
     return output.getvalue()
Exemple #18
0
def XML(uri, tc, ps, **keywords):
    '''Resolve a URI and return its content as an XML DOM.
    '''
    source = urllib.urlopen(uri, **keywords)
    enc = source.info().getencoding()
    if enc in ['7bit', '8bit', 'binary']:
        data = source
    else:
        data = StringIO.StringIO()
        mimetools.decode(source, data, enc)
        data.seek(0)
    dom = ps.readerclass().fromStream(data)
    return _child_elements(dom)[0]
Exemple #19
0
def XML(uri, tc, ps, **keywords):
    '''Resolve a URI and return its content as an XML DOM.
    '''
    source = urllib.request.urlopen(uri, **keywords)
    enc = source.info().getencoding()
    if enc in ['7bit', '8bit', 'binary']:
        data = source
    else:
        data = StringIO.StringIO()
        mimetools.decode(source, data, enc)
        data.seek(0)
    dom = ps.readerclass().fromStream(data)
    return _child_elements(dom)[0]
def extract_mime_part_matching(stream, mimetype):
	mfile = multifile.MultiFile(stream)
	mfile.push("_BOUNDRY_02468_STRING_13579_XXXXXXX")
	
	while 1:	
		submsg = mimetools.Message(mfile)
		data = StringIO.StringIO()
		mimetools.decode(mfile, data, submsg.getencoding())

		if (not mfile.next()) or submsg.gettype() == mimetype : break

	mfile.pop()

	return data.getvalue()
def extract_mime_part_matching(stream, mimetype):
    mfile = multifile.MultiFile(stream)
    mfile.push("_BOUNDRY_02468_STRING_13579_XXXXXXX")

    while 1:
        submsg = mimetools.Message(mfile)
        data = StringIO.StringIO()
        mimetools.decode(mfile, data, submsg.getencoding())

        if (not mfile.next()) or submsg.gettype() == mimetype: break

    mfile.pop()

    return data.getvalue()
Exemple #22
0
    def __init__(self, headers, infile, maintype = None, subtype = None):
        Part.__init__(self, headers, infile, maintype, subtype)
        if self.maintype == "multipart": raise ValueError

        encoding = self.headers.getencoding()
        if encoding in ["base64", "quoted-printable"]:
            # Decode using method in mimetools module
            csio = cStringIO.StringIO()
            mimetools.decode(infile, csio, encoding)
            self.data = csio.getvalue()
            csio.close()
        else:
            # Unknown coding or 7bit/8bit/binary: just read it
            self.data = infile.read()

        # If the content type is text, convert CRLF to LF
        # FIXME: does this create problems? Restrict to text/plain?
        if self.maintype == "text":
            self.data = string.replace(self.data, "\r\n", "\n")
Exemple #23
0
	def mimedecode(self, msg=None, id=""):
		if not msg:
			self.rewind()
			msg = mimetools.Message(self, 0)
		type = msg.gettype()
		if (len(id) > 5):
			# Emergency abort!
			return [["(diagnostic)", "text/plain", \
				"Attachments too deeply nested --- aborting (probably hit the Multifile bug)", \
				id+"A"]]

		disposition = msg.getheader("Content-Disposition")
		disposition = sqmail.utils.parse_mimeheader(disposition)
		name = msg.getparam("name")
		index = 65
		if not name:
			name = sqmail.utils.get_mime_param(disposition, "filename")
		if not name:
			name = "<unnamed>"
		if (type[:10] == "multipart/"):
			multi = multifile.MultiFile(msg.fp, 0)
			multi.push(msg.getparam("boundary"))
			l = []
			while multi.next():
				l.append(self.mimedecode(mimetools.Message(multi, 0), id+chr(index))[0])
				index = index + 1
				if (index > 65+32):
					# Emergency abort!
					raise MIMEDecodeAbortException
			multi.pop()
			return [[name, type, l, ""]]
		else:
			encoding = msg.getencoding()
			if (encoding != "7bit") and (encoding != "8bit"):
				data = cStringIO.StringIO()
				mimetools.decode(msg.fp, data, msg.getencoding())
				return [[name, type, data.getvalue(), id]]
			else:
				return [[name, type, string.join(msg.fp.readlines(), ""), id]]
Exemple #24
0
def maybe_add_attachment(current, file, submsg):
    """Adds the attachment to the current record"""
    cd = submsg["Content-Disposition"]
    m = re.search(r'filename="([^"]+)"', cd)
    if m == None:
        return
    attachment_filename = m.group(1)
    if (submsg.gettype() == 'application/octet-stream'):
        # try get a more specific content-type for this attachment
        type, encoding = mimetypes.guess_type(m.group(1))
        if type == None:
            type = submsg.gettype()
    else:
        type = submsg.gettype()

    try:
        data = StringIO.StringIO()
        mimetools.decode(file, data, submsg.getencoding())
    except:
        return

    current['attachments'].append( ( attachment_filename, type, data.getvalue() ) )
Exemple #25
0
    def _extractMimeParts(stream):
        msg = mimetools.Message(stream)
        msgtype = msg.gettype()
        params = msg.getplist()

        files = []

        raw_data = cStringIO.StringIO()
        if msgtype[:10] == "multipart/":
            f = multifile.MultiFile(stream)
            f.push(msg.getparam("boundary"))
            while f.next():
                submsg = mimetools.Message(f)
                filename = submsg.getheader(MIME_FILE_HEADER)
                content_hash = submsg.getheader(MIME_HASH_HEADER)
                try:
                    raw_data = cStringIO.StringIO()
                    mimetools.decode(f, raw_data, submsg.getencoding())
                except ValueError:
                    continue
                files.append((filename, content_hash, raw_data.getvalue()))
            f.pop()
        return files
Exemple #26
0
    def _extractMimeParts(stream):
        msg = mimetools.Message(stream)
        msgtype = msg.gettype()
        params = msg.getplist()

        files = []

        raw_data = cStringIO.StringIO()
        if msgtype[:10] == "multipart/":
            f = multifile.MultiFile(stream)
            f.push(msg.getparam("boundary"))
            while f.next():
                submsg = mimetools.Message(f)
                filename = submsg.getheader(MIME_FILE_HEADER)
                content_hash = submsg.getheader(MIME_HASH_HEADER)
                try:
                    raw_data = cStringIO.StringIO()
                    mimetools.decode(f, raw_data, submsg.getencoding())
                except ValueError:
                    continue
                files.append((filename, content_hash, raw_data.getvalue()))
            f.pop()
        return files
Exemple #27
0
def process(stream, i):

    # vado all'inizio del file
    stream.seek(0)

    # creo un'istanza di msg di posta elettronica
    msg = mimetools.Message(stream)
    # tipo di messaggio
    msgtype = msg.gettype()
    # codifica del messaggio
    encoding = msg.getencoding()

    # ricavo il mittente e la data
    sender = msg.get("From")
    date = parsedate(msg.get("Date"))

    # se il messaggio e' un multipart lo devo processare in maniera
    # un po' particolare
    if msgtype[:10] == "multipart/":

        # creo un istanza multifile
        file = multifile.MultiFile(stream)
        # ricavo il boundary
        file.push(msg.getparam("boundary"))
        # scorro i vari pezzi del file
        while file.next():
            # ricavo il sottomessaggio
            submsg = mimetools.Message(file)
            try:
                # provo a decodificare il sottomessaggio
                data = StringIO.StringIO()
                mimetools.decode(file, data, submsg.getencoding())
            except ValueError:
                # in caso di errore faccio finta di niente
                print "### ERRORI DI FORMATO NEL MSG %s (SENDER: %s)" % (
                    i, sender)
                continue
        # vado al file successivo
        file.pop()

    # se il messaggio e' singolo tento
    # di decodificare il singolo messaggio
    else:
        try:
            data = StringIO.StringIO()
            mimetools.decode(msg.fp, data, msg.getencoding())
        except ValueError:
            print "### CAZZI NEL DECODING MIME DEL MSG %s (SENDER: %s)" % (
                i, sender)

    # ecco che ho i dati del form depurati dalla codifica del client di posta elettronica
    form_data = data.getvalue()
    # rimuovo i ritorni a capo
    form_data = string.replace(form_data, "\n", "")

    # inserisco i dati nel database
    insertintodb(sender, date, form_data)

    # incremento il contatore
    i = i + 1

    # torno il valore del contatore
    return i
#!/usr/bin/env python
#################################################
# Decode mail attachments sent in base64 form.
# This version tests the mimetools module.     
#################################################

import sys, mimetools

iname = 'part.txt'
oname = 'part.doc'

if len(sys.argv) > 1:
    iname, oname = sys.argv[1:]      # % python prog [iname oname]?

input  = open(iname, 'r')
output = open(oname, 'wb')
mimetools.decode(input, output, 'base64')     # or 'uuencode', etc.
print 'done'
Exemple #29
0
from test_support import TestFailed
import mimetools
import string,StringIO
start = string.ascii_letters + "=" + string.digits + "\n"
for enc in ['7bit','8bit','base64','quoted-printable']:
    print enc,
    i = StringIO.StringIO(start)
    o = StringIO.StringIO()
    mimetools.encode(i,o,enc)
    i = StringIO.StringIO(o.getvalue())
    o = StringIO.StringIO()
    mimetools.decode(i,o,enc)
    if o.getvalue()==start:
        print "PASS"
    else:
        print "FAIL"
        print o.getvalue()
Exemple #30
0
    tp=part.get_content_type()
    if tp=='application/octet-stream':
        mimepart=mt.Message(StringIO.StringIO(part.as_string()))
print(mimepart.getencoding())
for part in msg.walk():
    tp=part.get_content_type()
    if tp=='application/octet-stream':
        mimepart=mt.Message(StringIO.StringIO(part.as_string()))
        print(mimepart.getencoding())
        

for part in msg.walk():
    tp=part.get_content_type()
    if tp=='application/octet-stream':
        mimepart=mt.Message(StringIO.StringIO(part.as_string()))
        mimeString=StringIO.StringIO(); mt.decode(mimepart, mimeString, mimepart.getencoding())
        

for part in msg.walk():
    tp=part.get_content_type()
    if tp=='application/octet-stream':
        mimepart=mt.Message(StringIO.StringIO(part.as_string()))
        mimeString=StringIO.StringIO(); mt.decode(mimepart.fp, mimeString, mimepart.getencoding())
        

for part in msg.walk():
    tp=part.get_content_type()
    if tp=='application/octet-stream':
        mimepart=mt.Message(StringIO.StringIO(part.as_string()))
        mimeString=StringIO.StringIO(); mt.decode(mimepart.fp, mimeString, mimepart.getencoding())
        mimeString.getvalue()
Exemple #31
0
from test_support import TestFailed
import mimetools

import string, StringIO
start = string.ascii_letters + "=" + string.digits + "\n"
for enc in ['7bit', '8bit', 'base64', 'quoted-printable']:
    print enc,
    i = StringIO.StringIO(start)
    o = StringIO.StringIO()
    mimetools.encode(i, o, enc)
    i = StringIO.StringIO(o.getvalue())
    o = StringIO.StringIO()
    mimetools.decode(i, o, enc)
    if o.getvalue() == start:
        print "PASS"
    else:
        print "FAIL"
        print o.getvalue()
Exemple #32
0
from test_support import TestFailed
import mimetools
import string, StringIO
start = string.ascii_letters + "=" + string.digits + "\n"
for enc in ['7bit', '8bit', 'base64', 'quoted-printable']:
    print enc,
    i = StringIO.StringIO(start)
    o = StringIO.StringIO()
    mimetools.encode(i, o, enc)
    i = StringIO.StringIO(o.getvalue())
    o = StringIO.StringIO()
    mimetools.decode(i, o, enc)
    if o.getvalue() == start:
        print "PASS"
    else:
        print "FAIL"
        print o.getvalue()

Exemple #33
0
retrieve_file('http://www.pythonchallenge.com/pc/hex/bin.html',fileName)

#file = open(fileName)


text = open(fileName).readlines()
mail = ''.join(text[14:1987])
att = ''.join(text[28:1986])

headers = Parser().parsestr(mail)

#print headers['Subject']
#print 'Content-type:',headers['Content-type']

audiodata = base64.b64decode(att)

input = open('data/indian.dat','wb')
input.write(att)
input.close()

input = open('data/indian.dat','rb')
output = open('data/indian.wav','wb')

#print dir(email.mime.Audio)
#aux = email.mime.audio.MIMEAudio(audiodata)

#headers = email.parser.Parser().parse(file)

exit = ''
mimetools.decode(input, output, 'base64')
def unpackMultifile(multifile, attachments=None):
    """ Unpack multifile into plainbody, content-type, htmlbody and attachments.
    """
    if attachments is None:
        attachments=[]
    textBody = htmlBody = contentType = ''

    msg = mimetools.Message(multifile)
    maintype = msg.getmaintype()
    subtype = msg.getsubtype()

    name = msg.getparam('name')

    if not name:
        # Check for disposition header (RFC:1806)
        disposition = msg.getheader('Content-Disposition')
        if disposition:
            matchObj = re.search('(?i)filename="*(?P<filename>[^\s"]*)"*',
                                   disposition)
            if matchObj:
                name = matchObj.group('filename')

    # Recurse over all nested multiparts
    if maintype == 'multipart':
        multifile.push(msg.getparam('boundary'))
        multifile.readlines()
        while not multifile.last:
            multifile.next()

            (tmpTextBody, tmpContentType, tmpHtmlBody, tmpAttachments) = \
                                       unpackMultifile(multifile, attachments)

            # Return ContentType only for the plain-body of a mail
            if tmpContentType and not textBody:
                textBody = tmpTextBody
                contentType = tmpContentType

            if tmpHtmlBody:
                htmlBody = tmpHtmlBody
        
            if tmpAttachments:
                attachments = tmpAttachments

        multifile.pop()
        return (textBody, contentType, htmlBody, attachments)

    # Process MIME-encoded data
    plainfile = StringIO.StringIO()

    try:
        mimetools.decode(multifile,plainfile,msg.getencoding())
    # unknown or no encoding? 7bit, 8bit or whatever... copy literal
    except ValueError:
        mimetools.copyliteral(multifile,plainfile)

    body = plainfile.getvalue()
    plainfile.close()

    # Get plain text
    if maintype == 'text' and subtype == 'plain' and not name:
        textBody = body
        contentType = msg.get('content-type', 'text/plain')
    else:
        # No name? This should be the html-body...
        if not name:
            name = '%s.%s' % (maintype,subtype)
            htmlBody = body
        
        attachments.append({'filename' : mime_decode_header(name), 
                            'filebody' : body,
                            'maintype' : maintype,
                            'subtype' : subtype})
            
    return (textBody, contentType, htmlBody, attachments)
Exemple #35
0
f = open("indian.txt", 'rb')

print "Writing initial indian.wav from email text file..."

input = ''.join([x.rstrip("\n") for x in f])
#input = [[x for x in reversed(input[i:i+8])] for i in range(0, len(input), 8)]
#input = sum(input, [])
#input = ''.join(input)

inputFile = open("indian.in", 'wb')
inputFile.write(input)
inputFile.close()

out = open("indian.wav", 'wb')

mt.decode(open("indian.in", 'rb'), out, 'base64')

print "Byteswapping the audio of indian.wav...."

a = array.array('i')
w = wave.open("indian.wav", 'rb')
wout = wave.open("final_indian.wav", 'wb')
a.fromstring(w.readframes(w.getnframes()))
a.byteswap()

wout.setparams(w.getparams())
wout.writeframes(a.tostring())
w.close()
wout.close()

print "Wrote byteswapped file to final_indian.wav"
Exemple #36
0
while mfile.next():
    submsg = mimetools.Message(mfile)
    if submsg.gettype() != "image/jpeg":
	continue
    now = time.localtime(time.time())
    fileNum = 1
    while True:
	format = "%%Y-%%m-%%d-%%H:%%M-%d.jpg" % fileNum
	jpegFileName = time.strftime(format,
				     # Backward compatible
				     now)
	if os.path.exists(jpegFileName) == False:
	    break
	fileNum = fileNum + 1
    jpegFile = file(jpegFileName, "w")
    mimetools.decode(mfile, jpegFile, submsg.getencoding())
    jpegFile.close()
mfile.pop()

# We have now created the file jpegFileName, time to add it to the
# index.html file
html = file("index.html", "r")
newhtml = file("index-new.html", "w")
asctime = time.asctime(now)
while True:
    line = html.readline()
    if len(line) == 0:
	break
    if line[0:13] == "<!-- MARK -->":
	newhtml.write("<!-- MARK --><img src=\"%s\"><br>\n" % jpegFileName)
	newhtml.write("<a href=\"%s\">%s</a><br>\n" % (jpegFileName, asctime))
#!/usr/bin/env python
#################################################
# Decode mail attachments sent in base64 form.
# This version tests the mimetools module.
#################################################

import sys, mimetools

iname = 'part.txt'
oname = 'part.doc'

if len(sys.argv) > 1:
    iname, oname = sys.argv[1:]  # % python prog [iname oname]?

input = open(iname, 'r')
output = open(oname, 'wb')
mimetools.decode(input, output, 'base64')  # or 'uuencode', etc.
print 'done'
Exemple #38
0
def process_mime_body(current, file, submsg):
    data = StringIO.StringIO()
    mimetools.decode(file, data, submsg.getencoding())
    current["description"] = data.getvalue()
Exemple #39
0
def unpackMultifile(multifile, attachments=None):
    """ Unpack multifile into plainbody, content-type, htmlbody and
    attachments.
    """
    if attachments is None:
        attachments = []
    textBody = htmlBody = contentType = ''

    msg = mimetools.Message(multifile)
    maintype = msg.getmaintype()
    subtype = msg.getsubtype()

    name = msg.getparam('name')

    if not name:
        # Check for disposition header (RFC:1806)
        disposition = msg.getheader('Content-Disposition')
        if disposition:
            matchObj = re.search(r'(?i)filename="*(?P<filename>[^\s"]*)"*',
                                 disposition)
            if matchObj:
                name = matchObj.group('filename')

    # Recurse over all nested multiparts
    if maintype == 'multipart':
        multifile.push(msg.getparam('boundary'))
        multifile.readlines()
        while not multifile.last:
            multifile.next()

            (tmpTextBody, tmpContentType, tmpHtmlBody, tmpAttachments) = \
                unpackMultifile(multifile, attachments)

            # Return ContentType only for the plain-body of a mail
            if tmpContentType and not textBody:
                textBody = tmpTextBody
                contentType = tmpContentType

            if tmpHtmlBody:
                htmlBody = tmpHtmlBody

            if tmpAttachments:
                attachments = tmpAttachments

        multifile.pop()
        return (textBody, contentType, htmlBody, attachments)

    # Process MIME-encoded data
    plainfile = StringIO.StringIO()

    try:
        mimetools.decode(multifile, plainfile, msg.getencoding())
    # unknown or no encoding? 7bit, 8bit or whatever... copy literal
    except ValueError:
        mimetools.copyliteral(multifile, plainfile)

    body = plainfile.getvalue()
    plainfile.close()

    # Get plain text
    if maintype == 'text' and subtype == 'plain' and not name:
        textBody = body
        contentType = msg.get('content-type', 'text/plain')
    else:
        # No name? This should be the html-body...
        if not name:
            name = '%s.%s' % (maintype, subtype)
            htmlBody = body

        attachments.append({
            'filename': mime_decode_header(name),
            'filebody': body,
            'maintype': maintype,
            'subtype': subtype
        })

    return (textBody, contentType, htmlBody, attachments)
from test_support import TestFailed
import mimetools

import string,StringIO
start = string.ascii_letters + "=" + string.digits + "\n"
for enc in ['7bit','8bit','base64','quoted-printable']:
    print enc,
    i = StringIO.StringIO(start)
    o = StringIO.StringIO()
    mimetools.encode(i,o,enc)
    i = StringIO.StringIO(o.getvalue())
    o = StringIO.StringIO()
    mimetools.decode(i,o,enc)
    if o.getvalue()==start:
        print "PASS"
    else:
        print "FAIL"
        print o.getvalue()
Exemple #41
0
"""MH interface -- purely object-oriented (well, almost)