Implement user blacklist

This commit is contained in:
rnhmjoj 2016-04-14 21:27:05 +02:00
parent e81249c79f
commit d5ffbaada4
2 changed files with 98 additions and 4 deletions

88
scripts/blacklist.coffee Normal file
View File

@ -0,0 +1,88 @@
# Description:
# user blacklist
#
# Requires:
# "fast-levenshtein": "1.0.6"
#
# Commands:
# asjon ignora <utente> - ignora i messaggi inviati da un utente
# asjon ascolta <utente> - 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

View File

@ -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