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 = {
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 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(

View File

@ -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,45 +91,42 @@ 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(
new models.Message({
user: target.network.get("nick"),
text: input.text
})
);
break;
case "NOTICE":
var irc = target.network.irc;
if (argv[2] && typeof irc !== "undefined") {
var irc = network.irc;
if (!argv[2] || typeof irc === "undefined") {
break;
}
var user = argv[1];
var text = argv
.slice(2)
.join(" ");
var text = argv.slice(2).join(" ");
irc.notice(user, text);
target.channel.get("messages").add(
channel.get("messages").add(
new models.Message({
user: target.network.get("nick"),
text: "to " + user + ": " + text
type: "notice",
text: "Notice to " + user + ": " + text
})
);
}
break;
case "QUERY":
var irc = target.network.irc;
if (argv[1] && typeof irc !== "undefined") {
var channels = target.network.get("channels");
var irc = network.irc;
if (!argv[1] || typeof irc === "undefined") {
break;
}
var channels = network.get("channels");
if (argv[1].charAt(0) != "#" && !channels.findWhere({name: argv[1]})) {
channels.add(
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;
case "TOPIC":
var irc = target.network.irc;
if (typeof irc !== "undefined") {
if (!argv[1] || argv[1].charAt(0) != "#") {
argv.splice(1, 0, target.channel.get("name"));
var irc = network.irc;
if (typeof irc === "undefined") {
break;
}
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,
argv.length == 2 ? argv
: argv.slice(0, 2).concat(topic)
params
);
break;
case "NICK":
var irc = network.irc;
if (typeof irc !== "undefined") {
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