transit-im
Version:
express-like framework for AIM bots creation (ICQ as well)
88 lines (83 loc) • 2.72 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var oscar, _;
oscar = require('oscar');
_ = require('underscore');
module.exports = function(icqConfig) {
var _ref;
if (!(((icqConfig != null ? icqConfig.login : void 0) != null) && ((icqConfig != null ? icqConfig.password : void 0) != null))) {
throw "You need to provide both login and password for icq account";
}
icqConfig.msgDelay = (_ref = icqConfig.msgDelay) != null ? _ref : 500;
return {
install: function(transit) {
return transit.client(this);
},
_onError: function(sender) {
return function(err) {
this.icq.notifyTyping(sender, oscar.TYPING_NOTIFY.TEXT_ENTERED);
if (err) {
return this.icq.sendIM(sender, "error: " + err);
}
};
},
start: function() {
var _this = this;
this._delayedTo = null;
this._delayedMessages = [];
this.icq = new oscar.OscarConnection({
connection: {
username: icqConfig.login + "",
password: icqConfig.password + "",
host: oscar.SERVER_ICQ
}
});
this.icq.on('im', function(text, sender, flags, ts) {
_this.icq.notifyTyping(sender.name, oscar.TYPING_NOTIFY.START);
return _this.callback(sender.name, text, _this._onError(sender).bind(_this));
});
this.icq.on('contactoffline', function(sender) {
return _this.callback(sender.name, {
command: "exit"
}, _this._onError(sender).bind(_this));
});
return this.icq.connect(function(error) {
if (error) {
_this.callback(null, {
error: error
}, function() {});
console.error("Cannot connect to ICQ server");
return console.error(error);
} else {
return _this.icq.getOfflineMsgs();
}
});
},
receive: function(callback) {
this.callback = callback;
},
_delayed: function(action) {
this._delayedMessages.push(action);
if (this._delayedTo === null) {
return this._doDelayed();
}
},
_doDelayed: function() {
var msg;
msg = this._delayedMessages.shift();
if (msg) {
msg();
return this._delayedTo = setTimeout(this._doDelayed.bind(this), icqConfig.msgDelay);
} else {
return this._delayedTo = null;
}
},
sendBack: function(userId, data, cb) {
var _this = this;
return this._delayed(function() {
return _this.icq.sendIM(userId, data, cb);
});
}
};
};
}).call(this);