diff --git a/.gitignore b/.gitignore index d60709477266..b2618534687c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ /logs/* /node_modules npm-debug.log +/config/bannedwords.txt # boilerplate # ############### diff --git a/chat-commands.js b/chat-commands.js index 55407fb4eff9..b07bb15823d0 100644 --- a/chat-commands.js +++ b/chat-commands.js @@ -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': diff --git a/users.js b/users.js index c8dba787f3fb..02894aa3e26d 100644 --- a/users.js +++ b/users.js @@ -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) { @@ -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+"\"."}); @@ -899,3 +932,6 @@ exports.connectUser = connectUser; exports.users = users; exports.prevUsers = prevUsers; exports.importUsergroups = importUsergroups; +exports.addBannedWord = addBannedWord; +exports.removeBannedWord = removeBannedWord; +