var password = null; var username = null; function error(data, stay) { $("#status").fadeOut("slow", function() { $("#status").addClass("error") $("#status").removeClass("notice") $("#status").removeClass("success") if (data) { if (typeof data == 'string') { $("#status").html(data); console.log("error: "+data); } else { $("#status").html('
'+JSON.stringify(data)+''); console.log("error: "+JSON.stringify(data)); } } else { $("#status").html('error'); console.log("error"); } $("#status").fadeIn("slow"); if (!stay) setTimeout(start, 5000); }); } function notice(text) { $("#status").fadeOut("slow", function() { $("#status").addClass("notice") $("#status").removeClass("error") $("#status").removeClass("success") if (text) { $("#status").html(text); console.log("notice: "+text); } else { $("#status").html(''); console.log("notice"); } $("#status").fadeIn("slow"); }); } function success(text) { $("#status").fadeOut("slow", function() { $("#status").addClass("success") $("#status").removeClass("error") $("#status").removeClass("notice") if (text) { $("#status").html(text); console.log("success: "+text); } else { $("#status").html(''); console.log("success"); } $("#status").fadeIn("slow"); }); } function status(text, msg) { $("#main").fadeOut("slow", function() { $("#main").html(text); success(msg); $("#main").fadeIn("slow", function() { $("form input:first-child").focus(); }) }); } function checkuser(user) { $("#register").submit(function(event) { return false; }); $.post("checknewuser.php", {user: user}) .done(function(res) { username=JSON.parse(res); if (!username||username.length<1) username=null; $("#createuser").prop("disabled", !(username && password)); if (username) { if (password) success("user is ready to be created"); else notice("user name is available, please set password"); } else notice("user name is not available"); }).fail(function(res) { username=null; $("#createuser").prop("disabled", !(username && password)); error(res); }); } function checkpwd(pwd, pwd2) { $("#register").submit(function(event) { return false; }); if (pwd==pwd2) password=pwd; else password=null; if (!password||password.length<1) password=null; $("#createuser").prop("disabled", !(username && password)); if (password) { if (username) success("user is ready to be created"); else notice("password matches, please chose a valid user name"); } else notice("passwords don't match"); } function checkpartner(user) { $("#chat").submit(function(event) { return false; }); $.post("checknewuser.php", {user: user}) .done(function(res) { if (JSON.parse(res)) { notice("receiver does not exist"); $("#send").prop("disabled", true); return; } $("#send").prop("disabled", false); success("receiver exists"); }).fail(function(res) { notice("cannot connect to server: "+res); $("#send").prop("disabled", true); }); } function createNewUser(user, pwd) { status("generate keys"); openpgp.generateKeyPair({ numBits: 1024, userId: user, passphrase: pwd }).then(function(keyPair) { success("keys generated"); localStorage.pubKey = keyPair.publicKeyArmored; localStorage.privKey = keyPair.privateKeyArmored; login(); }).catch(function(e) { error(e); }); } function publicKey() { if (typeof localStorage.pubKey == 'undefined') return null; return openpgp.key.readArmored(localStorage.pubKey); } function privateKey() { if (typeof localStorage.privKey == 'undefined') return null; return openpgp.key.readArmored(localStorage.privKey); } function userid() { if (!publicKey() || publicKey().keys.length < 1 || publicKey().keys[0].getUserIds().length < 1) return null return publicKey().keys[0].getUserIds()[0]; } function fileupload(evt) { if (!window.FileReader) return error("your browser dows not support file upload", true); var reader = new FileReader(); reader.onload = function(evt) { if (evt.target.readyState!=2) return notice("ReadyState="+evt.target.readyState); if (evt.target.error) return error("error reading file", true); filecontent = evt.target.result; success(filecontent); } reader.readAsText(evt.target.files[0]); } function setreceiver(name) { $("#recv").val(name); checkpartner(name); $("#msg").focus(); } var startmsg = 0; function get() { var beeped = false; $.post("get.php", {start: startmsg}).done(function(res) { var msgs = JSON.parse(res); if (msgs) { msgs.forEach(function(e) { if (startmsg