var connect = require("connect"); var io = require("socket.io"); // Local library var models = require("./models.js"); module.exports = Server; function Server() { this.sockets = false; this.networks = new models.NetworkCollection; } Server.prototype.listen = function(port) { var self = this; var http = connect() .use(connect.static("client")) .listen(port); this.networks.on( "all", function(type, data) { if ([ "users", "messages" ].indexOf(type) != -1) { self.sockets.emit(type, data); } else { self.sockets.emit("networks", self.networks); } // Debug console.log(type + ": " + data); } ); this.sockets = io.listen(http, {log: false}).sockets; this.sockets.on("connection", function(socket) { socket.emit( "networks", self.networks ); socket.on( "input", function(input) { handleInput.call(self, input); } ); }); return this; }; function handleInput(input) { 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( new models.Message({user: "user", text: input.text}) ); break; case "JOIN": if (!argv[1]) { return; } var channels = this.networks.at(0).get("channels"); channels.add(new models.Channel({ name: argv[1] })); break; case "CONNECT": if (!argv[1]) { return; } var network = this.networks.add(new models.Network({ host: argv[1] })); network.irc.addListener( "raw", function() { handleEvent.apply(network, arguments); } ); break; case "DISCONNECT": this.networks.remove(target.network); break; default: target.channel.get("messages").add( new models.Message({text: "Command `/" + cmd + "` does not exist."}) ); break; } } 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] }) ); // Debug console.log(argv); }