hardlounge/lib/client.js

125 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-07-06 15:22:43 +00:00
var _ = require("lodash");
2014-07-19 01:31:00 +00:00
var config = require("../config.json");
2014-07-06 15:22:43 +00:00
var net = require("net");
var Network = require("./models/network");
var slate = require("slate-irc");
var tls = require("tls");
module.exports = Client;
var id = 0;
var events = [
"errors",
"join",
"kick",
"mode",
"motd",
"message",
"names",
"nick",
"notice",
"part",
"quit",
"topic",
"welcome",
"whois"
];
2014-07-19 01:31:00 +00:00
function Client(sockets, config) {
2014-07-08 20:50:41 +00:00
_.merge(this, {
2014-07-19 01:31:00 +00:00
config: config,
2014-07-08 20:50:41 +00:00
id: id++,
2014-07-19 01:31:00 +00:00
name: "",
networks: [],
2014-07-08 20:50:41 +00:00
sockets: sockets
});
2014-07-19 01:31:00 +00:00
if (config) {
var client = this;
_.each(config.networks || [], function(n) {
client.connect(n);
});
}
2014-07-06 15:22:43 +00:00
}
Client.prototype.emit = function(event, data) {
if (this.sockets !== null) {
this.sockets.in(this.id).emit(event, data);
}
};
Client.prototype.find = function(id) {
var network = null;
var chan = null;
for (var i in this.networks) {
var n = this.networks[i];
chan = _.find(n.channels, {id: id});
if (chan) {
network = n;
break;
}
}
if (network && chan) {
return {
network: network,
chan: chan
};
} else {
return false;
}
};
Client.prototype.connect = function(args) {
var client = this;
2014-07-19 01:31:00 +00:00
var server = {
host: args.host || "irc.freenode.org",
port: args.port || 6667
2014-07-06 15:22:43 +00:00
};
2014-07-18 14:40:08 +00:00
2014-07-19 01:31:00 +00:00
var stream = args.tls ? tls.connect(server) : net.connect(server);
2014-07-06 15:22:43 +00:00
stream.on("error", function(e) {
console.log(e);
});
2014-07-18 14:40:08 +00:00
2014-07-19 01:31:00 +00:00
var nick = args.nick || "shout-user";
var realname = args.realname || "Shout User";
2014-07-18 14:40:08 +00:00
2014-07-06 15:22:43 +00:00
var irc = slate(stream);
2014-07-08 20:50:41 +00:00
irc.me = nick;
irc.nick(nick);
irc.user(nick, realname);
2014-07-18 14:40:08 +00:00
2014-07-06 15:22:43 +00:00
var network = new Network({
2014-07-19 01:31:00 +00:00
host: server.host,
2014-07-06 15:22:43 +00:00
irc: irc
});
2014-07-18 14:40:08 +00:00
2014-07-06 15:22:43 +00:00
client.networks.push(network);
client.emit("network", {
network: network
});
2014-07-18 14:40:08 +00:00
2014-07-06 15:22:43 +00:00
events.forEach(function(plugin) {
2014-07-19 01:31:00 +00:00
var path = "./plugins/irc-events/" + plugin;
require(path).apply(client, [
2014-07-08 20:50:41 +00:00
irc,
network
]);
2014-07-06 15:22:43 +00:00
});
2014-07-18 14:40:08 +00:00
2014-07-19 01:31:00 +00:00
var join = (args.join || "#shout-irc").replace(/\,/g, " ").split(/\s+/g);
2014-07-06 15:22:43 +00:00
irc.on("welcome", function() {
2014-07-08 20:50:41 +00:00
irc.join(join);
});
};
Client.prototype.quit = function() {
this.networks.forEach(function(network) {
var irc = network.irc;
if (network.connected) {
2014-07-19 01:31:00 +00:00
irc.quit();
2014-07-08 20:50:41 +00:00
} else {
irc.stream.end();
}
2014-07-06 15:22:43 +00:00
});
};