From ea2d20e1181998b21fef5aeebfb37398a8892d69 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 9 Mar 2014 22:22:37 +0100 Subject: [PATCH] Connect to IRC networks --- app.js | 4 ++- client/js/chat.js | 2 +- lib/models.js | 38 +++++++++++++++++++------ lib/server.js | 70 ++++++++++++++++++++++++++++++++++++----------- 4 files changed, 88 insertions(+), 26 deletions(-) diff --git a/app.js b/app.js index a41d97f..73ace2a 100644 --- a/app.js +++ b/app.js @@ -12,6 +12,7 @@ var server = .listen(PORT); // Temp +/* var models = require("./lib/models.js"); var network = new models.Network({host: "irc.network.org"}); @@ -26,5 +27,6 @@ network.get("channels").add(new models.Channel({ users: [ new models.User({name: "user"}), new models.User({name: "other_user"}), - ] + ]: })); +*/ diff --git a/client/js/chat.js b/client/js/chat.js index 3cf2c9a..d5ae48f 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -31,7 +31,7 @@ $(function() { }) ); - chat.find(".messages").sticky(); + chat.find(".messages").sticky().scrollToBottom(); chat.find(".window") // Sort windows by `data-id` value. .sort(function(a, b) { return ($(a).data("id") - $(b).data("id")); }) diff --git a/lib/models.js b/lib/models.js index a29b6e7..54337f7 100644 --- a/lib/models.js +++ b/lib/models.js @@ -1,3 +1,4 @@ +var irc = require("irc"); var Backbone = require("backbone"); var moment = require("moment"); @@ -14,25 +15,33 @@ models.User = Backbone.Model.extend({ } }); +models.UserCollection = Backbone.Collection.extend({ + model: models.User +}); + models.Message = Backbone.Model.extend({ defaults: { time: moment().format("HH:mm"), - user: "user", - text: "text" + user: "", + text: "" } }); +models.MessageCollection = Backbone.Collection.extend({ + model: models.Message +}); + models.Channel = Backbone.Model.extend({ defaults: { type: "channel", name: "", - topic: "", - users: [], - messages: [] + topic: "" }, initialize: function() { this.set({ - id: id++ + id: id++, + users: new models.UserCollection(), + messages: new models.MessageCollection() }); } }); @@ -44,12 +53,22 @@ models.ChannelCollection = Backbone.Collection.extend({ models.Network = Backbone.Model.extend({ defaults: { host: "", + nick: "default_username", + connect: false }, initialize: function() { this.set({ id: id++, channels: new models.ChannelCollection() }); + if (this.get("connect")) { + this.conn = new irc.Client( + this.get("host"), + this.get("nick"), { + channels: ["#testchan"] + } + ); + } this.get("channels").add(new models.Channel({ type: "network", name: this.get("host") @@ -64,12 +83,15 @@ models.NetworkCollection = Backbone.Collection.extend({ host: "Lobby" })); }, - getChannel: function(id) { + find: function(id) { var networks = this.models; for (var i = 0; i < networks.length; i++) { var find = networks[i].get("channels").findWhere({id: id}); if (find) { - return find; + return { + network: networks[i], + channel: find + }; } } } diff --git a/lib/server.js b/lib/server.js index edd1d53..5a1d6eb 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,8 +1,8 @@ var connect = require("connect"); -var io = require("socket.io"); +var io = require("socket.io"); // Local library -var models = require("./models.js"); +var models = require("./models.js"); module.exports = Server; @@ -19,7 +19,10 @@ Server.prototype.listen = function(port) { this.sockets = io.listen(http).sockets; this.sockets.on("connection", function(socket) { - init.call(self, socket); + socket.emit( + "event", + self.networks + ); socket.on( "input", function(input) { @@ -31,22 +34,57 @@ Server.prototype.listen = function(port) { return this; }; -function init(socket) { - socket.emit( - "event", - this.networks - ) -} - function handleUserInput(input) { - var channel = this.networks.getChannel(input.id); - if (channel) { - channel.get("messages").push( + 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}) ); - this.sockets.emit( - "event", - this.networks + break; + + case "CONNECT": + var network = new models.Network({ + host: "irc.freenode.org", + connect: true + }); + this.networks.add(network); + var messages = network.get("channels").at(0).get("messages"); + messages.add( + new models.Message({text: "Connecting..."}) ); + + var self = this; + network.conn.addListener("raw", function(argv) { + messages.add( + new models.Message({user: argv.args[0], text: argv.args[1]}) + ); + self.sockets.emit( + "event", + self.networks + ); + }); + break; + + default: + target.channel.get("messages").add( + new models.Message({text: "Command `/" + cmd + "` does not exist."}) + ); + break; + } + + this.sockets.emit( + "event", + this.networks + ); }