From a23a446145e24753da3711b715bf2528cfd8b7d8 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sun, 6 Apr 2014 00:01:19 +0200 Subject: [PATCH] Added config and auto-connect --- config.js | 11 +++++++++++ index.js | 2 +- lib/server.js | 33 ++++++++++++++++++++++++++------- 3 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 config.js diff --git a/config.js b/config.js new file mode 100644 index 00000000..bdb30bbe --- /dev/null +++ b/config.js @@ -0,0 +1,11 @@ +module.exports = { + port: 9000, + defaults: { + nick: "t_user", + realname: "Temp User", + }, + servers: [{ + host: "irc.freenode.org", + channels: ["#t_chan"], + }] +}; diff --git a/index.js b/index.js index 0ba544d1..a0e9965d 100644 --- a/index.js +++ b/index.js @@ -1 +1 @@ -require("./lib/server")(); \ No newline at end of file +require("./lib/server")(); diff --git a/lib/server.js b/lib/server.js index 9f521d81..61d9b6b1 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,7 +1,8 @@ +var models = require(__dirname + "/models.js"); +var config = require(__dirname + "/../config.js"); var http = require("connect"); var io = require("socket.io"); var irc = require("slate-irc"); -var models = require(__dirname + "/models"); var net = require("net"); var _ = require("lodash"); @@ -20,7 +21,7 @@ var events = [ "quit", "topic", "welcome", - "whois" + "whois", ]; module.exports = function listen() { @@ -36,11 +37,23 @@ module.exports = function listen() { networks.on("all", function() { sockets.emit.apply(sockets, arguments); }); + + config.servers.forEach(function(s) { + connect(s); + }); }; -function connect(host) { +function connect(params) { + params = _.extend( + config.defaults, + params + ); + + var host = params.host; + var port = params.port || 6667; + var stream = net.connect({ - port: 6667, + port: port, host: host, }); @@ -56,8 +69,14 @@ function connect(host) { networks.trigger("network", networks); - client.nick("t_user"); - client.user("t_user", "temp user"); + client.nick(params.nick); + client.user(params.nick, params.realname); + + client.once("welcome", function() { + params.channels.forEach(function(c) { + client.join(c); + }); + }); events.forEach(function(e) { client.on(e, function() { @@ -106,7 +125,7 @@ function input(json) { case "server": case "connect": if (args[1]) { - connect(args[1]); + connect({host: args[1]}); } break;