Added option to auto connect

This commit is contained in:
Mattias Erming 2014-03-13 18:50:02 +01:00
parent 87271193e2
commit 0d880802a9
3 changed files with 52 additions and 34 deletions

View File

@ -1,7 +1,9 @@
module.exports = { module.exports = {
nick: "temp_name",
fullname: "Temporary Name",
autoConnect: true,
host: "irc.freenode.org", host: "irc.freenode.org",
nick: "temp_user",
channels: [ channels: [
"#temp_chan" "#temp_chan"
] ],
}; };

View File

@ -77,8 +77,7 @@ models.ChannelCollection = Backbone.Collection.extend({
models.Network = Backbone.Model.extend({ models.Network = Backbone.Model.extend({
defaults: { defaults: {
host: "", host: "",
nick: config.nick, nick: config.nick
connect: true
}, },
initialize: function() { initialize: function() {
this.set({ this.set({
@ -97,26 +96,28 @@ models.Network = Backbone.Model.extend({
type: "network", type: "network",
name: this.get("host") name: this.get("host")
})); }));
},
connect: function(channels) {
var client = new irc.Client(
this.get("host"),
this.get("nick"), {
fullname: config.fullname,
channels: channels
}
);
if (this.get("connect")) { this.irc = client;
this.irc = new irc.Client( this.irc.addListener("error", function() {
this.get("host"), // ..
this.get("nick"), { });
channels: config.channels
}
);
this.irc.addListener(
"error", function() {
// ..
}
);
}
this.on("remove", function() { this.on("remove", function() {
if (typeof this.irc !== "undefined") { if (typeof this.irc !== "undefined") {
this.irc.disconnect(); this.irc.disconnect();
} }
}); });
return this.irc;
} }
}); });

View File

@ -13,6 +13,7 @@ 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);
@ -29,11 +30,7 @@ Server.prototype.listen = function(port) {
} }
}, this); }, this);
this.sockets = io this.sockets = io.listen(http, {log: false}).sockets;
.listen(http, {log: false})
.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
@ -46,9 +43,23 @@ Server.prototype.listen = function(port) {
); );
}); });
if (config.autoConnect) {
this.connect(config.host, config.channels);
}
return this; return this;
}; };
Server.prototype.connect = function(host, channels) {
var network = new models.Network({
host: host
});
this.networks.add(network);
network.connect(channels).addListener("raw", function() {
handleEvent.apply(network, arguments);
});
};
function handleInput(input) { function handleInput(input) {
var target = this.networks.find(input.id); var target = this.networks.find(input.id);
if (!target) { if (!target) {
@ -79,14 +90,7 @@ function handleInput(input) {
case "SERVER": case "SERVER":
case "CONNECT": case "CONNECT":
var network = new models.Network({ this.connect(argv[1]);
host: argv[1] || config.host
});
this.networks.add(network);
network.irc.addListener("raw", function() {
handleEvent.apply(network, arguments);
}
);
break; break;
case "QUIT": case "QUIT":
@ -183,6 +187,15 @@ function handleEvent(argv) {
switch (event) { switch (event) {
case "ERROR":
channels.first().get("messages").add(
new models.Message({
text: argv.args.slice(2).join(" "),
type: "error"
})
);
break;
case "PRIVMSG": case "PRIVMSG":
var target = argv.args[0]; var target = argv.args[0];
if (target.charAt(0) != "#") { if (target.charAt(0) != "#") {
@ -209,7 +222,7 @@ function handleEvent(argv) {
break; break;
case "NOTICE": case "NOTICE":
var from = argv.nick ? argv.nick : argv.prefix; var from = argv.nick ? argv.nick : "-!-";
var message = new models.Message({ var message = new models.Message({
user: from, user: from,
text: "notice: " + argv.args[1], text: "notice: " + argv.args[1],
@ -336,11 +349,13 @@ function handleEvent(argv) {
); );
break; break;
case "ERROR": case "rpl_motdstart":
case "rpl_endofmotd":
case "rpl_motd":
channels.first().get("messages").add( channels.first().get("messages").add(
new models.Message({ new models.Message({
text: argv.args.slice(2).join(" "), user: "-!-",
type: "error" text: argv.args[1]
}) })
); );
break; break;