Skip to content
This repository has been archived by the owner on Apr 1, 2018. It is now read-only.

unban command, ban duration, default value for ban duration in config #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
"host": "0.0.0.0",
"port": "6060",
"x_forwarded_for": false,
"salt": "insert-randomly-generated-string-here"
"salt": "insert-randomly-generated-string-here",
"defaultBanDuration": "1h"
}
57 changes: 51 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function loadConfig(filename) {
try {
var data = fs.readFileSync(filename, 'utf8')
config = JSON.parse(data)
config.defaultBanDuration = durationToMilliseconds(config.defaultBanDuration)
console.log("Loaded config '" + filename + "'")
}
catch (e) {
Expand Down Expand Up @@ -115,6 +116,30 @@ function hash(password) {
return sha.digest('base64').substr(0, 6)
}

function durationToMilliseconds(duration) {
if (duration == 'permanent') {
return Infinity
}

var durationValue = parseInt(duration)
var durationUnit = duration[duration.length-1]

switch (durationUnit) {
case '': // default: hours
case 'h': // fallthrough
durationValue *= 60
case 'm': // fallthrough
durationValue *= 60
case 's': // fallthrough
durationValue *= 1000
break
default:
durationValue = null // postfix is not known
}

return durationValue
}

function isAdmin(client) {
return client.nick == config.admin
}
Expand Down Expand Up @@ -280,6 +305,16 @@ var COMMANDS = {

// Moderator-only commands below this point

unban: function(args) {
if (!isMod(this)) {
return
}

var ip = String(args.ip)
POLICE.free(ip)
console.log(this.nick + " [" + this.trip + "] unbanned ip " + ip)
},

ban: function(args) {
if (!isMod(this)) {
return
Expand All @@ -304,7 +339,7 @@ var COMMANDS = {
return
}

POLICE.arrest(getAddress(badClient))
POLICE.arrest(getAddress(badClient), durationToMilliseconds(String(args.duration)))
console.log(this.nick + " [" + this.trip + "] banned " + nick + " in " + this.channel)
broadcast({cmd: 'info', text: "Banned " + nick}, this.channel)
},
Expand Down Expand Up @@ -380,8 +415,12 @@ var POLICE = {

frisk: function(id, deltaScore) {
var record = this.search(id)
if (record.arrested) {
return true
if (record.arrestedUntil) {
if (Date.now() > record.arrestedUntil) {
delete record.arrestedUntil
} else {
return true
}
}

record.score *= Math.pow(2, -(Date.now() - record.time)/POLICE.halflife)
Expand All @@ -393,11 +432,17 @@ var POLICE = {
return false
},

arrest: function(id) {
arrest: function(id, duration) {
var record = this.search(id)
if (record) {
record.arrested = true
if (!duration) {
duration = config.defaultBanDuration || 60 * 60 * 1000 // fallback: 1 hour
}
record.arrestedUntil = Date.now() + duration
},

free: function(id) {
var record = this.search(id)
delete record.arrestedUntil
},
}

Expand Down