messages reception support
This commit is contained in:
parent
20650e5ce1
commit
096cb39320
8 changed files with 242 additions and 184 deletions
158
lib/ns_client.js
158
lib/ns_client.js
|
@ -20,48 +20,84 @@ var NsClient = function() {
|
|||
this.allowed_statuses = ["actif", "away", "idle", "lock"];
|
||||
this.is_connected = false;
|
||||
this.socket = null;
|
||||
this.actions = {};
|
||||
|
||||
this.actions.ping = {};
|
||||
this.actions.ping.is = function(msg) {
|
||||
return msg.substr(0, 5) === "ping ";
|
||||
};
|
||||
this.actions.ping.act = (function(elem) {
|
||||
return function(msg) {
|
||||
chrome.socket.write(elem.socket, str2ab(msg), function(w_inf) {});
|
||||
};
|
||||
})(this);
|
||||
|
||||
this.actions.msg = {};
|
||||
this.actions.msg.is = function(msg) {
|
||||
var exp = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| msg ([^ ]*)/;
|
||||
return exp.exec(msg) !== null;
|
||||
};
|
||||
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 ([^ ]*)/;
|
||||
|
||||
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 = decodeURI(mch[4]);
|
||||
mo.group = mch[5];
|
||||
mo.message = decodeURI(mch[6]);
|
||||
|
||||
elem.msgHandler(mo);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
})(this);
|
||||
};
|
||||
|
||||
NsClient.prototype.connect = function() {
|
||||
var cnt = 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();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
if (!this.is_connected) {
|
||||
this.storage.get(null, cnt(this));
|
||||
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();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
})(this));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -75,31 +111,29 @@ NsClient.prototype.disconnect = function() {
|
|||
};
|
||||
|
||||
NsClient.prototype.daemonize = function() {
|
||||
var dm = function(elem) {
|
||||
return function(rd_inf) {
|
||||
if (rd_inf.resultCode > 0) {
|
||||
var data = ab2str(rd_inf.data);
|
||||
|
||||
if (data.substr(0, 5) === "ping ") {
|
||||
chrome.socket.write(elem.socket, rd_inf.data, function(w_inf) {
|
||||
elem.daemonize();
|
||||
});
|
||||
} else {
|
||||
elem.daemonize();
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* A read error is the only way to know if the remote peer has disconnected.
|
||||
* See <http://developer.chrome.com/apps/socket.html> for more informations.
|
||||
*/
|
||||
elem.disconnect();
|
||||
console.info('connection lost, reconnecting...');
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
if (this.is_connected) {
|
||||
chrome.socket.read(this.socket, dm(this));
|
||||
chrome.socket.read(this.socket, (function(elem) {
|
||||
return function(rd_inf) {
|
||||
if (rd_inf.resultCode > 0) {
|
||||
var at, data = ab2str(rd_inf.data);
|
||||
|
||||
for (at in elem.actions) {
|
||||
if (elem.actions[at].is(data)) {
|
||||
elem.actions[at].act(data);
|
||||
break ;
|
||||
}
|
||||
}
|
||||
elem.daemonize();
|
||||
} else {
|
||||
/*
|
||||
* A read error is the only way to know if the remote peer has disconnected.
|
||||
* See <http://developer.chrome.com/apps/socket.html> for more informations.
|
||||
*/
|
||||
elem.disconnect();
|
||||
console.info('connection lost, reconnecting...');
|
||||
}
|
||||
};
|
||||
})(this));
|
||||
} else {
|
||||
console.error("unable to daemonize: not connected");
|
||||
}
|
||||
|
|
75
lib/nsui.js
75
lib/nsui.js
|
@ -19,7 +19,7 @@ var Nsui = function() {
|
|||
};
|
||||
|
||||
Nsui.prototype.setReconnect = function(func) {
|
||||
document.getElementById('reconnect').addEventListener('click', func, false);
|
||||
document.getElementById("reconnect").addEventListener("click", func, false);
|
||||
};
|
||||
|
||||
Nsui.prototype.createTab = function(name) {
|
||||
|
@ -40,12 +40,12 @@ Nsui.prototype.deleteTab = function(tab) {
|
|||
if (new_tab !== null) {
|
||||
new_tab.show();
|
||||
} else {
|
||||
document.getElementById('configuration').style.display = 'block';
|
||||
document.getElementById("configuration").style.display = "block";
|
||||
}
|
||||
};
|
||||
|
||||
Nsui.prototype.hideAllTabs = function() {
|
||||
document.getElementById('configuration').style.display = 'none';
|
||||
document.getElementById("configuration").style.display = "none";
|
||||
for (i = this.tab_lst.length - 1; i >= 0; --i) {
|
||||
this.tab_lst[i].hide();
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ Nsui.prototype.getNextTab = function(current_name) {
|
|||
|
||||
for (i = this.tab_lst.length - 1; i >= 0; --i) {
|
||||
if (this.tab_lst[i].name === current_name) {
|
||||
if (prev === null && typeof this.tab_lst[i - 1] !== 'undefined') {
|
||||
if (prev === null && typeof this.tab_lst[i - 1] !== "undefined") {
|
||||
prev = this.tab_lst[i - 1];
|
||||
}
|
||||
break ;
|
||||
|
@ -82,35 +82,60 @@ Nsui.prototype.getNextTab = function(current_name) {
|
|||
|
||||
Nsui.prototype.addContentToTab = function(tab_name, content) {
|
||||
var tab = this.getTabByName(tab_name);
|
||||
|
||||
if (tab === null) {
|
||||
tab = this.createTab(tab_name);
|
||||
}
|
||||
tab.setActive();
|
||||
tab.appendText(content);
|
||||
if (!tab.isCurrent()) {
|
||||
tab.setActive();
|
||||
}
|
||||
tab.appendText(this.formatMessage(content));
|
||||
};
|
||||
|
||||
Nsui.prototype.formatMessage = function(msg) {
|
||||
var fmt = "";
|
||||
|
||||
if (msg.login !== null) {
|
||||
fmt += '<span class="spk-oth">' + msg.login + ': </span>';
|
||||
} else {
|
||||
fmt += '<span class="spk-me">' + document.getElementById('login').value + ': </span>';
|
||||
}
|
||||
|
||||
msg.message = msg.message.replace("<", "<");
|
||||
msg.message = msg.message.replace(">", ">");
|
||||
fmt += msg.message;
|
||||
|
||||
fmt += '<br>';
|
||||
|
||||
return fmt;
|
||||
};
|
||||
|
||||
Nsui.prototype.init = function() {
|
||||
var sh = function(elem) {
|
||||
document.getElementById("tab-config").addEventListener("click", (function(elem) {
|
||||
return function() {
|
||||
for (var i = elem.tab_lst.length - 1; i >= 0; --i) {
|
||||
for (var i = elem.tab_lst.length - 1; i >= 0; --i) {
|
||||
elem.tab_lst[i].hide();
|
||||
}
|
||||
this.classList.add('tab-current');
|
||||
document.getElementById('configuration').style.display = 'block';
|
||||
}
|
||||
this.classList.add("tab-current");
|
||||
document.getElementById("configuration").style.display = "block";
|
||||
};
|
||||
},
|
||||
tch = function(elem) {
|
||||
return function() {
|
||||
elem.deleteTab(this);
|
||||
};
|
||||
},
|
||||
tsh = function(elem) {
|
||||
return function() {
|
||||
elem.hideAllTabs();
|
||||
};
|
||||
};
|
||||
})(this), false);
|
||||
|
||||
document.getElementById('tab-config').addEventListener('click', sh(this), false);
|
||||
Tab.prototype.closeHandler = tch(this);
|
||||
Tab.prototype.showHandler = tsh(this);
|
||||
Tab.prototype.closeHandler = (function(elem) {
|
||||
return function() {
|
||||
elem.deleteTab(this);
|
||||
};
|
||||
})(this);
|
||||
|
||||
Tab.prototype.showHandler = (function(elem) {
|
||||
return function() {
|
||||
elem.hideAllTabs();
|
||||
};
|
||||
})(this);
|
||||
|
||||
NsClient.prototype.msgHandler = (function(elem) {
|
||||
return function(msg) {
|
||||
elem.addContentToTab(msg.login, msg);
|
||||
};
|
||||
})(this);
|
||||
};
|
||||
|
|
|
@ -51,12 +51,6 @@ OptionsManager.prototype.types = {
|
|||
};
|
||||
|
||||
OptionsManager.prototype.init = function() {
|
||||
var saveOpts = function(elem) {
|
||||
return function() {
|
||||
elem.save();
|
||||
};
|
||||
};
|
||||
|
||||
this.status = document.getElementById("status");
|
||||
this.save_btn = document.getElementById("save");
|
||||
this.opts = document.getElementsByClassName("opt");
|
||||
|
@ -64,52 +58,51 @@ OptionsManager.prototype.init = function() {
|
|||
this.restore();
|
||||
|
||||
if (this.save !== null) {
|
||||
this.save_btn.addEventListener("click", saveOpts(this));
|
||||
this.save_btn.addEventListener("click", (function(elem) {
|
||||
return function() {
|
||||
elem.save();
|
||||
};
|
||||
})(this));
|
||||
}
|
||||
};
|
||||
|
||||
OptionsManager.prototype.save = function() {
|
||||
var i = 0,
|
||||
data = {},
|
||||
notif = function(elem) {
|
||||
return function () {
|
||||
if (elem !== null) {
|
||||
elem.innerHTML = "Options saved.";
|
||||
setTimeout(function() {
|
||||
elem.innerHTML = "";
|
||||
}, 3000);
|
||||
}
|
||||
};
|
||||
};
|
||||
var i = 0, data = {};
|
||||
|
||||
if (this.opts !== null) {
|
||||
for (i = this.opts.length - 1; i >= 0; --i) {
|
||||
data[this.opts[i].id] = this.getElemValue(this.opts[i]);
|
||||
}
|
||||
this.storage.set(data, notif(this.status));
|
||||
this.storage.set(data, (function(elem) {
|
||||
return function () {
|
||||
if (elem !== null) {
|
||||
elem.innerHTML = "Options saved.";
|
||||
setTimeout(function() {
|
||||
elem.innerHTML = "";
|
||||
}, 3000);
|
||||
}
|
||||
};
|
||||
})(this.status));
|
||||
}
|
||||
};
|
||||
|
||||
OptionsManager.prototype.restore = function() {
|
||||
var i,
|
||||
el,
|
||||
rst = function(elem) {
|
||||
return function(items) {
|
||||
for (i in items) {
|
||||
el = document.getElementById(i);
|
||||
if (el !== null) {
|
||||
elem.setElemValue(el, items[i]);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
var i, el;
|
||||
|
||||
this.storage.get(null, rst(this));
|
||||
this.storage.get(null, (function(elem) {
|
||||
return function(items) {
|
||||
for (i in items) {
|
||||
el = document.getElementById(i);
|
||||
if (el !== null) {
|
||||
elem.setElemValue(el, items[i]);
|
||||
}
|
||||
}
|
||||
};
|
||||
})(this));
|
||||
};
|
||||
|
||||
OptionsManager.prototype.getElemValue = function(elem) {
|
||||
var val = null,
|
||||
type = elem.getAttribute('type');
|
||||
var val = null, type = elem.getAttribute('type');
|
||||
|
||||
if (typeof this.types.get[type] !== "undefined") {
|
||||
val = this.types.get[type](elem);
|
||||
|
|
|
@ -16,8 +16,8 @@
|
|||
|
||||
var Tab = function(name) {
|
||||
this.name = this.filterName(name);
|
||||
this.wr_lst = document.getElementById('tab-lst');
|
||||
this.wr_body = document.getElementById('tab-body-wrapper');
|
||||
this.wr_lst = document.getElementById("tab-lst");
|
||||
this.wr_body = document.getElementById("tab-body-wrapper");
|
||||
this.initListElement();
|
||||
this.initBodyElement();
|
||||
};
|
||||
|
@ -28,7 +28,7 @@ Tab.prototype.filterName = function(name) {
|
|||
};
|
||||
|
||||
Tab.prototype.initListElement = function() {
|
||||
var inner = document.createElement('span'),
|
||||
var inner = document.createElement("span"),
|
||||
evt_click = function(elem) {
|
||||
return function() {
|
||||
elem.show();
|
||||
|
@ -41,27 +41,35 @@ Tab.prototype.initListElement = function() {
|
|||
};
|
||||
|
||||
inner.innerHTML = this.name;
|
||||
this.el_lst = document.createElement('li');
|
||||
this.el_lst.addEventListener('click', evt_click(this), false);
|
||||
this.el_lst.addEventListener('dblclick', evt_dblclick(this), false);
|
||||
this.el_lst = document.createElement("li");
|
||||
this.el_lst.addEventListener("click", (function(elem) {
|
||||
return function() {
|
||||
elem.show();
|
||||
};
|
||||
})(this), false);
|
||||
this.el_lst.addEventListener("dblclick", (function(elem) {
|
||||
return function() {
|
||||
elem.close();
|
||||
};
|
||||
})(this), false);
|
||||
|
||||
this.el_lst.appendChild(inner);
|
||||
this.wr_lst.appendChild(this.el_lst);
|
||||
};
|
||||
|
||||
Tab.prototype.initBodyElement = function() {
|
||||
var chat_input = document.createElement('input'),
|
||||
chat_input_w = document.createElement('div');
|
||||
var chat_input = document.createElement("input"),
|
||||
chat_input_w = document.createElement("div");
|
||||
|
||||
this.chat_log = document.createElement('p');
|
||||
this.chat_log.classList.add('chat-log');
|
||||
this.el_body = document.createElement('div');
|
||||
this.el_body.classList.add('tab-body');
|
||||
this.chat_log = document.createElement("pre");
|
||||
this.chat_log.classList.add("chat-log");
|
||||
this.el_body = document.createElement("div");
|
||||
this.el_body.classList.add("tab-body");
|
||||
this.el_body.id = this.name;
|
||||
this.el_body.style.display = 'none';
|
||||
chat_input_w.classList.add('chat-input-wrapper');
|
||||
chat_input.classList.add('chat-input');
|
||||
chat_input.setAttribute('type', 'text');
|
||||
this.el_body.style.display = "none";
|
||||
chat_input_w.classList.add("chat-input-wrapper");
|
||||
chat_input.classList.add("chat-input");
|
||||
chat_input.setAttribute("type", "text");
|
||||
|
||||
chat_input_w.appendChild(chat_input);
|
||||
this.el_body.appendChild(this.chat_log);
|
||||
|
@ -70,7 +78,7 @@ Tab.prototype.initBodyElement = function() {
|
|||
};
|
||||
|
||||
Tab.prototype.close = function() {
|
||||
if (typeof this.closeHandler !== 'undefined') {
|
||||
if (typeof this.closeHandler !== "undefined") {
|
||||
this.closeHandler();
|
||||
}
|
||||
this.wr_body.removeChild(this.el_body);
|
||||
|
@ -78,25 +86,29 @@ Tab.prototype.close = function() {
|
|||
};
|
||||
|
||||
Tab.prototype.hide = function() {
|
||||
this.el_lst.classList.remove('tab-current');
|
||||
this.el_body.style.display = 'none';
|
||||
this.el_lst.classList.remove("tab-current");
|
||||
this.el_body.style.display = "none";
|
||||
};
|
||||
|
||||
Tab.prototype.show = function() {
|
||||
if (typeof this.showHandler !== 'undefined') {
|
||||
if (typeof this.showHandler !== "undefined") {
|
||||
this.showHandler();
|
||||
}
|
||||
this.el_lst.classList.remove('tab-active');
|
||||
this.el_lst.classList.add('tab-current');
|
||||
this.el_body.style.display = 'block';
|
||||
this.el_lst.classList.remove("tab-active");
|
||||
this.el_lst.classList.add("tab-current");
|
||||
this.el_body.style.display = "block";
|
||||
this.chat_log.scrollTop = 42000;
|
||||
};
|
||||
|
||||
Tab.prototype.isCurrent = function() {
|
||||
return this.el_lst.classList.contains("tab-current");
|
||||
};
|
||||
|
||||
Tab.prototype.setActive = function() {
|
||||
this.el_lst.classList.add('tab-active');
|
||||
this.el_lst.classList.add("tab-active");
|
||||
}
|
||||
|
||||
Tab.prototype.appendText = function(text) {
|
||||
// TODO: flush text
|
||||
this.chat_log.innerHTML += text + '<br>';
|
||||
this.chat_log.innerHTML += text;
|
||||
};
|
||||
|
|
Reference in a new issue