'Cache the message list from a Maildir.
If min_date is set, this finds the min UID of all messages newer than
min_date and uses it as the real cutoff for considering messages.
This handles the edge cases where the date is much earlier than messages
with similar UID\'s (e.g. the UID was reassigned much later).
Maildir flags are:
D (draft) F (flagged) R (replied) S (seen) T (trashed),
plus lower-case letters for custom flags.
:returns: dict that can be used as self.messagelist.'

def _scanfolder(self, min_date=None, min_uid=None):

    maxsize = self.getmaxsize()
    retval = {}
    files = []
    nouidcounter = (-1)
    for dirannex in ['new', 'cur']:
        fulldirname = os.path.join(self.getfullname(), dirannex)
        files.extend(((dirannex, filename) for filename in os.listdir(fulldirname)))
    date_excludees = {}
    for (dirannex, filename) in files:
        if filename.startswith('.'):
            continue
        filepath = os.path.join(dirannex, filename)
        if (maxsize and (os.path.getsize(os.path.join(self.getfullname(), filepath)) > maxsize)):
            continue
        (prefix, uid, fmd5, flags) = self._parse_filename(filename)
        if (uid is None):
            uid = nouidcounter
            nouidcounter -= 1
        else:
            uidmatch = re_uidmatch.search(filename)
            uid = None
            if (not uidmatch):
                uid = nouidcounter
                nouidcounter -= 1
            else:
                uid = int(uidmatch.group(1))
        if ((min_uid != None) and (uid > 0) and (uid < min_uid)):
            continue
        if ((min_date != None) and (not self._iswithintime(filename, min_date))):
            date_excludees[uid] = self.msglist_item_initializer(uid)
            date_excludees[uid]['flags'] = flags
            date_excludees[uid]['filename'] = filepath
        else:
            retval[uid] = self.msglist_item_initializer(uid)
            retval[uid]['flags'] = flags
            retval[uid]['filename'] = filepath
    if (min_date != None):
        positive_uids = [uid for uid in retval if (uid > 0)]
        if positive_uids:
            min_uid = min(positive_uids)
            for uid in date_excludees.keys():
                if (uid > min_uid):
                    retval[uid] = date_excludees[uid]
    return retval
