2015-09-07 14:53:43 -07:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-05-04 02:34:13 -04:00
|
|
|
*/
|
|
|
|
|
Finish abstracting native client
Firstly, don't initialize textsecure.nativclient unless the browser
supports it. The mimetype-check trick is hewn from nacl-common.js.
Secondly, nativeclient crypto functions will all automatically wait for
the module to load before sending messages, so we needn't register any
onload callbacks outside nativeclient.js. (Previously, if you wanted to
do crypto with native client, you would have to register a call back and
wait for the module to load.) Now that the native client crypto is
encapsulated behind a nice interface, it can handle all that
onload-callback jazz internally: if the module isn't loaded when you
call a nativeclient function, return a promise that waits for the load
callback, and eventually resolves with the result of the requested
command. This removes the need for textsecure.registerOnLoadCallback.
Finally, although native client has its quirks, it's significantly
faster than the alternative (emscripten compiled js), so this commit
also lets the crypto backend use native client opportunistically, if
it's available, falling back to js if not, which should make us
compatible with older versions of chrome and chromium.
2014-11-08 17:26:20 -08:00
|
|
|
;(function() {
|
|
|
|
'use strict';
|
2016-08-09 16:11:46 -07:00
|
|
|
window.onInvalidStateError = function(e) {
|
|
|
|
console.log(e);
|
|
|
|
};
|
|
|
|
|
2016-02-02 17:55:27 -08:00
|
|
|
console.log('background page reloaded');
|
2017-04-13 12:10:42 -07:00
|
|
|
console.log('environment:', window.config.environment);
|
2015-09-25 11:10:25 -07:00
|
|
|
|
2017-07-24 18:43:35 -07:00
|
|
|
var initialLoadComplete = false;
|
2017-07-27 18:38:41 -07:00
|
|
|
window.owsDesktopApp = {};
|
2015-05-22 17:06:49 -07:00
|
|
|
|
2015-09-25 11:10:25 -07:00
|
|
|
// start a background worker for ecc
|
2017-03-07 16:54:46 -08:00
|
|
|
textsecure.startWorker('js/libsignal-protocol-worker.js');
|
2017-05-07 14:18:22 -07:00
|
|
|
Whisper.KeyChangeListener.init(textsecure.storage.protocol);
|
2017-05-22 15:24:24 -07:00
|
|
|
textsecure.storage.protocol.on('removePreKey', function() {
|
|
|
|
getAccountManager().refreshPreKeys();
|
|
|
|
});
|
2015-09-01 16:56:17 -07:00
|
|
|
|
2017-04-13 10:47:30 -07:00
|
|
|
var SERVER_URL = window.config.serverUrl;
|
2017-09-11 18:50:35 +02:00
|
|
|
var CDN_URL = window.config.cdnUrl;
|
2015-09-21 17:05:19 -07:00
|
|
|
var messageReceiver;
|
|
|
|
window.getSocketStatus = function() {
|
|
|
|
if (messageReceiver) {
|
|
|
|
return messageReceiver.getStatus();
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
};
|
2017-04-11 17:50:13 -07:00
|
|
|
Whisper.events = _.clone(Backbone.Events);
|
2017-02-14 11:27:59 -08:00
|
|
|
var accountManager;
|
2015-09-01 12:57:02 -07:00
|
|
|
window.getAccountManager = function() {
|
2017-02-14 11:27:59 -08:00
|
|
|
if (!accountManager) {
|
|
|
|
var USERNAME = storage.get('number_id');
|
|
|
|
var PASSWORD = storage.get('password');
|
|
|
|
accountManager = new textsecure.AccountManager(
|
Certificate pinning via node XMLHttpRequest implementation (#1394)
* Add certificate pinning on https service requests
Make https requests to the server using node apis instead of browser apis, so we
can specify our own CA list, which contains only our own CA.
This protects us from MITM by a rogue CA.
As a bonus, this let's us drop the use of non-standard ports and just use good
ol' default 443 all the time, at least for http requests.
// FREEBIE
* Make certificateAuthorities an option on requests
Modify node-based xhr implementation based on driverdan/node-XMLHttpRequest,
adding support for setting certificate authorities on each request.
This allows us to pin our master CA for requests to the server and cdn but not
to the s3 attachment server, for instance. Also fix an exception when sending
binary data in a request: it is submitted as an array buffer, and must be
converted to a node Buffer since we are now using a node based request api.
// FREEBIE
* Import node-based xhr implementation
Add a copy of https://github.com/driverdan/node-XMLHttpRequest@86ff70e, and
expose it to the renderer in the preload script.
In later commits this module will be extended to support custom certificate
authorities.
// FREEBIE
* Support "arraybuffer" responseType on requests
When fetching attachments, we want the result as binary data rather than a utf8
string. This lets our node-based XMLHttpRequest honor the responseType property
if it is set on the xhr.
Note that naively using the raw `.buffer` from a node Buffer won't work, since
it is a reuseable backing buffer that is often much larger than the actual
content defined by the Buffer's offset and length.
Instead, we'll prepare a return buffer based on the response's content length
header, and incrementally write chunks of data into it as they arrive.
// FREEBIE
* Switch to self-signed server endpoint
* Log more error info on failed requests
With the node-based xhr, relevant error info are stored in statusText and
responseText when a request fails.
// FREEBIE
* Add node-based websocket w/ support for custom CA
// FREEBIE
* Support handling array buffers instead of blobs
Our node-based websocket calls onmessage with an arraybuffer instead of a blob.
For robustness (on the off chance we switch or update the socket implementation
agian) I've kept the machinery for converting blobs to array buffers.
// FREEBIE
* Destroy all wacky server ports
// FREEBIE
2017-09-01 17:58:58 +02:00
|
|
|
SERVER_URL, USERNAME, PASSWORD
|
2017-02-14 11:27:59 -08:00
|
|
|
);
|
|
|
|
accountManager.addEventListener('registration', function() {
|
|
|
|
if (!Whisper.Registration.everDone()) {
|
|
|
|
storage.put('safety-numbers-approval', false);
|
|
|
|
}
|
|
|
|
Whisper.Registration.markDone();
|
|
|
|
console.log("dispatching registration event");
|
2017-04-11 17:50:13 -07:00
|
|
|
Whisper.events.trigger('registration_done');
|
2017-02-14 11:27:59 -08:00
|
|
|
});
|
|
|
|
}
|
2016-09-20 13:42:33 -07:00
|
|
|
return accountManager;
|
2015-09-01 12:57:02 -07:00
|
|
|
};
|
|
|
|
|
2015-05-14 15:44:43 -07:00
|
|
|
storage.fetch();
|
2017-08-07 17:24:59 -07:00
|
|
|
|
|
|
|
// We need this 'first' check because we don't want to start the app up any other time
|
|
|
|
// than the first time. And storage.fetch() will cause onready() to fire.
|
|
|
|
var first = true;
|
2015-05-12 15:14:20 -07:00
|
|
|
storage.onready(function() {
|
2017-08-07 17:24:59 -07:00
|
|
|
if (!first) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
first = false;
|
|
|
|
|
|
|
|
start();
|
|
|
|
});
|
|
|
|
|
|
|
|
window.getSyncRequest = function() {
|
|
|
|
return new textsecure.SyncRequest(textsecure.messaging, messageReceiver);
|
|
|
|
};
|
|
|
|
|
|
|
|
Whisper.events.on('shutdown', function() {
|
|
|
|
if (messageReceiver) {
|
|
|
|
messageReceiver.close().then(function() {
|
|
|
|
messageReceiver = null;
|
|
|
|
Whisper.events.trigger('shutdown-complete');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Whisper.events.trigger('shutdown-complete');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function start() {
|
2017-09-06 18:18:46 -07:00
|
|
|
ConversationController.load();
|
|
|
|
|
2016-02-12 17:18:36 -08:00
|
|
|
window.dispatchEvent(new Event('storage_ready'));
|
2015-09-21 17:05:19 -07:00
|
|
|
|
2017-02-03 21:21:18 -08:00
|
|
|
console.log("listening for registration events");
|
2017-04-11 17:50:13 -07:00
|
|
|
Whisper.events.on('registration_done', function() {
|
2017-02-03 21:21:18 -08:00
|
|
|
console.log("handling registration event");
|
2017-08-07 17:24:59 -07:00
|
|
|
connect(true);
|
2015-08-31 10:40:25 -07:00
|
|
|
});
|
2014-12-19 16:49:18 -08:00
|
|
|
|
2017-04-24 17:59:11 -07:00
|
|
|
var appView = window.owsDesktopApp.appView = new Whisper.AppView({el: $('body')});
|
2017-04-12 14:20:56 -07:00
|
|
|
|
2017-04-11 17:50:13 -07:00
|
|
|
Whisper.WallClockListener.init(Whisper.events);
|
|
|
|
Whisper.RotateSignedPreKeyListener.init(Whisper.events);
|
|
|
|
Whisper.ExpiringMessagesListener.init(Whisper.events);
|
2017-03-30 18:11:13 -07:00
|
|
|
|
2017-08-07 17:24:59 -07:00
|
|
|
if (Whisper.Import.isIncomplete()) {
|
|
|
|
console.log('Import was interrupted, showing import error screen');
|
|
|
|
appView.openImporter();
|
|
|
|
} else if (Whisper.Registration.everDone()) {
|
|
|
|
connect();
|
2017-04-12 14:20:56 -07:00
|
|
|
appView.openInbox({
|
|
|
|
initialLoadComplete: initialLoadComplete
|
|
|
|
});
|
|
|
|
} else {
|
2017-08-07 17:24:59 -07:00
|
|
|
appView.openInstallChoice();
|
2017-03-30 18:11:13 -07:00
|
|
|
}
|
2017-04-12 14:20:56 -07:00
|
|
|
|
2017-07-24 16:58:20 +02:00
|
|
|
Whisper.events.on('showDebugLog', function() {
|
2017-07-24 17:09:26 +02:00
|
|
|
appView.openDebugLog();
|
2017-07-24 16:58:20 +02:00
|
|
|
});
|
2017-04-12 14:20:56 -07:00
|
|
|
Whisper.events.on('unauthorized', function() {
|
|
|
|
appView.inboxView.networkStatusView.update();
|
|
|
|
});
|
|
|
|
Whisper.events.on('reconnectTimer', function() {
|
|
|
|
appView.inboxView.networkStatusView.setSocketReconnectInterval(60000);
|
|
|
|
});
|
2017-04-26 12:12:08 -07:00
|
|
|
Whisper.events.on('contactsync', function() {
|
|
|
|
if (appView.installView) {
|
|
|
|
appView.openInbox();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
Whisper.events.on('contactsync:begin', function() {
|
|
|
|
if (appView.installView && appView.installView.showSync) {
|
|
|
|
appView.installView.showSync();
|
|
|
|
}
|
|
|
|
});
|
2017-04-12 14:20:56 -07:00
|
|
|
|
2017-09-01 11:30:41 -07:00
|
|
|
Whisper.Notifications.on('click', function(conversation) {
|
2017-04-27 18:21:52 -07:00
|
|
|
showWindow();
|
2017-09-01 11:30:41 -07:00
|
|
|
if (conversation) {
|
|
|
|
appView.openConversation(conversation);
|
|
|
|
} else {
|
|
|
|
appView.openInbox({
|
|
|
|
initialLoadComplete: initialLoadComplete
|
|
|
|
});
|
|
|
|
}
|
2017-04-24 17:59:11 -07:00
|
|
|
});
|
2017-08-07 17:24:59 -07:00
|
|
|
}
|
2015-08-31 10:40:25 -07:00
|
|
|
|
2016-06-16 15:33:18 -07:00
|
|
|
window.getSyncRequest = function() {
|
|
|
|
return new textsecure.SyncRequest(textsecure.messaging, messageReceiver);
|
|
|
|
};
|
|
|
|
|
Full export, migration banner, and full migration workflow - behind flag (#1342)
* Add support for backup and restore
This first pass works for all stores except messages, pending some scaling
improvements.
// FREEBIE
* Import of messages and attachments
Properly sanitize filenames. Logging information that will help with
debugging but won't threaten privacy (no contact or group names),
where the on-disk directories have this information to make things
human-readable
FREEBIE
* First fully operational single-action export and import!
FREEBIE
* Add migration export flow
A banner alert leads to a blocking ui for the migration. We close the socket and
wait for incoming messages to drain before starting the export.
FREEBIE
* A number of updates for the export flow
1. We don't immediately pop the directory selection dialog box, instead
showing an explicit 'choose directory' button after explaining what is
about to happen
2. We show a 'submit debug log' button on most steps of the process
3. We handle export errors and encourage the user to double-check their
filesystem then submit their log
4. We are resilient to restarts during the process
5. We handle the user cancelling out of the directory selection dialog
differently from other errors.
6. The export process is now serialized: non-messages, then messages.
7. After successful export, show where the data is on disk
FREEBUE
* Put migration behind a flag
FREEBIE
* Shut down websocket before proceeding with export
FREEBIE
* Add MigrationView to test/index.html to fix test
FREEBIE
* Remove 'Submit Debug Log' button when the export process is complete
FREEBIE
* Create a 'Signal Export' directory below user-chosen dir
This cleans things up a bit so we don't litter the user's target
directory with lots of stuff.
FREEBIE
* Clarify MessageReceiver.drain() method comments
FREEBIE
* A couple updates for clarity - event names, else handling
Also the removal of wait(), which wasn't used anywhere.
FREEBIE
* A number of wording updates for the export flow
FREEBIE
* Export complete: put dir on its own line, make text selectable
FREEBIE
2017-08-28 13:06:10 -07:00
|
|
|
Whisper.events.on('start-shutdown', function() {
|
|
|
|
if (messageReceiver) {
|
|
|
|
messageReceiver.close().then(function() {
|
|
|
|
messageReceiver = null;
|
|
|
|
Whisper.events.trigger('shutdown-complete');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Whisper.events.trigger('shutdown-complete');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-08-07 17:24:59 -07:00
|
|
|
function connect(firstRun) {
|
|
|
|
window.removeEventListener('online', connect);
|
2017-04-12 14:20:56 -07:00
|
|
|
|
2017-09-06 18:21:38 -07:00
|
|
|
if (!Whisper.Registration.everDone()) { return; }
|
2017-08-07 17:24:59 -07:00
|
|
|
if (Whisper.Import.isIncomplete()) { return; }
|
2014-12-19 16:49:18 -08:00
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
if (messageReceiver) { messageReceiver.close(); }
|
2015-09-01 12:13:38 -07:00
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
var USERNAME = storage.get('number_id');
|
|
|
|
var PASSWORD = storage.get('password');
|
|
|
|
var mySignalingKey = storage.get('signaling_key');
|
2015-09-01 12:13:38 -07:00
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
// initialize the socket and start listening for messages
|
2016-04-03 20:06:27 -07:00
|
|
|
messageReceiver = new textsecure.MessageReceiver(
|
Certificate pinning via node XMLHttpRequest implementation (#1394)
* Add certificate pinning on https service requests
Make https requests to the server using node apis instead of browser apis, so we
can specify our own CA list, which contains only our own CA.
This protects us from MITM by a rogue CA.
As a bonus, this let's us drop the use of non-standard ports and just use good
ol' default 443 all the time, at least for http requests.
// FREEBIE
* Make certificateAuthorities an option on requests
Modify node-based xhr implementation based on driverdan/node-XMLHttpRequest,
adding support for setting certificate authorities on each request.
This allows us to pin our master CA for requests to the server and cdn but not
to the s3 attachment server, for instance. Also fix an exception when sending
binary data in a request: it is submitted as an array buffer, and must be
converted to a node Buffer since we are now using a node based request api.
// FREEBIE
* Import node-based xhr implementation
Add a copy of https://github.com/driverdan/node-XMLHttpRequest@86ff70e, and
expose it to the renderer in the preload script.
In later commits this module will be extended to support custom certificate
authorities.
// FREEBIE
* Support "arraybuffer" responseType on requests
When fetching attachments, we want the result as binary data rather than a utf8
string. This lets our node-based XMLHttpRequest honor the responseType property
if it is set on the xhr.
Note that naively using the raw `.buffer` from a node Buffer won't work, since
it is a reuseable backing buffer that is often much larger than the actual
content defined by the Buffer's offset and length.
Instead, we'll prepare a return buffer based on the response's content length
header, and incrementally write chunks of data into it as they arrive.
// FREEBIE
* Switch to self-signed server endpoint
* Log more error info on failed requests
With the node-based xhr, relevant error info are stored in statusText and
responseText when a request fails.
// FREEBIE
* Add node-based websocket w/ support for custom CA
// FREEBIE
* Support handling array buffers instead of blobs
Our node-based websocket calls onmessage with an arraybuffer instead of a blob.
For robustness (on the off chance we switch or update the socket implementation
agian) I've kept the machinery for converting blobs to array buffers.
// FREEBIE
* Destroy all wacky server ports
// FREEBIE
2017-09-01 17:58:58 +02:00
|
|
|
SERVER_URL, USERNAME, PASSWORD, mySignalingKey
|
2016-04-03 20:06:27 -07:00
|
|
|
);
|
2015-09-21 17:05:19 -07:00
|
|
|
messageReceiver.addEventListener('message', onMessageReceived);
|
|
|
|
messageReceiver.addEventListener('receipt', onDeliveryReceipt);
|
|
|
|
messageReceiver.addEventListener('contact', onContactReceived);
|
|
|
|
messageReceiver.addEventListener('group', onGroupReceived);
|
|
|
|
messageReceiver.addEventListener('sent', onSentMessage);
|
2016-02-19 16:28:08 -08:00
|
|
|
messageReceiver.addEventListener('read', onReadReceipt);
|
2017-06-19 09:08:16 -07:00
|
|
|
messageReceiver.addEventListener('verified', onVerified);
|
2015-09-21 17:05:19 -07:00
|
|
|
messageReceiver.addEventListener('error', onError);
|
2017-07-24 18:43:35 -07:00
|
|
|
messageReceiver.addEventListener('empty', onEmpty);
|
2017-07-25 16:00:06 -07:00
|
|
|
messageReceiver.addEventListener('progress', onProgress);
|
2017-06-15 16:12:58 -07:00
|
|
|
|
2016-04-03 20:06:27 -07:00
|
|
|
window.textsecure.messaging = new textsecure.MessageSender(
|
2017-09-11 18:50:35 +02:00
|
|
|
SERVER_URL, USERNAME, PASSWORD, CDN_URL
|
2016-04-03 20:06:27 -07:00
|
|
|
);
|
2017-01-09 08:45:47 -06:00
|
|
|
|
2017-09-14 12:08:10 -07:00
|
|
|
// Because v0.43.2 introduced a bug that lost contact details, v0.43.4 introduces
|
|
|
|
// a one-time contact sync to restore all lost contact/group information. We
|
|
|
|
// disable this checking if a user is first registering.
|
|
|
|
var key = 'chrome-contact-sync-v0.43.4';
|
|
|
|
if (!storage.get(key)) {
|
|
|
|
storage.put(key, true);
|
|
|
|
|
|
|
|
if (!firstRun && textsecure.storage.user.getDeviceId() != '1') {
|
|
|
|
window.getSyncRequest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
if (firstRun === true && textsecure.storage.user.getDeviceId() != '1') {
|
2016-09-12 12:09:56 -07:00
|
|
|
if (!storage.get('theme-setting') && textsecure.storage.get('userAgent') === 'OWI') {
|
|
|
|
storage.put('theme-setting', 'ios');
|
|
|
|
}
|
2016-01-14 13:40:26 -08:00
|
|
|
var syncRequest = new textsecure.SyncRequest(textsecure.messaging, messageReceiver);
|
2017-04-12 18:43:00 -07:00
|
|
|
Whisper.events.trigger('contactsync:begin');
|
2016-01-14 13:40:26 -08:00
|
|
|
syncRequest.addEventListener('success', function() {
|
|
|
|
console.log('sync successful');
|
2016-06-16 15:33:18 -07:00
|
|
|
storage.put('synced_at', Date.now());
|
2017-04-12 18:43:00 -07:00
|
|
|
Whisper.events.trigger('contactsync');
|
2016-01-14 13:40:26 -08:00
|
|
|
});
|
|
|
|
syncRequest.addEventListener('timeout', function() {
|
|
|
|
console.log('sync timed out');
|
2017-04-12 18:43:00 -07:00
|
|
|
Whisper.events.trigger('contactsync');
|
2015-09-30 18:47:41 -07:00
|
|
|
});
|
2015-09-01 12:13:38 -07:00
|
|
|
}
|
2015-09-21 17:05:19 -07:00
|
|
|
}
|
2015-09-01 12:13:38 -07:00
|
|
|
|
2017-07-24 18:43:35 -07:00
|
|
|
function onEmpty() {
|
|
|
|
initialLoadComplete = true;
|
|
|
|
|
|
|
|
var interval = setInterval(function() {
|
2017-04-12 14:20:56 -07:00
|
|
|
var view = window.owsDesktopApp.appView;
|
2017-07-24 18:43:35 -07:00
|
|
|
if (view) {
|
|
|
|
clearInterval(interval);
|
|
|
|
interval = null;
|
|
|
|
view.onEmpty();
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
}
|
2017-07-25 16:00:06 -07:00
|
|
|
function onProgress(ev) {
|
|
|
|
var count = ev.count;
|
|
|
|
|
2017-04-12 14:20:56 -07:00
|
|
|
var view = window.owsDesktopApp.appView;
|
2017-07-25 16:00:06 -07:00
|
|
|
if (view) {
|
|
|
|
view.onProgress(count);
|
|
|
|
}
|
|
|
|
}
|
2017-07-24 18:43:35 -07:00
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
function onContactReceived(ev) {
|
2017-07-21 10:59:41 -07:00
|
|
|
var details = ev.contactDetails;
|
2016-12-17 13:59:07 +01:00
|
|
|
|
2017-07-21 10:59:41 -07:00
|
|
|
var id = details.number;
|
2017-09-11 18:50:35 +02:00
|
|
|
|
|
|
|
if (id === textsecure.storage.user.getNumber()) {
|
|
|
|
// special case for syncing details about ourselves
|
|
|
|
if (details.profileKey) {
|
|
|
|
console.log('Got sync message with our own profile key');
|
|
|
|
storage.put('profileKey', details.profileKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 13:59:07 +01:00
|
|
|
var c = new Whisper.Conversation({
|
2017-07-21 10:59:41 -07:00
|
|
|
id: id
|
2016-12-17 13:59:07 +01:00
|
|
|
});
|
2017-07-21 10:59:41 -07:00
|
|
|
var error = c.validateNumber();
|
|
|
|
if (error) {
|
|
|
|
console.log('Invalid contact received', error && error.stack ? error.stack : error);
|
|
|
|
return;
|
2016-12-17 13:59:07 +01:00
|
|
|
}
|
|
|
|
|
2017-09-01 09:10:41 -07:00
|
|
|
return ConversationController.getOrCreateAndWait(id, 'private')
|
2017-08-30 09:35:04 -07:00
|
|
|
.then(function(conversation) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
2017-09-11 18:50:35 +02:00
|
|
|
if (details.profileKey) {
|
|
|
|
conversation.set({profileKey: details.profileKey});
|
|
|
|
}
|
2017-08-30 09:35:04 -07:00
|
|
|
conversation.save({
|
|
|
|
name: details.name,
|
|
|
|
avatar: details.avatar,
|
|
|
|
color: details.color,
|
|
|
|
active_at: conversation.get('active_at') || Date.now(),
|
|
|
|
}).then(resolve, reject);
|
2017-09-01 16:42:41 +02:00
|
|
|
}).then(function() {
|
|
|
|
if (details.verified) {
|
|
|
|
var verified = details.verified;
|
|
|
|
var ev = new Event('verified');
|
|
|
|
ev.verified = {
|
|
|
|
state: verified.state,
|
|
|
|
destination: verified.destination,
|
|
|
|
identityKey: verified.identityKey.toArrayBuffer(),
|
|
|
|
};
|
|
|
|
ev.viaContactSync = true;
|
|
|
|
return onVerified(ev);
|
|
|
|
}
|
2017-08-30 09:35:04 -07:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(ev.confirm)
|
|
|
|
.catch(function(error) {
|
|
|
|
console.log(
|
|
|
|
'onContactReceived error:',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
2017-07-21 10:59:41 -07:00
|
|
|
});
|
2015-09-21 17:05:19 -07:00
|
|
|
}
|
2015-06-01 14:08:21 -07:00
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
function onGroupReceived(ev) {
|
2017-07-21 10:59:41 -07:00
|
|
|
var details = ev.groupDetails;
|
|
|
|
var id = details.id;
|
|
|
|
|
2017-09-01 09:10:41 -07:00
|
|
|
return ConversationController.getOrCreateAndWait(id, 'group').then(function(conversation) {
|
2017-07-21 10:59:41 -07:00
|
|
|
var updates = {
|
|
|
|
name: details.name,
|
|
|
|
members: details.members,
|
|
|
|
avatar: details.avatar,
|
|
|
|
type: 'group',
|
|
|
|
};
|
|
|
|
if (details.active) {
|
|
|
|
updates.active_at = Date.now();
|
|
|
|
} else {
|
|
|
|
updates.left = true;
|
|
|
|
}
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
conversation.save(updates).then(resolve, reject);
|
|
|
|
}).then(ev.confirm);
|
|
|
|
});
|
2015-09-21 17:05:19 -07:00
|
|
|
}
|
2015-06-01 14:08:21 -07:00
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
function onMessageReceived(ev) {
|
|
|
|
var data = ev.data;
|
2017-09-11 18:50:35 +02:00
|
|
|
if (data.message.flags & textsecure.protobuf.DataMessage.Flags.PROFILE_KEY_UPDATE) {
|
|
|
|
var profileKey = data.message.profileKey.toArrayBuffer();
|
|
|
|
return ConversationController.getOrCreateAndWait(data.source, 'private').then(function(sender) {
|
|
|
|
return sender.setProfileKey(profileKey).then(ev.confirm);
|
|
|
|
});
|
|
|
|
}
|
2017-07-17 15:46:00 -07:00
|
|
|
var message = initIncomingMessage(data);
|
|
|
|
|
2017-07-24 18:43:35 -07:00
|
|
|
return isMessageDuplicate(message).then(function(isDuplicate) {
|
2017-07-17 15:46:00 -07:00
|
|
|
if (isDuplicate) {
|
|
|
|
console.log('Received duplicate message', message.idForLogging());
|
|
|
|
ev.confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:10:41 -07:00
|
|
|
var type, id;
|
|
|
|
if (data.message.group) {
|
|
|
|
type = 'group';
|
|
|
|
id = data.message.group.id;
|
|
|
|
} else {
|
|
|
|
type = 'private';
|
|
|
|
id = data.source;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ConversationController.getOrCreateAndWait(id, type).then(function() {
|
|
|
|
return message.handleDataMessage(data.message, ev.confirm, {
|
|
|
|
initialLoadComplete: initialLoadComplete
|
|
|
|
});
|
2017-07-24 18:43:35 -07:00
|
|
|
});
|
2017-07-17 15:46:00 -07:00
|
|
|
});
|
2015-09-21 17:05:19 -07:00
|
|
|
}
|
2015-06-01 14:08:21 -07:00
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
function onSentMessage(ev) {
|
|
|
|
var now = new Date().getTime();
|
|
|
|
var data = ev.data;
|
|
|
|
|
2017-09-11 18:50:35 +02:00
|
|
|
if (data.message.flags & textsecure.protobuf.DataMessage.Flags.PROFILE_KEY_UPDATE) {
|
|
|
|
var id = data.message.group ? data.message.group.id : data.destination;
|
|
|
|
return ConversationController.getOrCreateAndWait(id, 'private').then(function(convo) {
|
|
|
|
return convo.save({profileSharing: true}).then(ev.confirm);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
var message = new Whisper.Message({
|
|
|
|
source : textsecure.storage.user.getNumber(),
|
2017-07-17 15:46:00 -07:00
|
|
|
sourceDevice : data.device,
|
2015-09-21 17:05:19 -07:00
|
|
|
sent_at : data.timestamp,
|
|
|
|
received_at : now,
|
|
|
|
conversationId : data.destination,
|
|
|
|
type : 'outgoing',
|
2016-09-20 17:19:51 -07:00
|
|
|
sent : true,
|
|
|
|
expirationStartTimestamp: data.expirationStartTimestamp,
|
2015-09-21 17:05:19 -07:00
|
|
|
});
|
2015-06-01 14:08:21 -07:00
|
|
|
|
2017-07-24 18:43:35 -07:00
|
|
|
return isMessageDuplicate(message).then(function(isDuplicate) {
|
2017-07-17 15:46:00 -07:00
|
|
|
if (isDuplicate) {
|
|
|
|
console.log('Received duplicate message', message.idForLogging());
|
|
|
|
ev.confirm();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-01 09:10:41 -07:00
|
|
|
var type, id;
|
|
|
|
if (data.message.group) {
|
|
|
|
type = 'group';
|
|
|
|
id = data.message.group.id;
|
|
|
|
} else {
|
|
|
|
type = 'private';
|
|
|
|
id = data.destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ConversationController.getOrCreateAndWait(id, type).then(function() {
|
|
|
|
return message.handleDataMessage(data.message, ev.confirm, {
|
|
|
|
initialLoadComplete: initialLoadComplete
|
|
|
|
});
|
2017-07-24 18:43:35 -07:00
|
|
|
});
|
2017-07-17 15:46:00 -07:00
|
|
|
});
|
2015-09-21 17:05:19 -07:00
|
|
|
}
|
2015-06-01 14:08:21 -07:00
|
|
|
|
2017-07-17 15:46:00 -07:00
|
|
|
function isMessageDuplicate(message) {
|
|
|
|
return new Promise(function(resolve) {
|
|
|
|
var fetcher = new Whisper.Message();
|
|
|
|
var options = {
|
|
|
|
index: {
|
|
|
|
name: 'unique',
|
|
|
|
value: [
|
|
|
|
message.get('source'),
|
|
|
|
message.get('sourceDevice'),
|
|
|
|
message.get('sent_at')
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
fetcher.fetch(options).always(function() {
|
|
|
|
if (fetcher.get('id')) {
|
|
|
|
return resolve(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(false);
|
|
|
|
});
|
|
|
|
}).catch(function(error) {
|
|
|
|
console.log('isMessageDuplicate error:', error && error.stack ? error.stack : error);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initIncomingMessage(data) {
|
2015-09-21 17:05:19 -07:00
|
|
|
var message = new Whisper.Message({
|
2017-07-17 15:46:00 -07:00
|
|
|
source : data.source,
|
|
|
|
sourceDevice : data.sourceDevice,
|
|
|
|
sent_at : data.timestamp,
|
2017-08-11 17:06:48 -07:00
|
|
|
received_at : data.receivedAt || Date.now(),
|
2017-07-17 15:46:00 -07:00
|
|
|
conversationId : data.source,
|
2016-02-21 21:37:47 -08:00
|
|
|
type : 'incoming',
|
2016-02-26 12:25:32 -08:00
|
|
|
unread : 1
|
2015-09-21 17:05:19 -07:00
|
|
|
});
|
2014-12-19 16:49:18 -08:00
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
return message;
|
|
|
|
}
|
2015-06-01 14:08:21 -07:00
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
function onError(ev) {
|
2017-08-25 14:24:16 -07:00
|
|
|
var error = ev.error;
|
|
|
|
console.log(error);
|
|
|
|
console.log(error.stack);
|
2015-06-19 15:32:25 -07:00
|
|
|
|
2017-08-25 14:24:16 -07:00
|
|
|
if (error.name === 'HTTPError' && (error.code == 401 || error.code == 403)) {
|
2016-09-20 13:42:33 -07:00
|
|
|
Whisper.Registration.remove();
|
2017-04-12 14:11:51 -07:00
|
|
|
Whisper.events.trigger('unauthorized');
|
2015-09-21 17:05:19 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-06-19 15:32:25 -07:00
|
|
|
|
2017-08-25 14:24:16 -07:00
|
|
|
if (error.name === 'HTTPError' && error.code == -1) {
|
2015-09-21 17:05:19 -07:00
|
|
|
// Failed to connect to server
|
|
|
|
if (navigator.onLine) {
|
|
|
|
console.log('retrying in 1 minute');
|
2017-08-07 17:24:59 -07:00
|
|
|
setTimeout(connect, 60000);
|
2017-01-03 21:37:56 -06:00
|
|
|
|
2017-04-12 14:11:51 -07:00
|
|
|
Whisper.events.trigger('reconnectTimer');
|
2015-09-21 17:05:19 -07:00
|
|
|
} else {
|
|
|
|
console.log('offline');
|
2017-08-28 13:20:53 -07:00
|
|
|
if (messageReceiver) { messageReceiver.close(); }
|
2017-08-07 17:24:59 -07:00
|
|
|
window.addEventListener('online', connect);
|
2015-07-16 13:16:25 -07:00
|
|
|
}
|
2015-09-21 17:05:19 -07:00
|
|
|
return;
|
|
|
|
}
|
2015-07-16 13:16:25 -07:00
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
if (ev.proto) {
|
2017-08-25 14:24:16 -07:00
|
|
|
if (error.name === 'MessageCounterError') {
|
|
|
|
if (ev.confirm) {
|
|
|
|
ev.confirm();
|
|
|
|
}
|
2015-12-03 18:37:45 -08:00
|
|
|
// Ignore this message. It is likely a duplicate delivery
|
|
|
|
// because the server lost our ack the first time.
|
|
|
|
return;
|
|
|
|
}
|
2015-09-21 17:05:19 -07:00
|
|
|
var envelope = ev.proto;
|
2017-08-08 14:03:57 -07:00
|
|
|
var message = initIncomingMessage(envelope);
|
2017-07-25 10:33:28 -07:00
|
|
|
|
2017-08-25 14:24:16 -07:00
|
|
|
return message.saveErrors(error).then(function() {
|
2017-07-25 10:33:28 -07:00
|
|
|
var id = message.get('conversationId');
|
2017-09-01 09:10:41 -07:00
|
|
|
return ConversationController.getOrCreateAndWait(id, 'private').then(function(conversation) {
|
2016-03-25 14:28:09 -07:00
|
|
|
conversation.set({
|
2015-09-21 17:05:19 -07:00
|
|
|
active_at: Date.now(),
|
|
|
|
unreadCount: conversation.get('unreadCount') + 1
|
2015-07-10 12:24:12 -07:00
|
|
|
});
|
2016-03-25 14:28:09 -07:00
|
|
|
|
|
|
|
var conversation_timestamp = conversation.get('timestamp');
|
|
|
|
var message_timestamp = message.get('timestamp');
|
|
|
|
if (!conversation_timestamp || message_timestamp > conversation_timestamp) {
|
|
|
|
conversation.set({ timestamp: message.get('sent_at') });
|
|
|
|
}
|
2017-07-25 10:41:51 -07:00
|
|
|
|
2015-11-07 14:11:13 -08:00
|
|
|
conversation.trigger('newmessage', message);
|
2017-07-24 18:43:35 -07:00
|
|
|
if (initialLoadComplete) {
|
|
|
|
conversation.notify(message);
|
|
|
|
}
|
2017-07-25 10:41:51 -07:00
|
|
|
|
2017-08-25 14:24:16 -07:00
|
|
|
if (ev.confirm) {
|
|
|
|
ev.confirm();
|
|
|
|
}
|
|
|
|
|
2017-07-25 10:41:51 -07:00
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
conversation.save().then(resolve, reject);
|
|
|
|
});
|
2015-09-15 14:40:37 -07:00
|
|
|
});
|
2015-09-21 17:05:19 -07:00
|
|
|
});
|
2015-05-12 15:14:20 -07:00
|
|
|
}
|
2014-12-19 16:49:18 -08:00
|
|
|
|
2017-08-25 14:24:16 -07:00
|
|
|
throw error;
|
2015-09-21 17:05:19 -07:00
|
|
|
}
|
2015-05-18 13:48:48 -07:00
|
|
|
|
2016-02-19 16:28:08 -08:00
|
|
|
function onReadReceipt(ev) {
|
2016-09-21 15:06:31 -07:00
|
|
|
var read_at = ev.timestamp;
|
|
|
|
var timestamp = ev.read.timestamp;
|
|
|
|
var sender = ev.read.sender;
|
2017-07-24 12:27:48 -07:00
|
|
|
console.log('read receipt', sender, timestamp);
|
2017-07-24 18:43:35 -07:00
|
|
|
|
2017-07-17 15:46:00 -07:00
|
|
|
var receipt = Whisper.ReadReceipts.add({
|
2016-09-21 15:06:31 -07:00
|
|
|
sender : sender,
|
|
|
|
timestamp : timestamp,
|
|
|
|
read_at : read_at
|
|
|
|
});
|
2017-07-24 18:43:35 -07:00
|
|
|
|
2017-07-17 15:46:00 -07:00
|
|
|
receipt.on('remove', ev.confirm);
|
2017-08-03 18:10:45 -07:00
|
|
|
|
|
|
|
// Calling this directly so we can wait for completion
|
|
|
|
return Whisper.ReadReceipts.onReceipt(receipt);
|
2016-02-19 16:28:08 -08:00
|
|
|
}
|
|
|
|
|
2017-06-19 09:08:16 -07:00
|
|
|
function onVerified(ev) {
|
|
|
|
var number = ev.verified.destination;
|
|
|
|
var key = ev.verified.identityKey;
|
2017-06-15 16:37:20 -07:00
|
|
|
var state;
|
|
|
|
|
2017-07-21 10:59:41 -07:00
|
|
|
var c = new Whisper.Conversation({
|
|
|
|
id: number
|
|
|
|
});
|
|
|
|
var error = c.validateNumber();
|
|
|
|
if (error) {
|
|
|
|
console.log(
|
|
|
|
'Invalid verified sync received',
|
|
|
|
error && error.stack ? error.stack : error
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-19 09:08:16 -07:00
|
|
|
switch(ev.verified.state) {
|
2017-07-21 10:59:41 -07:00
|
|
|
case textsecure.protobuf.Verified.State.DEFAULT:
|
|
|
|
state = 'DEFAULT';
|
|
|
|
break;
|
|
|
|
case textsecure.protobuf.Verified.State.VERIFIED:
|
|
|
|
state = 'VERIFIED';
|
|
|
|
break;
|
|
|
|
case textsecure.protobuf.Verified.State.UNVERIFIED:
|
|
|
|
state = 'UNVERIFIED';
|
|
|
|
break;
|
2017-06-15 16:37:20 -07:00
|
|
|
}
|
2017-06-15 16:12:58 -07:00
|
|
|
|
2017-07-10 18:25:21 -07:00
|
|
|
console.log('got verified sync for', number, state,
|
|
|
|
ev.viaContactSync ? 'via contact sync' : '');
|
2017-06-20 10:52:06 -07:00
|
|
|
|
2017-09-01 09:10:41 -07:00
|
|
|
return ConversationController.getOrCreateAndWait(number, 'private').then(function(contact) {
|
2017-07-21 10:59:41 -07:00
|
|
|
var options = {
|
|
|
|
viaSyncMessage: true,
|
|
|
|
viaContactSync: ev.viaContactSync,
|
|
|
|
key: key
|
|
|
|
};
|
2017-07-03 17:31:57 -07:00
|
|
|
|
2017-07-21 10:59:41 -07:00
|
|
|
if (state === 'VERIFIED') {
|
|
|
|
return contact.setVerified(options).then(ev.confirm);
|
|
|
|
} else if (state === 'DEFAULT') {
|
|
|
|
return contact.setVerifiedDefault(options).then(ev.confirm);
|
|
|
|
} else {
|
|
|
|
return contact.setUnverified(options).then(ev.confirm);
|
|
|
|
}
|
|
|
|
});
|
2017-06-15 16:12:58 -07:00
|
|
|
}
|
|
|
|
|
2015-09-21 17:05:19 -07:00
|
|
|
function onDeliveryReceipt(ev) {
|
|
|
|
var pushMessage = ev.proto;
|
|
|
|
var timestamp = pushMessage.timestamp.toNumber();
|
2016-04-09 00:16:21 -07:00
|
|
|
console.log(
|
|
|
|
'delivery receipt from',
|
|
|
|
pushMessage.source + '.' + pushMessage.sourceDevice,
|
|
|
|
timestamp
|
|
|
|
);
|
2015-12-09 14:43:53 -08:00
|
|
|
|
2017-07-17 15:46:00 -07:00
|
|
|
var receipt = Whisper.DeliveryReceipts.add({
|
|
|
|
timestamp: timestamp,
|
|
|
|
source: pushMessage.source
|
2015-09-21 17:05:19 -07:00
|
|
|
});
|
2017-07-24 18:43:35 -07:00
|
|
|
|
2017-07-17 15:46:00 -07:00
|
|
|
receipt.on('remove', ev.confirm);
|
2017-08-03 18:10:45 -07:00
|
|
|
|
|
|
|
// Calling this directly so we can wait for completion
|
|
|
|
return Whisper.DeliveryReceipts.onReceipt(receipt);
|
2015-09-21 17:05:19 -07:00
|
|
|
}
|
Finish abstracting native client
Firstly, don't initialize textsecure.nativclient unless the browser
supports it. The mimetype-check trick is hewn from nacl-common.js.
Secondly, nativeclient crypto functions will all automatically wait for
the module to load before sending messages, so we needn't register any
onload callbacks outside nativeclient.js. (Previously, if you wanted to
do crypto with native client, you would have to register a call back and
wait for the module to load.) Now that the native client crypto is
encapsulated behind a nice interface, it can handle all that
onload-callback jazz internally: if the module isn't loaded when you
call a nativeclient function, return a promise that waits for the load
callback, and eventually resolves with the result of the requested
command. This removes the need for textsecure.registerOnLoadCallback.
Finally, although native client has its quirks, it's significantly
faster than the alternative (emscripten compiled js), so this commit
also lets the crypto backend use native client opportunistically, if
it's available, falling back to js if not, which should make us
compatible with older versions of chrome and chromium.
2014-11-08 17:26:20 -08:00
|
|
|
})();
|