Skip to content

Commit

Permalink
Add support for lists of words to be banned from being used in userna…
Browse files Browse the repository at this point in the history
…mes (NOT chat).

& and higher can change the list with /banword and /unbanword (or /bw and /ubw respectively). The words/phrases are turned into IDs (only lowercase alphanumeric characters allowed) and checked against the ID form of a given username.

Keep in mind the list has no sense of context, and so overzealous banning, particularly of short or otherwise common words, should be avoided. Or else user "Cofagrigus" will want to have a word with you.
  • Loading branch information
bmelts committed Jul 2, 2012
1 parent 9edc789 commit 64d2109
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/logs/*
/node_modules
npm-debug.log
/config/bannedwords.txt

# boilerplate #
###############
Expand Down
30 changes: 30 additions & 0 deletions chat-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,36 @@ function parseCommandLocal(user, cmd, target, room, socket, message) {
});
return false;
break;
case 'banword':
case 'bw':
if (!user.can('announce')) {
socket.emit('console', '/banword - Access denied.');
return false;
}
target = toId(target);
if (!target) {
socket.emit('console', 'Specify a word or phrase to ban.');
return false;
}
Users.addBannedWord(target);
socket.emit('console', 'Added \"'+target+'\" to the list of banned words.');
return false;
break;
case 'unbanword':
case 'ubw':
if (!user.can('announce')) {
socket.emit('console', '/unbanword - Access denied.');
return false;
}
target = toId(target);
if (!target) {
socket.emit('console', 'Specify a word or phrase to unban.');
return false;
}
Users.removeBannedWord(target);
socket.emit('console', 'Removed \"'+target+'\" from the list of banned words.');
return false;
break;
case 'help':
case 'commands':
case 'h':
Expand Down
40 changes: 38 additions & 2 deletions users.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@ function exportUsergroups() {
}
importUsergroups();

var bannedWords = {};
function importBannedWords() {
fs.readFile('config/bannedwords.txt', function(err, data) {
if (err) return;
data = (''+data).split("\n");
bannedWords = {};
for (var i = 0; i < data.length; i++) {
if (!data[i]) continue;
bannedWords[data[i]] = true;
}
});
}
function exportBannedWords() {
fs.writeFile('config/bannedwords.txt', Object.keys(bannedWords).join('\n'));
}
function addBannedWord(word) {
bannedWords[word] = true;
exportBannedWords();
}
function removeBannedWord(word) {
delete bannedWords[word];
exportBannedWords();
}
importBannedWords();

// User
var User = (function () {
function User(name, person, token) {
Expand Down Expand Up @@ -332,8 +357,16 @@ var User = (function () {
// before it gets to this stage it's your own fault
this.emit('nameTaken', {userid: '', reason: "You did not specify a name."});
return false;
} else if (userid === this.userid && !auth) {
return this.forceRename(name, this.authenticated);
} else {
for (var w in bannedWords) {
if (userid.indexOf(w) >= 0) {
this.emit('nameTaken', {userid: '', reason: "That name contains a banned word or phrase."});
return false;
}
}
if (userid === this.userid && !auth) {
return this.forceRename(name, this.authenticated);
}
}
if (users[userid] && !users[userid].authenticated && users[userid].connected && !auth) {
this.emit('nameTaken', {userid:this.userid, token:token, reason: "Someone is already using the name \""+users[userid].name+"\"."});
Expand Down Expand Up @@ -899,3 +932,6 @@ exports.connectUser = connectUser;
exports.users = users;
exports.prevUsers = prevUsers;
exports.importUsergroups = importUsergroups;
exports.addBannedWord = addBannedWord;
exports.removeBannedWord = removeBannedWord;

0 comments on commit 64d2109

Please sign in to comment.