This repository has been archived on 2023-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
chromesoul/lib/ns_client.js

215 lines
6.3 KiB
JavaScript
Raw Normal View History

2012-10-21 19:45:45 +02:00
//
// Copyright (c) 2012 Rodolphe Breard
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
var NsClient = function() {
this.state = "actif";
this.allowed_statuses = ["actif", "away", "idle", "lock"];
this.is_connected = false;
2012-10-21 23:18:55 +02:00
this.socket = null;
2012-11-21 21:13:18 +01:00
this.actions = {};
2012-10-21 19:45:45 +02:00
2012-11-21 21:13:18 +01:00
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) {});
2012-10-21 19:45:45 +02:00
};
2012-11-21 21:13:18 +01:00
})(this);
this.actions.msg = {};
this.actions.msg.is = function(msg) {
var exp = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| msg ([^ ]*)/;
return exp.exec(msg) !== null;
2012-10-21 19:45:45 +02:00
};
2012-11-21 21:13:18 +01:00
this.actions.msg.act = (function(elem) {
return function(msg) {
2012-12-18 16:27:29 +01:00
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]);
elem.msgHandler(mo);
}
}
2012-11-21 21:13:18 +01:00
};
})(this);
};
2012-10-21 19:45:45 +02:00
2012-11-26 15:31:15 +01:00
NsClient.prototype.msgDecode = function(msg) {
msg = unescape(msg)
2012-12-18 16:39:37 +01:00
msg = msg.replace('@', '@');
msg = msg.replace('*', '*');
msg = msg.replace('/', '/');
msg = msg.replace('+', '+');
2012-11-26 15:31:15 +01:00
return msg;
};
NsClient.prototype.msgEncode = function(msg) {
msg = escape(msg)
2012-12-18 16:39:37 +01:00
msg = msg.replace('@', '@');
msg = msg.replace('*', '*');
msg = msg.replace('/', '/');
msg = msg.replace('+', '+');
2012-11-26 15:31:15 +01:00
return msg;
};
2012-11-21 21:13:18 +01:00
NsClient.prototype.connect = function() {
2012-12-18 16:27:29 +01:00
var login, pwd_socks;
2012-10-21 19:45:45 +02:00
if (!this.is_connected) {
2012-12-18 16:27:29 +01:00
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();
}
});
});
}
});
});
}
2012-11-21 21:13:18 +01:00
});
2012-12-18 16:27:29 +01:00
});
};
})(this));
}
2012-10-21 19:45:45 +02:00
}
};
NsClient.prototype.disconnect = function() {
if (this.is_connected) {
2012-10-21 23:18:55 +02:00
chrome.socket.disconnect(this.socket);
2012-10-21 19:45:45 +02:00
this.is_connected = false;
console.info("disconnected");
2012-11-13 16:29:52 +01:00
this.updateStatus();
2012-10-21 23:18:55 +02:00
}
};
NsClient.prototype.daemonize = function() {
2012-11-21 21:13:18 +01:00
if (this.is_connected) {
chrome.socket.read(this.socket, (function(elem) {
return function(rd_inf) {
if (rd_inf.resultCode > 0) {
var at, data = ab2str(rd_inf.data);
2012-10-21 23:48:41 +02:00
2012-11-21 21:13:18 +01:00
for (at in elem.actions) {
if (elem.actions[at].is(data)) {
elem.actions[at].act(data);
break ;
}
}
elem.daemonize();
2012-11-13 16:29:52 +01:00
} else {
2012-11-21 21:13:18 +01:00
/*
* 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...');
2012-10-21 23:18:55 +02:00
}
2012-11-21 21:13:18 +01:00
};
})(this));
2012-10-21 19:45:45 +02:00
} else {
2012-11-13 16:29:52 +01:00
console.error("unable to daemonize: not connected");
2012-10-21 19:45:45 +02:00
}
};
2012-11-26 15:31:15 +01:00
NsClient.prototype.sendMessage = function(to, message) {
var msg = "user_cmd msg_user " + to + " msg " + this.msgEncode(message) + "\n";
chrome.socket.write(this.socket, str2ab(msg), function(w_inf) {});
};
2012-10-21 19:45:45 +02:00
NsClient.prototype.updateStatus = function() {
2012-11-07 10:53:48 +01:00
var status_txt = document.getElementById("status_txt");
2012-10-21 19:45:45 +02:00
if (this.is_connected) {
2012-11-26 15:31:15 +01:00
if (status_txt !== null) {
2012-11-07 10:53:48 +01:00
status_txt.innerHTML = this.state;
2012-11-26 15:31:15 +01:00
}
2012-11-07 10:53:48 +01:00
2012-10-21 23:18:55 +02:00
var status_msg = "user_cmd state ";
status_msg += this.state + ":";
status_msg += Math.round(new Date().getTime() / 1000) + "\n";
chrome.socket.write(this.socket, str2ab(status_msg), function(w_inf) {});
2012-10-21 19:45:45 +02:00
} else {
2012-11-26 15:31:15 +01:00
if (status_txt !== null) {
2012-11-07 10:53:48 +01:00
status_txt.innerHTML = 'disconnected';
2012-11-26 15:31:15 +01:00
}
2012-10-21 19:45:45 +02:00
}
};
NsClient.prototype.changeStatus = function(new_status) {
if (this.is_connected) {
if (this.allowed_statuses.indexOf(new_status) !== -1) {
this.status = new_status;
} else {
console.warn("invalid status: " + new_status);
}
} else {
console.warn("not connected");
}
};
NsClient.prototype.init = function() {
2012-11-07 10:53:48 +01:00
var status_update = function(elem) {
2012-10-21 19:45:45 +02:00
return function() {
2012-11-07 10:53:48 +01:00
elem.updateStatus();
2012-10-21 19:45:45 +02:00
};
},
2012-11-07 10:53:48 +01:00
connect = function(elem) {
2012-10-21 19:45:45 +02:00
return function() {
if (!elem.is_connected) {
elem.connect();
}
};
};
2012-11-07 10:53:48 +01:00
status_update(this).apply();
2012-12-18 16:27:29 +01:00
setTimeout(connect(this), 500);
2012-11-07 10:53:48 +01:00
setInterval(connect(this), 10000);
setInterval(status_update(this), 600000);
2012-10-21 19:45:45 +02:00
};