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
env:
es6: true
browser: true
mocha: true
node: true
parserOptions:
ecmaVersion: 6
rules:
block-scoped-var: 2
block-spacing: [2, always]

View file

@ -1,68 +1,42 @@
"use strict";
var fs = require("fs");
var Helper = require("./helper");
const fs = require("fs");
const Helper = require("./helper");
function OidentdFile(file) {
this.file = Helper.expandHome(file);
this.connectionId = 0;
this.connections = {};
this.connections = new Map();
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};
const id = this.connectionId++;
this.connections.set(id, {socket: socket, user: user});
this.refresh();
return id;
},
removeSocket: function(socket) {
for (var id in this.connections) {
if (this.connections[id] === socket) {
delete this.connections[id];
break;
}
}
},
removeSocket: function(id) {
this.connections.delete(id);
removeConnection: function(id) {
delete this.connections[id];
},
getSockets: function() {
return this.connections;
this.refresh();
},
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) {
return "to " + connection.socket.remoteAddress
this.connections.forEach((connection) => {
file += "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) {

View file

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