From e69b438df0d6b8a97c2c1e8141e4d6edb7700a6d Mon Sep 17 00:00:00 2001
From: Oliver Wiese <oliver.wiese@fu-berlin.de>
Date: Mon, 25 Feb 2019 12:17:06 +0100
Subject: [PATCH] update imapconfig

---
 enzevalos_iphone/AppDelegate.swift     | 14 ++++--
 enzevalos_iphone/MailHandler.swift     | 37 +++++++++-----
 enzevalos_iphone/Onboarding.swift      | 67 +++++++-------------------
 enzevalos_iphone/ReadVENDelegate.swift |  1 -
 enzevalos_iphone/UserData.swift        | 18 ++-----
 5 files changed, 58 insertions(+), 79 deletions(-)

diff --git a/enzevalos_iphone/AppDelegate.swift b/enzevalos_iphone/AppDelegate.swift
index d1f61c4c..a1ce55df 100644
--- a/enzevalos_iphone/AppDelegate.swift
+++ b/enzevalos_iphone/AppDelegate.swift
@@ -75,7 +75,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
             selector: #selector(addressBookDidChange),
             name: NSNotification.Name.CNContactStoreDidChange,
             object: nil)
-
         if #available(iOS 11.0, *) {
             QAKit.Fingertips.start()
         }
@@ -83,7 +82,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
         // Alternative:  UIApplicationBackgroundFetchIntervalMinimum
         let backgroundFetchInterval : Double = 60*5  // =  seconds * minutes
         UIApplication.shared.setMinimumBackgroundFetchInterval(backgroundFetchInterval)
-        DataHandler.handler.startToSendMore()
+        
+        if hasUser() {
+            DataHandler.handler.startToSendMore()
+        }
         return true
     }
     
@@ -263,7 +265,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
             Logger.log(background: false)
 //        }
         resetApp()
