Added configuration file

This commit is contained in:
Mattias Erming 2014-03-13 13:44:54 +01:00
parent bc5e5ee5fc
commit e587a46242
3 changed files with 66 additions and 93 deletions

3
config.js Normal file
View File

@ -0,0 +1,3 @@
module.exports = {
host: "irc.freenode.org"
};

View File

@ -49,34 +49,20 @@ models.Channel = Backbone.Model.extend({
}); });
this.set("users", new models.UserCollection()); this.set("users", new models.UserCollection());
this.get("users").on( this.get("users").on("all", function() {
"add change remove", this.trigger("users", {
function() { target: this.get("id"),
// Bubble event data: this.get("users")
this.trigger( });
"users", { }, this);
target: this.get("id"),
data: this.get("users")
}
);
},
this
);
this.set("messages", new models.MessageCollection()); this.set("messages", new models.MessageCollection());
this.get("messages").on( this.get("messages").on("all", function() {
"add", this.trigger("messages", {
function() { target: this.get("id"),
// Bubble event data: this.get("messages").last()
this.trigger( });
"messages", { }, this);
target: this.get("id"),
data: this.get("messages").last()
}
);
},
this
);
} }
}); });
@ -96,26 +82,13 @@ models.Network = Backbone.Model.extend({
}); });
this.set("channels", new models.ChannelCollection()); this.set("channels", new models.ChannelCollection());
this.get("channels").on( this.get("channels").on("all", function(type, data) {
"all", if (type == "users" || type == "messages") {
function(type, model, collection) { this.trigger(type, data);
if ([ } else {
"users", this.trigger("channels");
"messages" }
].indexOf(type) != -1) { }, this);
this.trigger(type, model);
} else {
// Bubble event
this.trigger(
"channels", {
target: this.get("id"),
data: collection
}
);
}
},
this
);
this.get("channels").add(new models.Channel({ this.get("channels").add(new models.Channel({
type: "network", type: "network",
name: this.get("host") name: this.get("host")

View File

@ -1,7 +1,8 @@
var connect = require("connect"); var connect = require("connect");
var io = require("socket.io"); var io = require("socket.io");
// Local library // Local libraries
var config = require("./../config.js");
var models = require("./models.js"); var models = require("./models.js");
module.exports = Server; module.exports = Server;
@ -17,25 +18,24 @@ Server.prototype.listen = function(port) {
.use(connect.static("client")) .use(connect.static("client"))
.listen(port); .listen(port);
this.networks.on( this.networks.on("all", function(type, data) {
"all", if (type == "users" || type == "messages") {
function(type, data) { self.sockets.emit(type, data);
if (type != "users" && type != "messages") { } else {
type = "networks"; // Network and channel events will force
data = self.networks; // a full refresh.
} self.sockets.emit(
self.sockets "networks", self.networks
.emit(type, data); );
} }
); });
this.sockets = io this.sockets = io
.listen(http, {log: false}) .listen(http, {log: false})
.sockets; .sockets;
this.sockets.on("connection", function(socket) { this.sockets.on("connection", function(socket) {
socket.emit( socket.emit(
"networks", "networks", self.networks
self.networks
); );
socket.on( socket.on(
"input", "input",
@ -59,6 +59,25 @@ function handleInput(input) {
: ""; : "";
switch (cmd) { switch (cmd) {
case "SERVER":
case "CONNECT":
var network = this.networks.add(
new models.Network({
host: argv[1] || config.host
})
);
network.irc.addListener("raw", function() {
handleEvent.apply(network, arguments);
}
);
break;
case "QUIT":
case "DISCONNECT":
this.networks.remove(target.network);
break;
case "": case "":
var irc = target.network.irc; var irc = target.network.irc;
if (typeof irc !== "undefined") { if (typeof irc !== "undefined") {
@ -149,26 +168,6 @@ function handleInput(input) {
} }
break; break;
case "SERVER":
case "CONNECT":
if (argv[1]) {
var network = this.networks.add(
new models.Network({host: argv[1]})
);
network.irc.addListener(
"raw",
function() {
handleEvent.apply(network, arguments);
}
);
}
break;
case "QUIT":
case "DISCONNECT":
this.networks.remove(target.network);
break;
default: default:
target.channel.get("messages").add( target.channel.get("messages").add(
new models.Message({text: "Command `/" + cmd + "` does not exist."}) new models.Message({text: "Command `/" + cmd + "` does not exist."})
@ -181,7 +180,9 @@ function handleEvent(argv) {
var network = this; var network = this;
var channels = network.get("channels"); var channels = network.get("channels");
var event = argv.command; var event = argv.commandType != "error" ? argv.command
: "ERROR";
switch (event) { switch (event) {
case "PRIVMSG": case "PRIVMSG":
@ -334,12 +335,7 @@ function handleEvent(argv) {
); );
break; break;
case "err_nicknameinuse": case "ERROR":
case "err_chanoprivsneeded":
case "err_notonchannel":
case "err_nosuchchannel":
case "err_cannotsendtochan":
case "err_nosuchnick":
channels.first().get("messages").add( channels.first().get("messages").add(
new models.Message({ new models.Message({
text: argv.args.slice(2).join(" ") text: argv.args.slice(2).join(" ")
@ -347,13 +343,14 @@ function handleEvent(argv) {
); );
break; break;
default: //default:
channels.first().get("messages").add( // channels.first().get("messages").add(
new models.Message({ // new models.Message({
user: "-!-", // user: "-!-",
text: argv.args.slice(1).join(" ") // text: argv.args.slice(1).join(" ")
}) // })
); // );
} }
// Debug // Debug