transit-im
Version:
express-like framework for AIM bots creation (ICQ as well)
260 lines (224 loc) • 8.05 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var EventEmitter, Request, Response, Transit, callbackWrap, _,
__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;
_ = require('underscore');
Request = require('./core/request');
Response = require('./core/response');
EventEmitter = require('events').EventEmitter;
callbackWrap = require('./core/callbackWrapper');
Transit = (function(_super) {
__extends(Transit, _super);
function Transit() {
this._handlers = [];
this._chain = [];
this._formatters = {};
this._defaultFormattingMethod = function(data, options, cb) {
return cb(null, data);
};
}
Transit.prototype.use = function(middleware) {
var middlewareInstallingFunction, middlewareObject, _ref, _ref1,
_this = this;
middlewareObject = function() {
if (!_.isFunction(middleware)) {
return middleware.install(_this);
}
};
middlewareInstallingFunction = function() {
if (_.isFunction(middleware) && middleware.length <= 1) {
return middleware(_this);
}
};
middleware = (_ref = (_ref1 = middlewareObject()) != null ? _ref1 : middlewareInstallingFunction()) != null ? _ref : middleware;
if (_.isFunction(middleware)) {
return this._chain.push(middleware);
}
};
Transit.prototype.client = function(client) {
this._client = function() {
return client;
};
return void 0;
};
Transit.prototype.formatOutput = function(method, formatter) {
var _this = this;
if (_.isFunction(method)) {
formatter = method;
method = "_default";
this._defaultFormattingMethod = formatter;
}
this._formatters[method] = formatter;
this.extendResponse(method);
return this.sendBack[method] = function(userId, data, options, cb) {
return _this._send(userId, data, formatter, options, cb);
};
};
Transit.prototype._client = function() {
throw "Please install client";
};
Transit.prototype.start = function(options) {
this._client().receive(this._onRequest.bind(this));
return this._client().start(options);
};
Transit.prototype._onRequest = function(userId, data, doneCb) {
var chain, context, defineRenderer, doNext, method, name, _ref,
_this = this;
chain = this._chain.slice();
chain.push(function(req, res, next) {
var handler, _ref;
if (req.command) {
_this.emit(req.command, req, res);
return next();
} else {
handler = (_ref = req.handler) != null ? _ref : _this._defaultHandler();
if (!handler) {
throw "There is no handler defined";
}
return handler(req, res);
}
});
chain.push(function(req, res, next) {
doneCb(res.error);
return next();
});
context = {
req: new Request(userId, data, this._handlers),
res: new Response(callbackWrap(function(message) {
_this.sendBack(userId, message, function(err) {
if (err) {
return _this._onError(err);
}
});
return doNext();
}), function() {
return doNext();
})
};
defineRenderer = function(name, method) {
return context.res.attr(name, callbackWrap(function(data, options) {
_this._send(userId, data, method, options, function(err) {
if (err) {
return _this._onError(err);
}
});
return doNext();
}));
};
_ref = this._formatters;
for (name in _ref) {
method = _ref[name];
defineRenderer(name, method);
}
doNext = function(error) {
var nextStep;
if (error) {
_this._onError(error);
}
nextStep = chain.shift();
if (!nextStep) {
return;
}
return process.nextTick(function() {
var e;
try {
return nextStep(context.req.toJSON(), context.res.toJSON(), doNext);
} catch (_error) {
e = _error;
_this._onError(e);
context.res.attr("error", e);
return doNext();
}
});
};
return doNext();
};
Transit.prototype._onError = function(error) {
console.error(error != null ? error : "Unknown error");
if (error != null ? error.stack : void 0) {
return console.error(error != null ? error.stack : void 0);
}
};
Transit.prototype.sendBack = function(userId, data, options, cb) {
if (cb == null) {
cb = function() {};
}
return this._send(userId, data, this._defaultFormattingMethod, options, cb);
};
Transit.prototype._send = function(userId, data, formattingFunction, options, cb) {
var _this = this;
if (cb == null) {
cb = function() {};
}
if (_.isFunction(options)) {
cb = options;
options = null;
}
return formattingFunction(data, options, function(error, dataToFormat) {
if (error) {
return cb(error);
}
return _this._sendDataBack(userId, dataToFormat, cb);
});
};
Transit.prototype._sendDataBack = function(userId, data, cb) {
return this._client().sendBack(userId, data, cb);
};
Transit.prototype.extendRequest = function() {
var properties;
properties = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return Request.define.apply(Request, properties);
};
Transit.prototype.extendResponse = function() {
var properties;
properties = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
return Response.define.apply(Response, properties);
};
Transit.prototype.receive = function(pattern, optionsOrHandler, handler) {
var handlerDef, options;
options = {};
if (_.isFunction(handler)) {
options = optionsOrHandler;
} else if (_.isFunction(pattern)) {
handler = pattern;
pattern = null;
} else if (_.isObject(pattern)) {
options = pattern;
pattern = pattern.pattern;
handler = optionsOrHandler;
} else {
handler = optionsOrHandler;
}
if (!_.isFunction(handler)) {
throw new Error("Handler shall be a function, but it is " + (JSON.stringify(handler)));
}
handlerDef = _.extend(_.clone(options), {
pattern: pattern,
handler: handler
});
return this._handlers.push(handlerDef);
};
Transit.prototype._defaultHandler = function() {
var _ref;
return (_ref = _.findWhere(this._handlers, {
pattern: null
})) != null ? _ref.handler : void 0;
};
return Transit;
})(EventEmitter);
module.exports = function() {
return new Transit();
};
module.exports.commandLine = require('./clients/command_line/commandLine');
module.exports.icq = require('./clients/icq/icq');
module.exports.doNotWaitForResponse = require('./middleware/do_not_wait_for_response/doNotWaitForResponse');
module.exports.commandParser = require('./middleware/command_parser/commandParser');
module.exports.chain = require('./middleware/formatter_chain/formatterChain');
module.exports.html2txt = require('./middleware/html2txt/html2txt');
module.exports.sessions = require('./middleware/sessions/session_manager');
module.exports.echo = require('./middleware/echo/echo');
module.exports.autohelp = require('./middleware/autohelp/autohelp');
module.exports.alias = require('./middleware/alias/alias');
}).call(this);