From 57bd684c74a8e142a0c2678252efeb3d4971f96e Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Wed, 12 Mar 2014 20:45:41 +0100 Subject: [PATCH] Sending queries and messages to channels now works --- lib/models.js | 2 +- lib/server.js | 82 ++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/lib/models.js b/lib/models.js index 9c916ec..a1afa43 100644 --- a/lib/models.js +++ b/lib/models.js @@ -11,7 +11,7 @@ var id = 1; models.User = Backbone.Model.extend({ defaults: { mode: "", - name: "" + name: "user" } }); diff --git a/lib/server.js b/lib/server.js index 6952f96..384554b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -20,10 +20,7 @@ Server.prototype.listen = function(port) { this.networks.on( "all", function(type, data) { - if ([ - "users", - "messages" - ].indexOf(type) != -1) { + if (type == "users" || type == "messages") { self.sockets.emit(type, data); } else { self.sockets.emit("networks", self.networks); @@ -31,11 +28,8 @@ Server.prototype.listen = function(port) { } ); - var options = { - log: false - }; this.sockets = io - .listen(http, options) + .listen(http, {log: false}) .sockets; this.sockets.on("connection", function(socket) { socket.emit( @@ -58,18 +52,39 @@ function handleInput(input) { if (!target) { return; } - + var argv = input.text.substr(1).split(" "); var cmd = input.text.charAt(0) == "/" ? argv[0].toUpperCase() : ""; switch (cmd) { case "": + var irc = target.network.irc; + if (typeof irc !== "undefined") { + irc.say(target.channel.get("name"), input.text); + } target.channel.get("messages").add( - new models.Message({user: "user", text: input.text}) + new models.Message({ + user: target.network.get("nick"), + text: input.text + }) ); break; + case "QUERY": + var irc = target.network.irc; + if (argv[1] && typeof irc !== "undefined") { + var channels = target.network.get("channels"); + if (argv[1].charAt(0) != "#" && !channels.findWhere({name: argv[1]})) { + channels.add( + new models.Channel({ + name: argv[1] + }) + ); + } + } + break; + case "JOIN": var irc = target.network.irc; if (argv[1] && typeof irc !== "undefined") { @@ -79,15 +94,15 @@ function handleInput(input) { case "PART": var irc = target.network.irc; - if (argv[1] && typeof irc !== "undefined") { - irc.part(argv[1]); + if (typeof irc !== "undefined") { + irc.part(argv[1] ? argv[1] : target.channel.get("name")); } break; case "NAMES": var irc = target.network.irc; - if (argv[1] && typeof irc !== "undefined") { - irc.send("NAMES", argv[1]); + if (typeof irc !== "undefined") { + irc.send("NAMES", argv[1] ? argv[1] : target.channel.get("name")); } break; @@ -97,6 +112,9 @@ function handleInput(input) { var network = this.networks.add( new models.Network({host: argv[1]}) ); + network.irc.addListener( + "error", function() { /* .. */ } + ); network.irc.addListener( "raw", function() { @@ -135,6 +153,31 @@ function handleEvent(argv) { var event = argv.command; switch (event) { + case "PRIVMSG": + var target = argv.args[0]; + if (target.charAt(0) != "#") { + target = argv.nick; + } + + var channel = channels.findWhere({name: target}); + var message = argv.args[1]; + + if (typeof channel == "undefined") { + channel = channels.add( + new models.Channel({ + name: target + }) + ); + } + + channel.get("messages").add( + new models.Message({ + user: argv.nick, + text: message + }) + ); + break; + case "JOIN": if (argv.nick == network.get("nick")) { channels.add( @@ -205,6 +248,17 @@ function handleEvent(argv) { "add", {}, users ); break; + + case "err_cannotsendtochan": + case "err_nosuchnick": + var channel = network.get("channels").findWhere({name: argv.args[1]}); + var messages = channel.get("messages"); + messages.add( + new models.Message({ + text: argv.args[2] + }) + ); + break; } // Debug