pEpForiOS/UI/Settings/Setting/AccountSettings/ViewModel/AccountSettingsViewModel.swift
author Dirk Zimmermann <dz@pep.security>
Wed, 24 Apr 2019 12:43:18 +0200
branchIOS-1542
changeset 8360 5fc3e8979152
parent 8358 e663f13a57cc
child 8361 fa62d14b2f33
permissions -rw-r--r--
IOS-1542 Set SMTP data.
andreas@3697
     1
//
andreas@3697
     2
//  AccountSettingsViewModel.swift
andreas@3697
     3
//  pEpForiOS
andreas@3697
     4
//
andreas@3697
     5
//  Created by Xavier Algarra on 13/06/2017.
andreas@3697
     6
//  Copyright © 2017 p≡p Security S.A. All rights reserved.
andreas@3697
     7
//
andreas@3697
     8
andreas@3697
     9
import Foundation
andreas@3697
    10
import MessageModel
xavier@7644
    11
import pEpIOSToolbox
andreas@3697
    12
dz@8358
    13
import PantomimeFramework
dz@8358
    14
andreas@3697
    15
public class AccountSettingsViewModel {
andreas@3697
    16
    public struct ServerViewModel {
andreas@3697
    17
        var address: String?
andreas@3697
    18
        var port: String?
andreas@3697
    19
        var transport: String?
andreas@3697
    20
    }
andreas@3697
    21
andreas@3697
    22
    public struct SecurityViewModel {
andreas@3697
    23
        var options = Server.Transport.toArray()
andreas@3697
    24
        var size : Int {
andreas@3697
    25
            get {
andreas@3697
    26
                return options.count
andreas@3697
    27
            }
andreas@3697
    28
        }
andreas@3697
    29
andreas@3697
    30
        subscript(option: Int) -> String {
andreas@3697
    31
            get {
andreas@3697
    32
                return options[option].asString()
andreas@3697
    33
            }
andreas@3697
    34
        }
andreas@3697
    35
    }
andreas@3697
    36
andreas@3697
    37
    private let headers = [
andreas@3697
    38
        NSLocalizedString("Account", comment: "Account settings"),
andreas@3697
    39
        NSLocalizedString("IMAP Settings", comment: "Account settings title IMAP"),
andreas@3697
    40
        NSLocalizedString("SMTP Settings", comment: "Account settings title SMTP")
andreas@3697
    41
    ]
andreas@3697
    42
    private var controlWord = "noRealPassword"
andreas@3697
    43
andreas@3697
    44
    public let svm = SecurityViewModel()
dirk@4019
    45
    public let isOAuth2: Bool
andreas@3697
    46
andreas@3697
    47
    public init(account: Account) {
dz@8354
    48
        // We are using a copy of the data here.
dz@8354
    49
        // The outside world must not know changed settings until they have been verified.
dirk@4019
    50
        isOAuth2 = account.server(with: .imap)?.authMethod == AuthMethod.saslXoauth2.rawValue
dz@8354
    51
        self.email = account.user.address
dz@8354
    52
        self.loginName = account.server(with: .imap)?.credentials.loginName ?? ""
dz@8354
    53
        self.name = account.user.userName ?? ""
andreas@3697
    54
dz@8354
    55
        if let server = account.smtpServer {
dz@8354
    56
            self.smtpServer = ServerViewModel(
dz@8354
    57
                address: server.address,
dz@8354
    58
                port: "\(server.port)",
dz@8354
    59
                transport: server.transport?.asString())
dz@8354
    60
        } else {
dz@8354
    61
            self.smtpServer = ServerViewModel()
dz@8354
    62
        }
dz@8354
    63
dz@8354
    64
        if let server = account.imapServer {
dz@8354
    65
            self.imapServer = ServerViewModel(
dz@8354
    66
                address: server.address,
dz@8354
    67
                port: "\(server.port)",
dz@8354
    68
                transport: server.transport?.asString())
dz@8354
    69
        } else {
dz@8354
    70
            self.imapServer = ServerViewModel()
andreas@3697
    71
        }
andreas@3697
    72
    }
andreas@3697
    73
dz@8356
    74
    private(set) var email: String
dz@8354
    75
dz@8353
    76
    /// - Note: The email model is based on the assumption that imap.loginName == smtp.loginName
dz@8356
    77
    private(set) var loginName: String
andreas@3697
    78
dz@8356
    79
    private(set) var name: String
andreas@3697
    80
dz@8356
    81
    private(set) var smtpServer: ServerViewModel
andreas@3697
    82
dz@8356
    83
    private(set) var imapServer: ServerViewModel
andreas@3697
    84
agp@8142
    85
    var verificationService: VerificationService?
andreas@3697
    86
    weak var delegate: AccountVerificationResultDelegate?
andreas@3697
    87
dz@8357
    88
    /// Holding both the data of the current account in verification,
dz@8357
    89
    /// and also the implementation of the verification.
dz@8357
    90
    private var verifiableAccount: VerifiableAccountProtocol?
dz@8357
    91
dz@8354
    92
    // Currently we assume imap and smtp servers exist already (update).
dz@8354
    93
    // If we run into problems here modify to updateOrCreate.
andreas@3697
    94
    func update(loginName: String, name: String, password: String? = nil, imap: ServerViewModel,
andreas@3697
    95
                smtp: ServerViewModel) {
dz@8358
    96
        var theVerifier = verifiableAccount ?? VerifiableAccount()
dz@8358
    97
        verifiableAccount = theVerifier
dz@8358
    98
dz@8358
    99
        theVerifier.address = email
dz@8358
   100
        theVerifier.userName = name
dz@8358
   101
        theVerifier.password = password
dz@8358
   102
        if loginName != email {
dz@8358
   103
            theVerifier.loginName = loginName
dz@8358
   104
        }
dz@8358
   105
dz@8358
   106
        if isOAuth2 {
dz@8358
   107
            // TODO: Set correct auth method, etc.
dz@8358
   108
        }
dz@8358
   109
dz@8360
   110
        // IMAP
dz@8358
   111
        theVerifier.serverIMAP = imap.address
dz@8358
   112
        if let portString = imap.port, let port = UInt16(portString) {
dz@8358
   113
            theVerifier.portIMAP = port
dz@8358
   114
        }
dz@8360
   115
        if let transport = Server.Transport(fromString: imap.transport) {
dz@8360
   116
            theVerifier.transportIMAP = ConnectionTransport.init(transport: transport)
dz@8360
   117
        }
dz@8360
   118
dz@8360
   119
        // SMTP
dz@8360
   120
        theVerifier.serverSMTP = smtp.address
dz@8360
   121
        if let portString = smtp.port, let port = UInt16(portString) {
dz@8360
   122
            theVerifier.portSMTP = port
dz@8360
   123
        }
dz@8360
   124
        if let transport = Server.Transport(fromString: smtp.transport) {
dz@8360
   125
            theVerifier.transportSMTP = ConnectionTransport.init(transport: transport)
dz@8360
   126
        }
dz@8358
   127
dz@8354
   128
        // TODO: Implement
dz@8354
   129
        /*
andreas@3697
   130
        guard let serverImap = account.imapServer,
andreas@3697
   131
            let serverSmtp = account.smtpServer else {
dz@7517
   132
                Logger.frontendLogger.errorAndCrash("Account misses imap or smtp server.")
andreas@3697
   133
                return
andreas@3697
   134
        }
andreas@3697
   135
        let pass : String?
andreas@3697
   136
        if let p = password {
andreas@3697
   137
            pass = p
andreas@3697
   138
        } else {
andreas@3697
   139
            pass = serverImap.credentials.password
andreas@3697
   140
        }
andreas@3697
   141
        guard let editedServerImap = server(from: imap, serverType: .imap,
andreas@3697
   142
                                            loginName: loginName,
andreas@3697
   143
                                            password: pass,
andreas@3697
   144
                                            key: serverImap.credentials.key),
andreas@3697
   145
            let editedServerSmtp = server(from: smtp,
andreas@3697
   146
                                          serverType: .smtp,
andreas@3697
   147
                                          loginName: loginName,
andreas@3697
   148
                                          password: pass,
andreas@3697
   149
                                          key: serverSmtp.credentials.key)
andreas@3697
   150
            else {
dz@7517
   151
                Logger.frontendLogger.errorAndCrash("Invalid input.")
andreas@3697
   152
                return
andreas@3697
   153
        }
andreas@3697
   154
andreas@3697
   155
        serverImap.updateValues(with: editedServerImap)
andreas@3697
   156
        serverSmtp.updateValues(with: editedServerSmtp)
andreas@3697
   157
andreas@3697
   158
        self.account.user.userName = name
andreas@3697
   159
agp@8142
   160
        guard let verificationService = verificationService else {
agp@8142
   161
            Logger.frontendLogger.errorAndCrash("no VerificationService")
andreas@3697
   162
            return
andreas@3697
   163
        }
agp@8142
   164
        verificationService.requestVerification(account: account, delegate: self)
dz@8354
   165
         */
andreas@3697
   166
    }
andreas@3697
   167
andreas@3697
   168
    func sectionIsValid(section: Int) -> Bool {
borja@6875
   169
        return section >= 0 && section < headers.count
andreas@3697
   170
    }
andreas@3697
   171
andreas@3697
   172
    var count: Int {
andreas@3697
   173
        get {
andreas@3697
   174
            return headers.count
andreas@3697
   175
        }
andreas@3697
   176
    }
andreas@3697
   177
andreas@3697
   178
    subscript(section: Int) -> String {
andreas@3697
   179
        get {
andreas@3697
   180
            assert(sectionIsValid(section: section), "Section out of range")
andreas@3697
   181
            return headers[section]
andreas@3697
   182
        }
andreas@3697
   183
    }
andreas@3697
   184
andreas@3697
   185
    private func server(from viewModel:ServerViewModel, serverType:Server.ServerType,
andreas@3697
   186
                        loginName: String, password: String?, key: String? = nil) -> Server? {
andreas@3697
   187
        guard let viewModelPort = viewModel.port,
andreas@3697
   188
            let port = UInt16(viewModelPort),
andreas@3697
   189
            let address = viewModel.address else {
dz@7517
   190
                Logger.frontendLogger.errorAndCrash("viewModel misses required data.")
andreas@3697
   191
                return nil
andreas@3697
   192
        }
andreas@5601
   193
        let transport = Server.Transport(fromString: viewModel.transport)
andreas@3697
   194
andreas@3697
   195
        let credentials = ServerCredentials.create(loginName: loginName, key: key)
andreas@3697
   196
        if password != nil && password != "" {
andreas@3697
   197
            credentials.password = password
andreas@3697
   198
        }
andreas@3697
   199
andreas@3697
   200
        let server = Server.create(serverType: serverType, port: port, address: address,
andreas@3697
   201
                                   transport: transport, credentials: credentials)
andreas@3697
   202
andreas@3697
   203
        return server
andreas@3697
   204
    }
andreas@3697
   205
dirk@4052
   206
    func updateToken(accessToken: OAuth2AccessTokenProtocol) {
dz@8354
   207
        // TODO: What to do here? When does this get called?
dz@8354
   208
        /*
dirk@4053
   209
        guard let imapServer = account.imapServer,
dirk@4053
   210
            let smtpServer = account.smtpServer else {
dirk@4053
   211
                return
dirk@4052
   212
        }
dirk@4053
   213
        let password = accessToken.persistBase64Encoded()
dirk@4053
   214
        imapServer.credentials.password = password
dirk@4053
   215
        smtpServer.credentials.password = password
dz@8354
   216
         */
dirk@4052
   217
    }
andreas@3697
   218
}
andreas@3697
   219
andreas@5601
   220
// MARK: - AccountVerificationServiceDelegate
andreas@5601
   221
andreas@3697
   222
extension AccountSettingsViewModel: AccountVerificationServiceDelegate {
dz@7999
   223
    public func verified(account: Account,
andreas@5601
   224
                  service: AccountVerificationServiceProtocol,
andreas@3697
   225
                  result: AccountVerificationResult) {
andreas@5601
   226
        if result == .ok {
dz@7914
   227
            MessageModelUtil.performAndWait {
andreas@5601
   228
                account.save()
andreas@5601
   229
            }
andreas@5601
   230
        }
andreas@5601
   231
        GCD.onMainWait { [weak self] in
andreas@5601
   232
            guard let me = self else {
dz@7517
   233
                Logger.frontendLogger.lostMySelf()
andreas@5601
   234
                return
andreas@5601
   235
            }
andreas@5601
   236
            me.delegate?.didVerify(result: result, accountInput: nil)
andreas@3697
   237
        }
andreas@3697
   238
    }
andreas@3697
   239
}