-        DataHandler.handler.startToSendMore()
+        if hasUser() {
+            DataHandler.handler.startToSendMore()
+        }
+    }
+    
+    private func hasUser() -> Bool{
+        return UserManager.loadUserValue(.userAddr) != nil
     }
 
     func applicationWillTerminate(_ application: UIApplication) {
diff --git a/enzevalos_iphone/MailHandler.swift b/enzevalos_iphone/MailHandler.swift
index 320b94af..65e7bbee 100644
--- a/enzevalos_iphone/MailHandler.swift
+++ b/enzevalos_iphone/MailHandler.swift
@@ -66,8 +66,12 @@ class MailHandler {
     var IMAPIdleSupported: Bool?
 
     var shouldTryRefreshOAUTH: Bool {
-        return (UserManager.loadImapAuthType() == MCOAuthType.xoAuth2 || UserManager.loadSmtpAuthType() == MCOAuthType.xoAuth2) &&
-            !(EmailHelper.singleton().authorization?.authState.isTokenFresh() ?? false)
+        if let imapAuthType = UserManager.loadUserValue(.imapConnectionType) as? Int,
+            let smtpAuthType = UserManager.loadUserValue(.smtpAuthType) as? Int{
+            return (imapAuthType == MCOAuthType.xoAuth2.rawValue || smtpAuthType == MCOAuthType.xoAuth2.rawValue) &&
+                !(EmailHelper.singleton().authorization?.authState.isTokenFresh() ?? false)
+        }
+        return false
     }
     
     func sendSecretKey(keyID: String, key: String, callback: @escaping (MailServerConnectionError?) -> Void) {
@@ -192,6 +196,7 @@ class MailHandler {
         let imapsession = MCOIMAPSession()
         if let hostname = UserManager.loadUserValue(Attribute.imapHostname) as? String {
             imapsession.hostname = hostname
+            print(hostname)
         }
         else {
             allData = false
@@ -208,9 +213,14 @@ class MailHandler {
         else {
             allData = false
         }
-        imapsession.authType = UserManager.loadImapAuthType()
+        if let authType = UserManager.loadUserValue(.imapAuthType) as? Int {
+            imapsession.authType = MCOAuthType(rawValue: authType)
+        }
+        else {
+            allData = false
+        }
 
-        if UserManager.loadImapAuthType() == MCOAuthType.xoAuth2 {
+        if imapsession.authType == MCOAuthType.xoAuth2 {
             imapsession.oAuth2Token = EmailHelper.singleton().authorization?.authState.lastTokenResponse?.accessToken
         } else if let pw = UserManager.loadUserValue(Attribute.userPW) as? String {
             imapsession.password = pw
@@ -287,8 +297,10 @@ class MailHandler {
 
     fileprivate func createSMTPSession() -> MCOSMTPSession {
         let session = MCOSMTPSession()
-        session.authType = UserManager.loadSmtpAuthType()
-        if UserManager.loadSmtpAuthType() == MCOAuthType.xoAuth2 {
+        if let authType = UserManager.loadUserValue(.smtpAuthType) as? Int {
+            session.authType = MCOAuthType(rawValue: authType)
+        }
+        if session.authType == MCOAuthType.xoAuth2 {
             if let lastToken = EmailHelper.singleton().authorization?.authState.lastTokenResponse {
                 session.oAuth2Token = lastToken.accessToken
             }
@@ -298,7 +310,6 @@ class MailHandler {
         session.hostname = UserManager.loadUserValue(Attribute.smtpHostname) as! String
         session.port = UInt32(UserManager.loadUserValue(Attribute.smtpPort) as! Int)
         session.username = UserManager.loadUserValue(Attribute.userAddr) as! String
-        session.authType = UserManager.loadSmtpAuthType()
         session.connectionType = MCOConnectionType(rawValue: UserManager.loadUserValue(Attribute.smtpConnectionType) as! Int)
         return session
     }
@@ -771,13 +782,14 @@ class MailHandler {
         session.hostname = UserManager.loadUserValue(Attribute.smtpHostname) as! String
         session.port = UInt32(UserManager.loadUserValue(Attribute.smtpPort) as! Int)
         session.username = useraddr
-        
-        if UserManager.loadSmtpAuthType() == MCOAuthType.xoAuth2 {
+        if let type = UserManager.loadUserValue(.smtpAuthType) as? Int {
+            session.authType = MCOAuthType(rawValue: type)
+        }
+        if session.authType == MCOAuthType.xoAuth2 {
             session.oAuth2Token = EmailHelper.singleton().authorization?.authState.lastTokenResponse?.accessToken
         } else if let pw = UserManager.loadUserValue(Attribute.userPW) as? String {
             session.password = pw
         }
-        session.authType = UserManager.loadSmtpAuthType()
         session.connectionType = MCOConnectionType.init(rawValue: UserManager.loadUserValue(Attribute.smtpConnectionType) as! Int)
         session.checkAccountOperationWith(from: MCOAddress.init(mailbox: useraddr)).start({(error: Error?) -> () in
             if let error = error {
@@ -886,10 +898,13 @@ class MailHandler {
 
 
     func allFolders(_ completion: @escaping (Error?, [Any]?) -> Void) {
+        print("''''HELO########")
         guard IMAPSession != nil else {
+            print("COMPLE########")
             completion(MailServerConnectionError.NoData, nil)
             return
         }
+        print("''''HELO########22")
         let op = IMAPSession?.fetchAllFoldersOperation()
         op?.start(completion)
     }
@@ -986,7 +1001,7 @@ class MailHandler {
                         return
                     }
                     if let ids = uids {
-                        folder.lastUpdate = Date()
+                        //folder.lastUpdate = Date()
                         self.loadMessagesFromServer(ids, folderPath: folderPath, record: nil, completionCallback: completionCallback)
                     } else {
                         completionCallback(nil)
diff --git a/enzevalos_iphone/Onboarding.swift b/enzevalos_iphone/Onboarding.swift
index 96a45409..3d6d9442 100644
--- a/enzevalos_iphone/Onboarding.swift
+++ b/enzevalos_iphone/Onboarding.swift
@@ -61,6 +61,8 @@ class Onboarding: NSObject {
     static var work: () -> () = { () -> () in }
 
     static var credentialFails = 0
+    
+    static var authTypeTest = 0
 
     static var authenticationRows: [Int: String] = [MCOAuthType.saslLogin.rawValue: "Login", MCOAuthType.saslPlain.rawValue: NSLocalizedString("NormalPassword", comment: ""), MCOAuthType.SASLSRP.rawValue: "SRP", MCOAuthType.SASLCRAMMD5.rawValue: "CRAMMD5", MCOAuthType.SASLDIGESTMD5.rawValue: "DIGESTMD5", MCOAuthType.SASLNTLM.rawValue: "NTLM", MCOAuthType.SASLGSSAPI.rawValue: "GSSAPI", MCOAuthType.saslKerberosV4.rawValue: "KerberosV4", 0: "None"]
     static var transportRows: [Int: String] = [MCOConnectionType.clear.rawValue: NSLocalizedString("Plaintext", comment: ""), MCOConnectionType.startTLS.rawValue: "StartTLS", MCOConnectionType.TLS.rawValue: "TLS/SSL"]
@@ -551,7 +553,15 @@ class Onboarding: NSObject {
             return
         }
         if let e = error {
-            fail(MailServerConnectionError.findErrorCode(error: e))
+            let authtypes = [MCOAuthType.SASLCRAMMD5, MCOAuthType.SASLDIGESTMD5, MCOAuthType.SASLGSSAPI,
+                             MCOAuthType.saslKerberosV4, MCOAuthType.saslLogin, MCOAuthType.SASLNTLM, MCOAuthType.saslPlain, MCOAuthType.SASLSRP, MCOAuthType.xoAuth2, MCOAuthType.init(rawValue: 0)]
+            if self.authTypeTest > authtypes.count - 1{
+                fail(MailServerConnectionError.findErrorCode(error: e))
+            }
+            UserManager.storeUserValue(authtypes[self.authTypeTest].rawValue as? AnyObject, attribute: .imapAuthType)
+            print("New auttype : \(authtypes[self.authTypeTest]) | counter: \(self.authTypeTest)")
+            self.authTypeTest = self.authTypeTest + 1
+            checkConfig(AppDelegate.getAppDelegate().credentialsFailed, work: AppDelegate.getAppDelegate().credentialsWork)
         }
     }
 
@@ -611,32 +621,10 @@ class Onboarding: NSObject {
             } else if let trans = imapService.info()["starttls"] as? Bool, trans {
                 UserManager.storeUserValue(MCOConnectionType.startTLS.rawValue as AnyObject?, attribute: Attribute.imapConnectionType)
             } else {
-                UserManager.storeUserValue(MCOConnectionType.clear.rawValue as AnyObject?, attribute: Attribute.imapConnectionType)
-            }
-
-            if let auth = imapService.info()["auth"] as? String, auth == "saslPlain" {
-                UserManager.storeUserValue(MCOAuthType.saslPlain.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
-            } else if let auth = imapService.info()["auth"] as? String, auth == "saslLogin" {
-                UserManager.storeUserValue(MCOAuthType.saslLogin.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
-            } else if let auth = imapService.info()["auth"] as? String, auth == "saslKerberosV4" {
-                UserManager.storeUserValue(MCOAuthType.saslKerberosV4.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
-            } else if let auth = imapService.info()["auth"] as? String, auth == "saslCRAMMD5" {
-                UserManager.storeUserValue(MCOAuthType.SASLCRAMMD5.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
-            } else if let auth = imapService.info()["auth"] as? String, auth == "saslDIGESTMD5" {
-                UserManager.storeUserValue(MCOAuthType.SASLDIGESTMD5.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
-            } else if let auth = imapService.info()["auth"] as? String, auth == "saslGSSAPI" {
-                UserManager.storeUserValue(MCOAuthType.SASLGSSAPI.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
-            } else if let auth = imapService.info()["auth"] as? String, auth == "saslSRP" {
-                UserManager.storeUserValue(MCOAuthType.SASLSRP.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
-            } else if let auth = imapService.info()["auth"] as? String, auth == "saslNTLM" {
-                UserManager.storeUserValue(MCOAuthType.SASLNTLM.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
-            } else if let auth = imapService.info()["auth"] as? String, auth == "xoAuth2" {
-                UserManager.storeUserValue(MCOAuthType.xoAuth2.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
-            } else if let auth = imapService.info()["auth"] as? String, auth == "xoAuth2Outlook" {
-                UserManager.storeUserValue(MCOAuthType.SASLCRAMMD5.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
-            } else {
-                UserManager.storeUserValue(MCOAuthType.saslPlain.rawValue as AnyObject?, attribute: Attribute.imapAuthType)
+                UserManager.storeUserValue(MCOConnectionType.TLS.rawValue as AnyObject?, attribute: Attribute.imapConnectionType)
             }
+            
+            UserManager.storeUserValue(Attribute.imapAuthType.defaultValue, attribute: Attribute.imapAuthType)
 
             let smtpService = smtp[0]
             UserManager.storeUserValue((smtpService.info()["hostname"] ?? "smtp.example.com") as AnyObject?, attribute: Attribute.smtpHostname)
@@ -647,32 +635,11 @@ class Onboarding: NSObject {
             } else if let trans = smtpService.info()["starttls"] as? Bool, trans {
                 UserManager.storeUserValue(MCOConnectionType.startTLS.rawValue as AnyObject?, attribute: Attribute.smtpConnectionType)
             } else {
-                UserManager.storeUserValue(MCOConnectionType.clear.rawValue as AnyObject?, attribute: Attribute.smtpConnectionType)
+                UserManager.storeUserValue(MCOConnectionType.startTLS.rawValue as AnyObject?, attribute: Attribute.smtpConnectionType)
             }
 
-            if let auth = smtpService.info()["auth"] as? String, auth == "saslPlain" {
-                UserManager.storeUserValue(MCOAuthType.saslPlain.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            } else if let auth = smtpService.info()["auth"] as? String, auth == "saslLogin" {
-                UserManager.storeUserValue(MCOAuthType.saslLogin.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            } else if let auth = smtpService.info()["auth"] as? String, auth == "saslKerberosV4" {
-                UserManager.storeUserValue(MCOAuthType.saslKerberosV4.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            } else if let auth = smtpService.info()["auth"] as? String, auth == "saslCRAMMD5" {
-                UserManager.storeUserValue(MCOAuthType.SASLCRAMMD5.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            } else if let auth = smtpService.info()["auth"] as? String, auth == "saslDIGESTMD5" {
-                UserManager.storeUserValue(MCOAuthType.SASLDIGESTMD5.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            } else if let auth = smtpService.info()["auth"] as? String, auth == "saslGSSAPI" {
-                UserManager.storeUserValue(MCOAuthType.SASLGSSAPI.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            } else if let auth = smtpService.info()["auth"] as? String, auth == "saslSRP" {
-                UserManager.storeUserValue(MCOAuthType.SASLSRP.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            } else if let auth = smtpService.info()["auth"] as? String, auth == "saslNTLM" {
-                UserManager.storeUserValue(MCOAuthType.SASLNTLM.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            } else if let auth = smtpService.info()["auth"] as? String, auth == "xoAuth2" {
-                UserManager.storeUserValue(MCOAuthType.xoAuth2.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            } else if let auth = smtpService.info()["auth"] as? String, auth == "xoAuth2Outlook" {
-                UserManager.storeUserValue(MCOAuthType.SASLCRAMMD5.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            } else {
-                UserManager.storeUserValue(MCOAuthType.saslPlain.rawValue as AnyObject?, attribute: Attribute.smtpAuthType)
-            }
+            
+            UserManager.storeUserValue(Attribute.smtpAuthType.defaultValue as AnyObject?, attribute: Attribute.smtpAuthType)
             
             if let drafts = provider.draftsFolderPath() {
                 UserManager.storeUserValue(drafts as AnyObject?, attribute: Attribute.draftFolderPath)
diff --git a/enzevalos_iphone/ReadVENDelegate.swift b/enzevalos_iphone/ReadVENDelegate.swift
index ed58c80a..66e6f867 100644
--- a/enzevalos_iphone/ReadVENDelegate.swift
+++ b/enzevalos_iphone/ReadVENDelegate.swift
@@ -82,7 +82,6 @@ extension ReadVENDelegate: VENTokenFieldDelegate {
 
     func tokenField(_ tokenField: VENTokenField, colorSchemeForTokenAt index: UInt) -> UIColor {
         if let adr = DataHandler.handler.findMailAddress(adr: tokenField.mailTokens[Int(index)] as! String){
-            print(adr)
             if adr.hasKey {
                 return UIColor.init(red: 0, green: 122.0 / 255.0, blue: 1, alpha: 1)
             }
diff --git a/enzevalos_iphone/UserData.swift b/enzevalos_iphone/UserData.swift
index 4b1605b7..0cde72f7 100644
--- a/enzevalos_iphone/UserData.swift
+++ b/enzevalos_iphone/UserData.swift
@@ -44,6 +44,10 @@ enum Attribute: Int {
             return Date(timeIntervalSinceNow: TimeInterval(Logger.loggingInterval)) as AnyObject?
         case .loggingFolderPath: //save backendFolderPath here
             return "letterbox-study" as AnyObject?
+        case .imapAuthType:
+            return MCOAuthType.SASLDIGESTMD5.rawValue as AnyObject?
+        case .smtpAuthType:
+            return MCOAuthType.saslPlain.rawValue as AnyObject?
         default:
             return nil
         }
@@ -182,20 +186,6 @@ struct UserManager {
         }
     }
 
-    static func loadImapAuthType() -> MCOAuthType {
-        if let auth = UserManager.loadUserValue(Attribute.imapAuthType) as? Int, auth != 0 {
-            return MCOAuthType.init(rawValue: auth)
-        }
-        return MCOAuthType.saslPlain
-    }
-
-    static func loadSmtpAuthType() -> MCOAuthType {
-        if let auth = UserManager.loadUserValue(Attribute.smtpAuthType) as? Int, auth != 0 {
-            return MCOAuthType.init(rawValue: auth)
-        }
-        return MCOAuthType.saslPlain
-    }
-
     static func loadUserSignature() -> String {
         if UserDefaults.standard.bool(forKey: "Signature.Switch"), let sig = UserDefaults.standard.string(forKey: "Signature.Text") {
             return "\n\n______________________________\n\n\(sig.trimmingCharacters(in: .whitespacesAndNewlines))\n\n"
-- 
GitLab