From 3f1aea5ebb15df943a482ebc77dad82b3098c1df Mon Sep 17 00:00:00 2001 From: Felix Hammerl Date: Tue, 25 Feb 2014 19:18:37 +0100 Subject: [PATCH] integrate mailreader --- Gruntfile.js | 13 +++++++------ package.json | 3 ++- src/js/app-controller.js | 5 +++-- src/js/dao/email-dao.js | 9 +++++---- test/new-unit/email-dao-test.js | 15 +++++++-------- 5 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 09e9eb9..6f4f542 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -133,12 +133,13 @@ module.exports = function(grunt) { 'imap-client/node_modules/inbox/node_modules/node-shims/src/*.js', 'imap-client/node_modules/inbox/node_modules/utf7/src/utf7.js', 'imap-client/node_modules/inbox/node_modules/xoauth2/src/xoauth2.js', - 'imap-client/node_modules/mimelib/src/mimelib.js', - 'imap-client/node_modules/mimelib/node_modules/addressparser/src/addressparser.js', - 'imap-client/node_modules/mimelib/node_modules/encoding/src/encoding.js', - 'imap-client/node_modules/mimelib/node_modules/encoding/node_modules/iconv-lite/src/*.js', - 'imap-client/node_modules/mailparser/src/*.js', - 'imap-client/node_modules/mime/src/mime.js', + 'mailreader/src/*.js', + 'mailreader/node_modules/mailparser/src/*.js', + 'mailreader/node_modules/mailparser/node_modules/encoding/src/encoding.js', + 'mailreader/node_modules/mailparser/node_modules/mimelib/src/mimelib.js', + 'mailreader/node_modules/mailparser/node_modules/mimelib/node_modules/addressparser/src/addressparser.js', + 'mailreader/node_modules/mailparser/node_modules/encoding/node_modules/iconv-lite/src/*.js', + 'mailreader/node_modules/mailparser/node_modules/mime/src/mime.js', 'pgpmailer/src/*.js', 'pgpmailer/node_modules/simplesmtp/src/*', 'pgpbuilder/src/*.js', diff --git a/package.json b/package.json index 4e381c7..ff22afc 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ }, "dependencies": { "crypto-lib": "https://github.com/whiteout-io/crypto-lib/tarball/master", - "imap-client": "https://github.com/whiteout-io/imap-client/tarball/master", + "imap-client": "https://github.com/whiteout-io/imap-client/tarball/dev/offline-bug", + "mailreader": "https://github.com/whiteout-io/mailreader/tarball/master", "pgpmailer": "https://github.com/whiteout-io/pgpmailer/tarball/dev/pgpbuilder", "pgpbuilder": "https://github.com/whiteout-io/pgpbuilder/tarball/master", "requirejs": "2.1.10" diff --git a/src/js/app-controller.js b/src/js/app-controller.js index 1a7dc2e..507f377 100644 --- a/src/js/app-controller.js +++ b/src/js/app-controller.js @@ -6,6 +6,7 @@ define(function(require) { var $ = require('jquery'), ImapClient = require('imap-client'), + mailreader = require('mailreader'), PgpMailer = require('pgpmailer'), EmailDAO = require('js/dao/email-dao'), RestDAO = require('js/dao/rest-dao'), @@ -117,7 +118,7 @@ define(function(require) { onError: console.error }; - imapClient = new ImapClient(imapOptions); + imapClient = new ImapClient(imapOptions, mailreader); pgpMailer = new PgpMailer(smtpOptions, self._pgpbuilder); imapClient.onError = function(err) { @@ -356,7 +357,7 @@ define(function(require) { pgp = new PGP(); self._crypto = pgp; self._pgpbuilder = pgpbuilder = new PgpBuilder({}); // set the worker path?! - self._emailDao = emailDao = new EmailDAO(keychain, pgp, userStorage, pgpbuilder); + self._emailDao = emailDao = new EmailDAO(keychain, pgp, userStorage, pgpbuilder, mailreader); self._outboxBo = new OutboxBO(emailDao, keychain, userStorage, invitationDao); }; diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index 387606d..3bb0a20 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -6,11 +6,12 @@ define(function(require) { str = require('js/app-config').string, config = require('js/app-config').config; - var EmailDAO = function(keychain, crypto, devicestorage, pgpbuilder) { + var EmailDAO = function(keychain, crypto, devicestorage, pgpbuilder, mailreader) { this._keychain = keychain; this._crypto = crypto; this._devicestorage = devicestorage; this._pgpbuilder = pgpbuilder; + this._mailreader = mailreader; }; // @@ -969,7 +970,7 @@ define(function(require) { decrypted = decrypted || err.errMsg || 'Error occurred during decryption'; // this is a very primitive detection if we have PGP/MIME or PGP/INLINE - if (decrypted.indexOf('Content-Transfer-Encoding:') === -1 && decrypted.indexOf('Content-Type:') === -1) { + if (!self._mailreader.isRfc(decrypted)) { message.body = decrypted; message.decrypted = true; message.decryptingBody = false; @@ -980,7 +981,7 @@ define(function(require) { // parse the decrypted MIME message self._imapParseMessageBlock({ message: message, - block: decrypted + raw: decrypted }, function(error) { if (error) { message.decryptingBody = false; @@ -1204,7 +1205,7 @@ define(function(require) { }; EmailDAO.prototype._imapParseMessageBlock = function(options, callback) { - this._imapClient.parseDecryptedMessageBlock(options, callback); + this._mailreader.parseRfc(options, callback); }; /** diff --git a/test/new-unit/email-dao-test.js b/test/new-unit/email-dao-test.js index 46a73d2..dfd5120 100644 --- a/test/new-unit/email-dao-test.js +++ b/test/new-unit/email-dao-test.js @@ -8,6 +8,7 @@ define(function(require) { PgpBuilder = require('pgpbuilder'), PGP = require('js/crypto/pgp'), DeviceStorageDAO = require('js/dao/devicestorage-dao'), + mailreader = require('mailreader'), str = require('js/app-config').string, expect = chai.expect; @@ -116,7 +117,7 @@ define(function(require) { pgpStub = sinon.createStubInstance(PGP); devicestorageStub = sinon.createStubInstance(DeviceStorageDAO); - dao = new EmailDAO(keychainStub, pgpStub, devicestorageStub, pgpBuilderStub); + dao = new EmailDAO(keychainStub, pgpStub, devicestorageStub, pgpBuilderStub, mailreader); dao._account = account; expect(dao._keychain).to.equal(keychainStub); @@ -478,14 +479,12 @@ define(function(require) { describe('_imapParseMessageBlock', function() { it('should parse a message', function(done) { - imapClientStub.parseDecryptedMessageBlock.yields(null, {}); + var parseRfc = sinon.stub(mailreader, 'parseRfc').withArgs({}).yields(); - dao._imapParseMessageBlock(function(err, msg) { - expect(err).to.not.exist; - expect(msg).to.exist; + dao._imapParseMessageBlock({}, function() { + expect(parseRfc.calledOnce).to.be.true; done(); }); - }); }); @@ -1206,14 +1205,14 @@ define(function(require) { body: '-----BEGIN PGP MESSAGE-----asdasdasd-----END PGP MESSAGE-----' }; - mimeBody = 'Content-Transfer-Encoding: Content-Type:'; + mimeBody = 'Content-Type: asdasdasd'; parsedBody = 'body? yes.'; keychainStub.getReceiverPublicKey.withArgs(message.from[0].address).yieldsAsync(null, mockKeyPair.publicKey); pgpStub.decrypt.withArgs(message.body, mockKeyPair.publicKey.publicKey).yieldsAsync(null, mimeBody); parseStub = sinon.stub(dao, '_imapParseMessageBlock', function(o, cb) { expect(o.message).to.equal(message); - expect(o.block).to.equal(mimeBody); + expect(o.raw).to.equal(mimeBody); o.message.body = parsedBody; cb(null, o.message);