2015-09-07 21:53:43 +00:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2015-04-29 18:56:40 +00:00
|
|
|
*/
|
|
|
|
|
2015-11-24 20:40:00 +00:00
|
|
|
function MessageReceiver(url, username, password, signalingKey, attachment_server_url) {
|
2015-09-01 23:59:06 +00:00
|
|
|
this.url = url;
|
|
|
|
this.signalingKey = signalingKey;
|
|
|
|
this.username = username;
|
|
|
|
this.password = password;
|
2015-11-24 20:40:00 +00:00
|
|
|
this.server = new TextSecureServer(url, username, password, attachment_server_url);
|
2015-04-29 18:56:40 +00:00
|
|
|
|
2016-05-03 00:15:47 +00:00
|
|
|
var address = libsignal.SignalProtocolAddress.fromString(username);
|
|
|
|
this.number = address.getName();
|
|
|
|
this.deviceId = address.getDeviceId();
|
2015-09-01 23:59:06 +00:00
|
|
|
}
|
2016-01-14 22:07:00 +00:00
|
|
|
|
|
|
|
MessageReceiver.prototype = new textsecure.EventTarget();
|
|
|
|
MessageReceiver.prototype.extend({
|
2015-09-01 23:59:06 +00:00
|
|
|
constructor: MessageReceiver,
|
|
|
|
connect: function() {
|
|
|
|
if (this.socket && this.socket.readyState !== WebSocket.CLOSED) {
|
|
|
|
this.socket.close();
|
|
|
|
}
|
|
|
|
console.log('opening websocket');
|
|
|
|
// initialize the socket and start listening for messages
|
|
|
|
this.socket = this.server.getMessageSocket();
|
|
|
|
this.socket.onclose = this.onclose.bind(this);
|
|
|
|
this.socket.onerror = this.onerror.bind(this);
|
|
|
|
this.socket.onopen = this.onopen.bind(this);
|
|
|
|
this.wsr = new WebSocketResource(this.socket, {
|
2015-12-04 18:25:50 +00:00
|
|
|
handleRequest: this.handleRequest.bind(this),
|
2015-09-01 23:59:06 +00:00
|
|
|
keepalive: { path: '/v1/keepalive', disconnect: true }
|
|
|
|
});
|
2015-10-26 21:16:46 +00:00
|
|
|
this.pending = Promise.resolve();
|
2015-09-01 23:59:06 +00:00
|
|
|
},
|
|
|
|
close: function() {
|
2015-11-05 20:45:18 +00:00
|
|
|
this.socket.close(3000, 'called close');
|
2015-09-01 23:59:06 +00:00
|
|
|
delete this.listeners;
|
|
|
|
},
|
|
|
|
onopen: function() {
|
|
|
|
console.log('websocket open');
|
|
|
|
},
|
|
|
|
onerror: function(error) {
|
|
|
|
console.log('websocket error');
|
|
|
|
},
|
|
|
|
onclose: function(ev) {
|
2015-11-05 20:45:18 +00:00
|
|
|
console.log('websocket closed', ev.code, ev.reason || '');
|
|
|
|
if (ev.code === 3000) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-01 23:59:06 +00:00
|
|
|
var eventTarget = this;
|
|
|
|
// possible 403 or network issue. Make an request to confirm
|
|
|
|
this.server.getDevices(this.number).
|
|
|
|
then(this.connect.bind(this)). // No HTTP error? Reconnect
|
|
|
|
catch(function(e) {
|
2015-09-01 19:13:38 +00:00
|
|
|
var ev = new Event('error');
|
2015-06-01 21:08:21 +00:00
|
|
|
ev.error = e;
|
2015-09-01 23:59:06 +00:00
|
|
|
eventTarget.dispatchEvent(ev);
|
|
|
|
});
|
|
|
|
},
|
2015-12-04 18:25:50 +00:00
|
|
|
handleRequest: function(request) {
|
2015-12-04 06:29:49 +00:00
|
|
|
// We do the message decryption here, instead of in the ordered pending queue,
|
|
|
|
// to avoid exposing the time it took us to process messages through the time-to-ack.
|
|
|
|
|
|
|
|
// TODO: handle different types of requests. for now we blindly assume
|
2015-12-04 18:25:50 +00:00
|
|
|
// PUT /messages <encrypted Envelope>
|
2015-12-04 06:29:49 +00:00
|
|
|
textsecure.crypto.decryptWebsocketMessage(request.body, this.signalingKey).then(function(plaintext) {
|
2016-01-11 20:08:54 +00:00
|
|
|
var envelope = textsecure.protobuf.Envelope.decode(plaintext);
|
2015-09-01 23:59:06 +00:00
|
|
|
// After this point, decoding errors are not the server's
|
|
|
|
// fault, and we should handle them gracefully and tell the
|
|
|
|
// user they received an invalid message
|
|
|
|
request.respond(200, 'OK');
|
2016-01-11 20:08:54 +00:00
|
|
|
this.queueEnvelope(envelope);
|
2015-09-01 23:59:06 +00:00
|
|
|
|
|
|
|
}.bind(this)).catch(function(e) {
|
|
|
|
request.respond(500, 'Bad encrypted websocket message');
|
|
|
|
console.log("Error handling incoming message:", e);
|
|
|
|
var ev = new Event('error');
|
|
|
|
ev.error = e;
|
|
|
|
this.dispatchEvent(ev);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
2015-12-04 18:25:50 +00:00
|
|
|
queueEnvelope: function(envelope) {
|
|
|
|
var handleEnvelope = this.handleEnvelope.bind(this, envelope);
|
|
|
|
this.pending = this.pending.then(handleEnvelope, handleEnvelope);
|
|
|
|
},
|
2016-01-11 20:08:54 +00:00
|
|
|
handleEnvelope: function(envelope) {
|
2016-02-02 21:24:29 +00:00
|
|
|
console.log('envelope from', envelope.source + '.' + envelope.sourceDevice, envelope.timestamp.toNumber());
|
2015-12-04 18:25:50 +00:00
|
|
|
if (envelope.type === textsecure.protobuf.Envelope.Type.RECEIPT) {
|
|
|
|
return this.onDeliveryReceipt(envelope);
|
|
|
|
} else if (envelope.content) {
|
|
|
|
return this.handleContentMessage(envelope);
|
|
|
|
} else if (envelope.legacyMessage) {
|
|
|
|
return this.handleLegacyMessage(envelope);
|
|
|
|
} else {
|
|
|
|
throw new Error('Received message with no content and no legacyMessage');
|
|
|
|
}
|
|
|
|
},
|
2015-09-01 23:59:06 +00:00
|
|
|
getStatus: function() {
|
|
|
|
if (this.socket) {
|
|
|
|
return this.socket.readyState;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onDeliveryReceipt: function (envelope) {
|
|
|
|
var ev = new Event('receipt');
|
|
|
|
ev.proto = envelope;
|
|
|
|
this.dispatchEvent(ev);
|
|
|
|
},
|
|
|
|
decrypt: function(envelope, ciphertext) {
|
2015-09-26 00:25:25 +00:00
|
|
|
var promise;
|
2016-05-01 21:49:33 +00:00
|
|
|
var address = new libsignal.SignalProtocolAddress(envelope.source, envelope.sourceDevice);
|
|
|
|
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);
|
2015-09-26 00:25:25 +00:00
|
|
|
switch(envelope.type) {
|
|
|
|
case textsecure.protobuf.Envelope.Type.CIPHERTEXT:
|
2016-05-01 21:49:33 +00:00
|
|
|
promise = sessionCipher.decryptWhisperMessage(ciphertext.toArrayBuffer());
|
2015-09-26 00:25:25 +00:00
|
|
|
break;
|
|
|
|
case textsecure.protobuf.Envelope.Type.PREKEY_BUNDLE:
|
2016-05-01 22:00:06 +00:00
|
|
|
console.log('prekey whisper message');
|
|
|
|
ciphertext.mark();
|
|
|
|
var version = ciphertext.readUint8();
|
|
|
|
if ((version & 0xF) > 3 || (version >> 4) < 3) {
|
|
|
|
// min version > 3 or max version < 3
|
|
|
|
throw new Error("Incompatible version byte");
|
|
|
|
}
|
|
|
|
promise = sessionCipher.decryptPreKeyWhisperMessage(ciphertext).catch(function(e) {
|
|
|
|
if (e.message === 'Unknown identity key') {
|
|
|
|
ciphertext.reset(); // restore the version byte.
|
|
|
|
|
|
|
|
// create an error that the UI will pick up and ask the
|
|
|
|
// user if they want to re-negotiate
|
|
|
|
throw new textsecure.IncomingIdentityKeyError(
|
|
|
|
address.toString(),
|
|
|
|
ciphertext.toArrayBuffer(),
|
|
|
|
e.identityKey
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
});
|
2015-09-26 00:25:25 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
promise = Promise.reject(new Error("Unknown message type"));
|
|
|
|
}
|
|
|
|
return promise.catch(function(error) {
|
2015-09-01 23:59:06 +00:00
|
|
|
var ev = new Event('error');
|
|
|
|
ev.error = error;
|
2015-06-01 21:08:21 +00:00
|
|
|
ev.proto = envelope;
|
2015-09-01 19:13:38 +00:00
|
|
|
this.dispatchEvent(ev);
|
2015-10-04 06:19:53 +00:00
|
|
|
return Promise.reject(error);
|
2015-09-01 23:59:06 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
handleSentMessage: function(destination, timestamp, message) {
|
2016-02-04 19:25:25 +00:00
|
|
|
var p = Promise.resolve();
|
|
|
|
if ((message.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION) ==
|
|
|
|
textsecure.protobuf.DataMessage.Flags.END_SESSION ) {
|
|
|
|
p = this.handleEndSession(destination);
|
|
|
|
}
|
|
|
|
return p.then(function() {
|
|
|
|
return this.processDecrypted(message, this.number).then(function(message) {
|
|
|
|
var ev = new Event('sent');
|
|
|
|
ev.data = {
|
|
|
|
destination : destination,
|
|
|
|
timestamp : timestamp.toNumber(),
|
|
|
|
message : message
|
|
|
|
};
|
|
|
|
this.dispatchEvent(ev);
|
|
|
|
}.bind(this));
|
2016-02-05 19:10:46 +00:00
|
|
|
}.bind(this));
|
2015-09-01 23:59:06 +00:00
|
|
|
},
|
2016-04-02 01:12:38 +00:00
|
|
|
handleDataMessage: function(envelope, message) {
|
2016-02-02 21:24:29 +00:00
|
|
|
var encodedNumber = envelope.source + '.' + envelope.sourceDevice;
|
|
|
|
console.log('data message from', encodedNumber, envelope.timestamp.toNumber());
|
2016-02-03 19:04:39 +00:00
|
|
|
var p = Promise.resolve();
|
2015-09-01 23:59:06 +00:00
|
|
|
if ((message.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION) ==
|
|
|
|
textsecure.protobuf.DataMessage.Flags.END_SESSION ) {
|
2016-02-03 19:04:39 +00:00
|
|
|
p = this.handleEndSession(envelope.source);
|
2015-09-01 23:59:06 +00:00
|
|
|
}
|
2016-02-03 19:04:39 +00:00
|
|
|
return p.then(function() {
|
|
|
|
return this.processDecrypted(message, envelope.source).then(function(message) {
|
|
|
|
var ev = new Event('message');
|
|
|
|
ev.data = {
|
|
|
|
source : envelope.source,
|
|
|
|
timestamp : envelope.timestamp.toNumber(),
|
|
|
|
message : message
|
|
|
|
};
|
|
|
|
this.dispatchEvent(ev);
|
|
|
|
}.bind(this));
|
2015-09-01 23:59:06 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
handleLegacyMessage: function (envelope) {
|
|
|
|
return this.decrypt(envelope, envelope.legacyMessage).then(function(result) {
|
|
|
|
var plaintext = result[0]; // array buffer
|
|
|
|
var message = textsecure.protobuf.DataMessage.decode(plaintext);
|
2016-04-02 01:12:38 +00:00
|
|
|
return this.handleDataMessage(envelope, message);
|
2015-09-01 23:59:06 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
handleContentMessage: function (envelope) {
|
|
|
|
return this.decrypt(envelope, envelope.content).then(function(result) {
|
|
|
|
var plaintext = result[0]; // array buffer
|
|
|
|
var content = textsecure.protobuf.Content.decode(plaintext);
|
|
|
|
if (content.syncMessage) {
|
|
|
|
return this.handleSyncMessage(envelope, content.syncMessage);
|
|
|
|
} else if (content.dataMessage) {
|
2016-04-02 01:12:38 +00:00
|
|
|
return this.handleDataMessage(envelope, content.dataMessage);
|
2015-09-01 23:59:06 +00:00
|
|
|
} else {
|
|
|
|
throw new Error('Got Content message with no dataMessage and no syncMessage');
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
2015-09-01 23:59:06 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
handleSyncMessage: function(envelope, syncMessage) {
|
|
|
|
if (envelope.source !== this.number) {
|
|
|
|
throw new Error('Received sync message from another number');
|
|
|
|
}
|
|
|
|
if (envelope.sourceDevice == this.deviceId) {
|
|
|
|
throw new Error('Received sync message from our own device');
|
|
|
|
}
|
|
|
|
if (syncMessage.sent) {
|
|
|
|
var sentMessage = syncMessage.sent;
|
2016-02-20 00:28:08 +00:00
|
|
|
console.log('sent message to',
|
|
|
|
sentMessage.destination,
|
|
|
|
sentMessage.timestamp.toNumber(),
|
|
|
|
'from', envelope.source + '.' + envelope.sourceDevice
|
|
|
|
);
|
2015-09-01 23:59:06 +00:00
|
|
|
return this.handleSentMessage(
|
|
|
|
sentMessage.destination,
|
|
|
|
sentMessage.timestamp,
|
|
|
|
sentMessage.message
|
|
|
|
);
|
|
|
|
} else if (syncMessage.contacts) {
|
|
|
|
this.handleContacts(syncMessage.contacts);
|
|
|
|
} else if (syncMessage.groups) {
|
|
|
|
this.handleGroups(syncMessage.groups);
|
2015-12-16 22:15:43 +00:00
|
|
|
} else if (syncMessage.request) {
|
|
|
|
console.log('Got SyncMessage Request');
|
2016-02-20 00:28:08 +00:00
|
|
|
} else if (syncMessage.read) {
|
|
|
|
console.log('read messages',
|
|
|
|
'from', envelope.source + '.' + envelope.sourceDevice);
|
|
|
|
this.handleRead(syncMessage.read);
|
2015-09-01 23:59:06 +00:00
|
|
|
} else {
|
2015-12-16 22:15:43 +00:00
|
|
|
throw new Error('Got empty SyncMessage');
|
2015-09-01 23:59:06 +00:00
|
|
|
}
|
|
|
|
},
|
2016-02-20 00:28:08 +00:00
|
|
|
handleRead: function(read) {
|
|
|
|
for (var i = 0; i < read.length; ++i) {
|
|
|
|
var ev = new Event('read');
|
|
|
|
ev.timestamp = read[i].timestamp;
|
|
|
|
ev.sender = read[i].sender;
|
|
|
|
this.dispatchEvent(ev);
|
|
|
|
}
|
|
|
|
},
|
2015-09-01 23:59:06 +00:00
|
|
|
handleContacts: function(contacts) {
|
2015-12-16 23:24:07 +00:00
|
|
|
console.log('contact sync');
|
2015-09-01 23:59:06 +00:00
|
|
|
var eventTarget = this;
|
|
|
|
var attachmentPointer = contacts.blob;
|
|
|
|
return this.handleAttachment(attachmentPointer).then(function() {
|
|
|
|
var contactBuffer = new ContactBuffer(attachmentPointer.data);
|
|
|
|
var contactDetails = contactBuffer.next();
|
|
|
|
while (contactDetails !== undefined) {
|
|
|
|
var ev = new Event('contact');
|
|
|
|
ev.contactDetails = contactDetails;
|
|
|
|
eventTarget.dispatchEvent(ev);
|
|
|
|
contactDetails = contactBuffer.next();
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
2015-09-01 23:59:06 +00:00
|
|
|
eventTarget.dispatchEvent(new Event('contactsync'));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
handleGroups: function(groups) {
|
2015-12-16 23:24:07 +00:00
|
|
|
console.log('group sync');
|
2015-09-01 23:59:06 +00:00
|
|
|
var eventTarget = this;
|
|
|
|
var attachmentPointer = groups.blob;
|
|
|
|
return this.handleAttachment(attachmentPointer).then(function() {
|
|
|
|
var groupBuffer = new GroupBuffer(attachmentPointer.data);
|
|
|
|
var groupDetails = groupBuffer.next();
|
2016-01-01 03:19:28 +00:00
|
|
|
var promises = [];
|
2015-09-01 23:59:06 +00:00
|
|
|
while (groupDetails !== undefined) {
|
2016-01-01 03:19:28 +00:00
|
|
|
var promise = (function(groupDetails) {
|
2016-03-13 01:14:17 +00:00
|
|
|
groupDetails.id = groupDetails.id.toBinary();
|
2016-02-11 22:12:09 +00:00
|
|
|
if (groupDetails.active) {
|
|
|
|
return textsecure.storage.groups.getGroup(groupDetails.id).
|
|
|
|
then(function(existingGroup) {
|
|
|
|
if (existingGroup === undefined) {
|
|
|
|
return textsecure.storage.groups.createNewGroup(
|
|
|
|
groupDetails.members, groupDetails.id
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return textsecure.storage.groups.updateNumbers(
|
|
|
|
groupDetails.id, groupDetails.members
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}).then(function() { return groupDetails });
|
|
|
|
} else {
|
|
|
|
return Promise.resolve(groupDetails);
|
|
|
|
}
|
|
|
|
})(groupDetails).then(function(groupDetails) {
|
|
|
|
var ev = new Event('group');
|
|
|
|
ev.groupDetails = groupDetails;
|
|
|
|
eventTarget.dispatchEvent(ev);
|
|
|
|
}).catch(function(e) {
|
|
|
|
console.log('error processing group', e);
|
|
|
|
});
|
2015-09-01 23:59:06 +00:00
|
|
|
groupDetails = groupBuffer.next();
|
2016-01-01 03:19:28 +00:00
|
|
|
promises.push(promise);
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
2016-01-01 03:19:28 +00:00
|
|
|
Promise.all(promises).then(function() {
|
|
|
|
eventTarget.dispatchEvent(new Event('groupsync'));
|
|
|
|
});
|
2015-09-01 23:59:06 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
handleAttachment: function(attachment) {
|
|
|
|
function decryptAttachment(encrypted) {
|
|
|
|
return textsecure.crypto.decryptAttachment(
|
|
|
|
encrypted,
|
|
|
|
attachment.key.toArrayBuffer()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateAttachment(data) {
|
|
|
|
attachment.data = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.server.getAttachment(attachment.id.toString()).
|
|
|
|
then(decryptAttachment).
|
|
|
|
then(updateAttachment);
|
|
|
|
},
|
2015-09-26 00:25:25 +00:00
|
|
|
tryMessageAgain: function(from, encodedMessage) {
|
2015-10-02 07:02:18 +00:00
|
|
|
var bytes = dcodeIO.ByteBuffer.wrap(encodedMessage);
|
|
|
|
return textsecure.protocol_wrapper.handlePreKeyWhisperMessage(from, bytes).then(function(res) {
|
2015-09-26 00:25:25 +00:00
|
|
|
var finalMessage = textsecure.protobuf.DataMessage.decode(res[0]);
|
|
|
|
|
2016-02-03 19:04:39 +00:00
|
|
|
var p = Promise.resolve();
|
2015-09-26 00:25:25 +00:00
|
|
|
if ((finalMessage.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION)
|
|
|
|
== textsecure.protobuf.DataMessage.Flags.END_SESSION &&
|
2016-02-02 21:24:29 +00:00
|
|
|
finalMessage.sync !== null) {
|
2016-04-18 22:45:04 +00:00
|
|
|
var number = textsecure.utils.unencodeNumber(from)[0];
|
2016-02-03 19:04:39 +00:00
|
|
|
p = this.handleEndSession(number);
|
2016-02-02 21:24:29 +00:00
|
|
|
}
|
2015-09-26 00:25:25 +00:00
|
|
|
|
2016-02-03 19:04:39 +00:00
|
|
|
return p.then(function() {
|
|
|
|
return this.processDecrypted(finalMessage);
|
2016-02-08 23:57:59 +00:00
|
|
|
}.bind(this));
|
2015-09-26 00:25:25 +00:00
|
|
|
}.bind(this));
|
|
|
|
},
|
2016-02-03 19:04:39 +00:00
|
|
|
handleEndSession: function(number) {
|
|
|
|
console.log('got end session');
|
2016-05-01 22:47:07 +00:00
|
|
|
return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) {
|
|
|
|
return Promise.all(deviceIds.map(function(deviceId) {
|
|
|
|
var address = new libsignal.SignalProtocolAddress(number, deviceId);
|
2016-05-01 22:02:55 +00:00
|
|
|
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);
|
|
|
|
|
|
|
|
console.log('closing session for', address.toString());
|
|
|
|
return sessionCipher.closeOpenSessionForDevice();
|
2016-02-03 19:04:39 +00:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
},
|
2015-09-01 23:59:06 +00:00
|
|
|
processDecrypted: function(decrypted, source) {
|
|
|
|
// Now that its decrypted, validate the message and clean it up for consumer processing
|
|
|
|
// Note that messages may (generally) only perform one action and we ignore remaining fields
|
|
|
|
// after the first action.
|
|
|
|
|
|
|
|
if (decrypted.flags == null)
|
|
|
|
decrypted.flags = 0;
|
|
|
|
|
|
|
|
if ((decrypted.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION)
|
|
|
|
== textsecure.protobuf.DataMessage.Flags.END_SESSION) {
|
|
|
|
decrypted.body = null;
|
|
|
|
decrypted.attachments = [];
|
|
|
|
decrypted.group = null;
|
|
|
|
return Promise.resolve(decrypted);
|
|
|
|
}
|
|
|
|
if (decrypted.flags != 0) {
|
|
|
|
throw new Error("Unknown flags in message");
|
|
|
|
}
|
|
|
|
|
|
|
|
var promises = [];
|
|
|
|
|
|
|
|
if (decrypted.group !== null) {
|
2016-03-13 01:14:17 +00:00
|
|
|
decrypted.group.id = decrypted.group.id.toBinary();
|
2015-09-01 23:59:06 +00:00
|
|
|
|
|
|
|
if (decrypted.group.type == textsecure.protobuf.GroupContext.Type.UPDATE) {
|
|
|
|
if (decrypted.group.avatar !== null) {
|
|
|
|
promises.push(this.handleAttachment(decrypted.group.avatar));
|
2015-06-01 21:08:21 +00:00
|
|
|
}
|
2015-09-01 23:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
promises.push(textsecure.storage.groups.getNumbers(decrypted.group.id).then(function(existingGroup) {
|
|
|
|
if (existingGroup === undefined) {
|
|
|
|
if (decrypted.group.type != textsecure.protobuf.GroupContext.Type.UPDATE) {
|
2015-11-07 03:02:57 +00:00
|
|
|
decrypted.group.members = [source];
|
2015-10-02 19:53:45 +00:00
|
|
|
console.log("Got message for unknown group");
|
2015-09-01 23:59:06 +00:00
|
|
|
}
|
|
|
|
return textsecure.storage.groups.createNewGroup(decrypted.group.members, decrypted.group.id);
|
|
|
|
} else {
|
|
|
|
var fromIndex = existingGroup.indexOf(source);
|
|
|
|
|
|
|
|
if (fromIndex < 0) {
|
|
|
|
//TODO: This could be indication of a race...
|
2015-10-02 17:43:34 +00:00
|
|
|
console.log("Sender was not a member of the group they were sending from");
|
2015-09-01 23:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch(decrypted.group.type) {
|
|
|
|
case textsecure.protobuf.GroupContext.Type.UPDATE:
|
|
|
|
return textsecure.storage.groups.updateNumbers(
|
|
|
|
decrypted.group.id, decrypted.group.members
|
|
|
|
).then(function(added) {
|
|
|
|
decrypted.group.added = added;
|
|
|
|
|
|
|
|
if (decrypted.group.avatar === null &&
|
|
|
|
decrypted.group.added.length == 0 &&
|
|
|
|
decrypted.group.name === null) {
|
|
|
|
return;
|
2015-06-22 21:45:42 +00:00
|
|
|
}
|
2015-09-01 23:59:06 +00:00
|
|
|
|
|
|
|
decrypted.body = null;
|
|
|
|
decrypted.attachments = [];
|
2015-06-22 21:45:42 +00:00
|
|
|
});
|
2015-09-01 19:13:38 +00:00
|
|
|
|
2015-09-01 23:59:06 +00:00
|
|
|
break;
|
|
|
|
case textsecure.protobuf.GroupContext.Type.QUIT:
|
|
|
|
decrypted.body = null;
|
|
|
|
decrypted.attachments = [];
|
2015-12-04 02:48:04 +00:00
|
|
|
if (source === this.number) {
|
|
|
|
return textsecure.storage.groups.deleteGroup(decrypted.group.id);
|
|
|
|
} else {
|
|
|
|
return textsecure.storage.groups.removeNumber(decrypted.group.id, source);
|
|
|
|
}
|
2015-09-01 23:59:06 +00:00
|
|
|
case textsecure.protobuf.GroupContext.Type.DELIVER:
|
|
|
|
decrypted.group.name = null;
|
|
|
|
decrypted.group.members = [];
|
|
|
|
decrypted.group.avatar = null;
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Error("Unknown group message type");
|
2015-09-01 19:13:38 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 02:48:04 +00:00
|
|
|
}.bind(this)));
|
2015-09-01 23:59:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (var i in decrypted.attachments) {
|
|
|
|
promises.push(this.handleAttachment(decrypted.attachments[i]));
|
|
|
|
}
|
|
|
|
return Promise.all(promises).then(function() {
|
|
|
|
return decrypted;
|
|
|
|
});
|
|
|
|
}
|
2016-01-14 22:07:00 +00:00
|
|
|
});
|
2015-04-29 18:56:40 +00:00
|
|
|
|
2015-09-01 23:59:06 +00:00
|
|
|
window.textsecure = window.textsecure || {};
|
2015-09-01 19:13:38 +00:00
|
|
|
|
2015-11-24 20:40:00 +00:00
|
|
|
textsecure.MessageReceiver = function(url, username, password, signalingKey, attachment_server_url) {
|
|
|
|
var messageReceiver = new MessageReceiver(url, username, password, signalingKey, attachment_server_url);
|
2015-09-01 23:59:06 +00:00
|
|
|
this.addEventListener = messageReceiver.addEventListener.bind(messageReceiver);
|
|
|
|
this.removeEventListener = messageReceiver.removeEventListener.bind(messageReceiver);
|
|
|
|
this.getStatus = messageReceiver.getStatus.bind(messageReceiver);
|
|
|
|
this.close = messageReceiver.close.bind(messageReceiver);
|
|
|
|
messageReceiver.connect();
|
2015-09-26 00:25:25 +00:00
|
|
|
|
|
|
|
textsecure.replay.registerFunction(messageReceiver.tryMessageAgain.bind(messageReceiver), textsecure.replay.Type.INIT_SESSION);
|
2015-09-01 23:59:06 +00:00
|
|
|
};
|
2015-04-29 18:56:40 +00:00
|
|
|
|
2015-09-01 23:59:06 +00:00
|
|
|
textsecure.MessageReceiver.prototype = {
|
|
|
|
constructor: textsecure.MessageReceiver
|
|
|
|
};
|
2015-06-01 21:08:21 +00:00
|
|
|
|