2013-06-06 07:34:22 -04:00
|
|
|
/**
|
|
|
|
* The main application controller
|
|
|
|
*/
|
2013-08-20 09:19:13 -04:00
|
|
|
define(function(require) {
|
2013-08-20 13:48:49 -04:00
|
|
|
'use strict';
|
|
|
|
|
2014-04-01 07:16:39 -04:00
|
|
|
var Auth = require('js/bo/auth'),
|
|
|
|
PGP = require('js/crypto/pgp'),
|
2014-04-22 12:19:28 -04:00
|
|
|
PgpMailer = require('pgpmailer'),
|
2014-04-01 07:16:39 -04:00
|
|
|
OAuth = require('js/util/oauth'),
|
2014-04-22 12:19:28 -04:00
|
|
|
PgpBuilder = require('pgpbuilder'),
|
2014-04-01 07:16:39 -04:00
|
|
|
OutboxBO = require('js/bo/outbox'),
|
|
|
|
mailreader = require('mailreader'),
|
|
|
|
ImapClient = require('imap-client'),
|
|
|
|
RestDAO = require('js/dao/rest-dao'),
|
2013-08-20 13:48:49 -04:00
|
|
|
EmailDAO = require('js/dao/email-dao'),
|
2014-04-01 07:16:39 -04:00
|
|
|
config = require('js/app-config').config,
|
2014-03-11 13:57:03 -04:00
|
|
|
EmailSync = require('js/dao/email-sync'),
|
2014-04-01 07:16:39 -04:00
|
|
|
KeychainDAO = require('js/dao/keychain-dao'),
|
2013-10-29 07:19:27 -04:00
|
|
|
PublicKeyDAO = require('js/dao/publickey-dao'),
|
|
|
|
LawnchairDAO = require('js/dao/lawnchair-dao'),
|
2013-11-19 10:14:48 -05:00
|
|
|
InvitationDAO = require('js/dao/invitation-dao'),
|
2014-04-01 07:16:39 -04:00
|
|
|
DeviceStorageDAO = require('js/dao/devicestorage-dao'),
|
|
|
|
UpdateHandler = require('js/util/update/update-handler');
|
2013-08-20 13:48:49 -04:00
|
|
|
|
|
|
|
var self = {};
|
|
|
|
|
|
|
|
/**
|
2013-10-09 10:40:36 -04:00
|
|
|
* Start the application
|
2013-08-20 13:48:49 -04:00
|
|
|
*/
|
2013-12-09 13:21:52 -05:00
|
|
|
self.start = function(options, callback) {
|
2014-02-01 08:43:15 -05:00
|
|
|
// are we running in a cordova app or in a browser environment?
|
|
|
|
if (window.cordova) {
|
|
|
|
// wait for 'deviceready' event to make sure plugins are loaded
|
2013-08-20 13:48:49 -04:00
|
|
|
console.log('Assuming Cordova environment...');
|
|
|
|
document.addEventListener("deviceready", onDeviceReady, false);
|
2014-02-01 08:43:15 -05:00
|
|
|
} else {
|
|
|
|
// No need to wait on events... just start the app
|
|
|
|
console.log('Assuming Browser environment...');
|
|
|
|
onDeviceReady();
|
2013-08-20 13:48:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function onDeviceReady() {
|
|
|
|
console.log('Starting app.');
|
2013-12-09 13:21:52 -05:00
|
|
|
|
2014-04-01 07:16:39 -04:00
|
|
|
self.buildModules();
|
|
|
|
|
2013-12-09 13:21:52 -05:00
|
|
|
// Handle offline and online gracefully
|
|
|
|
window.addEventListener('online', self.onConnect.bind(self, options.onError));
|
|
|
|
window.addEventListener('offline', self.onDisconnect.bind(self, options.onError));
|
|
|
|
|
2013-10-09 10:40:36 -04:00
|
|
|
self._appConfigStore.init('app-config', callback);
|
2013-08-20 13:48:49 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-01 07:16:39 -04:00
|
|
|
self.buildModules = function() {
|
|
|
|
var lawnchairDao, restDao, pubkeyDao, emailDao, emailSync, keychain, pgp, userStorage, pgpbuilder, oauth, appConfigStore;
|
|
|
|
|
|
|
|
// start the mailreader's worker thread
|
|
|
|
mailreader.startWorker(config.workerPath + '/../lib/mailreader-parser-worker.js');
|
|
|
|
|
|
|
|
// init objects and inject dependencies
|
|
|
|
restDao = new RestDAO();
|
|
|
|
lawnchairDao = new LawnchairDAO();
|
|
|
|
pubkeyDao = new PublicKeyDAO(restDao);
|
|
|
|
oauth = new OAuth(new RestDAO('https://www.googleapis.com'));
|
|
|
|
|
|
|
|
self._appConfigStore = appConfigStore = new DeviceStorageDAO(new LawnchairDAO());
|
|
|
|
self._auth = new Auth(appConfigStore, oauth, new RestDAO('/ca'));
|
|
|
|
self._userStorage = userStorage = new DeviceStorageDAO(lawnchairDao);
|
|
|
|
self._invitationDao = new InvitationDAO(restDao);
|
|
|
|
self._keychain = keychain = new KeychainDAO(lawnchairDao, pubkeyDao);
|
|
|
|
self._crypto = pgp = new PGP();
|
2014-04-22 12:19:28 -04:00
|
|
|
self._pgpbuilder = pgpbuilder = new PgpBuilder();
|
2014-04-28 12:09:51 -04:00
|
|
|
self._emailSync = emailSync = new EmailSync(keychain, userStorage);
|
2014-04-01 07:16:39 -04:00
|
|
|
self._emailDao = emailDao = new EmailDAO(keychain, pgp, userStorage, pgpbuilder, mailreader, emailSync);
|
|
|
|
self._outboxBo = new OutboxBO(emailDao, keychain, userStorage);
|
|
|
|
self._updateHandler = new UpdateHandler(appConfigStore, userStorage);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.isOnline = function() {
|
|
|
|
return navigator.onLine;
|
|
|
|
};
|
2013-12-09 13:21:52 -05:00
|
|
|
|
2014-04-01 07:16:39 -04:00
|
|
|
self.onDisconnect = function(callback) {
|
2013-12-09 13:21:52 -05:00
|
|
|
self._emailDao.onDisconnect(null, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
self.onConnect = function(callback) {
|
2014-04-02 13:47:50 -04:00
|
|
|
if (!self.isOnline() || !self._emailDao || !self._emailDao._account) {
|
2013-12-09 13:21:52 -05:00
|
|
|
// prevent connection infinite loop
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch pinned local ssl certificate
|
2014-04-01 07:16:39 -04:00
|
|
|
self._auth.getCredentials({}, function(err, credentials) {
|
2013-12-09 13:21:52 -05:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-01 07:16:39 -04:00
|
|
|
initClients(credentials);
|
2013-12-09 13:21:52 -05:00
|
|
|
});
|
|
|
|
|
2014-04-01 07:16:39 -04:00
|
|
|
function initClients(credentials) {
|
2014-02-03 16:07:39 -05:00
|
|
|
var auth, imapOptions, imapClient, smtpOptions, pgpMailer;
|
2013-12-09 13:21:52 -05:00
|
|
|
|
|
|
|
auth = {
|
2014-04-17 09:13:18 -04:00
|
|
|
user: credentials.emailAddress,
|
|
|
|
xoauth2: credentials.oauthToken
|
2013-12-09 13:21:52 -05:00
|
|
|
};
|
|
|
|
imapOptions = {
|
|
|
|
secure: config.gmail.imap.secure,
|
|
|
|
port: config.gmail.imap.port,
|
|
|
|
host: config.gmail.imap.host,
|
|
|
|
auth: auth,
|
2014-04-01 07:16:39 -04:00
|
|
|
ca: [credentials.sslCert]
|
2013-12-09 13:21:52 -05:00
|
|
|
};
|
|
|
|
smtpOptions = {
|
2014-02-03 16:07:39 -05:00
|
|
|
secureConnection: config.gmail.smtp.secure,
|
2013-12-09 13:21:52 -05:00
|
|
|
port: config.gmail.smtp.port,
|
|
|
|
host: config.gmail.smtp.host,
|
|
|
|
auth: auth,
|
2014-02-03 16:07:39 -05:00
|
|
|
tls: {
|
2014-04-01 07:16:39 -04:00
|
|
|
ca: [credentials.sslCert]
|
2014-02-03 16:07:39 -05:00
|
|
|
},
|
|
|
|
onError: console.error
|
2013-12-09 13:21:52 -05:00
|
|
|
};
|
|
|
|
|
2014-04-22 12:19:28 -04:00
|
|
|
pgpMailer = new PgpMailer(smtpOptions, self._pgpbuilder);
|
2014-04-01 07:16:39 -04:00
|
|
|
imapClient = new ImapClient(imapOptions, mailreader);
|
|
|
|
imapClient.onError = onImapError;
|
2013-12-09 13:21:52 -05:00
|
|
|
|
|
|
|
// connect to clients
|
|
|
|
self._emailDao.onConnect({
|
|
|
|
imapClient: imapClient,
|
2014-02-03 16:07:39 -05:00
|
|
|
pgpMailer: pgpMailer
|
2013-12-09 13:21:52 -05:00
|
|
|
}, callback);
|
|
|
|
}
|
|
|
|
|
2014-04-01 07:16:39 -04:00
|
|
|
function onImapError(err) {
|
|
|
|
console.log('IMAP error.', err);
|
|
|
|
console.log('IMAP reconnecting...');
|
|
|
|
// re-init client modules on error
|
|
|
|
self.onConnect(function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.error('IMAP reconnect failed!', err);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-09 13:21:52 -05:00
|
|
|
|
2014-04-01 07:16:39 -04:00
|
|
|
console.log('IMAP reconnect attempt complete.');
|
|
|
|
});
|
|
|
|
}
|
2013-12-09 13:21:52 -05:00
|
|
|
};
|
|
|
|
|
2013-11-08 18:30:45 -05:00
|
|
|
self.checkForUpdate = function() {
|
2014-04-01 07:16:39 -04:00
|
|
|
if (!window.chrome || !chrome.runtime || !chrome.runtime.onUpdateAvailable) {
|
2013-11-08 15:35:30 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-08 18:30:45 -05:00
|
|
|
// check for update and restart
|
2013-11-08 13:08:34 -05:00
|
|
|
chrome.runtime.onUpdateAvailable.addListener(function(details) {
|
2013-11-08 18:30:45 -05:00
|
|
|
console.log("Updating to version " + details.version);
|
|
|
|
chrome.runtime.reload();
|
2013-11-08 13:08:34 -05:00
|
|
|
});
|
|
|
|
chrome.runtime.requestUpdateCheck(function(status) {
|
|
|
|
if (status === "update_found") {
|
|
|
|
console.log("Update pending...");
|
|
|
|
} else if (status === "no_update") {
|
|
|
|
console.log("No update found.");
|
|
|
|
} else if (status === "throttled") {
|
|
|
|
console.log("Checking updates too frequently.");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-12-09 13:21:52 -05:00
|
|
|
/**
|
|
|
|
* Instanciate the mail email data access object and its dependencies. Login to imap on init.
|
|
|
|
*/
|
|
|
|
self.init = function(options, callback) {
|
2014-03-11 12:49:47 -04:00
|
|
|
// init user's local database
|
2014-03-11 13:27:02 -04:00
|
|
|
self._userStorage.init(options.emailAddress, function(err) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-14 11:41:31 -05:00
|
|
|
|
2014-03-11 12:49:47 -04:00
|
|
|
// Migrate the databases if necessary
|
|
|
|
self._updateHandler.update(onUpdate);
|
|
|
|
});
|
|
|
|
|
|
|
|
function onUpdate(err) {
|
2013-12-10 17:05:17 -05:00
|
|
|
if (err) {
|
2014-03-11 12:49:47 -04:00
|
|
|
callback({
|
|
|
|
errMsg: 'Update failed, please reinstall the app.',
|
|
|
|
err: err
|
|
|
|
});
|
2013-12-09 13:21:52 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-11-14 11:41:31 -05:00
|
|
|
|
2014-03-11 12:49:47 -04:00
|
|
|
// account information for the email dao
|
|
|
|
var account = {
|
|
|
|
emailAddress: options.emailAddress,
|
|
|
|
asymKeySize: config.asymKeySize
|
|
|
|
};
|
|
|
|
|
|
|
|
// init email dao
|
|
|
|
self._emailDao.init({
|
|
|
|
account: account
|
|
|
|
}, function(err, keypair) {
|
2013-12-09 13:21:52 -05:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-14 11:41:31 -05:00
|
|
|
|
2014-03-28 13:08:04 -04:00
|
|
|
callback(null, keypair);
|
2013-12-09 13:21:52 -05:00
|
|
|
});
|
2014-03-11 12:49:47 -04:00
|
|
|
}
|
2013-08-20 13:48:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
2013-06-10 11:57:33 -04:00
|
|
|
});
|