Correctly remove closed sockets from oident file, remove unused functions

This commit is contained in:
Pavel Djundik 2016-11-20 15:23:35 +02:00
parent 303fab8519
commit 28056d678e
3 changed files with 20 additions and 48 deletions

View File

@ -3,13 +3,11 @@
root: true root: true
env: env:
es6: true
browser: true browser: true
mocha: true mocha: true
node: true node: true
parserOptions:
ecmaVersion: 6
rules: rules:
block-scoped-var: 2 block-scoped-var: 2
block-spacing: [2, always] block-spacing: [2, always]

View File

@ -1,68 +1,42 @@
"use strict"; "use strict";
var fs = require("fs"); const fs = require("fs");
var Helper = require("./helper"); const Helper = require("./helper");
function OidentdFile(file) { function OidentdFile(file) {
this.file = Helper.expandHome(file); this.file = Helper.expandHome(file);
this.connectionId = 0; this.connectionId = 0;
this.connections = {}; this.connections = new Map();
this.refresh(); this.refresh();
} }
OidentdFile.prototype = { 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) { addSocket: function(socket, user) {
var id = this.connectionId++; const id = this.connectionId++;
this.connections[id] = {socket: socket, user: user};
this.connections.set(id, {socket: socket, user: user});
this.refresh();
return id; return id;
}, },
removeSocket: function(socket) { removeSocket: function(id) {
for (var id in this.connections) { this.connections.delete(id);
if (this.connections[id] === socket) {
delete this.connections[id];
break;
}
}
},
removeConnection: function(id) { this.refresh();
delete this.connections[id];
},
getSockets: function() {
return this.connections;
}, },
refresh: function() { refresh: function() {
var file = "# Warning: file generated by The Lounge: changes will be overwritten!\n"; let file = "# Warning: file generated by The Lounge: changes will be overwritten!\n";
function makeRule(connection) { this.connections.forEach((connection) => {
return "to " + connection.socket.remoteAddress file += "to " + connection.socket.remoteAddress
+ " lport " + connection.socket.localPort + " lport " + connection.socket.localPort
+ " from " + connection.socket.localAddress + " from " + connection.socket.localAddress
+ " fport " + connection.socket.remotePort + " fport " + connection.socket.remotePort
+ " { reply \"" + connection.user + "\" }\n"; + " { reply \"" + connection.user + "\" }\n";
} });
for (var id in this.connections) {
file += makeRule(this.connections[id]);
}
fs.writeFile(this.file, file, {flag: "w+"}, function(err) { fs.writeFile(this.file, file, {flag: "w+"}, function(err) {
if (err) { if (err) {

View File

@ -70,14 +70,14 @@ module.exports = function(irc, network) {
} }
if (identHandler) { if (identHandler) {
let identSocketId;
irc.on("socket connected", function() { irc.on("socket connected", function() {
identHandler.addSocket(irc.connection.socket, client.name || network.username); identSocketId = identHandler.addSocket(irc.connection.socket, client.name || network.username);
identHandler.refresh();
}); });
irc.on("socket close", function() { irc.on("socket close", function() {
identHandler.removeSocket(irc.connection.socket); identHandler.removeSocket(identSocketId);
identHandler.refresh();
}); });
} }