2014-06-26 23:05:47 +00:00
|
|
|
var _ = require("lodash");
|
2014-07-06 15:22:43 +00:00
|
|
|
var Client = require("./client");
|
2014-06-26 23:05:47 +00:00
|
|
|
var config = require("../config") || {};
|
2014-07-17 13:38:41 +00:00
|
|
|
var fs = require("fs");
|
2014-06-26 23:05:47 +00:00
|
|
|
var http = require("connect");
|
2014-07-17 15:21:47 +00:00
|
|
|
var indicator = require("indicator");
|
2014-06-26 23:05:47 +00:00
|
|
|
var io = require("socket.io");
|
2014-06-26 16:14:45 +00:00
|
|
|
|
2014-06-26 23:05:47 +00:00
|
|
|
var sockets = null;
|
|
|
|
var clients = [];
|
2014-06-26 16:14:45 +00:00
|
|
|
|
2014-06-26 23:05:47 +00:00
|
|
|
var inputs = [
|
|
|
|
"action",
|
2014-07-02 22:00:11 +00:00
|
|
|
"connect",
|
2014-06-26 23:05:47 +00:00
|
|
|
"invite",
|
|
|
|
"join",
|
|
|
|
"kick",
|
|
|
|
"mode",
|
|
|
|
"msg",
|
|
|
|
"nick",
|
|
|
|
"notice",
|
|
|
|
"part",
|
|
|
|
"quit",
|
|
|
|
"raw",
|
|
|
|
"topic",
|
|
|
|
"whois"
|
|
|
|
];
|
2014-06-26 16:14:45 +00:00
|
|
|
|
2014-06-26 23:05:47 +00:00
|
|
|
module.exports = function() {
|
2014-07-17 15:21:47 +00:00
|
|
|
var port = config.port || 9000;
|
|
|
|
var app = http()
|
|
|
|
.use(index)
|
|
|
|
.use(http.static("client"))
|
|
|
|
.listen(port);
|
|
|
|
|
|
|
|
sockets = io(app);
|
2014-07-08 20:50:41 +00:00
|
|
|
sockets.on("connect", function(socket) {
|
2014-06-29 01:07:38 +00:00
|
|
|
if (config.public) {
|
2014-06-30 01:20:54 +00:00
|
|
|
auth.call(socket);
|
2014-06-29 01:07:38 +00:00
|
|
|
} else {
|
|
|
|
init(socket);
|
|
|
|
}
|
2014-06-26 23:05:47 +00:00
|
|
|
});
|
2014-07-17 15:21:47 +00:00
|
|
|
|
|
|
|
console.log("Shout started.");
|
|
|
|
indicator.start(
|
|
|
|
"Running on port "
|
|
|
|
+ port
|
|
|
|
);
|
2014-06-26 23:05:47 +00:00
|
|
|
};
|
|
|
|
|
2014-07-17 13:38:41 +00: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 01:07:38 +00:00
|
|
|
function init(socket, client) {
|
2014-06-26 23:05:47 +00:00
|
|
|
if (!client) {
|
2014-06-27 00:47:36 +00:00
|
|
|
socket.emit("auth");
|
|
|
|
socket.on("auth", auth);
|
2014-06-26 23:05:47 +00:00
|
|
|
} else {
|
2014-07-08 20:50:41 +00:00
|
|
|
socket.on(
|
|
|
|
"input",
|
|
|
|
function(data) {
|
|
|
|
input(client, data);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
socket.on(
|
|
|
|
"conn",
|
|
|
|
function(data) {
|
|
|
|
client.connect(data);
|
|
|
|
}
|
|
|
|
);
|
2014-06-29 01:07:38 +00:00
|
|
|
socket.join(client.id);
|
|
|
|
socket.emit("init", {
|
2014-06-30 01:20:54 +00:00
|
|
|
networks: client.networks
|
2014-06-29 01:07:38 +00:00
|
|
|
});
|
2014-06-26 23:05:47 +00:00
|
|
|
}
|
2014-06-29 19:41:02 +00:00
|
|
|
}
|
2014-06-26 23:05:47 +00:00
|
|
|
|
2014-06-30 01:20:54 +00:00
|
|
|
function auth(data) {
|
2014-06-27 00:47:36 +00:00
|
|
|
var socket = this;
|
2014-06-30 01:20:54 +00:00
|
|
|
if (config.public) {
|
2014-07-08 20:50:41 +00:00
|
|
|
var client = new Client(sockets);
|
|
|
|
clients.push(client);
|
|
|
|
socket.on("disconnect", function() {
|
|
|
|
clients = _.without(clients, client);
|
|
|
|
client.quit();
|
|
|
|
});
|
2014-06-30 01:20:54 +00:00
|
|
|
init(socket, client);
|
|
|
|
} else {
|
|
|
|
if (false) {
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
}
|
2014-06-29 19:41:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function input(client, data) {
|
2014-07-02 22:00:11 +00:00
|
|
|
var target = client.find(data.target);
|
2014-07-06 15:22:43 +00:00
|
|
|
|
2014-07-02 22:00:11 +00:00
|
|
|
var text = data.text;
|
|
|
|
if (text.charAt(0) !== "/") {
|
|
|
|
text = "/say " + text;
|
|
|
|
}
|
2014-07-17 13:38:41 +00:00
|
|
|
|
2014-07-02 22:00:11 +00:00
|
|
|
var args = text.split(" ");
|
|
|
|
var cmd = args.shift().replace("/", "").toLowerCase();
|
2014-07-17 13:38:41 +00:00
|
|
|
|
2014-07-02 22:00:11 +00:00
|
|
|
inputs.forEach(function(plugin) {
|
2014-07-06 15:22:43 +00:00
|
|
|
try {
|
|
|
|
var fn = require("./plugins/inputs/" + plugin);
|
|
|
|
fn.apply(client, [
|
|
|
|
target.network,
|
|
|
|
target.chan,
|
|
|
|
cmd,
|
|
|
|
args
|
|
|
|
]);
|
|
|
|
} catch (err) {
|
|
|
|
// ..
|
|
|
|
}
|
2014-07-02 22:00:11 +00:00
|
|
|
});
|
2014-06-29 19:41:02 +00:00
|
|
|
}
|