Example #1
0
def LoadMailboxFromConfiguration(mailbox_type: str,
                                 location: str) -> mailbox.Mailbox:
    mail_box = None
    if os.path.exists(location) is True:
        Logger.write().debug('Found mailbox type "%s"' % mailbox_type)
        for case in Switch(mailbox_type):
            if case('maildir'):
                mail_box = mailbox.Maildir(location)
                break
            if case('mbox'):
                mail_box = mailbox.mbox(location)
                break
            if case('mh'):
                mail_box = mailbox.MH(location)
                break
            if case('babyl'):
                mail_box = mailbox.Babyl(location)
                break
            if case('mmdf'):
                mail_box = mailbox.MMDF(location)
                break
            if case():
                Logger.write().error(
                    'Unknown mailbox type "%s" was specified' % mailbox_type)
                break
    else:
        Logger.write().error('The mailbox path given (%s) does not exist!' %
                             location)
    return mail_box
Example #2
0
    def read_maildir(self, location):
        """Import all :class:`items <Item>` from mailbox (python mailbox
        module, maildir variant).

        :param location: mailbox location
        """
        for message in mailbox.MH(location):
            _item.Item(self, eml=message.as_bytes(unixfrom=True), create=True)
Example #3
0
 def read_maildir(self, location):
     for message in mailbox.MH(location):
         if sys.hexversion >= 0x03000000:
             _item.Item(self,
                        eml=message.as_bytes(unixfrom=True),
                        create=True)
         else:
             _item.Item(self,
                        eml=message.as_string(unixfrom=True),
                        create=True)
Example #4
0
    def maildir(self, location='.'):
        """Export all :class:`items <Item>` to mailbox (using python mailbox
        module, maildir variant).

        :param location: mailbox location
        """
        destination = mailbox.MH(location + '/' + self.name)
        destination.lock()
        for item in self.items():
            destination.add(item.eml())
        destination.unlock()
Example #5
0
def cleanup_test_mode_dir(test_mode_dir):
    """
    Set up the 'test_mode' mail directory. Clean out any messages it
    may have from previous runs
    """

    # Make the test mode directory and inbox & Archive subdirectories
    # if they do not already exist. Delete any messages in the
    # mailboxes if they exist.
    #
    mh = mailbox.MH(test_mode_dir, create=True)

    folders = mh.list_folders()
    for f in ('inbox', 'Archive', 'Junk'):
        if f not in folders:
            mh_f = mh.add_folder(f)
        else:
            mh_f = mh.get_folder(f)

        # Delete any messages in these folders
        #
        mh_f.lock()
        try:
            for msg_key in mh_f.keys():
                mh_f.discard(msg_key)
        finally:
            mh_f.unlock()

        # See if we have a zipfile of messages to seed the now empty
        # folder with.
        #
        init_state_msgs_file = os.path.join(test_mode_dir, f + ".tar.gz")
        if os.path.exists(init_state_msgs_file):
            # We do not care about the names of the files in this zip
            # file. Each file we insert in to this mh folder.
            #
            print "Extracting initial messages from {}".format(
                init_state_msgs_file)
            mh_f.lock()
            try:
                with tarfile.open(init_state_msgs_file, 'r:gz') as tf:
                    for member in tf.getmembers():
                        if member.isfile():
                            print "    Adding message {}, size: {}".format(
                                member.name, member.size)
                            mh_f.add(tf.extractfile(member).read())
            finally:
                mh_f.unlock()
def load_from_file():

    if tornado.options.options.init:
        delete_index()
    create_index()

    if tornado.options.options.skip:
        logging.info("Skipping first %d messages" %
                     tornado.options.options.skip)

    upload_data = list()

    if tornado.options.options.infile:
        logging.info("Starting import from mbox file %s" %
                     tornado.options.options.infile)
        mbox = mailbox.mbox(tornado.options.options.infile)
    else:
        logging.info("Starting import from MH directory %s" %
                     tornado.options.options.indir)
        mbox = mailbox.MH(tornado.options.options.indir,
                          factory=None,
                          create=False)

    #Skipping on keys to avoid expensive read operations on skipped messages
    msgkeys = mbox.keys()[tornado.options.options.skip:]

    for msgkey in msgkeys:
        msg = mbox[msgkey]
        item = convert_msg_to_json(msg)

        if item:
            upload_data.append(item)
            if len(upload_data) == tornado.options.options.batch_size:
                upload_batch(upload_data)
                upload_data = list()

    # upload remaining items in `upload_batch`
    if upload_data:
        upload_batch(upload_data)

    logging.info("Import done - total count %d" % len(mbox.keys()))
