2014-03-04 16:31:52 +00:00
|
|
|
var connect = require("connect");
|
2014-03-09 21:22:37 +00:00
|
|
|
var io = require("socket.io");
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
// Local library
|
2014-03-09 21:22:37 +00:00
|
|
|
var models = require("./models.js");
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
module.exports = Server;
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
function Server() {
|
|
|
|
this.sockets = false;
|
|
|
|
this.networks = new models.NetworkCollection;
|
|
|
|
}
|
2014-03-06 15:11:25 +00:00
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
Server.prototype.listen = function(port) {
|
|
|
|
var self = this;
|
2014-03-06 15:11:25 +00:00
|
|
|
var http = connect()
|
|
|
|
.use(connect.static("client"))
|
|
|
|
.listen(port);
|
2014-03-11 22:57:29 +00:00
|
|
|
|
|
|
|
this.networks.on(
|
|
|
|
"all",
|
2014-03-12 13:10:53 +00:00
|
|
|
function(type, data) {
|
|
|
|
if ([
|
|
|
|
"users",
|
|
|
|
"messages"
|
|
|
|
].indexOf(type) != -1) {
|
|
|
|
self.sockets.emit(type, data);
|
|
|
|
} else {
|
|
|
|
self.sockets.emit("networks", self.networks);
|
|
|
|
}
|
2014-03-11 22:57:29 +00:00
|
|
|
|
|
|
|
// Debug
|
2014-03-12 13:10:53 +00:00
|
|
|
console.log(type + ": " + data);
|
2014-03-11 22:57:29 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.sockets = io.listen(http, {log: false}).sockets;
|
2014-03-07 21:24:02 +00:00
|
|
|
this.sockets.on("connection", function(socket) {
|
2014-03-09 21:22:37 +00:00
|
|
|
socket.emit(
|
2014-03-12 13:10:53 +00:00
|
|
|
"networks",
|
2014-03-09 21:22:37 +00:00
|
|
|
self.networks
|
|
|
|
);
|
2014-03-09 19:27:44 +00:00
|
|
|
socket.on(
|
|
|
|
"input",
|
|
|
|
function(input) {
|
2014-03-09 23:14:22 +00:00
|
|
|
handleInput.call(self, input);
|
2014-03-09 19:27:44 +00:00
|
|
|
}
|
|
|
|
);
|
2014-03-07 21:24:02 +00:00
|
|
|
});
|
2014-03-06 18:02:43 +00:00
|
|
|
|
2014-03-07 21:24:02 +00:00
|
|
|
return this;
|
|
|
|
};
|
2014-03-06 18:02:43 +00:00
|
|
|
|
2014-03-09 23:14:22 +00:00
|
|
|
function handleInput(input) {
|
2014-03-09 21:22:37 +00:00
|
|
|
var target = this.networks.find(input.id);
|
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var argv = input.text.substr(1).split(" ");
|
|
|
|
var cmd = input.text.charAt(0) == "/" ? argv[0].toUpperCase()
|
|
|
|
: "";
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case "":
|
|
|
|
target.channel.get("messages").add(
|
2014-03-09 19:27:44 +00:00
|
|
|
new models.Message({user: "user", text: input.text})
|
|
|
|
);
|
2014-03-09 21:22:37 +00:00
|
|
|
break;
|
|
|
|
|
2014-03-12 13:10:53 +00:00
|
|
|
case "JOIN":
|
|
|
|
if (!argv[1]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var channels = this.networks.at(0).get("channels");
|
|
|
|
channels.add(new models.Channel({
|
|
|
|
name: argv[1]
|
|
|
|
}));
|
|
|
|
break;
|
|
|
|
|
2014-03-09 21:22:37 +00:00
|
|
|
case "CONNECT":
|
2014-03-09 23:14:22 +00:00
|
|
|
if (!argv[1]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var network = this.networks.add(new models.Network({
|
|
|
|
host: argv[1]
|
|
|
|
}));
|
|
|
|
network.irc.addListener(
|
|
|
|
"raw",
|
|
|
|
function() {
|
|
|
|
handleEvent.apply(network, arguments);
|
|
|
|
}
|
2014-03-09 21:22:37 +00:00
|
|
|
);
|
2014-03-09 23:14:22 +00:00
|
|
|
break;
|
2014-03-09 21:22:37 +00:00
|
|
|
|
2014-03-09 23:14:22 +00:00
|
|
|
case "DISCONNECT":
|
|
|
|
this.networks.remove(target.network);
|
2014-03-09 21:22:37 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
target.channel.get("messages").add(
|
|
|
|
new models.Message({text: "Command `/" + cmd + "` does not exist."})
|
2014-03-09 19:27:44 +00:00
|
|
|
);
|
2014-03-09 21:22:37 +00:00
|
|
|
break;
|
2014-03-09 19:27:44 +00:00
|
|
|
}
|
2014-03-09 23:14:22 +00:00
|
|
|
}
|
2014-03-09 21:22:37 +00:00
|
|
|
|
2014-03-09 23:14:22 +00:00
|
|
|
function handleEvent(argv) {
|
|
|
|
var network = this;
|
|
|
|
network.get("channels").at(0).get("messages").add(
|
|
|
|
new models.Message({
|
|
|
|
user: argv.args[0],
|
|
|
|
text: argv.args[1]
|
|
|
|
})
|
2014-03-09 21:22:37 +00:00
|
|
|
);
|
2014-03-11 22:57:29 +00:00
|
|
|
|
|
|
|
// Debug
|
|
|
|
console.log(argv);
|
2014-03-09 19:27:44 +00:00
|
|
|
}
|