2013-10-05 08:16:04 -04:00
|
|
|
define(function(require) {
|
|
|
|
'use strict';
|
|
|
|
|
2013-12-03 07:15:10 -05:00
|
|
|
var appController = require('js/app-controller'),
|
2014-01-16 09:37:08 -05:00
|
|
|
download = require('js/util/download'),
|
2013-12-03 07:15:10 -05:00
|
|
|
angular = require('angular'),
|
2014-02-27 09:23:33 -05:00
|
|
|
str = require('js/app-config').string,
|
|
|
|
emailDao, invitationDao, outbox, crypto, keychain;
|
2013-10-05 08:16:04 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Controller
|
|
|
|
//
|
|
|
|
|
2013-11-08 16:05:08 -05:00
|
|
|
var ReadCtrl = function($scope) {
|
2014-01-16 09:37:08 -05:00
|
|
|
|
|
|
|
emailDao = appController._emailDao;
|
2014-02-27 09:23:33 -05:00
|
|
|
invitationDao = appController._invitationDao;
|
|
|
|
outbox = appController._outboxBo;
|
2013-12-03 07:15:10 -05:00
|
|
|
crypto = appController._crypto;
|
|
|
|
keychain = appController._keychain;
|
|
|
|
|
2013-12-04 05:50:20 -05:00
|
|
|
// set default value so that the popover height is correct on init
|
2014-02-27 12:14:38 -05:00
|
|
|
$scope.keyId = 'No key found.';
|
2013-12-04 05:50:20 -05:00
|
|
|
|
2013-11-08 16:05:08 -05:00
|
|
|
$scope.state.read = {
|
|
|
|
open: false,
|
|
|
|
toggle: function(to) {
|
|
|
|
this.open = to;
|
|
|
|
}
|
|
|
|
};
|
2013-11-18 13:53:31 -05:00
|
|
|
|
|
|
|
$scope.lineEmpty = function(line) {
|
|
|
|
return line.replace(/>/g, '').trim().length === 0;
|
|
|
|
};
|
2013-12-03 07:15:10 -05:00
|
|
|
|
2014-02-20 13:20:24 -05:00
|
|
|
$scope.getKeyId = function(address) {
|
2014-02-27 12:14:38 -05:00
|
|
|
$scope.keyId = 'Searching...';
|
2013-12-03 07:15:10 -05:00
|
|
|
keychain.getReceiverPublicKey(address, function(err, pubkey) {
|
2013-12-04 10:35:53 -05:00
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-20 13:20:24 -05:00
|
|
|
if (!pubkey) {
|
2014-02-27 12:14:38 -05:00
|
|
|
$scope.keyId = 'User has no key. Click to invite.';
|
|
|
|
$scope.$apply();
|
2014-02-20 13:20:24 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-03 07:15:10 -05:00
|
|
|
var fpr = crypto.getFingerprint(pubkey.publicKey);
|
2014-02-20 13:20:24 -05:00
|
|
|
var formatted = fpr.slice(32);
|
2013-12-03 07:15:10 -05:00
|
|
|
|
2014-02-27 12:14:38 -05:00
|
|
|
$scope.keyId = 'PGP key: ' + formatted;
|
2013-12-03 07:15:10 -05:00
|
|
|
$scope.$apply();
|
|
|
|
});
|
|
|
|
};
|
2013-12-04 10:35:53 -05:00
|
|
|
|
2014-02-23 17:20:47 -05:00
|
|
|
$scope.$watch('state.mailList.selected', function(mail) {
|
2013-12-04 10:42:17 -05:00
|
|
|
if (!mail) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-04 10:35:53 -05:00
|
|
|
// display sender security status
|
|
|
|
mail.from.forEach(checkPublicKey);
|
|
|
|
// display recipient security status
|
|
|
|
mail.to.forEach(checkPublicKey);
|
2014-01-13 16:43:43 -05:00
|
|
|
// display recipient security status
|
|
|
|
Array.isArray(mail.cc) && mail.cc.forEach(checkPublicKey);
|
2013-12-04 10:35:53 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
function checkPublicKey(user) {
|
|
|
|
user.secure = undefined;
|
|
|
|
keychain.getReceiverPublicKey(user.address, function(err, pubkey) {
|
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pubkey && pubkey.publicKey) {
|
|
|
|
user.secure = true;
|
|
|
|
} else {
|
|
|
|
user.secure = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.$apply();
|
|
|
|
});
|
|
|
|
}
|
2014-01-16 09:37:08 -05:00
|
|
|
|
|
|
|
$scope.download = function(attachment) {
|
|
|
|
// download file to disk if content is available
|
|
|
|
if (attachment.content) {
|
|
|
|
saveToDisk(attachment);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var folder = $scope.state.nav.currentFolder;
|
|
|
|
var email = $scope.state.mailList.selected;
|
|
|
|
|
|
|
|
emailDao.getAttachment({
|
|
|
|
path: folder.path,
|
|
|
|
uid: email.uid,
|
|
|
|
attachment: attachment
|
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
saveToDisk(attachment);
|
|
|
|
});
|
|
|
|
|
|
|
|
function saveToDisk(attachment) {
|
|
|
|
download.createDownload({
|
|
|
|
content: attachment.content,
|
|
|
|
filename: attachment.filename,
|
|
|
|
contentType: attachment.mimeType
|
|
|
|
}, $scope.onError);
|
|
|
|
}
|
|
|
|
};
|
2014-02-27 09:23:33 -05:00
|
|
|
|
2014-02-27 12:14:38 -05:00
|
|
|
$scope.invite = function(user) {
|
|
|
|
// only invite non-pgp users
|
|
|
|
if (user.secure) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.keyId = 'Sending invitation...';
|
|
|
|
|
|
|
|
var sender = emailDao._account.emailAddress,
|
|
|
|
recipient = user.address;
|
|
|
|
|
2014-02-27 09:23:33 -05:00
|
|
|
invitationDao.invite({
|
2014-02-27 12:14:38 -05:00
|
|
|
recipient: recipient,
|
|
|
|
sender: sender
|
2014-02-27 09:23:33 -05:00
|
|
|
}, function(err) {
|
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var invitationMail = {
|
2014-02-27 12:14:38 -05:00
|
|
|
from: [{
|
|
|
|
address: sender
|
|
|
|
}],
|
|
|
|
to: [{
|
|
|
|
address: recipient
|
|
|
|
}],
|
|
|
|
cc: [],
|
|
|
|
bcc: [],
|
2014-02-27 09:23:33 -05:00
|
|
|
subject: str.invitationSubject,
|
|
|
|
body: str.invitationMessage
|
|
|
|
};
|
|
|
|
|
|
|
|
// send invitation mail
|
|
|
|
outbox.put(invitationMail, $scope.onError);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-11-08 16:05:08 -05:00
|
|
|
};
|
2013-10-05 08:16:04 -04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Directives
|
|
|
|
//
|
|
|
|
|
|
|
|
var ngModule = angular.module('read', []);
|
|
|
|
ngModule.directive('frameLoad', function() {
|
|
|
|
return function(scope, elm) {
|
|
|
|
elm.bind('load', function() {
|
|
|
|
var frame = elm[0];
|
|
|
|
frame.height = frame.contentWindow.document.body.scrollHeight + 'px';
|
|
|
|
});
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return ReadCtrl;
|
|
|
|
});
|