diff --git a/lib/ns_client.js b/lib/ns_client.js index 0e552fd..1e1702f 100644 --- a/lib/ns_client.js +++ b/lib/ns_client.js @@ -15,7 +15,6 @@ // var NsClient = function() { - this.storage = chrome.storage.sync; this.state = "actif"; this.allowed_statuses = ["actif", "away", "idle", "lock"]; this.is_connected = false; @@ -39,23 +38,21 @@ var NsClient = function() { }; this.actions.msg.act = (function(elem) { return function(msg) { - elem.storage.get('enable_msg', function(infos) { - if (infos.enable_msg) { - var mo = {}, exp = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| msg ([^ ]*)/; + if ($cs.opts.get("enable_msg")) { + var mo = {}, exp = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| msg ([^ ]*)/; - mch = exp.exec(msg); - if (mch !== null && typeof elem.msgHandler !== "undefined") { - mo.socket = mch[1]; - mo.login = mch[2]; - mo.host = mch[3]; - mo.location = elem.msgDecode(mch[4]); - mo.group = mch[5]; - mo.message = elem.msgDecode(mch[6]); + mch = exp.exec(msg); + if (mch !== null && typeof elem.msgHandler !== "undefined") { + mo.socket = mch[1]; + mo.login = mch[2]; + mo.host = mch[3]; + mo.location = elem.msgDecode(mch[4]); + mo.group = mch[5]; + mo.message = elem.msgDecode(mch[6]); - elem.msgHandler(mo); - } - } - }); + elem.msgHandler(mo); + } + } }; })(this); }; @@ -73,43 +70,45 @@ NsClient.prototype.msgEncode = function(msg) { }; NsClient.prototype.connect = function() { + var login, pwd_socks; + if (!this.is_connected) { - this.storage.get(null, (function(elem) { - return function(infos) { - if (typeof infos.login !== "undefined" && typeof infos.pwd_socks !== "undefined") { - chrome.socket.create('tcp', {}, function(sock_inf) { - elem.socket = sock_inf.socketId; - chrome.socket.connect(elem.socket, "ns-server.epita.fr", 4242, function(res) { - chrome.socket.read(elem.socket, null, function(rd_inf) { - if (rd_inf.resultCode > 0) { - var data = ab2str(rd_inf.data).split(' '), - auth = "ext_user_log "; - auth += infos.login + " "; - auth += hex_md5(data[2] + "-" + data[3] + "/" + data[4] + infos.pwd_socks) + " "; - auth += "chromesoul chromesoul\n"; - chrome.socket.write(elem.socket, str2ab("auth_ag ext_user none none\n"), function(w_inf) { - chrome.socket.read(elem.socket, null, function(rd_inf) { - if (rd_inf.resultCode > 0) { - chrome.socket.write(elem.socket, str2ab(auth), function(w_inf) { - chrome.socket.read(elem.socket, null, function(rd_inf) { - if (rd_inf.resultCode > 0) { - elem.is_connected = true; - console.info("connected to the netsoul server"); - elem.updateStatus(); - elem.daemonize(); - } - }); - }); - } - }); - }); - } - }); + login = $cs.opts.get('login'); + pwd_socks = $cs.opts.get('pwd_socks'); + if (login !== null && pwd_socks !== null) { + chrome.socket.create('tcp', {}, (function(elem) { + return function(sock_inf) { + elem.socket = sock_inf.socketId; + chrome.socket.connect(elem.socket, "ns-server.epita.fr", 4242, function(res) { + chrome.socket.read(elem.socket, null, function(rd_inf) { + if (rd_inf.resultCode > 0) { + var data = ab2str(rd_inf.data).split(' '), + auth = "ext_user_log "; + auth += login + " "; + auth += hex_md5(data[2] + "-" + data[3] + "/" + data[4] + pwd_socks) + " "; + auth += "chromesoul chromesoul\n"; + chrome.socket.write(elem.socket, str2ab("auth_ag ext_user none none\n"), function(w_inf) { + chrome.socket.read(elem.socket, null, function(rd_inf) { + if (rd_inf.resultCode > 0) { + chrome.socket.write(elem.socket, str2ab(auth), function(w_inf) { + chrome.socket.read(elem.socket, null, function(rd_inf) { + if (rd_inf.resultCode > 0) { + elem.is_connected = true; + console.info("connected to the netsoul server"); + elem.updateStatus(); + elem.daemonize(); + } + }); + }); + } + }); + }); + } }); - }); - } - }; - })(this)); + }); + }; + })(this)); + } } }; @@ -203,7 +202,7 @@ NsClient.prototype.init = function() { }; status_update(this).apply(); - connect(this).apply(); + setTimeout(connect(this), 500); setInterval(connect(this), 10000); setInterval(status_update(this), 600000); }; diff --git a/lib/options.js b/lib/options.js index db9fdd1..4893a56 100644 --- a/lib/options.js +++ b/lib/options.js @@ -16,9 +16,11 @@ var OptionsManager = function() { this.storage = chrome.storage.sync; + this.pass_storage = chrome.storage.local; this.status = null; this.save_btn = null; this.opts = null; + this.values = {}; }; OptionsManager.prototype.types = { @@ -50,6 +52,16 @@ OptionsManager.prototype.types = { } }; +OptionsManager.prototype.get = function(name) { + var ret = null; + + if (typeof this.values[name] !== "undefined") { + ret = this.values[name]; + } + + return ret; +}; + OptionsManager.prototype.init = function() { this.status = document.getElementById("status"); this.save_btn = document.getElementById("save"); @@ -66,14 +78,24 @@ OptionsManager.prototype.init = function() { } }; -OptionsManager.prototype.save = function() { - var i = 0, data = {}; +OptionsManager.prototype.savePart = function(pass) { + var i = 0, data = {}, storage; + + if (pass) { + storage = this.pass_storage; + } else { + storage = this.storage; + } if (this.opts !== null) { for (i = this.opts.length - 1; i >= 0; --i) { - data[this.opts[i].id] = this.getElemValue(this.opts[i]); + if ((pass && this.getElemType(this.opts[i]) === "password") + || (!pass && this.getElemType(this.opts[i]) !== "password")) { + data[this.opts[i].id] = this.getElemValue(this.opts[i]); + this.values[this.opts[i].id] = data[this.opts[i].id]; + } } - this.storage.set(data, (function(elem) { + storage.set(data, (function(elem) { return function () { if (elem !== null) { elem.innerHTML = "Options saved."; @@ -86,23 +108,38 @@ OptionsManager.prototype.save = function() { } }; -OptionsManager.prototype.restore = function() { - var i, el; +OptionsManager.prototype.save = function() { + this.savePart(true); + this.savePart(false); +}; - this.storage.get(null, (function(elem) { +OptionsManager.prototype.restore = function() { + var i, el, cb = function(elem) { return function(items) { + console.log('----------------------------------'); + console.log('restoring...'); + console.log(items); for (i in items) { el = document.getElementById(i); if (el !== null) { + elem.values[i] = items[i]; elem.setElemValue(el, items[i]); } } + console.log('----------------------------------'); }; - })(this)); + }; + + this.storage.get(null, cb(this)); + this.pass_storage.get(null, cb(this)); +}; + +OptionsManager.prototype.getElemType = function(elem) { + return elem.getAttribute('type'); }; OptionsManager.prototype.getElemValue = function(elem) { - var val = null, type = elem.getAttribute('type'); + var val = null, type = this.getElemType(elem); if (typeof this.types.get[type] !== "undefined") { val = this.types.get[type](elem);