Shuo/lib/server.js

434 lines
7.4 KiB
JavaScript
Raw Normal View History

2014-04-24 20:34:40 +00:00
var _ = require("lodash");
var config = require("../config") || {};
var fs = require("fs");
var http = require("connect");
var io = require("socket.io");
var irc = require("slate-irc");
var net = require("net");
var tls = require("tls");
2014-03-06 15:11:25 +00:00
var Msg = require("./models/msg");
var Network = require("./models/network");
var sockets = null;
2014-05-02 17:46:18 +00:00
var networks = [];
2014-03-06 15:11:25 +00:00
2014-06-19 15:28:53 +00:00
var plugins = [
2014-06-13 13:57:11 +00:00
"errors",
2014-03-24 15:47:29 +00:00
"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
];
2014-06-19 15:28:53 +00:00
module.exports = function listen() {
2014-04-26 17:44:16 +00:00
var port = config.port || 9000;
2014-03-24 15:47:29 +00:00
var app = http()
2014-04-24 20:34:40 +00:00
.use(index)
2014-03-24 15:47:29 +00:00
.use(http.static("client"))
2014-04-26 17:44:16 +00:00
.listen(port);
2014-03-24 15:47:29 +00:00
2014-06-15 17:26:05 +00:00
sockets = io.listen(app, {log: 0}).sockets.on("connection", function(socket) {
2014-06-15 19:18:45 +00:00
init.call(
socket,
!!config.password
);
2014-03-13 17:50:02 +00:00
});
2014-03-24 13:44:41 +00:00
2014-04-24 20:34:40 +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-06-15 19:18:45 +00:00
function init(login) {
if (login) {
this.on("auth", auth);
this.emit("auth");
} else {
this.join("chat");
this.on("debug", debug);
this.on("input", input);
this.on("fetch", fetch);
this.emit(
"networks",
{networks: networks}
);
}
2014-06-15 17:26:05 +00:00
}
2014-04-24 20:34:40 +00:00
function index(req, res, next) {
2014-04-24 23:57:51 +00:00
if (req.url != "/") return next();
2014-06-19 15:28:53 +00:00
return fs.readFile("client/index.html", function(err, file) {
2014-04-24 23:57:51 +00:00
var data = _.merge(
2014-04-24 20:34:40 +00:00
require("../package.json"),
2014-06-14 20:28:17 +00:00
config
2014-04-24 20:34:40 +00:00
);
2014-04-24 23:57:51 +00:00
res.end(_.template(
file,
data
));
});
2014-04-24 20:34:40 +00:00
}
2014-04-05 22:01:19 +00:00
function connect(params) {
2014-04-24 23:57:51 +00:00
_.defaults(
params,
config.defaults
2014-04-05 22:01:19 +00:00
);
var host = params.host;
2014-04-24 23:57:51 +00:00
var port = params.port || 6667;
var options = {
2014-03-24 15:47:29 +00:00
host: host,
port: port,
};
var stream = params.tls ? tls.connect(options) : net.connect(options);
2014-03-24 15:47:29 +00:00
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);
2014-06-15 19:18:45 +00:00
sockets.in("chat").emit("networks", {networks: networks});
2014-04-05 22:01:19 +00:00
client.nick(params.nick);
client.user(params.nick, params.realname);
2014-06-13 14:45:32 +00:00
2014-06-19 15:28:53 +00:00
plugins.forEach(function(plugin) {
require("./plugins/" + plugin).apply(network, [client, sockets]);
2014-06-18 23:26:28 +00:00
});
if (!params.onConnect) {
return;
}
2014-04-05 22:01:19 +00:00
client.once("welcome", function() {
2014-06-19 15:28:53 +00:00
client.write("PING " + network.host);
var channels = params.onConnect.join || [];
channels.forEach(function(chan) {
2014-06-13 14:45:32 +00:00
client.join.apply(
client,
2014-06-17 20:30:17 +00:00
chan.split(' ')
2014-06-13 14:45:32 +00:00
);
2014-04-05 22:01:19 +00:00
});
2014-06-17 20:30:17 +00:00
});
client.once("pong", function() {
var delay = 1000;
2014-06-19 15:28:53 +00:00
var commands = params.onConnect.commands || [];
commands.forEach(function(cmd) {
2014-06-17 20:30:17 +00:00
setTimeout(function() {
input({
id: network.channels[0].id,
text: cmd
});
}, delay);
delay += 1000;
});
2014-04-05 22:01:19 +00:00
});
2014-03-24 13:44:41 +00:00
}
2014-06-15 19:18:45 +00:00
function auth(password) {
if (password == config.password) {
this.disable = false;
init.call(this);
}
}
2014-06-15 17:26:05 +00:00
function debug(data) {
console.log(data);
}
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-05-13 22:47:35 +00:00
if (!text) {
return;
}
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-05-15 21:56:23 +00:00
case "say":
// Remove '/say' and treat this command as a message.
args.shift();
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;
2014-05-03 19:22:14 +00:00
var text = args.slice(2).join(" ");
2014-03-30 03:12:29 +00:00
if (client) {
user = client.me;
client.send(args[1], text);
}
2014-05-04 00:33:05 +00:00
var chan = _.findWhere(network.channels, {name: args[1]});
if (typeof chan !== "undefined") {
var msg = new Msg({
from: user,
text: text,
});
2014-06-15 15:01:38 +00:00
chan.addMsg(msg)
2014-06-15 19:18:45 +00:00
sockets.in("chat").emit("msg", {
2014-05-04 00:33:05 +00:00
id: chan.id,
msg: msg,
});
}
2014-03-24 13:44:41 +00:00
break;
2014-05-03 17:21:11 +00:00
case "notice":
if (client && args[2]) {
client.notice(args[1], args.slice(2).join(" "));
}
break;
2014-05-28 14:12:16 +00:00
case "slap":
var slap = "slaps " + args[1] + " around a bit with a large trout";
2014-05-03 19:22:14 +00:00
case "me":
if (!args[1]) {
break;
}
var user;
2014-05-28 14:12:16 +00:00
var text = slap || args.slice(1).join(" ");
2014-05-03 19:22:14 +00:00
if (client) {
user = client.me;
client.action(chan.name, text);
}
var msg = new Msg({
type: "action",
from: user,
text: text,
});
2014-06-15 15:01:38 +00:00
chan.addMsg(msg)
2014-06-15 19:18:45 +00:00
sockets.in("chat").emit("msg", {
2014-05-03 19:22:14 +00:00
id: chan.id,
msg: msg,
});
break;
2014-05-18 16:18:19 +00:00
case "ame":
var type = "action";
case "amsg":
var user = client.me;
var text = args.slice(1).join(" ");
var channels = [];
network.channels.forEach(function(chan) {
if (chan.type == "channel") {
channels.push(chan.name);
var msg = new Msg({
2014-05-25 20:46:54 +00:00
type: type || "normal",
2014-05-18 16:18:19 +00:00
from: user,
text: text,
});
2014-06-15 15:01:38 +00:00
chan.addMsg(msg)
2014-06-15 19:18:45 +00:00
sockets.in("chat").emit("msg", {
2014-05-18 16:18:19 +00:00
id: chan.id,
msg: msg,
});
}
});
client[type || "send"](
channels,
text
);
break;
2014-03-24 13:44:41 +00:00
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-05-18 21:23:31 +00:00
if (client && args[1]) {
2014-06-12 22:35:39 +00:00
if (!args[2] || args[2].charAt(0) == "#") {
client.join(args.slice(1));
} else {
client.join(
args[1],
args[2] // Password
);
}
2014-05-18 21:23:31 +00:00
}
break;
2014-03-30 03:12:29 +00:00
case "nick":
if (client && args[1]) {
2014-05-18 21:23:31 +00:00
client.nick(args[1]);
2014-03-29 23:59:28 +00:00
}
break;
2014-05-16 13:12:57 +00:00
case "part":
if (chan.type != "channel") {
return;
}
2014-05-15 21:58:50 +00:00
case "close":
2014-04-06 20:53:07 +00:00
case "leave":
2014-05-16 13:12:57 +00:00
if (chan.type == "lobby") {
return;
}
var id = chan.id;
if (chan.type == "query" || !chan.users.length) {
remove(id);
2014-06-15 19:18:45 +00:00
sockets.in("chat").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-05-16 13:12:57 +00:00
case "partall":
var part = [];
network.channels.forEach(function(c) {
if (c.type == "channel") part.push(c.name);
});
2014-06-17 20:30:17 +00:00
console.log("PART");
console.log(part);
2014-05-18 21:23:31 +00:00
client.part(part);
2014-05-16 13:12:57 +00:00
break;
2014-05-17 22:17:50 +00:00
case "invite":
if (client && args[2]) {
client.invite(args[1], args[2]);
}
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-06-12 12:11:12 +00:00
case "whoami":
var user = client.me;
2014-04-02 15:24:33 +00:00
case "query":
case "whois":
2014-06-12 12:11:12 +00:00
var user = user || args[1];
if (client && user) {
client.whois(user);
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) {
networks = _.without(networks, network);
2014-06-15 19:18:45 +00:00
sockets.in("chat").emit("networks", {networks: networks});
2014-04-24 23:57:51 +00:00
client.quit();
}
2014-03-30 03:12:29 +00:00
break;
2014-05-07 19:30:14 +00:00
2014-05-15 21:58:50 +00:00
case "raw":
2014-05-07 19:30:14 +00:00
case "send":
if (client) {
client.write(args.slice(1).join(" "));
}
break;
2014-03-12 15:09:37 +00:00
}
2014-03-24 15:47:29 +00:00
}
2014-06-15 17:26:05 +00:00
function fetch(data) {
var socket = this;
2014-06-02 21:08:03 +00:00
var target = find(data.id);
if (!target) {
return;
}
var chan = target.chan;
var messages = chan
.messages
.slice(0, chan.messages.length - (data.count || 0));
socket.emit("messages", {
id: data.id,
msg: messages,
});
}
function find(id) {
2014-04-24 23:57:51 +00:00
for (var i = 0; i < networks.length; i++) {
var result = {
network: networks[i],
chan: _.findWhere(networks[i].channels, {id: id}),
};
2014-04-24 23:57:51 +00:00
if (result.chan) {
return result;
}
2014-04-24 23:57:51 +00:00
}
}
function remove(id) {
networks.forEach(function(n) {
n.channels = _.without(n.channels, _.findWhere(n.channels, {id: id}));
});
}