diff --git a/enzevalos_iphone/MailSession.swift b/enzevalos_iphone/MailSession.swift index 04dcd4242614ea4577d707707634d2b621e187b9..46bea2b46e43944e5c372aec1eb7e4848f5bb23c 100644 --- a/enzevalos_iphone/MailSession.swift +++ b/enzevalos_iphone/MailSession.swift @@ -183,22 +183,29 @@ class MailSession { listeners.append(listener) } + func testConfig(host: String, port: UInt32, authType: MCOAuthType, connType: MCOConnectionType) { + testServer(hostname: host, port: port, connectionType: connType, authType: authType, completion: completionTestConfig) + } func findInJSonFileAndTest() -> Bool{ (hostname, port, connectionType, authType) = loadFromProviderJson() if let h = hostname, let p = port, let con = connectionType { - counter = MailSession.AUTHTYPE.count - success = false - for authType in MailSession.AUTHTYPE { - testServer(hostname: h, port: p, connectionType: con, authType: authType, completion: completionTestAuthType) - } - return true + return findAndTestAuthType(hostname: h, port: p, connType: con) } else { return false } } + + private func findAndTestAuthType(hostname: String, port: UInt32, connType: MCOConnectionType) -> Bool { + counter = MailSession.AUTHTYPE.count + success = false + for authType in MailSession.AUTHTYPE { + testServer(hostname: hostname, port: port, connectionType: connType, authType: authType, completion: completionTestAuthType) + } + return true + } func findDefaultValuesAndTest() -> Bool { _ = self.fillWithDefaultValues() @@ -354,6 +361,42 @@ class MailSession { completionTestAuthType(error: error, smtpSession: smtpSession, imapSession: imapSession) } + private func completionTestConfig(error: MailServerConnectionError?, smtpSession: MCOSMTPSession?, imapSession: MCOIMAPSession?) { + if let e = error { + if e == MailServerConnectionError.AuthenticationError || e == MailServerConnectionError.NoInternetconnection { + errors.insert(e) + for l in listeners { + l.testFinish(result: false) + } + } + else { + var host: String + var port: UInt32 + var connType: MCOConnectionType + if let imap = imapSession { + host = imap.hostname + port = imap.port + connType = imap.connectionType + _ = findAndTestAuthType(hostname: host, port: port, connType: connType) + } + else if let smtp = smtpSession { + host = smtp.hostname + port = smtp.port + connType = smtp.connectionType + _ = findAndTestAuthType(hostname: host, port: port, connType: connType) + } + else { + errors.insert(e) + for l in listeners { + l.testFinish(result: false) + } + } + } + return + } + completionTestAuthType(error: error, smtpSession: smtpSession, imapSession: imapSession) + } + private func completionTestAuthType(error: MailServerConnectionError?, smtpSession: MCOSMTPSession?, imapSession: MCOIMAPSession?){ guard error == nil else { counter = counter - 1 diff --git a/enzevalos_iphone/Onboarding.swift b/enzevalos_iphone/Onboarding.swift index ad9858394991003f05f125debfc85b3e85762fba..4a6c82c666c5bf251bd431f095d930893073bd36 100644 --- a/enzevalos_iphone/Onboarding.swift +++ b/enzevalos_iphone/Onboarding.swift @@ -576,17 +576,72 @@ class Onboarding: NSObject { } } + static private func checkDetailConfig(imap: Bool) { + if let imapHost = imapServer.text, let imapPortString = imapPort.text, let smtpHost = smtpServer.text, let smtpPortString = smtpPort.text, let smtpPort = Int(smtpPortString), let imapPort = Int(imapPortString) { + let imapAuthName = imapTransDataDelegate.pickedValue + let imapConName = imapTransDataDelegate.pickedValue + let smtpConnName = smtpTransDataDelegate.pickedValue + let smtpAuthName = smtpAuthDataDelegate.pickedValue + var imapAuthValue = 0 + var imapConnValue = 0 + var smtpAuthValue = 0 + var smtpConnValue = 0 + + for (value, name) in Onboarding.authenticationRows { + if name == imapAuthName { + imapAuthValue = value + } + if name == smtpAuthName { + smtpAuthValue = value + } + } + + for (value, name) in Onboarding.transportRows { + if name == imapConName { + imapConnValue = value + } + if name == smtpConnName { + smtpConnValue = value + } + } + if imap { + if imapSession == nil { + _ = setIMAPSession() + } + if let imap = imapSession { + imap.testConfig(host: imapHost, port: UInt32(imapPort), authType: MCOAuthType.init(rawValue: imapAuthValue), connType: MCOConnectionType.init(rawValue: imapConnValue)) + } + } + else { + if smtpSession == nil { + _ = setSMTPSession() + } + if let smtp = smtpSession { + smtp.testConfig(host: smtpHost, port: UInt32(smtpPort), authType: MCOAuthType.init(rawValue: smtpAuthValue), connType: MCOConnectionType.init(rawValue: smtpConnValue)) + } + } + } + } - static func checkIMAPConfig() { + static private func setIMAPSession() -> MailSession { let mailSession = MailSession(configSession: SessionType.IMAP, mailAddress: mailaddress.text!, password: password.text!, username: mailaddress.text) mailSession.addListener(listener: listenerIMAP) - + Onboarding.imapSession = mailSession + return mailSession + } + + static func checkIMAPConfig() { + let mailSession = setIMAPSession() if let imap = imapSession, MailServerConnectionError.wrongServerConfig(errors: imap.errors) && Onboarding.credentialFails == 2 { if !mailSession.findAndTestLong() { imapCompletion(imapWorks: false) } - }else { + } + else if Onboarding.credentialFails >= 3{ + checkDetailConfig(imap: true) + } + else { if !mailSession.findInJSonFileAndTest() { if !mailSession.findDefaultValuesAndTest() { if !mailSession.findAndTestLong() { @@ -595,17 +650,26 @@ class Onboarding: NSObject { } } } - Onboarding.imapSession = mailSession } - - static func checkSMTPConfig() { + + static private func setSMTPSession() -> MailSession{ let mailSession = MailSession(configSession: SessionType.SMTP, mailAddress: mailaddress.text!, password: password.text!, username: mailaddress.text) mailSession.addListener(listener: listenerSMTP) + Onboarding.smtpSession = mailSession + return mailSession + } + + static func checkSMTPConfig() { + let mailSession = setSMTPSession() if let imap = imapSession, MailServerConnectionError.wrongServerConfig(errors: imap.errors) { if !mailSession.findAndTestLong() { smtpCompletion(smtpWorks: false) } - }else { + } + else if Onboarding.credentialFails >= 3 { + checkDetailConfig(imap: false) + } + else { if !mailSession.findInJSonFileAndTest() { if !mailSession.findDefaultValuesAndTest() { if !mailSession.findAndTestLong() { @@ -614,7 +678,6 @@ class Onboarding: NSObject { } } } - Onboarding.smtpSession = mailSession }