From d5ffbaada46d5ab132bcf8a20690e2ed7c318c0e Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Thu, 14 Apr 2016 21:27:05 +0200 Subject: [PATCH] Implement user blacklist --- scripts/blacklist.coffee | 88 ++++++++++++++++++++++++++++++++++++++++ scripts/meta.coffee | 14 +++++-- 2 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 scripts/blacklist.coffee diff --git a/scripts/blacklist.coffee b/scripts/blacklist.coffee new file mode 100644 index 0000000..fa291bf --- /dev/null +++ b/scripts/blacklist.coffee @@ -0,0 +1,88 @@ +# Description: +# user blacklist +# +# Requires: +# "fast-levenshtein": "1.0.6" +# +# Commands: +# asjon ignora - ignora i messaggi inviati da un utente +# asjon ascolta - riabilita un utente ignorato +# +# Author: +# Michele Guerini Rocco (rnhmjoj) + +lev = require 'fast-levenshtein' + +module.exports = (robot) -> + success = ['ok, farò finta di non sentire', 'si, non gli rispondo più', + 'va bene, da ora in poi lo ignoro', 'ok, lo ignoro'] + prevent = ['sei proprio sicuro?', 'non credo sia una buona idea', + 'ne sei certo?', 'direi che è meglio di no'] + notFound = ['non so chi sia', 'sicuro? non lo trovo', 'boh, chi è?', + 'mai sentito, hai scritto giusto?', 'boh, non lo trovo'] + noPermit = ['BZBZ 403-NOT-AUTHORIZED', 'BZBZ DOES-NOT-COMPUTE', + 'BZBZ ADMIN-NOT-DETECTED', 'BZBZ IS-NOT-AUTHORIZED', + 'BZBZ ACCESS-DENIED'] + + isFromAdmin = (res) -> + res.robot.adapterName is 'shell' or + res.message.room is process.env.ADMIN_ROOM + + + # find closest match with levenshtein metric + find_closest = (target, list) -> + dists = [].concat.apply [], ( + for str in list + for word in [str].concat str.split ' ' + [(lev.get target, word), str] ) + [dist, name] = dists.reduce (x, y) -> + if x[0] < y[0] then x else y + return if dist < 4 then name else null + + + robot.respond /ignora (.+)/i, (res) -> + if not isFromAdmin res + return res.send res.random noPermit + + name = res.match[1] + users = (v for k, v of robot.brain.users()) + match = find_closest name, (i.name for i in users) + + if match? + user = robot.brain.userForName match + list = robot.brain.get 'blacklist' + + if user.name is res.message.user.name + return res.send res.random prevent + + robot.brain.set 'blacklist', (list or []).concat [user.id] + robot.logger.info "user #{user.name} is now blacklisted" + res.send res.random success + else + robot.logger.info "no known user matches #{name}" + res.send res.random notFound + + + robot.respond /ascolta (.+)/i, (res) -> + if not isFromAdmin res + return res.send res.random noPermit + + name = res.match[1] + users = (v for k, v of robot.brain.users()) + match = find_closest name, (i.name for i in users) + + if match? + user = robot.brain.userForName match + list = (robot.brain.get 'blacklist') or [] + + index = list.indexOf user.id + if index > 0 + robot.brain.set 'blacklist', list + robot.logger.info "user #{user.name} restored" + res.send res.random success + else + robot.logger.info "user #{user.name} was not blacklisted" + res.send 'non era ignorato' + else + robot.logger.info "no known user matches #{name}" + res.send res.random notFound diff --git a/scripts/meta.coffee b/scripts/meta.coffee index 3311db1..5fdca51 100644 --- a/scripts/meta.coffee +++ b/scripts/meta.coffee @@ -13,8 +13,6 @@ { CatchAllMessage } = require 'hubot' module.exports = (robot) -> - - #direct talk in user chat receive = (message) -> listeners = -> res = [] @@ -27,12 +25,20 @@ module.exports = (robot) -> false res + # ignore blacklisted users + if message.user.id in (robot.brain.get('blacklist') or []) + robot.logger.info 'ignored message from user ' + message.user.name + return + results = listeners() - unless message.done or (results.reduce (x, y) -> x or y) + + # allow direct talk in user chat or campfire + unless message.done or (true in results) if message.room == message.user.id or message.user.id is 1 message.text = robot.name + ' ' + message.text results.push listeners()... - if message not instanceof CatchAllMessage and (results.indexOf true) is -1 + + if message not instanceof CatchAllMessage and (true not in results) robot.receive new CatchAllMessage message robot.receive = receive