diff --git a/background.html b/background.html index c87e1f0f..0cc11099 100644 --- a/background.html +++ b/background.html @@ -12,6 +12,7 @@ along with this program. If not, see . --> + @@ -24,6 +25,7 @@ + diff --git a/css/forms.css b/css/forms.css index dc49b42a..75412195 100644 --- a/css/forms.css +++ b/css/forms.css @@ -9,8 +9,7 @@ input[type=text], textarea { } input.invalid { - background-color: #ff6666; - border-color: #ff3333; + border-color: #ff3333; } input[type=submit]:focus, diff --git a/css/options.css b/css/options.css index d80acf7d..04408502 100644 --- a/css/options.css +++ b/css/options.css @@ -39,3 +39,10 @@ h2 { #init-go-single-client { display:block; } + +#phonenumberspan { + display: block; + margin: .5em auto 1em auto; } + +#countrycode { + text-align: right; } \ No newline at end of file diff --git a/js-deps/libphonenumber_api-compiled.js b/js-deps/libphonenumber_api-compiled.js new file mode 100644 index 00000000..b3c97939 --- /dev/null +++ b/js-deps/libphonenumber_api-compiled.js @@ -0,0 +1,463 @@ +(function(){function h(a){throw a;}var j=!0,k=null,m=!1,n,aa=this;function p(a,b){var c=a.split("."),e=aa;!(c[0]in e)&&e.execScript&&e.execScript("var "+c[0]);for(var d;c.length&&(d=c.shift());)!c.length&&void 0!==b?e[d]=b:e=e[d]?e[d]:e[d]={}}function q(a,b){function c(){}c.prototype=b.prototype;a.pa=b.prototype;a.prototype=new c;a.prototype.constructor=a};function r(a,b,c){this.fa=a;this.na=b.name||k;this.wa=b.k||k;this.ba=b.va;this.c={};for(a=0;a=e):-1!=na(d,e)}; +n.o=function(a){if(a==k)return k;var b=a.g(),b=K[b];if(b==k)b=k;else if(1==b.length)b=b[0];else a:{a=W(a);for(var c,e=b.length,d=0;d=a.b.length&&h("Phone number too short after IDD"); +a:{d=a.toString();if(!(0==d.length||"0"==d.charAt(0)))for(var g=d.length,i=1;3>=i&&i<=g;++i)if(f=parseInt(d.substring(0,i),10),f in K){c.append(d.substring(i));d=f;break a}d=0}if(0!=d)return e.l(d),d;h("Invalid country calling code")}if(b!=k&&(d=b.g(),f=""+d,g=a.toString(),0==g.lastIndexOf(f,0)&&(i=new E(g.substring(f.length)),g=x(b,1),f=RegExp(y(g,2)),qa(i,b,k),b=i.toString(),g=y(g,3),!U(f,a.toString())&&U(f,b)||3==(U(g,a.toString())?0:0==a.toString().search(g)?3:2))))return c.append(b),e.l(d),d; +e.l(0);return 0}function qa(a,b,c){var e=a.toString(),d=e.length,f=x(b,15);if(!(0==d||f==k||0==f.length))if(f=RegExp("^(?:"+f+")"),d=f.exec(e)){var g=RegExp,i;i=x(b,1);i=y(i,2);g=g(i);i=U(g,e);var l=d.length-1;b=x(b,16);if(b==k||0==b.length||d[l]==k||0==d[l].length){if(!i||U(g,e.substring(d[0].length)))c!=k&&(0c.toString().length?0:U(S,c.toString()))|| +h("The string supplied did not seem to be a phone number");b!=k&&isNaN(b)&&b.toUpperCase()in L||c.toString()!=k&&0d.substring(0,f).length?0:U(S,d.substring(0,f))))for(var g=d.match(R),i=g.length,l=1;ld.b.length&&h("The string supplied is too short to be a phone number");f!=k&&(g=new E,c=new E(d.toString()),qa(c,f,g),g=c.toString(),f=x(f,1),f=y(f,3),2==(U(f,g)?0:0==g.search(f)?3:2)||(d=c));c=d.toString();d=c.length;2>d&&h("The string supplied is too short to be a phone number");17 3 && number.length < 11; //XXX - } + self.getRegionCodeForNumber = function(number) { + try { + var parsedNumber = libphonenumber.parse(number); + return libphonenumber.getRegionCodeForNumber(parsedNumber); + } catch(e) { + return "ZZ"; + } + }; - function validateCountryCode(countryCode) { - return isNumeric(countryCode) && countryCode.length < 4 && countryCode.length > 0; - } + self.getCountryCodeForRegion = function(regionCode) { + var cc = libphonenumber.getCountryCodeForRegion(regionCode); + return (cc != 0) ? cc : ""; + }; - // Verifies a number (possibly tweaking its format) - // This should be used ONLY to verify numbers provided by the user - self.verifyNumber = function(number, countryCode) { - //XXX: All verifyNumber stuff needs to match the server-side verification - var countryCodeValid = true; - var numberValid = true; + self.verifyNumber = function(number, regionCode) { + var parsedNumber = libphonenumber.parse(number, regionCode); - if (number.substr(0, 1) == '+') { - if (countryCode === undefined) { - var numberCCPair = splitPrefixedNumber(number); - if (numberCCPair != null) { - countryCode = numberCCPair[0]; - number = numberCCPair[1]; - } else - numberValid = false; - } else - numberValid = false; - } else if (countryCode === undefined) - numberValid = false; + if(!regionCode || regionCode == 'ZZ') + regionCode = libphonenumber.getRegionCodeForNumber(parsedNumber); - if (numberValid && !validateNumber(number, countryCode)) - numberValid = false; - if (countryCode !== undefined) - countryCodeValid = validateCountryCode(countryCode); + var isValidNumber = libphonenumber.isValidNumber(parsedNumber); + var isValidNumberForRegion = libphonenumber.isValidNumberForRegion(parsedNumber, regionCode); - if (!countryCodeValid || !numberValid) - throw { countryCodeValid: countryCodeValid, numberValid: numberValid }; - - return '+' + countryCode + number; - } + if (isValidNumber && isValidNumberForRegion) { + return libphonenumber.format(parsedNumber, libphonenumber.PhoneNumberFormat.E164); + } else { + throw new Error("The number seems not to be valid."); + } + }; self.unencodeNumber = function(number) { return number.split("."); - } + }; /************************** *** JSON'ing Utilities *** @@ -770,6 +760,7 @@ window.textsecure.register = function() { response = 1; var numberId = number + "." + response; textsecure.storage.putUnencrypted("number_id", numberId); + textsecure.storage.putUnencrypted("regionCode", textsecure.utils.getRegionCodeForNumber(number)); stepDone(1); if (!singleDevice) { diff --git a/js/options.js b/js/options.js index a30e69c4..3d4ee4c7 100644 --- a/js/options.js +++ b/js/options.js @@ -16,25 +16,17 @@ function updateNumberColors() { try { - textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val()); + if($('#number').val() != "" && $('#regionCode').val() != "") + textsecure.utils.verifyNumber($('#number').val(), $('#regionCode').val()); + $('#countrycode').removeClass('invalid'); $('#number').removeClass('invalid'); - $('#number').removeClass('invalid'); - } catch (e) { - if (e.countryCodeValid) - $('#countrycode').removeClass('invalid'); - else - $('#countrycode').addClass('invalid'); - - if (e.numberValid) - $('#number').removeClass('invalid'); - else - $('#number').addClass('invalid'); + } catch (numberInvalidError) { + console.log(numberInvalidError); + $('#countrycode').addClass('invalid'); + $('#number').addClass('invalid'); } } -$('#number').on('change', updateNumberColors); -$('#countrycode').on('change', updateNumberColors); - function isCodeValid() { var code = $('#code'); return code.val().replace(/\D/g, '') == code.val() && code.val().length == 6; @@ -50,9 +42,14 @@ $('#code').on('change', function() { var single_device = false; $('#init-go-single-client').click(function() { - var number = textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val()); + try { + var parsedNumber = textsecure.utils.verifyNumber($('#number').val(), $('#regionCode').val()); + } catch(e) { + alert("Please enter a valid phone number first."); + return false; + } - $('#init-go').html('Setup'); + $('#init-go').text('Setup'); $('#countrycode').prop('disabled', 'disabled'); $('#number').prop('disabled', 'disabled'); $('#init-go-single-client').prop('disabled', 'disabled'); @@ -60,7 +57,7 @@ $('#init-go-single-client').click(function() { single_device = true; - textsecure.api.requestVerificationCode(number).catch(function(error) { + textsecure.api.requestVerificationCode(parsedNumber).catch(function(error) { //TODO: No alerts if (error.humanReadable) alert(error.humanReadable); @@ -70,7 +67,7 @@ $('#init-go-single-client').click(function() { }); $('#init-go').click(function() { - var number = textsecure.utils.verifyNumber($('#number').val(), $('#countrycode').val()); + var parsedNumber = textsecure.utils.verifyNumber($('#number').val(), $('#regionCode').val()); if (!isCodeValid()) { updateCodeColor(); return; @@ -78,25 +75,25 @@ $('#init-go').click(function() { $('#init-setup').hide(); - $('#verify1done').html(''); + $('#verify1done').text(''); $('#verify2').hide(); - $('#verify3done').html(''); - $('#verify4done').html(''); + $('#verify3done').text(''); + $('#verify4done').text(''); $('#verify').show(); - textsecure.register(number, $('#code').val(), single_device, function(step) { + textsecure.register(parsedNumber, $('#code').val(), single_device, function(step) { switch(step) { case 1: - $('#verify1done').html('done'); + $('#verify1done').text('done'); break; case 2: - $('#verify2done').html('done'); + $('#verify2done').text('done'); break; case 3: - $('#verify3done').html('done'); + $('#verify3done').text('done'); break; case 4: - $('#complete-number').html(number); + $('#complete-number').text(parsedNumber); $('#verify').hide(); $('#setup-complete').show(); registrationDone(); @@ -111,12 +108,37 @@ $('#init-go').click(function() { }); textsecure.registerOnLoadFunction(function() { - $(function() { - if (!isRegistrationDone()) { - $('#init-setup').show(); - } else { - $('#complete-number').html(textsecure.storage.getUnencrypted("number_id").split(".")[0]);//TODO: no - $('#setup-complete').show(); + $(function() { + if (!isRegistrationDone()) { + $('#init-setup').show(); + + var countrys = textsecure.utils.getAllRegionCodes(); + $.each(countrys, function (regionCode, countryName) { + $('#regionCode').append($('