netsoul
Version:
netsoul protocol library
230 lines (194 loc) • 7.86 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var ClientAuth, ClientBase, ClientDaemon, crypto, protocol, utils, _ref, _ref1,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
__slice = [].slice;
protocol = require('./protocol');
utils = require('./utils');
crypto = require('crypto');
ClientBase = (function(_super) {
__extends(ClientBase, _super);
function ClientBase(options) {
this.options = options != null ? options : {};
this.onConnect = __bind(this.onConnect, this);
this.onError = __bind(this.onError, this);
this.onMessage = __bind(this.onMessage, this);
this.onDisconnect = __bind(this.onDisconnect, this);
this.send = __bind(this.send, this);
this.connect = __bind(this.connect, this);
this.setupConnect = __bind(this.setupConnect, this);
this.setupClient = __bind(this.setupClient, this);
this.handleOptions = __bind(this.handleOptions, this);
this.verbose = __bind(this.verbose, this);
this.debug = __bind(this.debug, this);
ClientBase.__super__.constructor.apply(this, arguments);
this.handleOptions();
this.debug('ClientBase::constructor');
this.setupConnect();
this.setupClient();
}
ClientBase.prototype.debug = function() {
var args, _ref;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
if (this.options.debug) {
return (_ref = this.options).logFn.apply(_ref, ["" + this.constructor.name + "> "].concat(__slice.call(args)));
}
};
ClientBase.prototype.verbose = function() {
var args, _ref;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
if (this.options.verbose) {
return (_ref = this.options).logFn.apply(_ref, ["" + this.constructor.name + "> "].concat(__slice.call(args)));
}
};
ClientBase.prototype.handleOptions = function() {
var _base, _base1, _base2, _base3, _base4, _base5;
if ((_base = this.options).logFn == null) {
_base.logFn = console.log;
}
if ((_base1 = this.options).verbose == null) {
_base1.verbose = false;
}
if ((_base2 = this.options).debug == null) {
_base2.debug = false;
}
if ((_base3 = this.options).login == null) {
_base3.login = process.env.USER;
}
if ((_base4 = this.options).location == null) {
_base4.location = '-';
}
if ((_base5 = this.options).agent == null) {
_base5.agent = 'node-netsoul';
}
if (this.options.password == null) {
throw new Error('password is not specified');
}
if (this.options.nsconnect == null) {
throw new Error('nsconnect is not specified');
}
this.ns = this.options.nsconnect;
return this.debug('ClientBase::handleOptions', this.options);
};
ClientBase.prototype.setupClient = function() {
return this.debug('ClientBase::setupClient');
};
ClientBase.prototype.setupConnect = function() {
this.debug('ClientBase::setup');
this.ns.on('connect', this.onConnect);
this.ns.on('message', this.onMessage);
this.ns.on('error', this.onError);
return this.ns.on('disconnect', this.onDisconnect);
};
ClientBase.prototype.connect = function() {
this.debug('ClientBase::connect');
return this.ns.connect();
};
ClientBase.prototype.send = function(data) {
this.verbose('ClientBase::send', data.join(' '));
return this.ns.send(data);
};
ClientBase.prototype.onDisconnect = function() {
return this.debug('ClientBase::onDisconnect');
};
ClientBase.prototype.onMessage = function(message) {
var handlerName;
this.verbose('ClientBase::onMessage', message.line);
if (message.type != null) {
handlerName = "message_" + message.type;
this.debug("ClientBase::onMessage, type=" + handlerName);
this.emit(handlerName, message);
if (this[handlerName] != null) {
return this[handlerName](message);
}
}
};
ClientBase.prototype.onError = function(error) {
return this.debug('ClientBase::onError', error);
};
ClientBase.prototype.onConnect = function() {
return this.debug('ClientBase::onConnect');
};
return ClientBase;
})(utils.PubSub);
ClientAuth = (function(_super) {
__extends(ClientAuth, _super);
function ClientAuth() {
this.message_ping = __bind(this.message_ping, this);
this.handshakeAccepted = __bind(this.handshakeAccepted, this);
this.readyForHandshake = __bind(this.readyForHandshake, this);
this.message_rep = __bind(this.message_rep, this);
this.message_salut = __bind(this.message_salut, this);
this.setupClient = __bind(this.setupClient, this);
_ref = ClientAuth.__super__.constructor.apply(this, arguments);
return _ref;
}
ClientAuth.prototype.setupClient = function() {
ClientAuth.__super__.setupClient.apply(this, arguments);
this.debug("ClientAuth::setupClient");
this.authenticated = false;
return this.auth_step = 0;
};
ClientAuth.prototype.message_salut = function(message) {
this.debug("ClientAuth::message_salut", message.line);
this.handshake = message.split;
return this.send(['auth_ag', 'ext_user', 'none', 'none']);
};
ClientAuth.prototype.message_rep = function(message) {
var code;
code = message.split[1];
if (!this.authenticated) {
if (this.auth_step === 0) {
this.readyForHandshake();
}
if (this.auth_step === 1) {
this.handshakeAccepted();
}
return this.auth_step++;
}
};
ClientAuth.prototype.readyForHandshake = function() {
var agent, concat, hash, location, login;
concat = "" + this.handshake[2] + "-" + this.handshake[3] + "/" + this.handshake[4] + this.options.password;
login = this.options.login;
hash = crypto.createHash('md5').update(concat).digest("hex");
agent = encodeURIComponent(this.options.agent);
location = encodeURIComponent(this.options.location);
this.debug("ClientAuth::message_salut", "login=" + login + ", concat=" + concat + ", hash=" + hash + ", location=" + location + ", agent=" + agent);
return this.send(['ext_user_log', login, hash, agent, location]);
};
ClientAuth.prototype.handshakeAccepted = function() {
this.debug("ClientAuth::handshakeAccepted");
this.authenticated = true;
return this.emit('authenticate');
};
ClientAuth.prototype.message_ping = function(message) {
this.debug("ClientAuth::message_pong", message);
return this.send(message.split);
};
return ClientAuth;
})(ClientBase);
ClientDaemon = (function(_super) {
__extends(ClientDaemon, _super);
function ClientDaemon() {
_ref1 = ClientDaemon.__super__.constructor.apply(this, arguments);
return _ref1;
}
ClientDaemon.prototype.setupClient = function() {
var _this = this;
ClientDaemon.__super__.setupClient.apply(this, arguments);
this.debug("ClientDaemon::setupClient");
return this.on('authenticate', function() {
return _this.send(['user_cmd', 'state', 'actif']);
});
};
return ClientDaemon;
})(ClientAuth);
module.exports = {
base: ClientBase,
auth: ClientAuth,
daemon: ClientDaemon
};
}).call(this);