thelounge/src/client.js

226 lines
4.2 KiB
JavaScript
Raw Normal View History

2014-07-06 17:22:43 +02:00
var _ = require("lodash");
2014-09-13 18:41:11 +02:00
var config = require("../config");
2014-07-06 17:22:43 +02:00
var net = require("net");
2014-09-10 17:56:19 +02:00
var Msg = require("./models/msg");
2014-07-06 17:22:43 +02:00
var Network = require("./models/network");
var slate = require("slate-irc");
var tls = require("tls");
module.exports = Client;
var id = 0;
var events = [
2014-09-03 23:43:27 +02:00
"ctcp",
2014-08-16 21:49:28 +02:00
"error",
2014-08-17 23:40:08 +02:00
"image",
2014-07-06 17:22:43 +02:00
"join",
"kick",
"mode",
"motd",
"message",
"names",
"nick",
"notice",
"part",
"quit",
"topic",
"welcome",
"whois"
];
2014-09-09 21:31:23 +02:00
var inputs = [
"action",
"connect",
"invite",
"join",
"kick",
"mode",
"msg",
"nick",
"notice",
"part",
"quit",
"raw",
"topic",
"whois"
];
2014-07-06 17:22:43 +02:00
2014-07-19 03:31:00 +02:00
function Client(sockets, config) {
2014-07-08 22:50:41 +02:00
_.merge(this, {
2014-07-19 03:31:00 +02:00
config: config,
2014-07-08 22:50:41 +02:00
id: id++,
2014-07-19 03:31:00 +02:00
name: "",
networks: [],
2014-07-08 22:50:41 +02:00
sockets: sockets
});
2014-07-19 03:31:00 +02:00
if (config) {
var client = this;
2014-09-13 19:10:32 +02:00
var delay = 0;
(config.networks || []).forEach(function(n) {
setTimeout(function() {
client.connect(n);
2014-09-13 19:10:32 +02:00
}, delay);
delay += 1000;
2014-07-19 03:31:00 +02:00
});
}
2014-07-06 17:22:43 +02: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-09-13 19:10:32 +02:00
var server = {
host: args.host || "irc.freenode.org",
port: args.port || (args.tls ? 6697 : 6667),
name: args.name || "",
2014-09-10 17:56:19 +02:00
rejectUnauthorized: false
2014-09-13 19:10:32 +02:00
};
2014-07-18 16:40:08 +02:00
2014-07-19 03:31:00 +02:00
var stream = args.tls ? tls.connect(server) : net.connect(server);
2014-07-06 17:22:43 +02:00
stream.on("error", function(e) {
2014-09-10 17:56:19 +02:00
console.log("Client#connect():\n" + e);
stream.end();
var msg = new Msg({
type: Msg.Type.ERROR,
text: "Connection error."
});
client.emit("msg", {
msg: msg
});
2014-07-06 17:22:43 +02:00
});
2014-07-18 16:40:08 +02:00
2014-07-19 03:31:00 +02:00
var nick = args.nick || "shout-user";
var realname = args.realname || "Shout User";
2014-07-18 16:40:08 +02:00
2014-07-06 17:22:43 +02:00
var irc = slate(stream);
if (args.password) {
irc.pass(args.password);
}
2014-07-08 22:50:41 +02:00
irc.me = nick;
irc.nick(nick);
irc.user(nick, realname);
2014-07-18 16:40:08 +02:00
2014-07-06 17:22:43 +02:00
var network = new Network({
2014-07-19 03:31:00 +02:00
host: server.host,
name: server.name,
2014-07-06 17:22:43 +02:00
irc: irc
});
2014-07-18 16:40:08 +02:00
2014-07-06 17:22:43 +02:00
client.networks.push(network);
client.emit("network", {
network: network
});
2014-07-18 16:40:08 +02:00
2014-07-06 17:22:43 +02:00
events.forEach(function(plugin) {
2014-07-19 03:31:00 +02:00
var path = "./plugins/irc-events/" + plugin;
require(path).apply(client, [
2014-07-08 22:50:41 +02:00
irc,
network
]);
2014-07-06 17:22:43 +02:00
});
2014-07-18 16:40:08 +02:00
2014-09-09 21:31:23 +02:00
irc.once("welcome", function() {
var delay = 1000;
var commands = args.commands;
if (Array.isArray(commands)) {
commands.forEach(function(cmd) {
setTimeout(function() {
client.input({
target: network.channels[0].id,
text: cmd
});
}, delay);
delay += 1000;
});
}
setTimeout(function() {
irc.write("PING " + network.host);
}, delay);
});
irc.once("pong", function() {
var join = (args.join || "");
if (join) {
join = join.replace(/\,/g, " ").split(/\s+/g);
irc.join(join);
}
2014-07-08 22:50:41 +02:00
});
};
2014-09-09 21:31:23 +02:00
Client.prototype.input = function(data) {
var client = this;
var text = data.text;
var target = client.find(data.target);
if (text.charAt(0) !== "/") {
text = "/say " + text;
}
var args = text.split(" ");
var cmd = args.shift().replace("/", "").toLowerCase();
_.each(inputs, function(plugin) {
try {
var path = "./plugins/inputs/" + plugin;
var fn = require(path);
fn.apply(client, [
target.network,
target.chan,
cmd,
args
]);
} catch (e) {
console.log(path + ": " + e);
}
});
2014-09-13 19:18:42 +02:00
};
2014-09-09 21:31:23 +02:00
2014-09-10 21:23:56 +02:00
Client.prototype.more = function(data) {
var client = this;
var target = client.find(data.target);
if (!target) {
return;
}
var chan = target.chan;
var count = chan.messages.length - (data.count || 0);
var messages = chan.messages.slice(Math.max(0, count - 100), count);
client.emit("more", {
chan: chan.id,
messages: messages
});
2014-09-13 19:18:42 +02:00
};
2014-09-10 21:23:56 +02:00
2014-07-08 22:50:41 +02:00
Client.prototype.quit = function() {
this.networks.forEach(function(network) {
var irc = network.irc;
if (network.connected) {
2014-07-19 03:31:00 +02:00
irc.quit();
2014-07-08 22:50:41 +02:00
} else {
irc.stream.end();
}
2014-07-06 17:22:43 +02:00
});
};