'Walks the IMAP path recursively and returns a list of all found
mailboxes.'

def _walk_mailbox_path(self, conn, prefix):

    mboxes = []
    subtrees = []
    max_mailboxes = (5 + self.my_config.discovery.max_mailboxes)
    try:
        (ok, data) = self.timed_imap(conn.list, prefix, '%')
        while (ok and (len(data) >= 3)):
            ((flags, sep, path), data[:3]) = (data[:3], [])
            flags = [f.lower() for f in flags]
            if ('\\noselect' not in flags):
                self._cache_flags(path, flags)
                mboxes.append(self._fmt_path(path))
            if ('\\haschildren' in flags):
                subtrees.append(('%s%s' % (path, sep)))
            if (len(mboxes) > max_mailboxes):
                break
        for path in subtrees:
            if (len(mboxes) < max_mailboxes):
                mboxes.extend(self._walk_mailbox_path(conn, path))
    except self.CONN_ERRORS:
        pass
    finally:
        return mboxes
