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

211 lines
6.5 KiB
JavaScript
Raw Permalink 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() {
2013-02-27 15:37:08 +01:00
this.host = 'ns-server.epita.fr';
this.port = 4242;
this.default_status = 'actif';
this.status_disconnected = 'disconnected';
this.actions_enabled = ['ping', 'msg', 'status', 'login_out', 'who'];
this.action = {};
this.action.ping = function(client, str) {
2014-11-06 18:52:14 +01:00
if (str.substr(0, 5) !== 'ping ') {
return false;
}
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
client.socket.write(str, function(inf) {});
return true;
2012-11-21 21:13:18 +01:00
};
2013-02-27 15:37:08 +01:00
this.action.msg = (function(elem) {
2014-11-06 18:52:14 +01:00
return function(client, str) {
var mo = {}, mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| msg ([^ ]*)/.exec(str);
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
if (mch === null) {
return false;
}
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
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]);
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
$cs.ui.addContentToTab(mo.login, mo); // TODO: FIX ME!
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
return true;
};
2012-11-21 21:13:18 +01:00
})(this);
2013-02-27 15:37:08 +01:00
this.action.status = (function(elem) {
2014-11-06 18:52:14 +01:00
return function(client, str) {
var mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| state ([^ ]*):(\d+)/.exec(str);
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
if (mch === null) {
return false;
}
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
$cs.contacts.changeContactStatus(mch[2], elem.msgDecode(mch[6])); // TODO: FIX ME!
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
return true;
};
2012-11-21 21:13:18 +01:00
})(this);
2012-10-21 19:45:45 +02:00
2013-02-27 15:37:08 +01:00
this.action.login_out = function(client, str) {
2014-11-06 18:52:14 +01:00
var status = 'offline', mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| (login|logout)/.exec(str);
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
if (mch === null) {
return false;
}
2014-11-06 18:52:14 +01:00
if (mch[6] === 'login') {
status = 'actif';
}
2012-11-26 15:31:15 +01:00
2014-11-06 18:52:14 +01:00
$cs.contacts.changeContactStatus(mch[2], status); // TODO: FIX ME!
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
return true;
2013-02-27 15:37:08 +01:00
};
this.action.who = (function(elem) {
return function(client, str) {
2014-11-06 18:52:14 +01:00
var mch = /user_cmd (\d+):user:.*?:(.*?)@(.*?):.*?:(.*?):(.*?) \| who (\d+) (.*?) (.*?) (\d+) (\d+) (\d+) (\d+) (.*?) (.*?) (.*?) (.*?):(.*) (.*)/.exec(str);
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
if (mch === null) {
return false;
}
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
$cs.contacts.changeContactStatus(mch[7], elem.msgDecode(mch[16])); // TODO: FIX ME!
2013-02-27 15:37:08 +01:00
2014-11-06 18:52:14 +01:00
return true;
};
2013-02-27 15:37:08 +01:00
})(this);
2012-11-26 15:31:15 +01:00
};
2013-02-27 15:37:08 +01:00
NsClient.prototype.connect = function(client, callback) {
2012-12-18 16:27:29 +01:00
var login, pwd_socks;
2013-02-27 15:37:08 +01:00
if (!client.is_connected) {
2014-11-06 18:52:14 +01:00
login = $cs.opts.get('login');
pwd_socks = $cs.opts.get('pwd_socks');
if (login !== null && pwd_socks !== null) {
client.socket.read(function(data) {
var auth = '';
data = data.split(' ');
auth = 'ext_user_log ';
auth += login + ' ';
auth += hex_md5(data[2] + '-' + data[3] + '/' + data[4] + pwd_socks) + ' ';
auth += 'chromesoul chromesoul\n';
client.socket.write('auth_ag ext_user none none\n', function(inf) {
client.socket.read(function(data) {
client.socket.write(auth, function(inf) {
client.socket.read(function(data) {
if (data === 'rep 002 -- cmd end') {
client.is_connected = true;
console.info('connected to the netsoul server');
} else {
console.error('authentication failure');
}
callback();
});
});
});
});
});
} else {
callback();
}
2013-02-27 15:37:08 +01:00
} else {
2014-11-06 18:52:14 +01:00
callback();
2012-10-21 19:45:45 +02:00
}
};
2013-02-27 15:37:08 +01:00
NsClient.prototype.disconnect = function(client) {
var msg = 'user_cmd msg_user exit\n';
client.socket.write(msg, function() {
2014-11-06 18:52:14 +01:00
client.socket.disconnect();
2013-02-27 15:37:08 +01:00
});
2012-10-21 23:18:55 +02:00
};
2013-02-27 15:37:08 +01:00
NsClient.prototype.changeStatus = function(client) {
if (client.is_connected) {
2014-11-06 18:52:14 +01:00
status_msg = 'user_cmd state ';
status_msg += this.msgEncode(client.status) + ':';
status_msg += Math.round(new Date().getTime() / 1000) + '\n';
client.socket.write(status_msg, function(inf) {
$cs.ui.onUserStatusChange(client.status);
});
2012-10-21 19:45:45 +02:00
} else {
2014-11-06 18:52:14 +01:00
$cs.ui.onUserStatusChange(client.status);
2012-10-21 19:45:45 +02:00
}
};
2013-02-27 15:37:08 +01:00
NsClient.prototype.recv = function(client, data) {
for (var i = this.actions_enabled.length - 1; i >= 0; --i) {
2014-11-06 18:52:14 +01:00
if (this.action[this.actions_enabled[i]](client, data)) {
break;
}
2013-02-27 15:37:08 +01:00
}
};
2012-11-26 15:31:15 +01:00
2013-02-27 15:37:08 +01:00
NsClient.prototype.speak = function(client, to, msg) {
msg = 'user_cmd msg_user ' + to + ' msg ' + this.msgEncode(msg) + '\n';
client.socket.write(msg, function(inf) {});
2012-10-21 19:45:45 +02:00
};
2013-03-01 17:44:34 +01:00
NsClient.prototype.addContact = function(client, lst) {
var msg = 'user_cmd watch_log_user {' + lst.join(',') + '}\n';
2013-02-27 15:37:08 +01:00
client.socket.write(msg, function(inf) {
2014-11-06 18:52:14 +01:00
var msg = 'user_cmd who {' + lst.join(',') + '}\n';
client.socket.write(msg, function(inf) {});
2013-02-27 15:37:08 +01:00
});
};
NsClient.prototype.rmContact = function(client, name) {
2013-03-01 17:44:34 +01:00
// TODO
2013-02-27 15:37:08 +01:00
};
NsClient.prototype.replacePairs = function(str, pairs) {
for (var i in pairs) {
2014-11-06 18:52:14 +01:00
if (pairs.hasOwnProperty(i)) {
str = str.replace(pairs[i], i);
}
2012-10-21 19:45:45 +02:00
}
2013-02-27 15:37:08 +01:00
return str;
}
2012-10-21 19:45:45 +02:00
2013-02-27 15:37:08 +01:00
NsClient.prototype.msgDecode = function(msg) {
return this.replacePairs(unescape(msg), {
2014-11-06 18:52:14 +01:00
'@': /%40/g,
'*': /%2A/g,
'/': /%2F/g,
'+': /%2B/g
2013-02-27 15:37:08 +01:00
});
};
2012-10-21 19:45:45 +02:00
2013-02-27 15:37:08 +01:00
NsClient.prototype.msgEncode = function(msg) {
return this.replacePairs(escape(msg), {
2014-11-06 18:52:14 +01:00
'%40': /@/g,
'%2A': /\*/g,
'%2F': /\//g,
'%2B': /\+/g,
2013-02-27 15:37:08 +01:00
});
2012-10-21 19:45:45 +02:00
};