thelounge/lib/server.js

552 lines
9.7 KiB
JavaScript
Raw Normal View History

var config = require("../config");
var http = require("connect");
var io = require("socket.io");
var irc = require("slate-irc");
var net = require("net");
var _ = require("lodash");
2014-03-06 15:11:25 +00:00
// Models
var Chan = require("./models/chan");
var Msg = require("./models/msg");
var Network = require("./models/network");
var User = require("./models/user");
var sockets = null;
var networks = [
new Network({host: "Status"})
];
2014-03-06 15:11:25 +00:00
2014-03-24 15:47:29 +00:00
var events = [
"join",
"kick",
2014-04-07 22:03:21 +00:00
"mode",
2014-04-03 20:55:51 +00:00
"motd",
2014-03-30 03:12:29 +00:00
"message",
2014-03-24 15:47:29 +00:00
"names",
"nick",
"notice",
"part",
"quit",
"topic",
"welcome",
2014-04-05 22:01:19 +00:00
"whois",
2014-03-24 15:47:29 +00:00
];
module.exports = listen;
function listen() {
2014-03-24 15:47:29 +00:00
var app = http()
.use(http.static("client"))
.listen(9000);
var self = this;
2014-03-24 15:47:29 +00:00
sockets = io.listen(app, {log: 0}).sockets.on("connection", function(s) {
s.on("input", input);
s.emit("networks", {
networks: networks
2014-04-19 21:40:36 +00:00
});
2014-03-13 17:50:02 +00:00
});
2014-03-24 13:44:41 +00:00
config.networks.forEach(function(n) {
connect(n);
2014-04-05 22:01:19 +00:00
});
}
2014-03-13 17:50:02 +00:00
2014-04-05 22:01:19 +00:00
function connect(params) {
params = _.extend(
config.defaults,
params
);
var host = params.host;
var port = params.port;
2014-04-05 22:01:19 +00:00
2014-03-24 15:47:29 +00:00
var stream = net.connect({
2014-04-05 22:01:19 +00:00
port: port,
2014-03-24 15:47:29 +00:00
host: host,
});
stream.on("error", function(e) {
console.log(e);
});
var client = irc(stream);
var network = new Network({
2014-03-29 23:59:28 +00:00
host: host,
client: client,
});
2014-03-24 15:47:29 +00:00
networks.push(network);
sockets.emit("networks", {
networks: networks
});
2014-04-05 22:01:19 +00:00
client.nick(params.nick);
client.user(params.nick, params.realname);
client.once("welcome", function() {
(params.channels || []).forEach(function(c) {
2014-04-05 22:01:19 +00:00
client.join(c);
});
});
2014-03-24 15:47:29 +00:00
events.forEach(function(e) {
2014-04-02 15:24:33 +00:00
client.on(e, function() {
event.apply(network, [e, arguments]);
2014-03-24 15:47:29 +00:00
});
});
2014-03-24 13:44:41 +00:00
}
function input(data) {
var target = find(data.id);
2014-03-09 21:22:37 +00:00
if (!target) {
return;
}
2014-03-24 13:44:41 +00:00
var network = target.network;
var chan = target.chan;
2014-03-30 03:12:29 +00:00
var client = network.client;
var id = data.id;
var text = data.text;
2014-03-24 13:44:41 +00:00
var args = text.replace(/^\//, '').split(" ");
var cmd = text.charAt(0) == "/" ? args[0].toLowerCase() : "";
2014-03-24 13:44:41 +00:00
2014-03-09 21:22:37 +00:00
switch (cmd) {
2014-03-24 13:44:41 +00:00
case "":
args.unshift(
"msg",
chan.name
2014-03-13 15:25:01 +00:00
);
2014-03-29 15:36:12 +00:00
case "msg":
2014-03-30 03:12:29 +00:00
var user;
var text = _.tail(args, 2).join(" ");
if (cmd == "msg") console.log(args);
2014-03-30 03:12:29 +00:00
if (client) {
user = client.me;
client.send(args[1], text);
}
var chan = _.findWhere(network.channels, {name: args[1]});
if (typeof chan !== "undefined") {
var msg = new Msg({
from: user,
text: text,
});
chan.messages.push(msg)
sockets.emit("msg", {
id: chan.id,
msg: msg,
});
2014-03-30 03:12:29 +00:00
}
2014-03-24 13:44:41 +00:00
break;
case "server":
case "connect":
2014-03-24 15:47:29 +00:00
if (args[1]) {
2014-04-05 22:01:19 +00:00
connect({host: args[1]});
2014-03-14 15:12:01 +00:00
}
2014-03-12 18:06:08 +00:00
break;
2014-03-24 13:44:41 +00:00
2014-03-29 23:59:28 +00:00
case "join":
2014-03-30 03:12:29 +00:00
case "nick":
if (client && args[1]) {
2014-04-06 15:46:42 +00:00
client[cmd].apply(client, args.slice(1));
2014-03-29 23:59:28 +00:00
}
break;
2014-04-06 20:53:07 +00:00
case "leave":
case "part":
var id = chan.id;
if (chan.type == "query" || !chan.users.length) {
remove(id);
sockets.emit("part", {
id: id,
});
2014-04-06 20:53:07 +00:00
} else if (client) {
client.part(chan.name);
2014-04-06 20:53:07 +00:00
}
break;
2014-03-30 03:12:29 +00:00
case "topic":
2014-04-06 18:04:11 +00:00
if (client) {
var msg = "TOPIC";
msg += " " + chan.name;
msg += args[1] ? " :" + args.slice(1).join(" ") : "";
client.write(msg);
2014-03-29 23:59:28 +00:00
}
break;
2014-04-02 15:24:33 +00:00
case "query":
case "whois":
if (client && args[1]) {
2014-04-06 15:46:42 +00:00
client.whois(args[1]);
2014-04-02 15:24:33 +00:00
}
break;
case "kick":
2014-04-06 15:46:42 +00:00
if (client && args[1]) {
client.kick(chan.name, args[1]);
2014-04-06 15:46:42 +00:00
}
break;
2014-04-07 22:03:21 +00:00
case "op":
case "deop":
case "voice":
case "devoice":
2014-04-06 15:46:42 +00:00
case "mode":
2014-04-07 22:03:21 +00:00
if (!client || !args[1]) {
break;
}
var mode;
var user;
if (cmd != "mode") {
user = args[1];
mode = {
"op": "+o",
"deop": "-o",
"voice": "+v",
"devoice": "-v",
}[cmd];
} else if (!args[2]) {
break;
} else {
mode = args[1];
user = args[2];
}
client.mode(
chan.name,
2014-04-07 22:03:21 +00:00
mode,
user
);
2014-04-02 15:24:33 +00:00
break;
2014-03-30 03:12:29 +00:00
case "quit":
case "disconnect":
if (client) {
client.quit();
networks = _.without(networks, network);
sockets.emit("networks", {
networks: networks
});
}
2014-03-30 03:12:29 +00:00
break;
2014-03-12 15:09:37 +00:00
}
2014-03-24 15:47:29 +00:00
}
function event(e, data) {
var data = _.last(data);
var channels = this.channels;
2014-03-24 15:47:29 +00:00
switch (e) {
2014-03-24 15:47:29 +00:00
case "join":
var chan = _.findWhere(channels, {name: data.channel});
if (typeof chan === "undefined") {
chan = new Chan({
name: data.channel,
});
channels.push(chan);
sockets.emit("join", {
id: this.id,
chan: chan,
});
2014-04-19 21:40:36 +00:00
}
var users = chan.users;
users.push(new User({name: data.nick}));
sockets.emit("users", {
id: chan.id,
users: users,
});
var msg = new Msg({
from: data.nick,
2014-03-29 23:59:28 +00:00
type: "join",
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
});
2014-04-06 15:46:42 +00:00
break;
2014-03-24 15:47:29 +00:00
case "kick":
var chan = _.findWhere(channels, {name: data.channel});
if (data.client == this.client.me) {
chan.users = [];
2014-04-06 15:46:42 +00:00
} else {
chan.users = _.without(chan.users, _.findWhere(chan.users, {name: data.client}));
2014-04-06 15:46:42 +00:00
}
sockets.emit("users", {
id: chan.id,
users: chan.users,
});
var msg = new Msg({
2014-04-06 15:46:42 +00:00
type: "kick",
from: data.nick,
text: data.client,
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
2014-04-06 15:46:42 +00:00
});
2014-04-03 20:55:51 +00:00
break;
2014-04-07 22:03:21 +00:00
case "mode":
var chan = _.findWhere(channels, {name: data.target});
2014-04-07 22:03:21 +00:00
if (typeof chan !== "undefined") {
var msg = new Msg({
2014-04-07 22:03:21 +00:00
type: "mode",
from: data.nick,
text: data.mode + " " + data.client,
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
2014-04-07 22:03:21 +00:00
});
}
break;
2014-04-03 20:55:51 +00:00
case "motd":
var chan = channels[0];
data.motd.forEach(function(m) {
var msg = new Msg({
type: "motd",
from: "-!-",
text: m,
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
});
2014-04-03 20:55:51 +00:00
});
2014-03-24 15:47:29 +00:00
break;
2014-03-30 03:12:29 +00:00
case "message":
var chan = _.findWhere(channels, {name: data.to});
if (typeof chan === "undefined") {
chan = new Chan({
name: data.from,
type: "query",
});
channels.push(chan);
sockets.emit("join", {
id: this.id,
chan: chan,
});
}
2014-04-21 15:08:57 +00:00
var type = "";
var text = data.message;
2014-04-21 15:08:57 +00:00
var network = this;
text.split(" ").forEach(function(w) {
if (w == network.client.me) type = "highlight";
2014-04-21 15:08:57 +00:00
});
var msg = new Msg({
2014-04-21 15:08:57 +00:00
type: type,
from: data.from,
2014-04-21 15:08:57 +00:00
text: text,
2014-03-30 03:12:29 +00:00
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
});
2014-03-30 03:12:29 +00:00
break;
2014-03-24 15:47:29 +00:00
case "names":
var chan = _.findWhere(channels, {name: data.channel});
chan.users = [];
data.names.forEach(function(n) {
chan.users.push(new User({name: n}));
});
sockets.emit("users", {
id: chan.id,
users: chan.users,
});
2014-03-24 15:47:29 +00:00
break;
case "nick":
if (data["new"] == this.client.me) {
var chan = channels[0];
var msg = new Msg({
from: "-!-",
text: "You're now known as " + data["new"],
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
2014-04-02 15:24:33 +00:00
});
}
channels.forEach(function(chan) {
var user = _.findWhere(chan.users, {name: data.nick});
2014-03-30 03:12:29 +00:00
if (!user) {
return;
}
user.name = data["new"];
sockets.emit("users", {
id: chan.id,
users: chan.users,
});
var msg = new Msg({
2014-03-30 03:12:29 +00:00
type: "nick",
from: data.nick,
text: data["new"],
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
2014-03-30 03:12:29 +00:00
});
});
2014-03-24 15:47:29 +00:00
break;
case "notice":
var chan = channels[0];
var msg = new Msg({
2014-04-20 22:12:07 +00:00
type: "notice",
from: data.to == "*" ? "-!-" : data.from,
text: data.message,
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
2014-04-20 22:12:07 +00:00
});
2014-03-24 15:47:29 +00:00
break;
2014-04-07 21:19:20 +00:00
case "part":
var chan = _.findWhere(channels, {name: data.channels[0]});
if (data.nick == this.client.me) {
remove(chan.id);
sockets.emit("part", {
id: chan.id,
});
} else {
chan.users = _.without(chan.users, _.findWhere(chan.users, {name: data.nick}));
sockets.emit("users", {
id: chan.id,
users: chan.users,
});
var msg = new Msg({
type: "part",
from: data.nick,
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
});
2014-04-07 21:19:20 +00:00
}
break;
2014-03-24 15:47:29 +00:00
case "quit":
channels.forEach(function(chan) {
var user = _.findWhere(chan.users, {name: data.nick});
if (!user) {
return;
2014-03-30 03:12:29 +00:00
}
chan.users = _.without(chan.users, user);
sockets.emit("users", {
id: chan.id,
users: chan.users,
});
var msg = new Msg({
type: "quit",
from: data.nick,
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
});
2014-03-30 03:12:29 +00:00
});
2014-03-24 15:47:29 +00:00
break;
case "topic":
var chan = _.findWhere(channels, {name: data.channel});
var from = data.nick || chan.name;
var msg = new Msg({
2014-03-30 03:12:29 +00:00
type: "topic",
from: from,
text: data.topic,
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
2014-03-30 03:12:29 +00:00
});
2014-03-24 15:47:29 +00:00
break;
case "welcome":
// Leaving this empty for now.
2014-03-24 15:47:29 +00:00
break;
case "whois":
if (!data) {
var chan = channels[0];
var msg = new Msg({
2014-04-02 15:24:33 +00:00
type: "error",
text: "No such nick/channel.",
2014-04-02 15:24:33 +00:00
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
2014-04-02 15:24:33 +00:00
});
} else {
var chan = _.findWhere(channels, {name: data.nickname});
if (typeof chan === "undefined") {
chan = new Chan({
type: "query",
name: data.nickname,
});
channels.push(chan);
sockets.emit("join", {
id: this.id,
chan: chan,
});
}
var i = 0;
for (var k in data) {
if (i++ == 5) break;
var msg = new Msg({
type: "whois",
from: "-!-",
text: k + ": " + data[k],
});
chan.messages.push(msg);
sockets.emit("msg", {
id: chan.id,
msg: msg,
});
}
2014-04-02 15:24:33 +00:00
}
2014-03-24 15:47:29 +00:00
break;
}
2014-04-02 15:24:33 +00:00
}
// Utils
function find(id) {
var result = false;
networks.forEach(function(n) {
result = {
network: n,
chan: _.findWhere(n.channels, {id: id}),
};
if (!result.chan) {
result = false;
}
});
return result;
}
function remove(id) {
networks.forEach(function(n) {
n.channels = _.without(n.channels, _.findWhere(n.channels, {id: id}));
});
}