Example #7
0
def mail_container(value):
    """
    Check that the value points to a valid mail container,
    in URI-style, e.g.: `mbox:///home/username/mail/mail.box`.
    The value is cast to a :class:`mailbox.Mailbox` object.
    """
    if not re.match(r'.*://.*', value):
        raise VdtTypeError(value)
    mburl = urlparse(value)
    if mburl.scheme == 'mbox':
        box = mailbox.mbox(mburl.path)
    elif mburl.scheme == 'maildir':
        box = mailbox.Maildir(mburl.path)
    elif mburl.scheme == 'mh':
        box = mailbox.MH(mburl.path)
    elif mburl.scheme == 'babyl':
        box = mailbox.Babyl(mburl.path)
    elif mburl.scheme == 'mmdf':
        box = mailbox.MMDF(mburl.path)
    else:
        raise VdtTypeError(value)
    return box
Example #8
0
def parse_mailbox():
    """Main routine"""
    mbox = mailbox.MH(settings.MAILDIR)

    read = set(mbox.get_sequences().get('read', ''))
    unread = set(mbox.keys()) - read

    logger.info('Parsing mailbox. Got %d unread messages' % len(unread))

    for msg_id in unread:
        record = parse_message(mbox[msg_id])

        if not record:
            logger.info('Message can not be parsed')
            continue

        if record.push_date >= two_weeks:
            csets = get_revisions(record.changeset)
        else:
            csets = set()

        check_for_backout(record)
        merged = is_merged(record, csets)
        duplicate = check_for_duplicate(record)
        link = build_tbpl_link(record)

        update_database(record, merged, link, csets)
        #TODO: JMAHER: I am not sure how to handle this duplicate code, it needs to be reworked
        duplicate = check_for_duplicate(record)
        if merged:
            mark_merged(duplicate, merged)
        add_tbpl_url(duplicate, link)

    all_read = unread | read
    mbox.set_sequences({'read': all_read})

    logger.info('DONE')
Example #9
0
 def maildir(self, location='.'):
     destination = mailbox.MH(location + '/' + self.name)
     destination.lock()
     for item in self.items():
         destination.add(item.eml())
     destination.unlock()
Example #10
0
def mailbox(path):
  if ismaildir(path):
    return pyMailbox.Maildir(path)
  if ismhdir(path):
    return pyMailbox.MH(path)
  return None
# Python Email Communication System
# How to use the email package to read, write, and send
# To copy all mail from a Babyl mailbox to an MH mailbox, converting
# all of the format-specific information that can be converted:

import mailbox

destination = mailbox.MH('~/Mail')
destination.lock()

for message in mailbox.Babyl('~/RMAIL'):
    destination.add(mailbox.MHMessage(message))

destination.flush()
destination.unlock()
Example #12
0
 def read_maildir(self, location):
     for message in mailbox.MH(location):
         _item.Item(self, eml=message.as_bytes(unixfrom=True), create=True)
