Send-to-self works =D
This commit is contained in:
parent
136a8941c1
commit
2ae5628122
5 changed files with 43 additions and 33 deletions
13
js/api.js
13
js/api.js
|
@ -1,8 +1,12 @@
|
||||||
/************************************************
|
/************************************************
|
||||||
*** Utilities to communicate with the server ***
|
*** Utilities to communicate with the server ***
|
||||||
************************************************/
|
************************************************/
|
||||||
|
// WARNING: THIS SERVER LOGS KEY MATERIAL FOR TESTING
|
||||||
var URL_BASE = "http://sushiforeveryone.bluematt.me";
|
var URL_BASE = "http://sushiforeveryone.bluematt.me";
|
||||||
|
|
||||||
|
// This is the real server
|
||||||
//var URL_BASE = "https://textsecure-service.whispersystems.org";
|
//var URL_BASE = "https://textsecure-service.whispersystems.org";
|
||||||
|
|
||||||
var URL_CALLS = {};
|
var URL_CALLS = {};
|
||||||
URL_CALLS['accounts'] = "/v1/accounts";
|
URL_CALLS['accounts'] = "/v1/accounts";
|
||||||
URL_CALLS['devices'] = "/v1/devices";
|
URL_CALLS['devices'] = "/v1/devices";
|
||||||
|
@ -153,13 +157,4 @@ var API = new function() {
|
||||||
error_callback : error_callback
|
error_callback : error_callback
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
this.pushMessage = function(messageId) {
|
|
||||||
this.doAjax({
|
|
||||||
call : 'push',
|
|
||||||
httpType : 'PUT',
|
|
||||||
urlParameters : '/' + message.id,
|
|
||||||
do_auth : true
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}(); // API
|
}(); // API
|
||||||
|
|
|
@ -5,7 +5,8 @@ registerOnLoadFunction(function() {
|
||||||
} else {
|
} else {
|
||||||
if (isRegistrationDone()) {
|
if (isRegistrationDone()) {
|
||||||
subscribeToPush(function(message) {
|
subscribeToPush(function(message) {
|
||||||
console.log("Got message from " + message.source + ": \"" + getString(message.message));
|
console.log("Got message from " + message.pushMessage.source + "." + message.pushMessage.sourceDevice +
|
||||||
|
': "' + getString(message.message.body) + '"');
|
||||||
var newUnreadCount = storage.getUnencrypted("unreadCount") + 1;
|
var newUnreadCount = storage.getUnencrypted("unreadCount") + 1;
|
||||||
storage.putUnencrypted("unreadCount", newUnreadCount);
|
storage.putUnencrypted("unreadCount", newUnreadCount);
|
||||||
chrome.browserAction.setBadgeText({text: newUnreadCount + ""});
|
chrome.browserAction.setBadgeText({text: newUnreadCount + ""});
|
||||||
|
|
|
@ -290,18 +290,17 @@ function getMessageMap() {
|
||||||
return storage.getEncrypted("messageMap", {});
|
return storage.getEncrypted("messageMap", {});
|
||||||
}
|
}
|
||||||
|
|
||||||
function storeMessage(outgoingMessageSignal) {
|
function storeMessage(messageObject) {
|
||||||
var messageMap = getMessageMap();
|
var messageMap = getMessageMap();
|
||||||
var conversation = messageMap[outgoingMessageSignal.source]; //TODO: Also support Group message IDs here
|
var conversation = messageMap[messageObject.pushMessage.source]; //TODO: Also support Group message IDs here
|
||||||
if (conversation === undefined) {
|
if (conversation === undefined) {
|
||||||
conversation = []
|
conversation = []
|
||||||
messageMap[outgoingMessageSignal.source] = conversation;
|
messageMap[messageObject.pushMessage.source] = conversation;
|
||||||
}
|
}
|
||||||
|
|
||||||
conversation[conversation.length] = { message: getString(outgoingMessageSignal.message),
|
conversation[conversation.length] = { message: getString(messageObject.message.body),
|
||||||
destinations: outgoingMessageSignal.destinations,
|
sender: messageObject.pushMessage.source,
|
||||||
sender: outgoingMessageSignal.source,
|
timestamp: messageObject.pushMessage.timestamp.div(dcodeIO.Long.fromNumber(1000)).toNumber() };
|
||||||
timestamp: outgoingMessageSignal.timestamp.div(dcodeIO.Long.fromNumber(1000)).toNumber() };
|
|
||||||
storage.putEncrypted("messageMap", messageMap);
|
storage.putEncrypted("messageMap", messageMap);
|
||||||
chrome.runtime.sendMessage(conversation[conversation.length - 1]);
|
chrome.runtime.sendMessage(conversation[conversation.length - 1]);
|
||||||
}
|
}
|
||||||
|
@ -756,17 +755,21 @@ var crypto_tests = {};
|
||||||
crypto.handleIncomingPushMessageProto = function(proto, callback) {
|
crypto.handleIncomingPushMessageProto = function(proto, callback) {
|
||||||
switch(proto.type) {
|
switch(proto.type) {
|
||||||
case 0: //TYPE_MESSAGE_PLAINTEXT
|
case 0: //TYPE_MESSAGE_PLAINTEXT
|
||||||
callback(decodePushMessageContentProtobuf(getString(proto.message)));
|
callback({message: decodePushMessageContentProtobuf(getString(proto.message)), pushMessage:proto});
|
||||||
break;
|
break;
|
||||||
case 1: //TYPE_MESSAGE_CIPHERTEXT
|
case 1: //TYPE_MESSAGE_CIPHERTEXT
|
||||||
decryptWhisperMessage(proto.source, getString(proto.message), function(result) { callback(result); });
|
decryptWhisperMessage(proto.source, getString(proto.message), function(result) {
|
||||||
|
callback({message: result, pushMessage: proto});
|
||||||
|
});
|
||||||
break;
|
break;
|
||||||
case 3: //TYPE_MESSAGE_PREKEY_BUNDLE
|
case 3: //TYPE_MESSAGE_PREKEY_BUNDLE
|
||||||
if (proto.message.readUint8() != (2 << 4 | 2))
|
if (proto.message.readUint8() != (2 << 4 | 2))
|
||||||
throw "Bad version byte"; //TODO: I don't believe this actually happens on the wire
|
throw "Bad version byte";
|
||||||
var preKeyProto = decodePreKeyWhisperMessageProtobuf(getString(proto.message));
|
var preKeyProto = decodePreKeyWhisperMessageProtobuf(getString(proto.message));
|
||||||
initSessionFromPreKeyWhisperMessage(proto.source, preKeyProto, function() {
|
initSessionFromPreKeyWhisperMessage(proto.source, preKeyProto, function() {
|
||||||
decryptWhisperMessage(proto.source, getString(preKeyProto.message), function(result) { callback(result); });
|
decryptWhisperMessage(proto.source, getString(preKeyProto.message), function(result) {
|
||||||
|
callback({message: result, pushMessage: proto});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -867,7 +870,12 @@ var crypto_tests = {};
|
||||||
|
|
||||||
|
|
||||||
// message_callback(decoded_protobuf) (use decodeMessage(proto))
|
// message_callback(decoded_protobuf) (use decodeMessage(proto))
|
||||||
|
var subscribeToPushMessageSemaphore = 1;
|
||||||
function subscribeToPush(message_callback) {
|
function subscribeToPush(message_callback) {
|
||||||
|
if (subscribeToPushMessageSemaphore <= 0)
|
||||||
|
return;
|
||||||
|
subscribeToPushMessageSemaphore--;
|
||||||
|
|
||||||
var user = storage.getUnencrypted("number_id");
|
var user = storage.getUnencrypted("number_id");
|
||||||
var password = storage.getEncrypted("password");
|
var password = storage.getEncrypted("password");
|
||||||
var URL = URL_BASE.replace(/^http:/g, "ws:").replace(/^https:/g, "wss:") + URL_CALLS['push'] + "/?user=%2B" + getString(user).substring(1) + "&password=" + getString(password);
|
var URL = URL_BASE.replace(/^http:/g, "ws:").replace(/^https:/g, "wss:") + URL_CALLS['push'] + "/?user=%2B" + getString(user).substring(1) + "&password=" + getString(password);
|
||||||
|
@ -876,10 +884,12 @@ function subscribeToPush(message_callback) {
|
||||||
//TODO: GUI
|
//TODO: GUI
|
||||||
socket.onerror = function(socketEvent) {
|
socket.onerror = function(socketEvent) {
|
||||||
console.log('Server is down :(');
|
console.log('Server is down :(');
|
||||||
|
subscribeToPushMessageSemaphore++;
|
||||||
setTimeout(function() { subscribeToPush(message_callback); }, 1000);
|
setTimeout(function() { subscribeToPush(message_callback); }, 1000);
|
||||||
};
|
};
|
||||||
socket.onclose = function(socketEvent) {
|
socket.onclose = function(socketEvent) {
|
||||||
console.log('Server closed :(');
|
console.log('Server closed :(');
|
||||||
|
subscribeToPushMessageSemaphore++;
|
||||||
setTimeout(function() { subscribeToPush(message_callback); }, 1000);
|
setTimeout(function() { subscribeToPush(message_callback); }, 1000);
|
||||||
};
|
};
|
||||||
socket.onopen = function(socketEvent) {
|
socket.onopen = function(socketEvent) {
|
||||||
|
@ -888,8 +898,7 @@ function subscribeToPush(message_callback) {
|
||||||
|
|
||||||
socket.onmessage = function(response) {
|
socket.onmessage = function(response) {
|
||||||
try {
|
try {
|
||||||
// Some bug in Atmosphere.js is forcing trackMessageLength to true
|
var message = JSON.parse(response.data);
|
||||||
var message = JSON.parse(response.responseBody.split("|")[1]);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Error parsing server JSON message: ' + response.responseBody.split("|")[1]);
|
console.log('Error parsing server JSON message: ' + response.responseBody.split("|")[1]);
|
||||||
return;
|
return;
|
||||||
|
@ -901,7 +910,7 @@ function subscribeToPush(message_callback) {
|
||||||
var proto = decodeIncomingPushMessageProtobuf(plaintext);
|
var proto = decodeIncomingPushMessageProtobuf(plaintext);
|
||||||
// After this point, a) decoding errors are not the server's fault, and
|
// After this point, a) decoding errors are not the server's fault, and
|
||||||
// b) we should handle them gracefully and tell the user they received an invalid message
|
// b) we should handle them gracefully and tell the user they received an invalid message
|
||||||
API.pushMessage(message.id);
|
socket.send(JSON.stringify({type: 1, id: message.id}));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Error decoding message: " + e);
|
console.log("Error decoding message: " + e);
|
||||||
return;
|
return;
|
||||||
|
@ -1023,16 +1032,21 @@ function sendMessageToNumbers(numbers, message, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < numbers.length; i++) {
|
for (var i = 0; i < numbers.length; i++) {
|
||||||
var devicesForNumber = getDeviceObjectListFromNumber(numbers[i]);
|
var number = numbers[i];
|
||||||
|
var devicesForNumber = getDeviceObjectListFromNumber(number);
|
||||||
|
|
||||||
if (devicesForNumber.length == 0) {
|
if (devicesForNumber.length == 0) {
|
||||||
getKeysForNumber(numbers[i], function(identity_key) {
|
getKeysForNumber(number, function(identity_key) {
|
||||||
doSendMessage(numbers[i], devicesForNumber, message);
|
devicesForNumber = getDeviceObjectListFromNumber(number);
|
||||||
|
if (devicesForNumber.length == 0)
|
||||||
|
registerError(number, "Failed to retreive new device keys for number " + number);
|
||||||
|
else
|
||||||
|
doSendMessage(number, devicesForNumber, message);
|
||||||
}, function(error_msg) {
|
}, function(error_msg) {
|
||||||
registerError(numbers[i], "Failed to retreive new device keys for number " + numbers[i]);
|
registerError(number, "Failed to retreive new device keys for number " + number);
|
||||||
});
|
});
|
||||||
} else
|
} else
|
||||||
doSendMessage(numbers[i], devicesForNumber, message);
|
doSendMessage(number, devicesForNumber, message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -74,7 +74,7 @@ registerOnLoadFunction(function() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var messageProto = new PushMessageContentProtobuf();
|
var messageProto = new PushMessageContentProtobuf();
|
||||||
messageProto.body = $("#popup_send_message").val();
|
messageProto.body = $("#popup_send_text").val();
|
||||||
sendMessageToNumbers(numbers, messageProto,
|
sendMessageToNumbers(numbers, messageProto,
|
||||||
//TODO: Handle result
|
//TODO: Handle result
|
||||||
function(thing) {console.log(thing);});
|
function(thing) {console.log(thing);});
|
||||||
|
|
|
@ -104,8 +104,8 @@ registerOnLoadFunction(function() {
|
||||||
source: "+19999999999", timestamp: 42, message: text_message.encode() };
|
source: "+19999999999", timestamp: 42, message: text_message.encode() };
|
||||||
|
|
||||||
crypto.handleIncomingPushMessageProto(server_message, function(message) {
|
crypto.handleIncomingPushMessageProto(server_message, function(message) {
|
||||||
callback(message.body == text_message.body &&
|
callback(message.message.body == text_message.body &&
|
||||||
message.attachments.length == text_message.attachments.length &&
|
message.message.attachments.length == text_message.attachments.length &&
|
||||||
text_message.attachments.length == 0);
|
text_message.attachments.length == 0);
|
||||||
});
|
});
|
||||||
}, 'Unencrypted PushMessageProto "decrypt"', true);
|
}, 'Unencrypted PushMessageProto "decrypt"', true);
|
||||||
|
@ -211,7 +211,7 @@ encryptedMessage: hexToArrayBuffer("415a326e6f457937756a6c5355785876342f6b585634
|
||||||
var b64 = base64EncArr(new Uint8Array(toArrayBuffer(v.aliceToBob)));
|
var b64 = base64EncArr(new Uint8Array(toArrayBuffer(v.aliceToBob)));
|
||||||
var thing = IncomingPushMessageProtobuf.decode(b64);
|
var thing = IncomingPushMessageProtobuf.decode(b64);
|
||||||
crypto.handleIncomingPushMessageProto(thing, function(decrypted_message) {
|
crypto.handleIncomingPushMessageProto(thing, function(decrypted_message) {
|
||||||
callback(decrypted_message.body == "Hi Bob!" && decrypted_message.attachments.length == 0);
|
callback(decrypted_message.message.body == "Hi Bob!" && decrypted_message.message.attachments.length == 0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue