예제 #1
0
파일: email_message.py 프로젝트: jeske/csla
 def decode_part (self, m, as_utf8 = 0):
   ct = m.getmaintype("text")
   if ct != "text": as_utf8 = 0
   body = m.get_payload()
   if not isinstance(body, StringType):
     body = str(body)
   encoding = string.lower(m.get('content-transfer-encoding', '7bit'))
   if encoding == "quoted-printable":
     from quopri import decode
     ## how annoying
     inb = StringIO(body)
     outb = StringIO()
     decode(inb, outb)
     body = outb.getvalue()
   elif encoding == "base64":
     from base64 import decodestring
     try:
       body = decodestring(body)
     except:
       pass
   if ct == "text":
     # don't allow embedded nulls in text parts
     body = string.replace(body, '\0', '')
   if as_utf8: return to_utf8(body, self.charset(m))
   return body
예제 #2
0
def quopri_decode(input, errors = 'strict'):
    assert errors == 'strict'
    f = StringIO(str(input))
    g = StringIO()
    quopri.decode(f, g)
    output = g.getvalue()
    return (output, len(input))
예제 #3
0
 def test_decode(self):
     for p, e in self.STRINGS:
         infp = cStringIO.StringIO(e)
         outfp = cStringIO.StringIO()
         if not test_support.due_to_ironpython_bug("http://tkbgitvstfat01:8080/WorkItemTracking/WorkItem.aspx?artifactMoniker=317834"):
             quopri.decode(infp, outfp) #binascii.a2b_qp() not implemented which is needed in function decode() 
             self.assertTrue(outfp.getvalue() == p)
예제 #4
0
파일: unpack.py 프로젝트: donnut/software
 def mime_unpack(self, input, stack):
     entity = mimetools.Message(input)
     stack = (entity,) + stack
     if entity.getmaintype() == 'multipart':
         self.parts.append(MessagePart(None, None, stack))
         boundary = entity.getparam('boundary')
         if boundary:
             input.push(boundary)
             while input.next():
                 self.mime_unpack(input, stack)
             input.pop()
     elif entity.gettype() == 'message/rfc822':
         self.parts.append(MessagePart(None, None, stack))
         self.mime_unpack(input, stack)
     else:
         name = get_filename(entity)
         work = tempfile.mktemp()
         cte = entity.getencoding()
         if cte == 'quoted-printable':
             output = cStringIO.StringIO()
             quopri.decode(input, output)
         elif cte == 'base64':
             output = cStringIO.StringIO()
             base64_decode(input, output)
         else:
             output = None
         if output:
             output.reset()
             if entity.gettype() == "application/x-gzip" or \
                    (name and name.endswith(".gz")):
                 work2 = tempfile.mktemp()
                 tmp2 = open(work2+".gz", "w")
                 tmp2.write(output.read())
                 tmp2.close()
                 inst = popen2.Popen4("gunzip %s.gz" % work2)
                 data = inst.fromchild.read()
                 if inst.wait() != 0:
                     self.write_fatal(_("Uncompressing your submission with gzip has failed. gzip said:\n%s\n") % data)
                     try:
                         os.unlink(work2+".gz")
                     except OSError:
                         pass
                     return
                 output=open(work2)
                 os.unlink(work2)
                 if name and name.endswith(".gz"):
                     name=name[:-3]
             tmp = open(work,"w")
             tmp.write(output.read())
             tmp.close()
             del output
             self.simple_unpack(open(work), name, stack)
             os.remove(work)
         else:
             self.simple_unpack(input, name, stack)
예제 #5
0
    def _qdecode(s):
        import quopri as _quopri

        if not s:
            return s
        infp = StringIO(s)
        outfp = StringIO()
        _quopri.decode(infp, outfp)
        value = outfp.getvalue()
        if not s.endswith('\n') and value.endswith('\n'):
            return value[:-1]
        return value
