Refactoring

This commit is contained in:
Mattias Erming 2014-03-13 16:25:01 +01:00
parent e587a46242
commit defffe5e9b
3 changed files with 90 additions and 94 deletions

View File

@ -1,3 +1,7 @@
module.exports = { module.exports = {
host: "irc.freenode.org" host: "irc.freenode.org",
nick: "temp_user",
channels: [
"#temp_chan"
]
}; };

View File

@ -2,6 +2,9 @@ var irc = require("irc");
var Backbone = require("backbone"); var Backbone = require("backbone");
var moment = require("moment"); var moment = require("moment");
// Local library
var config = require("./../config.js");
var models = var models =
module.exports = module.exports =
{}; {};
@ -11,7 +14,7 @@ var id = 1;
models.User = Backbone.Model.extend({ models.User = Backbone.Model.extend({
defaults: { defaults: {
mode: "", mode: "",
name: "user" name: ""
} }
}); });
@ -73,12 +76,12 @@ models.ChannelCollection = Backbone.Collection.extend({
models.Network = Backbone.Model.extend({ models.Network = Backbone.Model.extend({
defaults: { defaults: {
host: "", host: "",
nick: "default_username", nick: config.nick,
connect: true connect: true
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
id: id++, id: id++
}); });
this.set("channels", new models.ChannelCollection()); this.set("channels", new models.ChannelCollection());
@ -98,7 +101,7 @@ models.Network = Backbone.Model.extend({
this.irc = new irc.Client( this.irc = new irc.Client(
this.get("host"), this.get("host"),
this.get("nick"), { this.get("nick"), {
channels: ["#test_channel"] channels: config.channels
} }
); );
this.irc.addListener( this.irc.addListener(

View File

@ -13,26 +13,27 @@ function Server() {
} }
Server.prototype.listen = function(port) { Server.prototype.listen = function(port) {
var self = this;
var http = connect() var http = connect()
.use(connect.static("client")) .use(connect.static("client"))
.listen(port); .listen(port);
this.networks.on("all", function(type, data) { this.networks.on("all", function(type, data) {
if (type == "users" || type == "messages") { if (type == "users" || type == "messages") {
self.sockets.emit(type, data); this.sockets.emit(type, data);
} else { } else {
// Network and channel events will force // Network and channel events will force
// a full refresh. // a full refresh.
self.sockets.emit( this.sockets.emit(
"networks", self.networks "networks", self.networks
); );
} }
}); }, this);
this.sockets = io this.sockets = io
.listen(http, {log: false}) .listen(http, {log: false})
.sockets; .sockets;
var self = this;
this.sockets.on("connection", function(socket) { this.sockets.on("connection", function(socket) {
socket.emit( socket.emit(
"networks", self.networks "networks", self.networks
@ -54,19 +55,34 @@ function handleInput(input) {
return; return;
} }
var network = target.network;
var channel = target.channel;
var argv = input.text.substr(1).split(" "); var argv = input.text.substr(1).split(" ");
var cmd = input.text.charAt(0) == "/" ? argv[0].toUpperCase() var cmd = input.text.charAt(0) == "/" ? argv[0].toUpperCase()
: ""; : "MESSAGE";
switch (cmd) { switch (cmd) {
case "SERVER": case "MESSAGE":
case "CONNECT": var irc = network.irc;
var network = this.networks.add( if (typeof irc !== "undefined") {
new models.Network({ irc.say(target.channel.get("name"), input.text);
host: argv[1] || config.host }
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() { network.irc.addListener("raw", function() {
handleEvent.apply(network, arguments); handleEvent.apply(network, arguments);
} }
@ -75,45 +91,42 @@ function handleInput(input) {
case "QUIT": case "QUIT":
case "DISCONNECT": case "DISCONNECT":
this.networks.remove(target.network); this.networks.remove(network);
break; break;
case "": case "JOIN":
var irc = target.network.irc; case "PART":
var irc = network.irc;
if (typeof irc !== "undefined") { if (typeof irc !== "undefined") {
var chan = target.channel.get("name"); irc.send(cmd, argv[1] || target.channel.get("name"));
irc.say(chan, input.text);
} }
target.channel.get("messages").add(
new models.Message({
user: target.network.get("nick"),
text: input.text
})
);
break; break;
case "NOTICE": case "NOTICE":
var irc = target.network.irc; var irc = network.irc;
if (argv[2] && typeof irc !== "undefined") { if (!argv[2] || typeof irc === "undefined") {
break;
}
var user = argv[1]; var user = argv[1];
var text = argv var text = argv.slice(2).join(" ");
.slice(2)
.join(" ");
irc.notice(user, text); irc.notice(user, text);
target.channel.get("messages").add( channel.get("messages").add(
new models.Message({ new models.Message({
user: target.network.get("nick"), type: "notice",
text: "to " + user + ": " + text text: "Notice to " + user + ": " + text
}) })
); );
}
break; break;
case "QUERY": case "QUERY":
var irc = target.network.irc; var irc = network.irc;
if (argv[1] && typeof irc !== "undefined") { if (!argv[1] || typeof irc === "undefined") {
var channels = target.network.get("channels"); break;
}
var channels = network.get("channels");
if (argv[1].charAt(0) != "#" && !channels.findWhere({name: argv[1]})) { if (argv[1].charAt(0) != "#" && !channels.findWhere({name: argv[1]})) {
channels.add( channels.add(
new models.Channel({ new models.Channel({
@ -121,58 +134,42 @@ function handleInput(input) {
}) })
); );
} }
}
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);
}
break;
case "NICK":
var irc = target.network.irc;
if (argv[1] && typeof irc !== "undefined") {
irc.send("NICK", argv[1]);
}
break; break;
case "TOPIC": case "TOPIC":
var irc = target.network.irc; var irc = network.irc;
if (typeof irc !== "undefined") { if (typeof irc === "undefined") {
if (!argv[1] || argv[1].charAt(0) != "#") { break;
argv.splice(1, 0, target.channel.get("name")); }
var params = [
"topic",
channel.get("name")
];
if (argv[1]) {
var text = argv.slice(1).join(" ");
params.push(text);
} }
var topic = argv.slice(2).join(" ");
irc.send.apply( irc.send.apply(
irc, irc,
argv.length == 2 ? argv params
: argv.slice(0, 2).concat(topic)
); );
break;
case "NICK":
var irc = network.irc;
if (typeof irc !== "undefined") {
irc.send(cmd, argv[1] || config.nick);
} }
break; break;
default: default:
target.channel.get("messages").add( channel.get("messages").add(
new models.Message({text: "Command `/" + cmd + "` does not exist."}) new models.Message({
text: "Unknown command: `/" + cmd + "`"
})
); );
break;
} }
} }
@ -343,14 +340,6 @@ function handleEvent(argv) {
); );
break; break;
//default:
// channels.first().get("messages").add(
// new models.Message({
// user: "-!-",
// text: argv.args.slice(1).join(" ")
// })
// );
} }
// Debug // Debug