thelounge/lib/server.js

183 lines
3.3 KiB
JavaScript
Raw Normal View History

2014-06-27 01:05:47 +02:00
var _ = require("lodash");
2014-07-06 17:22:43 +02:00
var Client = require("./client");
2014-07-19 03:31:00 +02:00
var config = require("../config.json");
2014-07-17 15:38:41 +02:00
var fs = require("fs");
2014-06-27 01:05:47 +02:00
var http = require("connect");
var io = require("socket.io");
2014-06-26 18:14:45 +02:00
2014-06-27 01:05:47 +02:00
var sockets = null;
var clients = [];
2014-06-26 18:14:45 +02:00
2014-06-27 01:05:47 +02:00
var inputs = [
"action",
2014-07-03 00:00:11 +02:00
"connect",
2014-06-27 01:05:47 +02:00
"invite",
"join",
"kick",
"mode",
"msg",
"nick",
"notice",
"part",
"quit",
"raw",
"topic",
"whois"
];
2014-06-26 18:14:45 +02:00
2014-06-27 01:05:47 +02:00
module.exports = function() {
2014-07-17 17:21:47 +02:00
var port = config.port || 9000;
var app = http()
.use(index)
.use(http.static("client"))
.listen(port);
sockets = io(app);
2014-07-08 22:50:41 +02:00
sockets.on("connect", function(socket) {
2014-06-29 03:07:38 +02:00
if (config.public) {
2014-06-30 03:20:54 +02:00
auth.call(socket);
2014-06-29 03:07:38 +02:00
} else {
init(socket);
}
2014-06-27 01:05:47 +02:00
});
2014-07-17 17:21:47 +02:00
2014-07-21 03:18:20 +02:00
console.log("Server started.");
2014-07-19 03:31:00 +02:00
console.log("Shout is now running on port " + port);
if (config.public) {
return;
}
fs.readdir("users/", function(err, files) {
if (err) {
console.log(err);
return;
}
_.each(files, function(file) {
fs.readFile("users/" + file + "/user.json", "utf-8", function(err, json) {
if (err) {
console.log(err);
return;
}
try {
json = JSON.parse(json);
} catch(e) {
console.log(e);
return;
}
clients.push(new Client(
sockets,
json
));
});
});
});
2014-06-27 01:05:47 +02:00
};
2014-07-17 15:38:41 +02:00
function index(req, res, next) {
if (req.url != "/") return next();
return fs.readFile("client/index.html", "utf-8", function(err, file) {
var data = _.merge(
require("../package.json"),
config
);
res.end(_.template(
file,
data
));
});
}
2014-06-29 03:07:38 +02:00
function init(socket, client) {
2014-06-27 01:05:47 +02:00
if (!client) {
2014-06-27 02:47:36 +02:00
socket.emit("auth");
socket.on("auth", auth);
2014-06-27 01:05:47 +02:00
} else {
2014-07-08 22:50:41 +02:00
socket.on(
"input",
function(data) {
input(client, data);
}
);
2014-07-20 21:49:44 +02:00
socket.on(
"showMore",
function(data) {
showMore(client, data);
}
);
2014-07-08 22:50:41 +02:00
socket.on(
"conn",
function(data) {
client.connect(data);
}
);
2014-06-29 03:07:38 +02:00
socket.join(client.id);
socket.emit("init", {
2014-06-30 03:20:54 +02:00
networks: client.networks
2014-06-29 03:07:38 +02:00
});
2014-06-27 01:05:47 +02:00
}
2014-06-29 21:41:02 +02:00
}
2014-06-27 01:05:47 +02:00
2014-06-30 03:20:54 +02:00
function auth(data) {
2014-06-27 02:47:36 +02:00
var socket = this;
2014-06-30 03:20:54 +02:00
if (config.public) {
2014-07-08 22:50:41 +02:00
var client = new Client(sockets);
clients.push(client);
socket.on("disconnect", function() {
clients = _.without(clients, client);
client.quit();
});
2014-06-30 03:20:54 +02:00
init(socket, client);
} else {
2014-07-25 13:36:09 +02:00
var success = 0;
2014-07-19 03:31:00 +02:00
_.each(clients, function(client) {
if (client.config.name == data.name && client.config.password == data.password) {
init(socket, client);
2014-07-25 13:36:09 +02:00
success++;
2014-07-19 03:31:00 +02:00
}
});
2014-07-25 13:36:09 +02:00
if (!success) {
socket.emit("auth");
}
2014-06-30 03:20:54 +02:00
}
2014-06-29 21:41:02 +02:00
}
function input(client, data) {
2014-07-03 00:00:11 +02:00
var text = data.text;
2014-07-20 21:49:44 +02:00
var target = client.find(data.target);
2014-07-03 00:00:11 +02:00
if (text.charAt(0) !== "/") {
text = "/say " + text;
}
2014-07-17 15:38:41 +02:00
2014-07-03 00:00:11 +02:00
var args = text.split(" ");
var cmd = args.shift().replace("/", "").toLowerCase();
2014-07-17 15:38:41 +02:00
2014-07-19 03:31:00 +02:00
_.each(inputs, function(plugin) {
2014-07-06 17:22:43 +02:00
try {
2014-07-19 03:31:00 +02:00
var path = "./plugins/inputs/" + plugin;
var fn = require(path);
2014-07-06 17:22:43 +02:00
fn.apply(client, [
target.network,
target.chan,
cmd,
args
]);
2014-07-19 03:31:00 +02:00
} catch (e) {
console.log(path + ": " + e);
2014-07-06 17:22:43 +02:00
}
2014-07-03 00:00:11 +02:00
});
2014-06-29 21:41:02 +02:00
}
2014-07-20 21:49:44 +02:00
function showMore(client, data) {
var target = client.find(data.target);
if (!target) {
return;
}
var chan = target.chan;
var messages = chan.messages.slice(0, chan.messages.length - (data.count || 0));
client.emit("showMore", {
chan: chan.id,
messages: messages
});
}