예제 #6
0
def quopri_decode(input, errors='strict'):
    """Decode the input, returning a tuple (output object, length consumed).

    errors defines the error handling to apply. It defaults to
    'strict' handling which is the only currently supported
    error handling for this codec.

    """
    assert errors == 'strict'
    f = StringIO(str(input))
    g = StringIO()
    quopri.decode(f, g)
    output = g.getvalue()
    return (output, len(input))
예제 #7
0
def decode(encoding, s):
  if encoding == '7bit' or encoding == '8bit' or encoding == 'binary':
    return s
  elif encoding == 'quoted-printable':
    import quopri
    ifp = StringIO.StringIO(s)
    ofp = StringIO.StringIO()
    quopri.decode(ifp, ofp)
    return ofp.getvalue()
  elif encoding == 'base64':
    import base64
    return base64.decodestring(s)
  else:
    raise Error("Unknown encoding %s" % repr(encoding))
예제 #8
0
def decode(encoding, s):
  if encoding == '7bit' or encoding == '8bit' or encoding == 'binary':
    return s
  elif encoding == 'quoted-printable':
    import quopri
    ifp = StringIO.StringIO(s)
    ofp = StringIO.StringIO()
    quopri.decode(ifp, ofp)
    return ofp.getvalue()
  elif encoding == 'base64':
    import base64
    return base64.decodestring(s)
  else:
    raise Error("Unknown encoding %s" % repr(encoding))
def quopri_decode(input, errors='strict'):
    """Decode the input, returning a tuple (output object, length consumed).

    errors defines the error handling to apply. It defaults to
    'strict' handling which is the only currently supported
    error handling for this codec.

    """
    assert errors == 'strict'
    f = StringIO(str(input))
    g = StringIO()
    quopri.decode(f, g)
    output = g.getvalue()
    return (output, len(input))
예제 #10
0
	def __init__(self, reader, a):
		sqmail.gui.viewer.Viewer.__init__(self, reader, a, "textmessage")
		font = gtk.load_font(sqmail.preferences.get_textmessagefont())
		# Ensure the text box is 80 columns wide.
		width = gtk.gdk_char_width(font, "m")*82
		# The text box is guaranteed to be empty.
		self.viewer_widget.messagetext.freeze()
		self.viewer_widget.messagetext.set_usize(width, 0)
		if (self.attachment[1] == "text/quoted-printable"):
			infp = cStringIO.StringIO(self.attachment[2])
			outfp = cStringIO.StringIO()
			quopri.decode(infp, outfp)
			body = outfp.getvalue()
		else:
			body = self.attachment[2]
		self.viewer_widget.messagetext.insert(font, None, None, body)
		self.viewer_widget.messagetext.thaw()
예제 #11
0
 def __init__(self, reader, a):
     sqmail.gui.viewer.Viewer.__init__(self, reader, a, "htmlmessage")
     # Glade doesn't do the GtkXmHTML widget yet. So we need to add
     # it manually.
     self.htmlwidget = gnome.xmhtml.GtkXmHTML()
     self.viewer_widget.frame.add(self.htmlwidget)
     self.htmlwidget.show()
     self.htmlwidget.freeze()
     if self.attachment[1] == "text/quoted-printable":
         infp = cStringIO.StringIO(self.attachment[2])
         outfp = cStringIO.StringIO()
         quopri.decode(infp, outfp)
         body = outfp.getvalue()
     else:
         body = self.attachment[2]
     self.htmlwidget.set_allow_images(1)
     self.htmlwidget.source(body)
     self.htmlwidget.thaw()
예제 #12
0
    def _decode(self, headers, fileobj):
        encoding = headers[-1].get_all("content-transfer-encoding", ["7bit"])[0]
        encoding = encoding.lower()

        if encoding == "base64":
            try:
                data = base64.b64decode(fileobj.read())
            except TypeError as error:
                self.log.error("Base64 decoding failed ({0})".format(error))
                idiokit.stop(False)
            return StringIO(data)

        if encoding == "quoted-printable":
            output = StringIO()
            quopri.decode(fileobj, output)
            output.seek(0)
            return output

        return fileobj
예제 #13
0
 def parseFrame(self, frame):
     # Parse a passed-in frame for name-value pairs.
     # Same as from_rfc_822_format in cgi-bin/PubSub/EventFormat.pm .
     message = {}
     while len(frame):
         pos = string.index(frame, "\n")
         header = None
         value = None
         header = frame[:pos]
         frame = frame[pos + 1:]
         if not pos:
             # The rest of the frame is the "kn_payload" value.
             name = "kn_payload"
             value = frame
             frame = ""
         else:
             # Now we've parsed out a header line.  Split into name and value.
             sep = string.index(header, ":")
             nameEscaped = header[:sep]
             valueEscaped = string.lstrip(header[sep + 1:])
             # Decode them.
             nameEscapedStream = cStringIO.StringIO(nameEscaped)
             valueEscapedStream = cStringIO.StringIO(valueEscaped)
             nameStream = cStringIO.StringIO()
             valueStream = cStringIO.StringIO()
             quopri.decode(nameEscapedStream, nameStream)
             quopri.decode(valueEscapedStream, valueStream)
             nameStream.seek(0)
             valueStream.seek(0)
             name = nameStream.read()
             value = valueStream.read()
         # Decode UTF-8.
         nameU = unicode(name, "UTF-8", "replace")
         valueU = unicode(value, "UTF-8", "replace")
         # Add this name-value pair to the message.
         if message.has_key(nameU):
             valueU = message[nameU] + ", " + valueU
         message[nameU] = valueU
         continue
     self.onMessage(message)
예제 #14
0
 def parseFrame(self, frame):
     # Parse a passed-in frame for name-value pairs.
     # Same as from_rfc_822_format in cgi-bin/PubSub/EventFormat.pm .
     message = { }
     while len(frame):
         pos = string.index(frame, "\n")
         header = None
         value = None
         header = frame[ : pos ]
         frame = frame[ pos + 1 : ]
         if not pos:
             # The rest of the frame is the "kn_payload" value.
             name = "kn_payload"
             value = frame
             frame = ""
         else:
             # Now we've parsed out a header line.  Split into name and value.
             sep = string.index(header, ":")
             nameEscaped = header[ : sep ]
             valueEscaped = string.lstrip(header[ sep + 1 : ])
             # Decode them.
             nameEscapedStream = cStringIO.StringIO(nameEscaped)
             valueEscapedStream = cStringIO.StringIO(valueEscaped)
             nameStream = cStringIO.StringIO()
             valueStream = cStringIO.StringIO()
             quopri.decode(nameEscapedStream, nameStream)
             quopri.decode(valueEscapedStream, valueStream)
             nameStream.seek(0)
             valueStream.seek(0)
             name = nameStream.read()
             value = valueStream.read()
         # Decode UTF-8.
         nameU = unicode(name, "UTF-8", "replace")
         valueU = unicode(value, "UTF-8", "replace")
         # Add this name-value pair to the message.
         if message.has_key(nameU):
             valueU = message[nameU] + ", " + valueU
         message[nameU] = valueU
         continue
     self.onMessage(message)
예제 #15
0
    def fetch_file(self, f):
        """Fetch the body part into a file-like object."""
        # XXX This is icky. This means that on multipart messages we will
        # fetch everything but on non-multipart messages we only fetch the
        # first element. I also tried creating a fake multi-part body but
        # that ends up in an even worse abstraction once you hand it out to
        # the json encoding code.
        if (not self['content_type'].startswith('multipart/')
                and self['partnumber'] == ''):
            # RfC 3501: The part number of a single non-multipart message is
            # always 1.
            partnumber = '1'
        else:
            partnumber = self['partnumber']

        encoding = self.get('encoding')
        if encoding in ['base64', 'quoted-printable']:
            # XXX Make StringIO first, swap to temporary file on a size threshold.
            encoded = tempfile.NamedTemporaryFile()
        else:
            encoded = f

        for chunk_no in itertools.count():
            data = _fetch(self.server, self.message.parent, self.message.UID,
                          'BODY[%s]' % partnumber, chunk_no)
            if data == '':
                break
            if data == gocept.imapapi.parser.NIL:
                raise gocept.imapapi.interfaces.BrokenMIMEPart()
            encoded.write(data)

        encoded.seek(0)
        if encoding == 'base64':
            base64.decode(encoded, f)
        elif encoding == 'quoted-printable':
            quopri.decode(encoded, f)
        f.seek(0)
