Эх сурвалжийг харах

MRConfig getters for SMTP etc

Bastian van de Wetering 6 жил өмнө
parent
commit
332656db55

+ 34 - 48
deltachat-ios/AccountSetupController.swift

@@ -20,14 +20,14 @@ class AccountSetupController: UITableViewController {
 		return hudHandler
 	}()
 
-	private lazy var emailCell:TextFieldCell = {
+	private lazy var emailCell: TextFieldCell = {
 		let cell = TextFieldCell.makeEmailCell(delegate: self)
 		cell.textField.tag = 0
 		cell.textField.accessibilityIdentifier = "emailTextField"	// will be used to eventually show oAuth-Dialogue when pressing return key
 		return cell
 	}()
 
-	private lazy var passwordCell:TextFieldCell = {
+	private lazy var passwordCell: TextFieldCell = {
 		let cell = TextFieldCell.makePasswordCell(delegate: self)
 		cell.textField.tag = 1
 		cell.accessibilityIdentifier = "passwordCell"	// will be used to eventually show oAuth-Dialogue when selecting
@@ -42,73 +42,74 @@ class AccountSetupController: UITableViewController {
 	lazy var imapServerCell = TextFieldCell(description: "IMAP Server", placeholder: MRConfig.mailServer ?? MRConfig.configuredMailServer)
 	lazy var imapUserCell = TextFieldCell(description: "IMAP User", placeholder: MRConfig.mailUser ?? MRConfig.configuredMailUser)
 	lazy var imapPortCell = TextFieldCell(description: "IMAP Port", placeholder: MRConfig.mailPort ?? MRConfig.configuredMailPort)
-	lazy var imapSecurityCell = TextFieldCell(description: "IMAP Security", placeholder: "TODO")
+	lazy var imapSecurityCell = TextFieldCell(description: "IMAP Security", placeholder: "to do")
 
 	lazy var smtpServerCell = TextFieldCell(description: "SMTP Server", placeholder: MRConfig.sendServer ?? MRConfig.configuredSendServer)
 	lazy var smtpUserCell = TextFieldCell(description: "SMTP User", placeholder: MRConfig.sendUser ?? MRConfig.configuredSendUser)
 	lazy var smtpPortCell = TextFieldCell(description: "SMTP Port", placeholder: MRConfig.sendPort ?? MRConfig.configuredSendPort)
 	lazy var smtpPasswordCell = TextFieldCell(description: "SMTP Password", placeholder: "*************")
-	lazy var smtpSecurityCell = TextFieldCell(description: "SMTP Security", placeholder: "TODO")
+	lazy var smtpSecurityCell = TextFieldCell(description: "SMTP Security", placeholder: "to do")
 	*/
 
-
 	// TODO: consider adding delegates and tags by loop - leave for now like this
-	lazy var imapServerCell:TextFieldCell = {
+	lazy var imapServerCell: TextFieldCell = {
 		let cell = TextFieldCell(description: "IMAP Server", placeholder: MRConfig.mailServer ?? MRConfig.configuredMailServer, delegate: self)
 		cell.accessibilityIdentifier = "IMAPServerCell"
 		cell.textField.tag = 2
 		return cell
 	}()
 
-	lazy var imapUserCell:TextFieldCell = {
+	lazy var imapUserCell: TextFieldCell = {
 		let cell = TextFieldCell(description: "IMAP User", placeholder: MRConfig.mailUser ?? MRConfig.configuredMailUser, delegate: self)
 		cell.accessibilityIdentifier = "IMAPUserCell"
 		cell.textField.tag = 3
 		return cell
 	}()
 
-	lazy var imapPortCell:TextFieldCell = {
+	lazy var imapPortCell: TextFieldCell = {
 		let cell = TextFieldCell(description: "IMAP Port", placeholder: MRConfig.mailPort ?? MRConfig.configuredMailPort, delegate: self)
 		cell.accessibilityIdentifier = "IMAPPortCell"
 		cell.textField.tag = 4
 		return cell
 	}()
 
-	lazy var imapSecurityCell:TextFieldCell = {
-		let cell = TextFieldCell(description: "IMAP Security", placeholder: "TODO", delegate: self)
+	lazy var imapSecurityCell: TextFieldCell = {
+		let text = "\(MRConfig.getImapSecurity())"
+		let cell = TextFieldCell(description: "IMAP Security", placeholder: text, delegate: self)
 		cell.accessibilityIdentifier = "IMAPSecurityCell"
 		cell.textField.tag = 5
 		cell.textField.keyboardType = UIKeyboardType.numberPad
 		return cell
 	}()
 
-	lazy var smtpServerCell:TextFieldCell = {
+	lazy var smtpServerCell: TextFieldCell = {
 		let cell = TextFieldCell(description: "SMTP Server", placeholder: MRConfig.sendServer ?? MRConfig.configuredSendServer, delegate: self)
 		cell.accessibilityIdentifier = "SMTPServerCell"
 		cell.textField.tag = 6
 		return cell
 	}()
-	lazy var smtpUserCell:TextFieldCell = {
+	lazy var smtpUserCell: TextFieldCell = {
 		let cell = TextFieldCell(description: "SMTP User", placeholder: MRConfig.sendUser ?? MRConfig.configuredSendUser, delegate: self)
 		cell.accessibilityIdentifier = "SMTPUserCell"
 		cell.textField.tag = 7
 		return cell
 	}()
-	lazy var smtpPortCell:TextFieldCell = {
+	lazy var smtpPortCell: TextFieldCell = {
 		let cell = TextFieldCell(description: "SMTP Port", placeholder: MRConfig.sendPort ?? MRConfig.configuredSendPort, delegate: self)
 		cell.accessibilityIdentifier = "SMTPPortCell"
 		cell.textField.tag = 8
 		return cell
 	}()
-	lazy var smtpPasswordCell:TextFieldCell = {
+	lazy var smtpPasswordCell: TextFieldCell = {
 		let cell = TextFieldCell(description: "SMTP Password", placeholder: "*************", delegate: self)
 		cell.accessibilityIdentifier = "SMTPPasswordCell"
 		cell.textField.tag = 9
 		return cell
 	}()
 
-	lazy var smtpSecurityCell:TextFieldCell = {
-		let cell = TextFieldCell(description: "SMTP Security", placeholder: "TODO", delegate: self)
+	lazy var smtpSecurityCell: TextFieldCell = {
+		let text = "\(MRConfig.getSmtpSecurity())"
+		let cell = TextFieldCell(description: "SMTP Security", placeholder: text, delegate: self)
 		cell.accessibilityIdentifier = "SMTPSecurityCell"
 		cell.textField.tag = 10
 		cell.textField.keyboardType = UIKeyboardType.numberPad
@@ -116,11 +117,20 @@ class AccountSetupController: UITableViewController {
 	}()
 
 	// this loginButton can be enabled and disabled
-	let loginButton:UIBarButtonItem = UIBarButtonItem(title: "Login", style: .done, target: self, action: #selector(loginButtonPressed))
-
-
-	private lazy var basicSectionCells:[UITableViewCell] = [emailCell, passwordCell]
-	private lazy var advancedSectionCells:[UITableViewCell] = [imapServerCell,imapUserCell,imapPortCell,imapSecurityCell,smtpServerCell,smtpUserCell,smtpPortCell,smtpPasswordCell,smtpSecurityCell]
+	let loginButton: UIBarButtonItem = UIBarButtonItem(title: "Login", style: .done, target: self, action: #selector(loginButtonPressed))
+
+	private lazy var basicSectionCells: [UITableViewCell] = [emailCell, passwordCell]
+	private lazy var advancedSectionCells: [UITableViewCell] = [
+		imapServerCell,
+		imapUserCell,
+		imapPortCell,
+		imapSecurityCell,
+		smtpServerCell,
+		smtpUserCell,
+		smtpPortCell,
+		smtpPasswordCell,
+		smtpSecurityCell
+	]
 
 	private var advancedSectionShowing: Bool = false
 
@@ -234,7 +244,7 @@ class AccountSetupController: UITableViewController {
 		let willShow = !self.advancedSectionShowing
 
 		// extract indexPaths from advancedCells
-		let advancedIndexPaths:[IndexPath] = advancedSectionCells.indices.map({IndexPath(row: $0, section: 1)})
+		let advancedIndexPaths: [IndexPath] = advancedSectionCells.indices.map({IndexPath(row: $0, section: 1)})
 
 		//advancedSectionCells.indices.map({indexPaths.append(IndexPath(row: $0, section: 1))}
 
@@ -251,30 +261,7 @@ class AccountSetupController: UITableViewController {
 		}
 
 	}
-	/*
-	private func toggleAdvancedSection(button: UIButton) {
-
-		let willShow = !self.advancedSectionShowing
-
-		// extract indexPaths from advancedCells
-		let advancedIndexPaths:[IndexPath] = advancedSectionCells.indices.map({IndexPath(row: $0, section: 1)})
-
-		//advancedSectionCells.indices.map({indexPaths.append(IndexPath(row: $0, section: 1))}
-
-		// set flag before delete/insert operation, because cellForRowAt will be triggered and uses this flag
-		self.advancedSectionShowing = willShow
-
-		button.setTitle(willShow ? "Hide":"Show", for: .normal)
-
-		if willShow {
-			tableView.insertRows(at: advancedIndexPaths, with: .fade)
-		} else {
-			tableView.deleteRows(at: advancedIndexPaths, with: .fade)
-
-		}
 
-	}
-	*/
 	@objc func loginButtonPressed() {
 
 		guard let emailAddress = emailCell.getText() else {
@@ -303,7 +290,7 @@ class AccountSetupController: UITableViewController {
 	}
 
 	// returns true if needed
-	private func showOAuthAlertIfNeeded(emailAddress: String, handleCancel: (()->())?) -> Bool {
+	private func showOAuthAlertIfNeeded(emailAddress: String, handleCancel: (() -> Void)?) -> Bool {
 		if oAuthDenied {
 			return false
 		}
@@ -314,8 +301,7 @@ class AccountSetupController: UITableViewController {
 
 		let oAuth2Url = String(cString: oAuth2UrlPointer)
 
-		if let url = URL.init(string: oAuth2Url)  {
-
+		if let url = URL.init(string: oAuth2Url) {
 			let title = "Continue with simplified setup"
 			//swiftlint:disable all
 			let message = "The entered e-mail address supports a simlified setup (oAuth2).\n\nIn the next step, please allow Delta Chat to act as your Chat with E-Mail app.\n\nThere are no Delta Chat servers, your data stays on your device."

+ 288 - 291
deltachat-ios/AppDelegate.swift

@@ -17,299 +17,296 @@ var mailboxPointer: UnsafeMutablePointer<dc_context_t>!
 let logger = SwiftyBeaver.self
 
 enum ApplicationState {
-  case stopped
-  case running
-  case background
-  case backgroundFetch
+	case stopped
+	case running
+	case background
+	case backgroundFetch
 }
 
 @UIApplicationMain
 class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
-  static let appCoordinator = AppCoordinator()
-  static var progress: Float = 0
-  static var lastErrorDuringConfig: String?
-  var backgroundTask: UIBackgroundTaskIdentifier = .invalid
-
-  var reachability = Reachability()!
-  var window: UIWindow?
-
-  var state = ApplicationState.stopped
-
-  private func getCoreInfo() -> [[String]] {
-    if let cInfo = dc_get_info(mailboxPointer) {
-      let info = String(cString: cInfo)
-      logger.info(info)
-      return info.components(separatedBy: "\n").map { val in
-        val.components(separatedBy: "=")
-      }
-    }
-
-    return []
-  }
-
-  func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
-    DBDebugToolkit.setup()
-    DBDebugToolkit.setupCrashReporting()
-
-    let console = ConsoleDestination()
-    logger.addDestination(console)
-
-    logger.info("launching")
-
-    // Override point for customization after application launch.
-
-    window = UIWindow(frame: UIScreen.main.bounds)
-    guard let window = window else {
-      fatalError("window was nil in app delegate")
-    }
-    
-    // setup deltachat core context
-      //       - second param remains nil (user data for more than one mailbox)
-    mailboxPointer = dc_context_new(callback_ios, nil, "iOS")
-    guard mailboxPointer != nil else {
-      fatalError("Error: dc_context_new returned nil")
-    }
-    
-    open()
-    let isConfigured = dc_is_configured(mailboxPointer) != 0
-    
-    AppDelegate.appCoordinator.setupViewControllers(window: window)
-    
-    
-    UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
-    
-    start()
-    
-    registerForPushNotifications()
-    if !isConfigured {
-      AppDelegate.appCoordinator.presentAccountSetup(animated: false)
-    }
-    return true
-  }
-
-  func application(_: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
-    logger.info("---- background-fetch ----")
-
-    start {
-      // TODO: actually set the right value depending on if we found sth
-      completionHandler(.newData)
-    }
-  }
-
-  func applicationWillEnterForeground(_: UIApplication) {
-    logger.info("---- foreground ----")
-    start()
-  }
-
-  func applicationDidEnterBackground(_: UIApplication) {
-    logger.info("---- background ----")
-
-    reachability.stopNotifier()
-    NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
-
-    maybeStop()
-  }
-
-  func maybeStop() {
-    DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
-      let app = UIApplication.shared
-      logger.info("state: \(app.applicationState) time remaining \(app.backgroundTimeRemaining)")
-
-      if app.applicationState != .background {
-        // only need to do sth in the background
-        return
-      } else if app.backgroundTimeRemaining < 10 {
-        self.stop()
-      } else {
-        self.maybeStop()
-      }
-    }
-  }
-
-  func applicationWillTerminate(_: UIApplication) {
-    logger.info("---- terminate ----")
-    close()
-
-    reachability.stopNotifier()
-    NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
-  }
-
-  func dbfile() -> String {
-    let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)
-    let documentsPath = paths[0]
-
-    return documentsPath + "/messenger.db"
-  }
-
-  func open() {
-    logger.info("open: \(dbfile())")
-
-    _ = dc_open(mailboxPointer, dbfile(), nil)
-  }
-
-  func stop() {
-    state = .background
-
-    dc_interrupt_imap_idle(mailboxPointer)
-    dc_interrupt_smtp_idle(mailboxPointer)
-    dc_interrupt_mvbox_idle(mailboxPointer)
-    dc_interrupt_sentbox_idle(mailboxPointer)
-  }
-
-  func close() {
-    state = .stopped
-
-    dc_close(mailboxPointer)
-    mailboxPointer = nil
-  }
-
-  func start(_ completion: (() -> Void)? = nil) {
-    logger.info("---- start ----")
-
-    if state == .running {
-      return
-    }
-
-    
-    state = .running
-
-    DispatchQueue.global(qos: .background).async {
-      self.registerBackgroundTask()
-      while self.state == .running {
-        dc_perform_imap_jobs(mailboxPointer)
-        dc_perform_imap_fetch(mailboxPointer)
-        dc_perform_imap_idle(mailboxPointer)
-      }
-      if self.backgroundTask != .invalid {
-        completion?()
-        self.endBackgroundTask()
-      }
-    }
-
-    DispatchQueue.global(qos: .utility).async {
-      self.registerBackgroundTask()
-      while self.state == .running {
-        dc_perform_smtp_jobs(mailboxPointer)
-        dc_perform_smtp_idle(mailboxPointer)
-      }
-      if self.backgroundTask != .invalid {
-        self.endBackgroundTask()
-      }
-    }
-
-    if MRConfig.sentboxWatch {
-      DispatchQueue.global(qos: .background).async {
-        while self.state == .running {
-          dc_perform_sentbox_fetch(mailboxPointer)
-          dc_perform_sentbox_idle(mailboxPointer)
-        }
-      }
-    }
-
-    if MRConfig.mvboxWatch {
-      DispatchQueue.global(qos: .background).async {
-        while self.state == .running {
-          dc_perform_mvbox_fetch(mailboxPointer)
-          dc_perform_mvbox_idle(mailboxPointer)
-        }
-      }
-    }
-
-    NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
-    do {
-      try reachability.startNotifier()
-    } catch {
-      logger.info("could not start reachability notifier")
-    }
-
-    let info: [DBCustomVariable] = getCoreInfo().map { kv in
-      let value = kv.count > 1 ? kv[1] : ""
-      return DBCustomVariable(name: kv[0], value: value)
-    }
-
-    DBDebugToolkit.add(info)
-  }
-
-  @objc func reachabilityChanged(note: Notification) {
-    let reachability = note.object as! Reachability
-
-    switch reachability.connection {
-    case .wifi, .cellular:
-      logger.info("network: reachable", reachability.connection.description)
-      dc_maybe_network(mailboxPointer)
-
-      let nc = NotificationCenter.default
-      DispatchQueue.main.async {
-        nc.post(name: dcNotificationStateChanged,
-                object: nil,
-                userInfo: ["state": "online"])
-      }
-    case .none:
-      logger.info("network: not reachable")
-      let nc = NotificationCenter.default
-      DispatchQueue.main.async {
-        nc.post(name: dcNotificationStateChanged,
-                object: nil,
-                userInfo: ["state": "offline"])
-      }
-    }
-  }
-
-  // MARK: - BackgroundTask
-
-  func registerBackgroundTask() {
-    logger.info("background task registered")
-    backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
-      self?.endBackgroundTask()
-    }
-    assert(backgroundTask != .invalid)
-  }
-
-  func endBackgroundTask() {
-    logger.info("background task ended")
-    UIApplication.shared.endBackgroundTask(backgroundTask)
-    backgroundTask = .invalid
-  }
-
-  // MARK: - PushNotifications
-
-  func registerForPushNotifications() {
-    UNUserNotificationCenter.current().delegate = self
-
-    UNUserNotificationCenter.current()
-      .requestAuthorization(options: [.alert, .sound, .badge]) {
-        granted, _ in
-        logger.info("permission granted: \(granted)")
-        guard granted else { return }
-        self.getNotificationSettings()
-      }
-  }
-
-  func getNotificationSettings() {
-    UNUserNotificationCenter.current().getNotificationSettings { settings in
-      logger.info("Notification settings: \(settings)")
-    }
-  }
-
-  func userNotificationCenter(_: UNUserNotificationCenter, willPresent _: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
-    logger.info("forground notification")
-    completionHandler([.alert, .sound])
-  }
-
-  func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
-    if response.notification.request.identifier == Constants.notificationIdentifier {
-      logger.info("handling notifications")
-      let userInfo = response.notification.request.content.userInfo
-      let nc = NotificationCenter.default
-      DispatchQueue.main.async {
-        nc.post(
-          name: dcNotificationViewChat,
-          object: nil,
-          userInfo: userInfo
-        )
-      }
-    }
-
-    completionHandler()
-  }
+	static let appCoordinator = AppCoordinator()
+	static var progress: Float = 0
+	static var lastErrorDuringConfig: String?
+	var backgroundTask: UIBackgroundTaskIdentifier = .invalid
+
+	var reachability = Reachability()!
+	var window: UIWindow?
+
+	var state = ApplicationState.stopped
+
+	private func getCoreInfo() -> [[String]] {
+		if let cInfo = dc_get_info(mailboxPointer) {
+			let info = String(cString: cInfo)
+			logger.info(info)
+			return info.components(separatedBy: "\n").map { val in
+				val.components(separatedBy: "=")
+			}
+		}
+
+		return []
+	}
+
+	func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
+		print(url)
+		return true
+	}
+
+	func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
+		DBDebugToolkit.setup()
+		DBDebugToolkit.setupCrashReporting()
+
+		let console = ConsoleDestination()
+		logger.addDestination(console)
+
+		logger.info("launching")
+
+		// Override point for customization after application launch.
+
+		window = UIWindow(frame: UIScreen.main.bounds)
+		guard let window = window else {
+			fatalError("window was nil in app delegate")
+		}
+		// setup deltachat core context
+		//       - second param remains nil (user data for more than one mailbox)
+		mailboxPointer = dc_context_new(callback_ios, nil, "iOS")
+		guard mailboxPointer != nil else {
+			fatalError("Error: dc_context_new returned nil")
+		}
+		open()
+		let isConfigured = dc_is_configured(mailboxPointer) != 0
+		AppDelegate.appCoordinator.setupViewControllers(window: window)
+		UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
+		start()
+		registerForPushNotifications()
+		if !isConfigured {
+			AppDelegate.appCoordinator.presentAccountSetup(animated: false)
+		}
+		return true
+	}
+
+	func application(_: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
+		logger.info("---- background-fetch ----")
+
+		start {
+			// TODO: actually set the right value depending on if we found sth
+			completionHandler(.newData)
+		}
+	}
+
+	func applicationWillEnterForeground(_: UIApplication) {
+		logger.info("---- foreground ----")
+		start()
+	}
+
+	func applicationDidEnterBackground(_: UIApplication) {
+		logger.info("---- background ----")
+
+		reachability.stopNotifier()
+		NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
+
+		maybeStop()
+	}
+
+	func maybeStop() {
+		DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
+			let app = UIApplication.shared
+			logger.info("state: \(app.applicationState) time remaining \(app.backgroundTimeRemaining)")
+
+			if app.applicationState != .background {
+				// only need to do sth in the background
+				return
+			} else if app.backgroundTimeRemaining < 10 {
+				self.stop()
+			} else {
+				self.maybeStop()
+			}
+		}
+	}
+
+	func applicationWillTerminate(_: UIApplication) {
+		logger.info("---- terminate ----")
+		close()
+
+		reachability.stopNotifier()
+		NotificationCenter.default.removeObserver(self, name: .reachabilityChanged, object: reachability)
+	}
+
+	func dbfile() -> String {
+		let paths = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)
+		let documentsPath = paths[0]
+
+		return documentsPath + "/messenger.db"
+	}
+
+	func open() {
+		logger.info("open: \(dbfile())")
+
+		_ = dc_open(mailboxPointer, dbfile(), nil)
+	}
+
+	func stop() {
+		state = .background
+
+		dc_interrupt_imap_idle(mailboxPointer)
+		dc_interrupt_smtp_idle(mailboxPointer)
+		dc_interrupt_mvbox_idle(mailboxPointer)
+		dc_interrupt_sentbox_idle(mailboxPointer)
+	}
+
+	func close() {
+		state = .stopped
+
+		dc_close(mailboxPointer)
+		mailboxPointer = nil
+	}
+
+	func start(_ completion: (() -> Void)? = nil) {
+		logger.info("---- start ----")
+
+		if state == .running {
+			return
+		}
+
+		state = .running
+
+		DispatchQueue.global(qos: .background).async {
+			self.registerBackgroundTask()
+			while self.state == .running {
+				dc_perform_imap_jobs(mailboxPointer)
+				dc_perform_imap_fetch(mailboxPointer)
+				dc_perform_imap_idle(mailboxPointer)
+			}
+			if self.backgroundTask != .invalid {
+				completion?()
+				self.endBackgroundTask()
+			}
+		}
+
+		DispatchQueue.global(qos: .utility).async {
+			self.registerBackgroundTask()
+			while self.state == .running {
+				dc_perform_smtp_jobs(mailboxPointer)
+				dc_perform_smtp_idle(mailboxPointer)
+			}
+			if self.backgroundTask != .invalid {
+				self.endBackgroundTask()
+			}
+		}
+
+		if MRConfig.sentboxWatch {
+			DispatchQueue.global(qos: .background).async {
+				while self.state == .running {
+					dc_perform_sentbox_fetch(mailboxPointer)
+					dc_perform_sentbox_idle(mailboxPointer)
+				}
+			}
+		}
+
+		if MRConfig.mvboxWatch {
+			DispatchQueue.global(qos: .background).async {
+				while self.state == .running {
+					dc_perform_mvbox_fetch(mailboxPointer)
+					dc_perform_mvbox_idle(mailboxPointer)
+				}
+			}
+		}
+
+		NotificationCenter.default.addObserver(self, selector: #selector(reachabilityChanged(note:)), name: .reachabilityChanged, object: reachability)
+		do {
+			try reachability.startNotifier()
+		} catch {
+			logger.info("could not start reachability notifier")
+		}
+
+		let info: [DBCustomVariable] = getCoreInfo().map { kv in
+			let value = kv.count > 1 ? kv[1] : ""
+			return DBCustomVariable(name: kv[0], value: value)
+		}
+
+		DBDebugToolkit.add(info)
+	}
+
+	@objc func reachabilityChanged(note: Notification) {
+		let reachability = note.object as! Reachability
+
+		switch reachability.connection {
+		case .wifi, .cellular:
+			logger.info("network: reachable", reachability.connection.description)
+			dc_maybe_network(mailboxPointer)
+
+			let nc = NotificationCenter.default
+			DispatchQueue.main.async {
+				nc.post(name: dcNotificationStateChanged,
+						object: nil,
+						userInfo: ["state": "online"])
+			}
+		case .none:
+			logger.info("network: not reachable")
+			let nc = NotificationCenter.default
+			DispatchQueue.main.async {
+				nc.post(name: dcNotificationStateChanged,
+						object: nil,
+						userInfo: ["state": "offline"])
+			}
+		}
+	}
+
+	// MARK: - BackgroundTask
+
+	func registerBackgroundTask() {
+		logger.info("background task registered")
+		backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
+			self?.endBackgroundTask()
+		}
+		assert(backgroundTask != .invalid)
+	}
+
+	func endBackgroundTask() {
+		logger.info("background task ended")
+		UIApplication.shared.endBackgroundTask(backgroundTask)
+		backgroundTask = .invalid
+	}
+
+	// MARK: - PushNotifications
+
+	func registerForPushNotifications() {
+		UNUserNotificationCenter.current().delegate = self
+
+		UNUserNotificationCenter.current()
+			.requestAuthorization(options: [.alert, .sound, .badge]) {
+				granted, _ in
+				logger.info("permission granted: \(granted)")
+				guard granted else { return }
+				self.getNotificationSettings()
+		}
+	}
+
+	func getNotificationSettings() {
+		UNUserNotificationCenter.current().getNotificationSettings { settings in
+			logger.info("Notification settings: \(settings)")
+		}
+	}
+
+	func userNotificationCenter(_: UNUserNotificationCenter, willPresent _: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
+		logger.info("forground notification")
+		completionHandler([.alert, .sound])
+	}
+
+	func userNotificationCenter(_: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
+		if response.notification.request.identifier == Constants.notificationIdentifier {
+			logger.info("handling notifications")
+			let userInfo = response.notification.request.content.userInfo
+			let nc = NotificationCenter.default
+			DispatchQueue.main.async {
+				nc.post(
+					name: dcNotificationViewChat,
+					object: nil,
+					userInfo: userInfo
+				)
+			}
+		}
+
+		completionHandler()
+	}
 }

+ 22 - 0
deltachat-ios/Wrapper.swift

@@ -686,6 +686,28 @@ class MRConfig {
         sf = sf | flags
         serverFlags = sf
     }
+
+	// returns one of DC_LP_IMAP_SOCKET_STARTTLS, DC_LP_IMAP_SOCKET_SSL,
+	class func getImapSecurity() -> Int {
+		var sf = serverFlags
+		sf = sf & 0x700
+		return sf
+	}
+
+	// returns one of DC_LP_SMTP_SOCKET_STARTTLS, DC_LP_SMTP_SOCKET_SSL,
+	class func getSmtpSecurity() -> Int {
+		var sf = serverFlags
+		sf = sf & 0x70000
+		return sf
+	}
+
+	// returns on of DC_LP_AUTH_OAUTH2 or 0
+	class func getAuthFlags() -> Int {
+		var sf = serverFlags
+		sf = sf & 0x6
+		serverFlags = sf
+		return sf
+	}
     
   /**
    * Own name to use when sending messages. MUAs are allowed to spread this way eg. using CC, defaults to empty