2014-03-04 16:31:52 +00:00
|
|
|
var connect = require("connect");
|
2014-03-07 03:18:53 +00:00
|
|
|
var models = require("./models.js");
|
|
|
|
var _ = require("lodash");
|
2014-03-06 18:02:43 +00:00
|
|
|
var irc = require("irc");
|
2014-03-07 03:18:53 +00:00
|
|
|
var io = require("socket.io");
|
2014-03-06 15:11:25 +00:00
|
|
|
|
|
|
|
exports.listen = listen;
|
|
|
|
|
2014-03-07 03:18:53 +00:00
|
|
|
var sockets = false;;
|
2014-03-06 15:11:25 +00:00
|
|
|
var networks = [];
|
|
|
|
|
2014-03-07 03:18:53 +00:00
|
|
|
addToServer(
|
|
|
|
"NETWORK",
|
|
|
|
new models.Network({address: "Start"})
|
|
|
|
);
|
2014-03-06 15:11:25 +00:00
|
|
|
|
|
|
|
function listen(port) {
|
|
|
|
var http = connect()
|
|
|
|
.use(connect.static("client"))
|
|
|
|
.listen(port);
|
2014-03-07 03:18:53 +00:00
|
|
|
sockets = io
|
|
|
|
.listen(http)
|
|
|
|
.on("connection", initSocket)
|
|
|
|
.sockets;
|
2014-03-06 18:02:43 +00:00
|
|
|
}
|
2014-03-04 16:31:52 +00:00
|
|
|
|
2014-03-07 03:18:53 +00:00
|
|
|
function initSocket(socket) {
|
|
|
|
socket.on("input", handleUserInput);
|
2014-03-06 18:02:43 +00:00
|
|
|
refresh();
|
2014-03-07 03:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendEvent(params) {
|
|
|
|
if (sockets) {
|
|
|
|
sockets.emit("event", new models.Event(params));
|
|
|
|
}
|
2014-03-06 18:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function refresh() {
|
2014-03-07 03:18:53 +00:00
|
|
|
sendEvent({action: "RENDER", type: "NETWORK", data: networks});
|
|
|
|
}
|
|
|
|
|
|
|
|
function addToServer(type, model, target) {
|
|
|
|
switch (type) {
|
|
|
|
|
|
|
|
case "NETWORK":
|
|
|
|
var channel = new models.Channel({
|
|
|
|
name: model.address,
|
|
|
|
type: "network"
|
|
|
|
});
|
|
|
|
|
|
|
|
model.channels.push(channel);
|
|
|
|
networks.push(model);
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "CHANNEL":
|
|
|
|
target.network.channels.push(model);
|
|
|
|
refresh();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "MESSAGE":
|
|
|
|
target.channel.messages
|
|
|
|
.push(model);
|
|
|
|
sendEvent({
|
|
|
|
action: "RENDER",
|
|
|
|
type: "MESSAGE",
|
|
|
|
target: target.channel.id,
|
|
|
|
data: model
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
|
2014-03-06 18:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-05 17:33:11 +00:00
|
|
|
|
2014-03-06 15:11:25 +00:00
|
|
|
function handleUserInput(input) {
|
2014-03-07 03:18:53 +00:00
|
|
|
var id = input.id;
|
2014-03-06 15:11:25 +00:00
|
|
|
var text = input.text;
|
2014-03-06 18:02:43 +00:00
|
|
|
|
2014-03-07 03:18:53 +00:00
|
|
|
var args = text.substr(1).split(' ');
|
|
|
|
var cmd = text.charAt(0) == "/" ? args[0].toUpperCase()
|
|
|
|
: "MESSAGE";
|
|
|
|
|
|
|
|
var target = getTarget(id);
|
2014-03-06 22:36:56 +00:00
|
|
|
|
|
|
|
switch (cmd) {
|
2014-03-07 03:18:53 +00:00
|
|
|
|
2014-03-06 22:36:56 +00:00
|
|
|
case "SERVER":
|
|
|
|
case "CONNECT":
|
2014-03-07 03:18:53 +00:00
|
|
|
addToServer(
|
|
|
|
"NETWORK",
|
|
|
|
new models.Network({address: args[1]})
|
|
|
|
);
|
2014-03-06 18:02:43 +00:00
|
|
|
break;
|
2014-03-07 03:18:53 +00:00
|
|
|
|
2014-03-06 22:36:56 +00:00
|
|
|
case "JOIN":
|
2014-03-07 03:18:53 +00:00
|
|
|
addToServer(
|
|
|
|
"CHANNEL",
|
|
|
|
new models.Channel({name: args[1]}),
|
|
|
|
target
|
|
|
|
);
|
2014-03-06 22:36:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "PART":
|
2014-03-07 03:18:53 +00:00
|
|
|
target.network.channels = _.reject(target.network.channels, {id: id});
|
2014-03-06 18:02:43 +00:00
|
|
|
refresh();
|
|
|
|
break;
|
|
|
|
|
2014-03-07 03:18:53 +00:00
|
|
|
case "MESSAGE":
|
|
|
|
addToServer(
|
|
|
|
"MESSAGE",
|
|
|
|
new models.Message({text: input.text}),
|
|
|
|
getTarget(id)
|
2014-03-06 18:02:43 +00:00
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
2014-03-06 22:36:56 +00:00
|
|
|
}
|
2014-03-06 18:02:43 +00:00
|
|
|
}
|
|
|
|
|
2014-03-07 03:18:53 +00:00
|
|
|
function getTarget(id) {
|
|
|
|
var find;
|
|
|
|
_.each(networks, function(n) {
|
|
|
|
find = {network: n, channel: _.findWhere(n.channels, {id: id})};
|
|
|
|
if (find.channel)
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
if (find.channel) {
|
|
|
|
return new models.Target(find);
|
2014-03-06 18:02:43 +00:00
|
|
|
}
|
|
|
|
}
|