diff --git a/config.js b/config.js index 7010c07d..55d222f7 100644 --- a/config.js +++ b/config.js @@ -1,3 +1,7 @@ module.exports = { - host: "irc.freenode.org" + host: "irc.freenode.org", + nick: "temp_user", + channels: [ + "#temp_chan" + ] }; \ No newline at end of file diff --git a/lib/models.js b/lib/models.js index 65a022d2..457f5b7f 100644 --- a/lib/models.js +++ b/lib/models.js @@ -2,6 +2,9 @@ var irc = require("irc"); var Backbone = require("backbone"); var moment = require("moment"); +// Local library +var config = require("./../config.js"); + var models = module.exports = {}; @@ -11,7 +14,7 @@ var id = 1; models.User = Backbone.Model.extend({ defaults: { mode: "", - name: "user" + name: "" } }); @@ -73,12 +76,12 @@ models.ChannelCollection = Backbone.Collection.extend({ models.Network = Backbone.Model.extend({ defaults: { host: "", - nick: "default_username", + nick: config.nick, connect: true }, initialize: function() { this.set({ - id: id++, + id: id++ }); this.set("channels", new models.ChannelCollection()); @@ -98,7 +101,7 @@ models.Network = Backbone.Model.extend({ this.irc = new irc.Client( this.get("host"), this.get("nick"), { - channels: ["#test_channel"] + channels: config.channels } ); this.irc.addListener( diff --git a/lib/server.js b/lib/server.js index 06e2625a..6048a96c 100644 --- a/lib/server.js +++ b/lib/server.js @@ -13,26 +13,27 @@ function Server() { } 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 (type == "users" || type == "messages") { - self.sockets.emit(type, data); + this.sockets.emit(type, data); } else { // Network and channel events will force // a full refresh. - self.sockets.emit( + this.sockets.emit( "networks", self.networks ); } - }); + }, this); this.sockets = io .listen(http, {log: false}) .sockets; + + var self = this; this.sockets.on("connection", function(socket) { socket.emit( "networks", self.networks @@ -54,19 +55,34 @@ function handleInput(input) { return; } + var network = target.network; + var channel = target.channel; + var argv = input.text.substr(1).split(" "); var cmd = input.text.charAt(0) == "/" ? argv[0].toUpperCase() - : ""; + : "MESSAGE"; switch (cmd) { - case "SERVER": - case "CONNECT": - var network = this.networks.add( - new models.Network({ - host: argv[1] || config.host + case "MESSAGE": + var irc = network.irc; + if (typeof irc !== "undefined") { + irc.say(target.channel.get("name"), input.text); + } + channel.get("messages").add( + new models.Message({ + user: network.get("nick"), + text: input.text }) ); + break; + + case "SERVER": + case "CONNECT": + var network = new models.Network({ + host: argv[1] || config.host + }); + this.networks.add(network); network.irc.addListener("raw", function() { handleEvent.apply(network, arguments); } @@ -75,104 +91,85 @@ function handleInput(input) { case "QUIT": case "DISCONNECT": - this.networks.remove(target.network); + this.networks.remove(network); break; - case "": - var irc = target.network.irc; + case "JOIN": + case "PART": + var irc = network.irc; if (typeof irc !== "undefined") { - var chan = target.channel.get("name"); - irc.say(chan, input.text); + irc.send(cmd, argv[1] || target.channel.get("name")); } - target.channel.get("messages").add( + break; + + case "NOTICE": + var irc = network.irc; + if (!argv[2] || typeof irc === "undefined") { + break; + } + + var user = argv[1]; + var text = argv.slice(2).join(" "); + + irc.notice(user, text); + channel.get("messages").add( new models.Message({ - user: target.network.get("nick"), - text: input.text + type: "notice", + text: "Notice to " + user + ": " + text }) ); break; - case "NOTICE": - var irc = target.network.irc; - if (argv[2] && typeof irc !== "undefined") { - var user = argv[1]; - var text = argv - .slice(2) - .join(" "); + case "QUERY": + var irc = network.irc; + if (!argv[1] || typeof irc === "undefined") { + break; + } - irc.notice(user, text); - target.channel.get("messages").add( - new models.Message({ - user: target.network.get("nick"), - text: "to " + user + ": " + text + var channels = network.get("channels"); + if (argv[1].charAt(0) != "#" && !channels.findWhere({name: argv[1]})) { + channels.add( + new models.Channel({ + name: argv[1] }) ); } 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] - }) - ); - } + case "TOPIC": + var irc = network.irc; + if (typeof irc === "undefined") { + break; } - break; - case "JOIN": - var irc = target.network.irc; - if (argv[1] && typeof irc !== "undefined") { - irc.join(argv[1]); - } - break; - - case "PART": - var irc = target.network.irc; - if (typeof irc !== "undefined") { - irc.part(argv[1] ? argv[1] : target.channel.get("name")); - } - break; - - case "NAMES": - var irc = target.network.irc; - if (typeof irc !== "undefined") { - var channel = argv[1] ? argv[1] : target.channel.get("name"); - irc.send("NAMES", channel); + var params = [ + "topic", + channel.get("name") + ]; + if (argv[1]) { + var text = argv.slice(1).join(" "); + params.push(text); } + irc.send.apply( + irc, + params + ); break; case "NICK": - var irc = target.network.irc; - if (argv[1] && typeof irc !== "undefined") { - irc.send("NICK", argv[1]); - } - break; - - case "TOPIC": - var irc = target.network.irc; + var irc = network.irc; if (typeof irc !== "undefined") { - if (!argv[1] || argv[1].charAt(0) != "#") { - argv.splice(1, 0, target.channel.get("name")); - } - var topic = argv.slice(2).join(" "); - irc.send.apply( - irc, - argv.length == 2 ? argv - : argv.slice(0, 2).concat(topic) - ); + irc.send(cmd, argv[1] || config.nick); } break; default: - target.channel.get("messages").add( - new models.Message({text: "Command `/" + cmd + "` does not exist."}) + channel.get("messages").add( + new models.Message({ + text: "Unknown command: `/" + cmd + "`" + }) ); - break; + } } @@ -343,14 +340,6 @@ function handleEvent(argv) { ); break; - //default: - // channels.first().get("messages").add( - // new models.Message({ - // user: "-!-", - // text: argv.args.slice(1).join(" ") - // }) - // ); - } // Debug