2014-05-14 18:58:12 +00:00
|
|
|
/* vim: ts=4:sw=4
|
|
|
|
*
|
2014-05-04 06:34:13 +00:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-10-10 23:58:34 +00:00
|
|
|
;(function() {
|
2014-10-14 07:41:18 +00:00
|
|
|
function validateNumber() {
|
2014-10-10 23:58:34 +00:00
|
|
|
try {
|
2014-10-14 07:41:18 +00:00
|
|
|
var regionCode = $('#regionCode').val();
|
|
|
|
var number = $('#number').val();
|
2014-10-10 23:58:34 +00:00
|
|
|
|
2014-10-14 07:41:18 +00:00
|
|
|
var parsedNumber = libphonenumber.util.verifyNumber(number, regionCode);
|
2014-10-10 23:58:34 +00:00
|
|
|
|
2014-10-14 07:41:18 +00:00
|
|
|
$('#regionCode').val(libphonenumber.util.getRegionCodeForNumber(parsedNumber));
|
|
|
|
$('#number-container').removeClass('invalid');
|
|
|
|
$('#number-container').addClass('valid');
|
|
|
|
$('#request-sms, #request-voice').removeAttr('disabled');
|
|
|
|
return parsedNumber;
|
2014-10-10 23:58:34 +00:00
|
|
|
} catch(e) {
|
2014-10-14 07:41:18 +00:00
|
|
|
$('#number-container').removeClass('valid');
|
|
|
|
$('#request-sms, #request-voice').prop('disabled', 'disabled');
|
2014-10-10 23:58:34 +00:00
|
|
|
}
|
2014-10-14 07:41:18 +00:00
|
|
|
};
|
2014-10-10 23:58:34 +00:00
|
|
|
|
2014-10-14 07:41:18 +00:00
|
|
|
function validateCode() {
|
|
|
|
var verificationCode = $('#code').val().replace(/\D/g, '');
|
|
|
|
if (verificationCode.length == 6) {
|
|
|
|
return verificationCode;
|
2014-10-10 23:58:34 +00:00
|
|
|
}
|
2014-10-14 07:41:18 +00:00
|
|
|
};
|
2014-10-10 23:58:34 +00:00
|
|
|
|
2014-10-14 07:41:18 +00:00
|
|
|
function displayError(error) {
|
|
|
|
$('#error').hide().text(error).fadeIn();
|
|
|
|
};
|
2014-10-10 23:58:34 +00:00
|
|
|
|
|
|
|
textsecure.registerOnLoadFunction(function() {
|
|
|
|
$(function() {
|
2014-10-14 07:41:18 +00:00
|
|
|
if (isRegistrationDone()) {
|
|
|
|
$('#complete-number').text(textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0]);//TODO: no
|
|
|
|
$('#setup-complete').show();
|
|
|
|
} else {
|
|
|
|
$('#choose-setup').show();
|
|
|
|
$('#number').keyup(validateNumber);
|
|
|
|
$('#regionCode').change(validateNumber);
|
|
|
|
|
|
|
|
$.each(libphonenumber.util.getAllRegionCodes(), function (regionCode, countryName) {
|
|
|
|
$('#regionCode').append(
|
|
|
|
$('<option>', { value: regionCode, text: countryName })
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#code').on('change', function() {
|
|
|
|
if (!validateCode())
|
|
|
|
$('#code').addClass('invalid');
|
|
|
|
else
|
|
|
|
$('#code').removeClass('invalid');
|
2014-10-10 23:58:34 +00:00
|
|
|
});
|
|
|
|
|
2014-10-14 07:41:18 +00:00
|
|
|
$('#request-voice').click(function() {
|
|
|
|
var number = validateNumber();
|
|
|
|
if (number) {
|
|
|
|
textsecure.api.requestVerificationVoice(number).catch(displayError);
|
|
|
|
$('#step2').fadeIn();
|
|
|
|
} else {
|
|
|
|
$('#number-container').addClass('invalid');
|
|
|
|
}
|
2014-10-10 23:58:34 +00:00
|
|
|
});
|
|
|
|
|
2014-10-14 07:41:18 +00:00
|
|
|
$('#request-sms').click(function() {
|
|
|
|
var number = validateNumber();
|
|
|
|
if (number) {
|
|
|
|
textsecure.api.requestVerificationSMS(number).catch(displayError);
|
|
|
|
$('#step2').fadeIn();
|
|
|
|
} else {
|
|
|
|
$('#number-container').addClass('invalid');
|
|
|
|
}
|
2014-10-10 23:58:34 +00:00
|
|
|
});
|
|
|
|
|
2014-10-14 07:41:18 +00:00
|
|
|
$('#new-account').click(function(){
|
|
|
|
$('#choose-setup').fadeOut(function() {
|
|
|
|
$('#single-device').fadeIn();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#single-device .back').click(function() {
|
|
|
|
$('#single-device').fadeOut(function() {
|
|
|
|
$('#choose-setup').fadeIn();
|
|
|
|
$('#number').removeClass('invalid');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#single-device form').submit(function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
$('#error').hide();
|
|
|
|
var number = validateNumber();
|
|
|
|
var verificationCode = validateCode();
|
|
|
|
if (number && verificationCode) {
|
|
|
|
$('#verify1').hide();
|
2014-10-10 23:58:34 +00:00
|
|
|
$('#verify2done').text('');
|
|
|
|
$('#verify3done').text('');
|
|
|
|
$('#verify4done').text('');
|
2014-10-14 07:41:18 +00:00
|
|
|
$('#verify5').hide();
|
2014-10-10 23:58:34 +00:00
|
|
|
$('#verify').show();
|
|
|
|
|
2014-10-14 07:41:18 +00:00
|
|
|
textsecure.registerSingleDevice(number, verificationCode, function(step) {
|
2014-10-10 23:58:34 +00:00
|
|
|
switch(step) {
|
|
|
|
case 1:
|
|
|
|
$('#verify2done').text('done');
|
|
|
|
break;
|
2014-10-14 07:41:18 +00:00
|
|
|
case 2:
|
2014-10-10 23:58:34 +00:00
|
|
|
$('#verify3done').text('done');
|
|
|
|
break;
|
2014-10-14 07:41:18 +00:00
|
|
|
case 3:
|
|
|
|
$('#complete-number').text(number);
|
2014-10-10 23:58:34 +00:00
|
|
|
$('#verify').hide();
|
|
|
|
$('#setup-complete').show();
|
|
|
|
registrationDone();
|
|
|
|
}
|
2014-10-14 07:41:18 +00:00
|
|
|
}).catch(function(error) {
|
|
|
|
//TODO: No alerts...
|
|
|
|
if (error.humanError)
|
|
|
|
displayError(error.humanError);
|
|
|
|
else
|
|
|
|
displayError(error); //XXX
|
2014-10-10 23:58:34 +00:00
|
|
|
});
|
|
|
|
}
|
2014-10-14 07:41:18 +00:00
|
|
|
});
|
|
|
|
});
|
2014-10-10 23:58:34 +00:00
|
|
|
|
2014-10-14 07:41:18 +00:00
|
|
|
$('#new-device').click(function(){
|
|
|
|
$('#choose-setup').fadeOut(function() {
|
|
|
|
$('#multi-device').fadeIn();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#multi-device .back').click(function() {
|
|
|
|
$('#multi-device').fadeOut(function() {
|
|
|
|
$('#choose-setup').fadeIn();
|
|
|
|
$('#number').removeClass('invalid');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$('#multi-device .status').text("Connecting...");
|
|
|
|
$('#setup-qr').html('');
|
|
|
|
textsecure.crypto.prepareTempWebsocket().then(function(cryptoInfo) {
|
|
|
|
var qrCode = new QRCode(document.getElementById('setup-qr'));
|
|
|
|
var socket = textsecure.api.getTempWebsocket();
|
|
|
|
|
|
|
|
socket.onmessage = function(message) {
|
|
|
|
if (message.uuid) {
|
|
|
|
qrCode.makeCode('textsecure-device-init:/' +
|
|
|
|
'?channel_uuid=' + message.uuid +
|
|
|
|
'&channel_server=' + textsecure.api.relay +
|
|
|
|
'&publicKey=' + btoa(getString(cryptoInfo.publicKey)));
|
|
|
|
$('img').removeAttr('style');
|
|
|
|
$('#multi-device .status').text("Use your phone to scan the QR code.")
|
|
|
|
} else {
|
|
|
|
$('#init-setup').hide();
|
|
|
|
$('#verify1done').text('');
|
|
|
|
$('#verify2done').text('');
|
|
|
|
$('#verify3done').text('');
|
|
|
|
$('#verify4done').text('');
|
|
|
|
$('#verify5done').text('');
|
|
|
|
$('#verify').show();
|
|
|
|
|
|
|
|
|
|
|
|
textsecure.registerSecondDevice(cryptoInfo, message.message, function(step) {
|
|
|
|
switch(step) {
|
|
|
|
case 1:
|
|
|
|
$('#verify1done').text('done');
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
$('#verify2done').text('done');
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
$('#verify3done').text('done');
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
//TODO: User needs to verify number before we continue
|
|
|
|
$('#complete-number').text(parsedNumber);
|
|
|
|
$('#verify4done').text('done');
|
|
|
|
case 5:
|
|
|
|
$('#verify').hide();
|
|
|
|
$('#setup-complete').show();
|
|
|
|
registrationDone();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
socket.ondisconnect = function() {
|
|
|
|
$('#multi-device .status').text("The push server disconnected, please wait while we reconnect...");
|
|
|
|
};
|
|
|
|
});
|
2014-10-10 23:58:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})();
|