Example #13
0
File: mbox.py Project: pombreda/elm
def main():
    global root, tk, top, mid, bot
    global folderbox, foldermenu, scanbox, scanmenu, viewer
    global folder, seq
    global mh, mhf

    # Parse command line options

    folder = 'inbox'
    seq = 'all'
    try:
        opts, args = getopt.getopt(sys.argv[1:], '')
    except getopt.error as msg:
        print(msg)
        sys.exit(2)
    for arg in args:
        if arg[:1] == '+':
            folder = arg[1:]
        else:
            seq = arg

    # Initialize MH

    mh = mailbox.MH(MBOXPATH)
    mhf = mh.get_folder(folder)

    # Build widget hierarchy

    root = Tk()
    tk = root.tk

    top = Frame(root)
    top.pack({'expand': 1, 'fill': 'both'})

    # Build right part: folder list

    right = Frame(top)
    right.pack({'fill': 'y', 'side': 'right'})

    folderbar = Scrollbar(right, {'relief': 'sunken', 'bd': 2})
    folderbar.pack({'fill': 'y', 'side': 'right'})

    folderbox = Listbox(right, {'exportselection': 0})
    folderbox.pack({'expand': 1, 'fill': 'both', 'side': 'left'})

    foldermenu = Menu(root)
    foldermenu.add('command', {'label': 'Open Folder', 'command': open_folder})
    foldermenu.add('separator')
    foldermenu.add('command', {'label': 'Quit', 'command': 'exit'})
    foldermenu.bind('<ButtonRelease-3>', folder_unpost)

    folderbox['yscrollcommand'] = (folderbar, 'set')
    folderbar['command'] = (folderbox, 'yview')
    folderbox.bind('<Double-1>', open_folder, 1)
    folderbox.bind('<3>', folder_post)

    # Build left part: scan list

    left = Frame(top)
    left.pack({'expand': 1, 'fill': 'both', 'side': 'left'})

    scanbar = Scrollbar(left, {'relief': 'sunken', 'bd': 2})
    scanbar.pack({'fill': 'y', 'side': 'right'})

    scanbox = Listbox(left, {'font': 'fixed'})
    scanbox.pack({'expand': 1, 'fill': 'both', 'side': 'left'})

    scanmenu = Menu(root)
    scanmenu.add('command', {'label': 'Open Message', 'command': open_message})
    scanmenu.add('command', {
        'label': 'Remove Message',
        'command': remove_message
    })
    scanmenu.add('command', {
        'label': 'Refile Message',
        'command': refile_message
    })
    scanmenu.add('separator')
    scanmenu.add('command', {'label': 'Quit', 'command': 'exit'})
    scanmenu.bind('<ButtonRelease-3>', scan_unpost)

    scanbox['yscrollcommand'] = (scanbar, 'set')
    scanbar['command'] = (scanbox, 'yview')
    scanbox.bind('<Double-1>', open_message)
    scanbox.bind('<3>', scan_post)

    # Separator between middle and bottom part

    rule2 = Frame(root, {'bg': 'black'})
    rule2.pack({'fill': 'x'})

    # Build bottom part: current message

    bot = Frame(root)
    bot.pack({'expand': 1, 'fill': 'both'})
    #
    viewer = None

    # Window manager commands

    root.minsize(800, 1)  # Make window resizable

    # Fill folderbox with text

    setfolders()

    # Fill scanbox with text

    rescan()

    # Enter mainloop

    root.mainloop()

def fix_headers():
    if "X-RPKI-PID" in srcmsg or "X-RPKI-Object" in srcmsg:
        msg["X-RPKI-PID"] = srcmsg["X-RPKI-PID"]
        msg["X-RPKI-Object"] = srcmsg["X-RPKI-Object"]
    else:
        words = srcmsg["Subject"].split()
        msg["X-RPKI-PID"] = words[1]
        msg["X-RPKI-Object"] = " ".join(words[4:])


destination = None
source = None
try:
    destination = mailbox.MH(args.output, factory=None, create=True)
    source = mailbox.Maildir(args.input, factory=None)

    for srckey, srcmsg in source.iteritems():
        if args.unseen and "S" in srcmsg.get_flags():
            continue
        assert not srcmsg.is_multipart() and srcmsg.get_content_type(
        ) == "application/x-rpki"
        payload = srcmsg.get_payload(decode=True)
        cms = rpki.POW.CMS.derRead(payload)
        txt = cms.verify(
            rpki.POW.X509Store(), None,
            rpki.POW.CMS_NOCRL | rpki.POW.CMS_NO_SIGNER_CERT_VERIFY
            | rpki.POW.CMS_NO_ATTR_VERIFY | rpki.POW.CMS_NO_CONTENT_VERIFY)
        xml = lxml.etree.fromstring(txt)
        tag = xml.tag
Example #15
0
            print "Doing a dry run! So nothing is actually being done.."

        cur_year = 0
        for date, msg_key in msg_array:
            msg = mbox[msg_key]
            tt = time.gmtime(date)
            year = tt.tm_year

            if cur_year != year:
                cur_year = year
                folder_name = "%s_%04d" % \
                    (os.path.basename(source_folder), year)
                folder_path = os.path.join(source_folder, folder_name)
                print "making folder: %s" % folder_path
                if not options.dry_run:
                    subfolder = mailbox.MH(os.path.join(folder_path),
                                           create = True)


            if not options.dry_run:
                mtime = os.path.getmtime(os.path.join(source_folder,str(msg_key)))
                new_msg_key = subfolder.add(msg)
                os.utime(os.path.join(folder_path, str(new_msg_key)),
                         (mtime,mtime))
                mbox.unlock()
                mbox.remove(msg_key)
                mbox.lock()

    finally:
        mbox.unlock()
    return
