Cable-Desktop/js/options.js

146 lines
4.5 KiB
JavaScript
Raw Normal View History

/* vim: ts=4:sw=4
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
function updateCodeNumberColors() {
try {
textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val());
$('#number').attr('style', '');
$('#code').attr('style', '');
} catch (e) {
if (e.countryCodeValid)
$('#code').attr('style', '');
else
$('#code').attr('style', 'background-color:#ff6666;');
2014-01-10 08:48:05 +01:00
if (e.numberValid)
$('#number').attr('style', '');
else
$('#number').attr('style', 'background-color:#ff6666;');
}
2014-01-10 08:48:05 +01:00
}
$('#code').on('change', updateCodeNumberColors);
$('#number').on('change', updateCodeNumberColors);
2014-01-10 08:48:05 +01:00
var single_device = false;
2014-05-17 06:54:12 +02:00
var signaling_key = textsecure.crypto.getRandomBytes(32 + 20);
var password = btoa(getString(textsecure.crypto.getRandomBytes(16)));
2014-01-17 07:08:33 +01:00
password = password.substring(0, password.length - 2);
2014-05-17 06:54:12 +02:00
var registrationId = new Uint16Array(textsecure.crypto.getRandomBytes(2))[0];
registrationId = registrationId & 0x3fff;
2014-01-17 07:08:33 +01:00
$('#init-go-single-client').click(function() {
var number = textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val());
2014-01-17 07:08:33 +01:00
$('#init-go').html('Setup');
$('#countrycode').prop('disabled', 'disabled');
$('#number').prop('disabled', 'disabled');
$('#init-go-single-client').prop('disabled', 'disabled');
2014-01-17 07:08:33 +01:00
single_device = true;
2014-01-17 07:08:33 +01:00
textsecure.api.requestVerificationCode(number,
function(response) { },
function(code) {
alert("Failed to send key?" + code); //TODO
}
);
2014-01-17 07:08:33 +01:00
});
2014-01-10 08:48:05 +01:00
$('#init-go').click(function() {
if (codeMatches() && numberMatches()) {
2014-01-12 08:32:13 +01:00
var number = "+" + $('#countrycode').val().replace(/\D/g, '') + $('#number').val().replace(/\D/g, '');
2014-01-10 08:48:05 +01:00
$('#init-setup').hide();
$('#verify1done').html('');
2014-01-17 07:08:33 +01:00
$('#verify2').hide();
2014-01-10 08:48:05 +01:00
$('#verify3done').html('');
2014-01-17 07:08:33 +01:00
$('#verify4done').html('');
2014-01-10 08:48:05 +01:00
$('#verify').show();
2014-05-17 07:53:58 +02:00
textsecure.api.confirmCode($('#code').val(), number, password, signaling_key, registrationId, single_device,
function(response) {
2014-01-17 07:08:33 +01:00
if (single_device)
response = 1;
var number_id = number + "." + response;
2014-05-21 04:21:07 +02:00
textsecure.storage.putEncrypted("password", password);
textsecure.storage.putEncrypted('signaling_key', signaling_key);
textsecure.storage.putUnencrypted("number_id", number_id);
textsecure.storage.putUnencrypted("registrationId", registrationId);
2014-01-12 09:49:46 +01:00
$('#verify1done').html('done');
2014-01-12 08:32:13 +01:00
2014-01-17 07:08:33 +01:00
var register_keys_func = function() {
$('#verify2done').html('done');
2014-05-17 06:54:12 +02:00
textsecure.crypto.generateKeys().then(function(keys) {
2014-01-22 07:23:41 +01:00
$('#verify3done').html('done');
2014-05-17 07:53:58 +02:00
textsecure.api.registerKeys(keys,
function(response) {
2014-01-22 07:23:41 +01:00
$('#complete-number').html(number);
$('#verify').hide();
$('#setup-complete').show();
registrationDone();
}, function(code) {
2014-01-22 07:23:41 +01:00
alert(code); //TODO
}
);
2014-01-17 07:08:33 +01:00
});
}
if (!single_device) {
2014-05-17 07:53:58 +02:00
//TODO: Redo all this
/*getKeysForNumber(number).then(function(identityKey) {
textsecure.subscribeToPush(function(message) {
2014-01-17 07:08:33 +01:00
//TODO receive shared identity key
register_keys_func();
2014-01-12 08:32:13 +01:00
});
2014-01-17 07:08:33 +01:00
requestIdentityPrivKeyFromMasterDevice(number);
2014-05-13 21:15:45 +02:00
}).catch(function(error) {
alert(error); //TODO
2014-05-17 07:53:58 +02:00
});*/
register_keys_func();
2014-01-17 07:08:33 +01:00
} else {
register_keys_func();
}
}, function(code) {
2014-01-10 08:48:05 +01:00
var error;
switch(code) {
case 403:
error = "Invalid code, please try again.";
break;
case -1:
error = "Error connecting to server, please check your network connection.";
break;
default:
error = "Unknown error, please try again later.";
console.log("Got error code " + code);
}
alert(error); //TODO
2014-01-12 09:49:46 +01:00
}
);
2014-01-10 08:48:05 +01:00
}
});
2014-05-17 07:53:58 +02:00
textsecure.registerOnLoadFunction(function() {
$(function() {
if (!isRegistrationDone()) {
$('#init-setup').show();
} else {
$('#complete-number').html(textsecure.storage.getUnencrypted("number_id").split(".")[0]);
$('#setup-complete').show();
}
});
2014-01-22 07:23:41 +01:00
});