예제 #16
0
def decode(input, output, encoding):
    if encoding == 'base64':
        import base64
        return base64.decode(input, output)
    if encoding == 'quoted-printable':
        import quopri
        return quopri.decode(input, output)
    if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
        import uu
        return uu.decode(input, output)
    if encoding in ('7bit', '8bit'):
        return output.write(input.read())
    else:
        raise ValueError, \
              'unknown Content-Transfer-Encoding: %s' % encoding
예제 #17
0
def decode(input, output, encoding):
    if encoding == 'base64':
        import base64
        return base64.decode(input, output)
    if encoding == 'quoted-printable':
        import quopri
        return quopri.decode(input, output)
    if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
        import uu
        return uu.decode(input, output)
    if decodetab.has_key(encoding):
        pipethrough(input, decodetab[encoding], output)
    else:
        raise ValueError, \
              'unknown Content-Transfer-Encoding: %s' % encoding
예제 #18
0
def decode(input, output, encoding):
	if encoding == 'base64':
		import base64
		return base64.decode(input, output)
	if encoding == 'quoted-printable':
		import quopri
		return quopri.decode(input, output)
	if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
		import uu
		return uu.decode(input, output)
	if decodetab.has_key(encoding):
		pipethrough(input, decodetab[encoding], output)
	else:
		raise ValueError, \
		      'unknown Content-Transfer-Encoding: %s' % encoding
예제 #19
0
def decode(input, output, encoding):
    if encoding == 'base64':
        import base64
        return base64.decode(input, output)
    if encoding == 'quoted-printable':
        import quopri
        return quopri.decode(input, output)
    if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
        import uu
        return uu.decode(input, output)
    if encoding in ('7bit', '8bit'):
        return output.write(input.read())
    if encoding in decodetab:
        pipethrough(input, decodetab[encoding], output)
    else:
        raise ValueError, 'unknown Content-Transfer-Encoding: %s' % encoding
예제 #20
0
def decode(input, output, encoding):
    """Decode common content-transfer-encodings (base64, quopri, uuencode)."""
    if encoding == 'base64':
        import base64
        return base64.decode(input, output)
    if encoding == 'quoted-printable':
        import quopri
        return quopri.decode(input, output)
    if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
        import uu
        return uu.decode(input, output)
    if encoding in ('7bit', '8bit'):
        return output.write(input.read())
    if encoding in decodetab:
        pipethrough(input, decodetab[encoding], output)
    else:
        raise ValueError, 'unknown Content-Transfer-Encoding: %s' % encoding
예제 #21
0
def decode(input, output, encoding):
	"""Decode common content-transfer-encodings (base64, quopri, uuencode)."""
	if encoding == 'base64':
		import base64
		return base64.decode(input, output)
	if encoding == 'quoted-printable':
		import quopri
		return quopri.decode(input, output)
	if encoding in ('uuencode', 'x-uuencode', 'uue', 'x-uue'):
		import uu
		return uu.decode(input, output)
	if encoding in ('7bit', '8bit'):
		output.write(input.read())
	if decodetab.has_key(encoding):
		pipethrough(input, decodetab[encoding], output)
	else:
		raise ValueError, \
		      'unknown Content-Transfer-Encoding: %s' % encoding
예제 #22
0
def parse_body(currentLine, line, lines, boundary):
    offset=1
    nextFirstLine = lines[currentLine+1]