Example #16
0
    def __init__(self, options, maildir):
        """
        Setup our dispatcher.. listen on a port we are supposed to accept
        connections on. When something connects to it create an
        IMAPClientHandler and pass it the socket.

        Arguments:
        - `options` : The options set on the command line
        - `maildir` : The directory our mailspool and database are in
        """
        self.options = options

        asyncore.dispatcher.__init__(self)
        self.log = logging.getLogger("%s.%s" %
                                     (__name__, self.__class__.__name__))

        # Do NOT create our socket if we are running in standalone mode
        #
        if self.options.standalone_mode is False:
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.set_reuse_addr()
            self.bind(("127.0.0.1", 0))
            self.address = self.socket.getsockname()
            self.listen(BACKLOG)

        self.maildir = maildir
        self.mailbox = mailbox.MH(self.maildir, create=True)

        # A global counter for the next available uid_vv is stored in the user
        # server object. Mailboxes will get this value and increment it when
        # they need a new uid_vv.
        #
        self.uid_vv = 0

        # A handle to the sqlite3 database where we store our persistent
        # information.
        #
        self.db = Database(maildir)

        # A dict of the active mailboxes. An active mailbox is one that has an
        # instance of an asimap.mbox.Mailbox class.
        #
        # We keep active mailboxes around when IMAP clients are poking them in
        # some way. Active mailboxes are gotten rid of after a certain amount
        # of time during which no client pokes it.
        #
        # The key is the mailbox name.
        #
        self.active_mailboxes = {}

        # A dict of the active IMAP clients that are talking to us.
        #
        # The key is the port number of the attached client.
        #
        self.clients = {}

        # There is a single message cache per user server instance.
        #
        self.msg_cache = asimap.message_cache.MessageCache()

        # When we have any connected clients this time gets set to
        # None. Otherwise use it to determine when we have hung around long
        # enough with no connected clients and decide to exit.
        #
        self.expiry = time.time() + 1800

        # and finally restore any pesistent state stored in the db for the user
        # server.
        #
        self._restore_from_db()
        return
Example #17
0
def main():
    """

    """
    parser = setup_option_parser()
    (options, args) = parser.parse_args()
    if len(args) != 1:
        print "need to supply a MH folder to operation on"
        return

    source_folder = args[0]

    # Trim off any trailing "/" because basename will trim it not how
    # we want if the path ends in a "/"
    #
    if source_folder[-1] == "/":
        source_folder = source_folder[:-1]

    mbox = mailbox.MH(source_folder)
    mbox.lock()

    print "Collecting timestamps for all messages.."
    try:
        msg_array = []
        msgs = mbox.keys()

        if len(msgs) < options.keep:
            print "Less than %s messages in folder. Nothing to do." % \
                options.keep
            return

        # Find the dates of all the messages and sort them so we know
        # which ones to move in to which sub-folders.
        #
        for i,msg_key in enumerate(msgs):

            if i % 200 == 0:
                print "%d out of %d" % (i, len(msgs) - i)

            msg = mbox[msg_key]

            tt = None
            try:
                if 'delivery-date' in msg:
                    tt = email.utils.parsedate_tz(msg['delivery-date'])
                    date = email.utils.mktime_tz(tt)
            except (ValueError, TypeError):
                print "Yow. Message %d's 'delivery-date'(%s) resulted in "
                "a: %s" % (msg_key, msg['delivery-date'], str(e))
                tt = None

            try:
                if tt is None and 'date' in msg:
                    tt = email.utils.parsedate_tz(msg['date'])
                    date = email.utils.mktime_tz(tt)
            except (ValueError, TypeError), e:
                print "Yow. Message %d's 'date'(%s) resulted in a: %s" % \
                    (msg_key, msg['date'], str(e))
                tt = None
            except OverflowError, e:
                print "Yow. Message %d's 'date'(%s) resulted in a: %s" % \
                    (msg_key, msg['date'], str(e))
                tt = None