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 = {
nick: "temp_name",
fullname: "Temporary Name",
autoConnect: true,
host: "irc.freenode.org",
nick: "temp_user",
channels: [
"#temp_chan"
]
],
};

View File

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

View File

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