From 33fc476ccaea3873d1be39319e4bfb2966f7c29f Mon Sep 17 00:00:00 2001
From: Oliver Wiese <oliver.wiese@fu-berlin.de>
Date: Tue, 23 Oct 2018 13:26:18 +0200
Subject: [PATCH] update setup message

---
 enzevalos_iphone/ExportViewController.swift |  7 ++--
 enzevalos_iphone/Mail.swift                 |  3 +-
 enzevalos_iphone/MailHandler.swift          | 36 ++++++++++++++++++---
 enzevalos_iphone/ReadViewController.swift   |  2 +-
 4 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/enzevalos_iphone/ExportViewController.swift b/enzevalos_iphone/ExportViewController.swift
index d28d221b..7320da58 100644
--- a/enzevalos_iphone/ExportViewController.swift
+++ b/enzevalos_iphone/ExportViewController.swift
@@ -48,13 +48,13 @@ class ExportViewController: UITableViewController {
                     if let message = pgp.exportKey(id: keyId, isSecretkey: true, autocrypt: true, newPasscode: true) {
                         passcode = pgp.loadExportPasscode(id: keyId)!
                         let mailHandler = AppDelegate.getAppDelegate().mailHandler
-                        mailHandler.sendSecretKey(key: message, passcode: passcode, callback: mailSend)
+                        mailHandler.sendSecretKey(keyID: keyId, key: message, passcode: passcode, callback: mailSend)
                     }
                 } else {
                     if let message = pgp.exportKey(id: keyId, isSecretkey: true, autocrypt: true, newPasscode: false) {
                         passcode = pgp.loadExportPasscode(id: keyId)!
                         let mailHandler = AppDelegate.getAppDelegate().mailHandler
-                        mailHandler.sendSecretKey(key: message, passcode: passcode, callback: mailSend)
+                        mailHandler.sendSecretKey(keyID: keyId, key: message, passcode: passcode, callback: mailSend)
                     }
                     alreadySent = true
                 }
