diff --git a/enzevalos_iphone/MailHandler.swift b/enzevalos_iphone/MailHandler.swift index 0dc397f21207a93db7c6d0b16712ecf3d2b23d1c..f1d27d9876b88eb2e8a3f03d0b3d9e6deaee78a0 100644 --- a/enzevalos_iphone/MailHandler.swift +++ b/enzevalos_iphone/MailHandler.swift @@ -467,7 +467,7 @@ class MailHandler { let userAgent = header?.userAgent var references = [String]() var msgParser = parser - + // 1. parse header if header?.from == nil { // Drops mails with no from field. Otherwise it becomes ugly with no ezcontact,fromadress etc. @@ -512,6 +512,7 @@ class MailHandler { secretKey = sk } } + body = msgParser.plainTextBodyRenderingAndStripWhitespace(false) if isEnc { html = msgParser.plainTextRendering() lineArray = html.components(separatedBy: "\n") @@ -522,8 +523,7 @@ class MailHandler { dec = decryptText(body: body, from: header?.from, autocrypt: autocrypt) if (dec?.plaintext != nil) { msgParser = MCOMessageParser(data: dec?.decryptedData) - body = msgParser.plainTextBodyRendering().removeNewLines() - body = body.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) + body = parseBody(msgParser: msgParser) for a in (msgParser.attachments())! { let at = a as! MCOAttachment newKeyIds.append(contentsOf: parsePublicKeys(attachment: at)) @@ -534,8 +534,7 @@ class MailHandler { } } } else { - body = msgParser.plainTextBodyRendering().removeNewLines() - body = body.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) + body = parseBody(msgParser: msgParser) if let chipher = findInlinePGP(text: msgParser.plainTextRendering()) { dec = decryptText(body: chipher, from: header?.from, autocrypt: autocrypt) if dec != nil { @@ -566,6 +565,25 @@ class MailHandler { return nil } + + private func parseBody(msgParser: MCOMessageParser) -> String { + if let text = msgParser.plainTextBodyRenderingAndStripWhitespace(false) { + var body = text + var c = body.first + while (c != nil && CharacterSet.whitespacesAndNewlines.contains((c?.unicodeScalars.first)!)) { + body.removeFirst() + c = body.first + } + c = body.last + while (c != nil && CharacterSet.whitespacesAndNewlines.contains((c?.unicodeScalars.first)!)) { + body.removeLast() + c = body.last + } + return body + } + return "" + } + private func findInlinePGP(text: String) -> String? { var range = text.range(of: "-----BEGIN PGP MESSAGE-----") if let lower = range?.lowerBound { diff --git a/enzevalos_iphoneTests/MailTest.swift b/enzevalos_iphoneTests/MailTest.swift index 04b85e4668423cfc0b817c278f1d0772fc9c73ed..319fdd85fec529b1bf93bdc31247276a2b215fc7 100644 --- a/enzevalos_iphoneTests/MailTest.swift +++ b/enzevalos_iphoneTests/MailTest.swift @@ -101,6 +101,42 @@ class MailTest: XCTestCase { } } + + func testFormatMail() { + // Init + let tos = ["to1@example.com", "to2@example.com"] + let ccs = ["cc1@example.com"] + let bccs = ["bcc1@example.com"] + let subject = "subject" + let body = """ + Another at: + + https://www.fu-berlin.de + + Found that one because I was submitting the same URL from a phishing email. + + See: mi.fu-berlin.de + For host name format of Azure static web sites. + https certs will be valid – Microsoft signed. + """ + let outMail = OutgoingMail(toEntrys: tos, ccEntrys: ccs, bccEntrys: bccs, subject: subject, textContent: body, htmlContent: nil) + if let parser = MCOMessageParser(data: outMail.plainData) { + // Test parsing! + if let mail = mailHandler.parseMail(parser: parser, record: nil, folderPath: "INBOX", uid: 0, flags: MCOMessageFlag.seen){ + XCTAssertEqual(subject, mail.subject) + XCTAssertEqual(body, mail.body) + } + else { + XCTFail() + } + } + else { + XCTFail() + } + } + + + func testSecureMailCreation() { let encAdr = "enc@example.com" let subject = "subject" @@ -210,7 +246,7 @@ class MailTest: XCTestCase { XCTAssertEqual(mail.sigState, sigState) } if let body = mail.body { - XCTAssertEqual(body.removeNewLines(), MailTest.body.removeNewLines()) + XCTAssertEqual(body, MailTest.body) } else { XCTFail() diff --git a/enzevalos_iphoneTests/testMails/plainThunderbird.eml b/enzevalos_iphoneTests/testMails/plainThunderbird.eml index f016d94f0a3e915922c3a6e60b6c0d1c473da75f..842c1dfa3055cad8cda6ac2d18af20bfab14e446 100644 --- a/enzevalos_iphoneTests/testMails/plainThunderbird.eml +++ b/enzevalos_iphoneTests/testMails/plainThunderbird.eml @@ -54,17 +54,5 @@ Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US -Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque dapibus -id diam ac volutpat. Sed quis cursus ante. Vestibulum eget gravida -felis. Nullam accumsan diam quis sem ornare lacinia. Aenean risus risus, -maximus quis faucibus et, maximus at nunc. Duis pharetra augue libero, -et congue diam varius eget. Nullam efficitur ex purus, non accumsan -tellus laoreet hendrerit. Suspendisse gravida interdum eros, eu -venenatis ante suscipit nec. Class aptent taciti sociosqu ad litora -torquent per conubia nostra, per inceptos himenaeos. Praesent -pellentesque cursus sem, non ornare nunc commodo vel. Praesent sed magna -at ligula ultricies sagittis malesuada non est. Nam maximus varius -mauris. Etiam dignissim congue ligula eu porta. Nunc rutrum nisl id -mauris efficitur ultrices. Maecenas sit amet velit ac mauris consequat -sagittis at et lorem. +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque dapibus id diam ac volutpat. Sed quis cursus ante. Vestibulum eget gravida felis. Nullam accumsan diam quis sem ornare lacinia. Aenean risus risus, maximus quis faucibus et, maximus at nunc. Duis pharetra augue libero, et congue diam varius eget. Nullam efficitur ex purus, non accumsan tellus laoreet hendrerit. Suspendisse gravida interdum eros, eu venenatis ante suscipit nec. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Praesent pellentesque cursus sem, non ornare nunc commodo vel. Praesent sed magna at ligula ultricies sagittis malesuada non est. Nam maximus varius mauris. Etiam dignissim congue ligula eu porta. Nunc rutrum nisl id mauris efficitur ultrices. Maecenas sit amet velit ac mauris consequat sagittis at et lorem.