'Authentication machinery for self.acquireconnection().
Raises OfflineImapError() of type ERROR.REPO when
there are either fatal problems or no authentications
succeeded.
If any authentication method succeeds, routine should exit:
warnings for failed methods are to be produced in the
respective except blocks.'

def __authn_helper(self, imapobj):

    exc_stack = []
    tried_to_authn = False
    tried_tls = False
    auth_methods = {'GSSAPI': (self.__authn_gssapi, False, True), 'XOAUTH2': (self.__authn_xoauth2, True, True), 'CRAM-MD5': (self.__authn_cram_md5, True, True), 'PLAIN': (self.__authn_plain, True, True), 'LOGIN': (self.__authn_login, True, False)}
    for m in self.authmechs:
        if (m not in auth_methods):
            raise Exception(('Bad    authentication    method    %s,    please,    file    OfflineIMAP    bug' % m))
        (func, tryTLS, check_cap) = auth_methods[m]
        if (tryTLS and self.starttls and (not tried_tls)):
            tried_tls = True
            self.__start_tls(imapobj)
        if check_cap:
            cap = ('AUTH=' + m)
            if (cap not in imapobj.capabilities):
                continue
        tried_to_authn = True
        self.ui.debug('imap', (u'Attempting    %s    authentication' % m))
        try:
            if func(imapobj):
                return
        except (imapobj.error, OfflineImapError) as e:
            self.ui.warn(('%s    authentication    failed:    %s' % (m, e)))
            exc_stack.append((m, e))
    if len(exc_stack):
        msg = '\n DCTB '.join([':    '.join((x[0], str(x[1]))) for x in exc_stack])
        raise OfflineImapError(('All    authentication    types    failed:\n DCTB %s' % msg), OfflineImapError.ERROR.REPO)
    if (not tried_to_authn):
        methods = ',    '.join([x[5:] for x in [x for x in imapobj.capabilities if (x[0:5] == 'AUTH=')]])
        raise OfflineImapError((u'Repository    %s:    no    supported    authentication    mechanisms    found;    configured    %s,    server    advertises    %s' % (self.repos, ',    '.join(self.authmechs), methods)), OfflineImapError.ERROR.REPO)
