Пример #1
0
def pipe_message(message, command):
    """ Pipe the message through a shell command:
        cat message | commmand > message
        message is assumed to be an instance of ImapMessage
        Returns modified message as instance of ImapMessage
    """
    p = subprocess.Popen([command],
                         shell=True,
                         stdin=subprocess.PIPE,
                         stdout=subprocess.PIPE,
                         close_fds=True)
    (child_stdout, child_stdin) = (p.stdout, p.stdin)

    memoryfile = StringIO()
    generator = Generator(memoryfile, mangle_from_=False, maxheaderlen=60)
    generator.flatten(message)
    child_stdin.write(memoryfile.getvalue())
    child_stdin.close()
    modified_message = ImapMessage(child_stdout)
    child_stdout.close()
    modified_message.set_imapflags(message.get_imapflags())
    modified_message.internaldate = message.internaldate
    if hasattr(message, 'myflags'):
        modified_message.myflags = message.myflags
    if hasattr(message, 'mailbox'):
        modified_message.mailbox = message.mailbox
    return modified_message
Пример #2
0
 def get_message(self, uid):
     """ Return an ImapMessage object created from the message with UID.
         Raise KeyError if there if there is no message with that UID.
     """
     rfc822string = self._cache_message(uid)
     result = ImapMessage(rfc822string)
     result.set_imapflags(self.get_imapflags(uid))
     result.internaldate = self.get_internaldate(uid)
     result.size = self.get_size(uid)
     if self._factory is ImapMessage:
         return result
     return self._factory(result)
Пример #3
0
 def get_header(self, uid):
     """ Return an ImapMessage object containing only the Header
         of the message with UID.
         Raise KeyError if there if there is no message with that UID.
     """
     (code, data) = self._server.uid('fetch', uid, "(BODY.PEEK[HEADER])")
     if code != 'OK':
         raise ImapNotOkError("%s in fetch_header(%s)" % (code, uid))
     try:
         rfc822string = data[0][1]
     except TypeError:
         raise KeyError("No UID %s in get_header" % uid)
     result = ImapMessage(rfc822string)
     result.set_imapflags(self.get_imapflags(uid))
     result.internaldate = self.get_internaldate(uid)
     result.size = self.get_size(uid)
     if self._factory is ImapMessage:
         return result
     return self._factory(result)
Пример #4
0
def pipe_message(message, command):
    """ Pipe the message through a shell command:
        cat message | commmand > message
        message is assumed to be an instance of ImapMessage
        Returns modified message as instance of ImapMessage
    """
    p = subprocess.Popen([command], shell=True,
            stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
    (child_stdout, child_stdin) = (p.stdout, p.stdin)

    memoryfile = StringIO()
    generator = Generator(memoryfile, mangle_from_=False, maxheaderlen=60)
    generator.flatten(message)
    child_stdin.write(memoryfile.getvalue())
    child_stdin.close()
    modified_message = ImapMessage(child_stdout)
    child_stdout.close()
    modified_message.set_imapflags(message.get_imapflags())
    modified_message.internaldate = message.internaldate
    if hasattr(message, 'myflags'):
        modified_message.myflags = message.myflags
    if hasattr(message, 'mailbox'):
        modified_message.mailbox = message.mailbox
    return modified_message