Jelajahi Sumber

Merge pull request #277 from deltachat/receive-asm

handling received autocrypt-setup-messages
cyBerta 5 tahun lalu
induk
melakukan
53d41ef8c3

+ 42 - 2
deltachat-ios/Controller/ChatViewController.swift

@@ -807,8 +807,9 @@ extension ChatViewController: MessageCellDelegate {
     @objc func didTapMessage(in cell: MessageCollectionViewCell) {
         if let indexPath = messagesCollectionView.indexPath(for: cell) {
             let message = messageList[indexPath.section]
-
-            if let url = message.fileURL {
+            if message.isSetupMessage {
+                didTapAsm(msg: message, orgText: "")
+            } else if let url = message.fileURL {
                 // find all other messages with same message type
                 var previousUrls: [URL] = []
                 var nextUrls: [URL] = []
@@ -839,6 +840,45 @@ extension ChatViewController: MessageCellDelegate {
         }
     }
 
+    private func didTapAsm(msg: DcMsg, orgText: String) {
+        let inputDlg = UIAlertController(
+            title: String.localized("autocrypt_continue_transfer_title"),
+            message: String.localized("autocrypt_continue_transfer_please_enter_code"),
+            preferredStyle: .alert)
+        inputDlg.addTextField(configurationHandler: { (textField) in
+            textField.placeholder = msg.setupCodeBegin + ".."
+            textField.text = orgText
+            textField.keyboardType = UIKeyboardType.numbersAndPunctuation // allows entering spaces; decimalPad would require a mask to keep things readable
+        })
+        inputDlg.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
+
+        let okAction = UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
+            let textField = inputDlg.textFields![0]
+            let modText = textField.text ?? ""
+            let success = self.dcContext.continueKeyTransfer(msgId: msg.id, setupCode: modText)
+
+            let alert = UIAlertController(
+                title: String.localized("autocrypt_continue_transfer_title"),
+                message: String.localized(success ? "autocrypt_continue_transfer_succeeded" : "autocrypt_bad_setup_code"),
+                preferredStyle: .alert)
+            if success {
+                alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
+            } else {
+                alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
+                let retryAction = UIAlertAction(title: String.localized("autocrypt_continue_transfer_retry"), style: .default, handler: { _ in
+                    self.didTapAsm(msg: msg, orgText: modText)
+                })
+                alert.addAction(retryAction)
+                alert.preferredAction = retryAction
+            }
+            self.navigationController?.present(alert, animated: true, completion: nil)
+        })
+
+        inputDlg.addAction(okAction)
+        inputDlg.preferredAction = okAction // without setting preferredAction, cancel become shown *bold* as the preferred action
+        navigationController?.present(inputDlg, animated: true, completion: nil)
+    }
+
     @objc func didTapAvatar(in _: MessageCollectionViewCell) {
         logger.info("Avatar tapped")
     }

+ 17 - 0
deltachat-ios/DC/Wrapper.swift

@@ -69,6 +69,10 @@ class DcContext {
         }
         return nil
     }
+
+    func continueKeyTransfer(msgId: Int, setupCode: String) -> Bool {
+        return dc_continue_key_transfer(self.contextPointer, UInt32(msgId), setupCode) != 0
+    }
 }
 
 class DcConfig {
@@ -453,6 +457,8 @@ class DcMsg: MessageType {
                 NSAttributedString.Key.foregroundColor: UIColor.darkGray,
                 ])
             return MessageKind.attributedText(text)
+        } else if isSetupMessage {
+            return MessageKind.text(String.localized("autocrypt_asm_click_body"))
         }
 
         let text = self.text ?? ""
@@ -626,6 +632,17 @@ class DcMsg: MessageType {
         return dc_msg_is_info(messagePointer) == 1
     }
 
+    var isSetupMessage: Bool {
+        return dc_msg_is_setupmessage(messagePointer) == 1
+    }
+
+    var setupCodeBegin: String {
+        guard let cString = dc_msg_get_setupcodebegin(messagePointer) else { return "" }
+        let swiftString = String(cString: cString)
+        free(cString)
+        return swiftString
+    }
+
     func summary(chars: Int) -> String? {
         guard let cString = dc_msg_get_summarytext(messagePointer, Int32(chars)) else { return nil }
         let swiftString = String(cString: cString)