2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-12-19 01:50:04 +00:00
|
|
|
*/
|
|
|
|
;(function() {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var registeredFunctions = {};
|
|
|
|
var Type = {
|
2015-10-03 01:31:07 +00:00
|
|
|
ENCRYPT_MESSAGE: 1,
|
2014-12-19 01:50:04 +00:00
|
|
|
INIT_SESSION: 2,
|
2015-10-03 01:31:07 +00:00
|
|
|
TRANSMIT_MESSAGE: 3,
|
2016-02-06 00:42:53 +00:00
|
|
|
REBUILD_MESSAGE: 4,
|
2017-02-16 02:27:06 +00:00
|
|
|
RETRY_SEND_MESSAGE_PROTO: 5
|
2014-12-19 01:50:04 +00:00
|
|
|
};
|
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
window.textsecure.replay = {
|
|
|
|
Type: Type,
|
|
|
|
registerFunction: function(func, functionCode) {
|
|
|
|
registeredFunctions[functionCode] = func;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-08 19:08:25 +00:00
|
|
|
function inherit(Parent, Child) {
|
|
|
|
Child.prototype = Object.create(Parent.prototype, {
|
|
|
|
constructor: {
|
|
|
|
value: Child,
|
|
|
|
writable: true,
|
|
|
|
configurable: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function appendStack(newError, originalError) {
|
|
|
|
newError.stack += '\nOriginal stack:\n' + originalError.stack;
|
|
|
|
}
|
|
|
|
|
2014-12-19 01:50:04 +00:00
|
|
|
function ReplayableError(options) {
|
|
|
|
options = options || {};
|
2018-01-08 19:08:25 +00:00
|
|
|
this.name = options.name || 'ReplayableError';
|
|
|
|
this.message = options.message;
|
|
|
|
|
|
|
|
Error.call(this, options.message);
|
|
|
|
|
|
|
|
// Maintains proper stack trace, where our error was thrown (only available on V8)
|
|
|
|
// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
|
|
|
if (Error.captureStackTrace) {
|
|
|
|
Error.captureStackTrace(this);
|
|
|
|
}
|
|
|
|
|
2014-12-19 01:50:04 +00:00
|
|
|
this.functionCode = options.functionCode;
|
2018-01-08 19:08:25 +00:00
|
|
|
this.args = options.args;
|
2014-12-19 01:50:04 +00:00
|
|
|
}
|
2018-01-08 19:08:25 +00:00
|
|
|
inherit(Error, ReplayableError);
|
2014-12-19 01:50:04 +00:00
|
|
|
|
|
|
|
ReplayableError.prototype.replay = function() {
|
2017-08-12 00:16:22 +00:00
|
|
|
var argumentsAsArray = Array.prototype.slice.call(arguments, 0);
|
|
|
|
var args = this.args.concat(argumentsAsArray);
|
|
|
|
return registeredFunctions[this.functionCode].apply(window, args);
|
2014-12-19 01:50:04 +00:00
|
|
|
};
|
|
|
|
|
2015-07-20 21:13:18 +00:00
|
|
|
function IncomingIdentityKeyError(number, message, key) {
|
2018-01-08 19:08:25 +00:00
|
|
|
this.number = number.split('.')[0];
|
|
|
|
this.identityKey = key;
|
|
|
|
|
2014-12-19 01:50:04 +00:00
|
|
|
ReplayableError.call(this, {
|
|
|
|
functionCode : Type.INIT_SESSION,
|
2018-01-08 19:08:25 +00:00
|
|
|
args : [number, message],
|
|
|
|
name : 'IncomingIdentityKeyError',
|
|
|
|
message : "The identity of " + this.number + " has changed."
|
2014-12-19 01:50:04 +00:00
|
|
|
});
|
|
|
|
}
|
2018-01-08 19:08:25 +00:00
|
|
|
inherit(ReplayableError, IncomingIdentityKeyError);
|
2014-12-19 01:50:04 +00:00
|
|
|
|
2015-07-20 23:13:17 +00:00
|
|
|
function OutgoingIdentityKeyError(number, message, timestamp, identityKey) {
|
2018-01-08 19:08:25 +00:00
|
|
|
this.number = number.split('.')[0];
|
|
|
|
this.identityKey = identityKey;
|
|
|
|
|
2014-12-19 01:50:04 +00:00
|
|
|
ReplayableError.call(this, {
|
2015-10-03 01:31:07 +00:00
|
|
|
functionCode : Type.ENCRYPT_MESSAGE,
|
2018-01-08 19:08:25 +00:00
|
|
|
args : [number, message, timestamp],
|
|
|
|
name : 'OutgoingIdentityKeyError',
|
|
|
|
message : "The identity of " + this.number + " has changed."
|
2014-12-19 01:50:04 +00:00
|
|
|
});
|
|
|
|
}
|
2018-01-08 19:08:25 +00:00
|
|
|
inherit(ReplayableError, OutgoingIdentityKeyError);
|
2014-12-19 01:50:04 +00:00
|
|
|
|
2015-10-03 01:31:07 +00:00
|
|
|
function OutgoingMessageError(number, message, timestamp, httpError) {
|
2015-09-22 22:52:42 +00:00
|
|
|
ReplayableError.call(this, {
|
2015-10-03 01:31:07 +00:00
|
|
|
functionCode : Type.ENCRYPT_MESSAGE,
|
2018-01-08 19:08:25 +00:00
|
|
|
args : [number, message, timestamp],
|
|
|
|
name : 'OutgoingMessageError',
|
|
|
|
message : httpError ? httpError.message : 'no http error'
|
2015-10-03 01:31:07 +00:00
|
|
|
});
|
2018-01-08 19:08:25 +00:00
|
|
|
|
2015-10-03 01:31:07 +00:00
|
|
|
if (httpError) {
|
|
|
|
this.code = httpError.code;
|
2018-01-08 19:08:25 +00:00
|
|
|
appendStack(this, httpError);
|
2015-10-03 01:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-08 19:08:25 +00:00
|
|
|
inherit(ReplayableError, OutgoingMessageError);
|
2015-10-03 01:31:07 +00:00
|
|
|
|
2015-11-29 19:25:27 +00:00
|
|
|
function SendMessageNetworkError(number, jsonData, httpError, timestamp) {
|
2018-01-08 19:08:25 +00:00
|
|
|
this.number = number;
|
|
|
|
this.code = httpError.code;
|
|
|
|
|
2015-10-03 01:31:07 +00:00
|
|
|
ReplayableError.call(this, {
|
|
|
|
functionCode : Type.TRANSMIT_MESSAGE,
|
2018-01-08 19:08:25 +00:00
|
|
|
args : [number, jsonData, timestamp],
|
|
|
|
name : 'SendMessageNetworkError',
|
|
|
|
message : httpError.message
|
2015-09-22 22:52:42 +00:00
|
|
|
});
|
2018-01-08 19:08:25 +00:00
|
|
|
|
|
|
|
appendStack(this, httpError);
|
2015-09-22 22:52:42 +00:00
|
|
|
}
|
2018-01-08 19:08:25 +00:00
|
|
|
inherit(ReplayableError, SendMessageNetworkError);
|
2015-09-22 22:52:42 +00:00
|
|
|
|
2017-02-16 02:27:06 +00:00
|
|
|
function SignedPreKeyRotationError(numbers, message, timestamp) {
|
|
|
|
ReplayableError.call(this, {
|
|
|
|
functionCode : Type.RETRY_SEND_MESSAGE_PROTO,
|
2018-01-08 19:08:25 +00:00
|
|
|
args : [numbers, message, timestamp],
|
|
|
|
name : 'SignedPreKeyRotationError',
|
|
|
|
message : "Too many signed prekey rotation failures"
|
2017-02-16 02:27:06 +00:00
|
|
|
});
|
|
|
|
}
|
2018-01-08 19:08:25 +00:00
|
|
|
inherit(ReplayableError, SignedPreKeyRotationError);
|
2017-02-16 02:27:06 +00:00
|
|
|
|
2016-02-06 00:42:53 +00:00
|
|
|
function MessageError(message, httpError) {
|
2018-01-08 19:08:25 +00:00
|
|
|
this.code = httpError.code;
|
|
|
|
|
2016-02-06 00:42:53 +00:00
|
|
|
ReplayableError.call(this, {
|
|
|
|
functionCode : Type.REBUILD_MESSAGE,
|
2018-01-08 19:08:25 +00:00
|
|
|
args : [message],
|
|
|
|
name : 'MessageError',
|
|
|
|
message : httpError.message
|
2016-02-06 00:42:53 +00:00
|
|
|
});
|
2018-01-08 19:08:25 +00:00
|
|
|
|
|
|
|
appendStack(this, httpError);
|
2016-02-06 00:42:53 +00:00
|
|
|
}
|
2018-01-08 19:08:25 +00:00
|
|
|
inherit(ReplayableError, MessageError);
|
2016-02-06 00:42:53 +00:00
|
|
|
|
2016-05-25 22:04:03 +00:00
|
|
|
function UnregisteredUserError(number, httpError) {
|
2018-01-08 19:08:25 +00:00
|
|
|
this.message = httpError.message;
|
2016-05-25 22:04:03 +00:00
|
|
|
this.name = 'UnregisteredUserError';
|
2018-01-08 19:08:25 +00:00
|
|
|
|
|
|
|
Error.call(this, this.message);
|
|
|
|
|
|
|
|
// Maintains proper stack trace, where our error was thrown (only available on V8)
|
|
|
|
// via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
|
|
|
|
if (Error.captureStackTrace) {
|
|
|
|
Error.captureStackTrace(this);
|
|
|
|
}
|
|
|
|
|
2016-05-25 22:04:03 +00:00
|
|
|
this.number = number;
|
|
|
|
this.code = httpError.code;
|
2018-01-08 19:08:25 +00:00
|
|
|
|
|
|
|
appendStack(this, httpError);
|
2016-05-25 22:04:03 +00:00
|
|
|
}
|
2018-01-08 19:08:25 +00:00
|
|
|
inherit(Error, UnregisteredUserError);
|
2016-02-06 00:42:53 +00:00
|
|
|
|
2016-05-25 22:04:03 +00:00
|
|
|
window.textsecure.UnregisteredUserError = UnregisteredUserError;
|
2015-10-03 01:31:07 +00:00
|
|
|
window.textsecure.SendMessageNetworkError = SendMessageNetworkError;
|
2014-12-19 01:50:04 +00:00
|
|
|
window.textsecure.IncomingIdentityKeyError = IncomingIdentityKeyError;
|
|
|
|
window.textsecure.OutgoingIdentityKeyError = OutgoingIdentityKeyError;
|
|
|
|
window.textsecure.ReplayableError = ReplayableError;
|
2015-10-03 01:31:07 +00:00
|
|
|
window.textsecure.OutgoingMessageError = OutgoingMessageError;
|
2016-02-06 00:42:53 +00:00
|
|
|
window.textsecure.MessageError = MessageError;
|
2017-02-16 02:27:06 +00:00
|
|
|
window.textsecure.SignedPreKeyRotationError = SignedPreKeyRotationError;
|
2014-12-19 01:50:04 +00:00
|
|
|
|
|
|
|
})();
|