diff --git a/defaults/config.js b/defaults/config.js index 5481a5cf..47387f66 100644 --- a/defaults/config.js +++ b/defaults/config.js @@ -309,5 +309,15 @@ module.exports = { // @default 113 // port: 113 - } + }, + + // + // Enable oidentd support using the specified file + // + // Example: oidentd: "~/.oidentd.conf", + // + // @type string + // @default null + // + oidentd: null, }; diff --git a/src/clientManager.js b/src/clientManager.js index 33db9474..420d4bf2 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -3,11 +3,18 @@ var fs = require("fs"); var Client = require("./client"); var mkdirp = require("mkdirp"); var Helper = require("./helper"); +var oidentd = require("./oidentd"); module.exports = ClientManager; function ClientManager() { + var config = Helper.getConfig(); + this.clients = []; + + if (typeof config.oidentd === "string") { + this.identHandler = new oidentd(config.oidentd); + } } ClientManager.prototype.findClient = function(name) { diff --git a/src/oidentd.js b/src/oidentd.js new file mode 100644 index 00000000..d8193677 --- /dev/null +++ b/src/oidentd.js @@ -0,0 +1,73 @@ +var fs = require("fs"); +var Helper = require("./helper"); + +function oidentdFile(file) { + this.file = Helper.expandHome(file); + this.connectionId = 0; + this.connections = {}; + + this.refresh(); +} + +oidentdFile.prototype = { + hookSocket: function(socket, user) { + var that = this; + var id = null; + + socket.on("connect", function() { + id = that.addSocket(socket, user); + that.refresh(); + }); + socket.on("close", function() { + that.removeConnection(id); + that.refresh(); + }); + }, + + addSocket: function(socket, user) { + var id = this.connectionId++; + this.connections[id] = {socket: socket, user: user}; + return id; + }, + + removeSocket: function(socket) { + for (var id in this.connections) { + if (this.connections[id] === socket) { + delete this.connections[id]; + break; + } + } + }, + + removeConnection: function(id) { + delete this.connections[id]; + }, + + getSockets: function() { + return this.connections; + }, + + refresh: function() { + var file = "# Warning: file generated by The Lounge: changes will be overwritten!\n"; + + function makeRule(connection) { + return "to " + connection.socket.remoteAddress + + " lport " + connection.socket.localPort + + " from " + connection.socket.localAddress + + " fport " + connection.socket.remotePort + + " { reply \"" + connection.user + "\" }\n"; + } + + for (var id in this.connections) { + file += makeRule(this.connections[id]); + } + + fs.writeFile(this.file, file, {flag: "w+"}, function(err) { + if (err) { + log.error("Failed to update oidentd file!", err); + } + }); + }, +}; + +module.exports = oidentdFile; diff --git a/src/plugins/irc-events/connection.js b/src/plugins/irc-events/connection.js index a4226693..c2114e12 100644 --- a/src/plugins/irc-events/connection.js +++ b/src/plugins/irc-events/connection.js @@ -4,6 +4,7 @@ var Msg = require("../../models/msg"); module.exports = function(irc, network) { var client = this; + var identHandler = this.manager.identHandler; network.channels[0].pushMessage(client, new Msg({ text: "Network created, connecting to " + network.host + ":" + network.port + "..." @@ -27,6 +28,18 @@ module.exports = function(irc, network) { }); } + if (identHandler) { + irc.on("socket connected", function() { + identHandler.addSocket(irc.connection.socket, client.name || network.username); + identHandler.refresh(); + }); + + irc.on("socket close", function() { + identHandler.removeSocket(irc.connection.socket); + identHandler.refresh(); + }); + } + irc.on("socket error", function(err) { log.debug("IRC socket error", err); network.channels[0].pushMessage(client, new Msg({ diff --git a/src/server.js b/src/server.js index 7fa3dffe..dd7e5a9a 100644 --- a/src/server.js +++ b/src/server.js @@ -10,9 +10,10 @@ var dns = require("dns"); var Helper = require("./helper"); var config = {}; -var manager = new ClientManager(); +var manager = null; module.exports = function(options) { + manager = new ClientManager(); config = Helper.getConfig(); config = _.extend(config, options);