#    if nextFirstLine.startswith("Content-Type"):
    encoding = None
    indexPlainText = nextFirstLine.find("text/plain")
    if indexPlainText == -1:
        return encoding, None
    else:
        offset = offset + 1

    nextOfNextLine = lines[currentLine+2]
    if nextOfNextLine.startswith("Content-Transfer-Encoding"):
        encoding = nextOfNextLine.split(':')[1].strip()
        offset = offset + 1
    textLines=lines[currentLine + offset:]
    actualText=[]
    for txtLine in textLines:
        if txtLine == "--{}".format(boundary) or txtLine == "--{}--".format(boundary):
            break
        else:
            actualText.append(txtLine)
    text = "\r\n".join(actualText)
    logger.info("text found")
    logger.info(text)
    logger.info("~"*8)
    #deal with encoding
    if encoding == "base64":
        text = base64.b64decode(text)
    #elif encoding == "7bit" or encoding == "8bit":
    #    text = quopri.decodestring(text)
    elif encoding == "quoted-printable":
        try:
            text = quopri.decode(text)
        except:
            pass
    elif encoding == "uuencode" or encoding == "x-uuencode" or encoding == "uue" or encoding == "x-uue":
        text = uu.decode(text)




    return encoding, text
예제 #23
0
def quopri_decode(input, errors='strict'):
    assert errors == 'strict'
    f = BytesIO(input)
    g = BytesIO()
    quopri.decode(f, g)
    return (g.getvalue(), len(input))
예제 #24
0
def decodestring(instring):
    outfile = StringIO.StringIO()
    quopri.decode(StringIO.StringIO(instring), outfile)

    return outfile.getvalue()
예제 #25
0
def quopri_decode(input, errors='strict'):
    assert errors == 'strict'
    f = BytesIO(input)
    g = BytesIO()
    quopri.decode(f, g)
    return (g.getvalue(), len(input))
예제 #26
0
 def test_decode(self):
     for p, e in self.STRINGS:
         infp = cStringIO.StringIO(e)
         outfp = cStringIO.StringIO()
         quopri.decode(infp, outfp)
         self.assert_(outfp.getvalue() == p)
예제 #27
0
"""Various tools used by MIME-reading or MIME-writing programs."""
예제 #28
0
"""Codec for quoted-printable encoding.
예제 #29
0
"""Codec for quoted-printable encoding.
예제 #30
0
 def test_decode(self):
     for p, e in self.STRINGS:
         infp = cStringIO.StringIO(e)
         outfp = cStringIO.StringIO()
         quopri.decode(infp, outfp)
         self.assert_(outfp.getvalue() == p)
예제 #31
0
 def test_decode(self):
     for p, e in self.STRINGS:
         infp = io.BytesIO(e)
         outfp = io.BytesIO()
         quopri.decode(infp, outfp)
         self.assertEqual(outfp.getvalue(), p)
 def test_decode(self):
     for p, e in self.STRINGS:
         infp = io.BytesIO(e)
         outfp = io.BytesIO()
         quopri.decode(infp, outfp)
         self.assertEqual(outfp.getvalue(), p)
예제 #33
0
"""Various tools used by MIME-reading or MIME-writing programs."""
예제 #34
0
def quopri_decode(input, errors='strict'):
    f = StringIO(str(input))
    g = StringIO()
    quopri.decode(f, g)
    output = g.getvalue()
    return (output, len(input))
예제 #35
0
파일: nodes.py 프로젝트: xxoolm/Ryven
 def update_event(self, inp=-1):
     self.set_output_val(
         0, quopri.decode(self.input(0), self.input(1), self.input(2)))
예제 #36
0
def decodestring(instring):
	outfile = io.StringIO()
	quopri.decode(io.StringIO(instring), outfile)
	return outfile.getvalue()
def decodestring(instring):
    outfile = io.BytesIO()
    quopri.decode(io.BytesIO(instring.encode()), outfile)
    return outfile.getvalue().decode("utf-8", "ignore")