@@ -105,8 +105,9 @@ class ExportViewController: UITableViewController {
             } else if indexPath.row == 1 {
                 let cell = tableView.dequeueReusableCell(withIdentifier: "ExportSendButtonCell") as! ExportSendButtonCell
                 if alreadySent {
-                    cell.sendButton.setTitle(NSLocalizedString("DeleteCode", comment: "delete (pass-)code, which was used to symmetrically encrypt the secret key"), for: UIControlState.normal) //geht besser...
+                    cell.sendButton.setTitle(NSLocalizedString("DeleteCode", comment: "delete (pass-)code, which was used to symmetrically encrypt the secret key"), for: UIControlState.normal)
                     cell.sendButton.setTitleColor(UIColor.red, for: .normal)
+                    
                     //TODO: delete code from keychain
                 } else {
                     cell.sendButton.setTitle(NSLocalizedString("Send", comment: "send mail with secret key attached"), for: .normal)
diff --git a/enzevalos_iphone/Mail.swift b/enzevalos_iphone/Mail.swift
index 8f35b0f2..c05a9485 100644
--- a/enzevalos_iphone/Mail.swift
+++ b/enzevalos_iphone/Mail.swift
@@ -34,7 +34,8 @@ public protocol Mail: Comparable {
 }
 
 public func == <T: Mail> (lhs: T, rhs: T) -> Bool {
-    return lhs.date == rhs.date && lhs.uid == rhs.uid //TODO: update see: https://www.limilabs.com/blog/unique-id-in-imap-protocol
+    return lhs.date == rhs.date && lhs.uid == rhs.uid
+    //TODO: update see: https://www.limilabs.com/blog/unique-id-in-imap-protocol
 }
 
 public func << T: Mail > (lhs: T, rhs: T) -> Bool {
diff --git a/enzevalos_iphone/MailHandler.swift b/enzevalos_iphone/MailHandler.swift
index 19b3c982..4982e28d 100644
--- a/enzevalos_iphone/MailHandler.swift
+++ b/enzevalos_iphone/MailHandler.swift
@@ -236,26 +236,52 @@ class MailHandler {
         return ids
     }
 
-    func sendSecretKey(key: String, passcode: String, callback: @escaping (Error?) -> Void) {
+    func sendSecretKey(keyID: String, key: String, passcode: String, callback: @escaping (Error?) -> Void) {
         let useraddr = (UserManager.loadUserValue(Attribute.userAddr) as! String)
         let session = createSMTPSession()
         let builder = MCOMessageBuilder()
         let userID: MCOAddress = MCOAddress(displayName: useraddr, mailbox: useraddr)
 
         createHeader(builder, toEntrys: [useraddr], ccEntrys: [], bccEntrys: [], subject: "Autocrypt Setup Message")
-        builder.header.setExtraHeaderValue("v0", forName: SETUPMESSAGE)
+        builder.header.setExtraHeaderValue("v1", forName: SETUPMESSAGE)
 
 
         builder.addAttachment(MCOAttachment.init(text: "This message contains a secret for reading secure mails on other devices. \n 1) Input the passcode from your smartphone to unlock the message on your other device. \n 2) Import the secret key into your pgp program on the device.  \n\n For more information visit:https://userpage.fu-berlin.de/letterbox/faq.html#otherDevices \n\n"))
 
         // See: https://autocrypt.org/level1.html#autocrypt-setup-message
-        let keyAttachment = MCOAttachment.init(text: key)
-        builder.addAttachment(keyAttachment)
+        let filename = keyID+".asc.asc"
+        if let keyAttachment = MCOAttachment.init(contentsOfFile: filename){
+            keyAttachment.mimeType = "application/autocrypt-setup"
+            keyAttachment.setContentTypeParameterValue("UTF-8", forName: "charset")
+            keyAttachment.setContentTypeParameterValue(filename, forName: "name")
+            keyAttachment.filename = filename
+            keyAttachment.data = key.data(using: .utf8)
+
+            builder.addAttachment(keyAttachment)
+
+        }
+        /*
+        if let keyAttachment = MCOAttachment.init(contentsOfFile: filename){
+            keyAttachment.mimeType = "text/plain"
+            keyAttachment.setContentTypeParameterValue("UTF-8", forName: "charset")
+            keyAttachment.setContentTypeParameterValue(filename, forName: "name")
+            keyAttachment.data = key.data(using: .utf8)
+            keyAttachment.filename = filename
+
+            builder.addAttachment(keyAttachment)
+
+        }
+  */
+         if let keyAttachment = MCOAttachment.init(text: key){
+            builder.addAttachment(keyAttachment)
+        }
+        
+        
 
         let sendOperation = session.sendOperation(with: builder.data(), from: userID, recipients: [userID])
         sendOperation?.start({ error in
             guard error == nil else {
-                self.errorhandling(error: error, originalCall: {self.sendSecretKey(key: key, passcode: passcode, callback: callback)}, completionCallback: nil)
+                self.errorhandling(error: error, originalCall: {self.sendSecretKey(keyID: keyID, key: key, passcode: passcode, callback: callback)}, completionCallback: nil)
                 return
             }
             callback(nil)
diff --git a/enzevalos_iphone/ReadViewController.swift b/enzevalos_iphone/ReadViewController.swift
index d7db0433..88d36cac 100644
--- a/enzevalos_iphone/ReadViewController.swift
+++ b/enzevalos_iphone/ReadViewController.swift
@@ -566,7 +566,7 @@ class ReadViewController: UITableViewController {
 
                 let answerTo = sender as? String ?? "" // TODO: Convert String into MailAddress(?)
 
-                let answerMail = EphemeralMail(to: NSSet.init(array: [answerTo]), cc: NSSet.init(array: []), bcc: [], date: Date(), subject: "", body: "", uid: 0, predecessor: nil) // TODO: are these the best values?
+                let answerMail = EphemeralMail(to: NSSet.init(array: [answerTo]), cc: NSSet.init(array: []), bcc: [], date: Date(), subject: "", body: "", uid: 0, predecessor: nil) 
 
                 controller.sendViewDelegate = self
                 controller.prefilledMail = answerMail
-- 
GitLab