asjon/scripts/blacklist.coffee

92 lines
2.9 KiB
CoffeeScript
Raw Normal View History

2016-04-14 21:27:05 +02:00
# 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) ->
2016-04-14 21:41:16 +02:00
restored = ['ok, lo riabilito', 'va bene, smetto di ignorarlo',
'daccordo, lo ascolto', 'si, adesso la smetto']
ignored = ['ok, farò finta di non sentire', 'si, non gli rispondo più',
2016-04-14 21:27:05 +02:00
'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']
2017-02-22 22:11:20 +01:00
noPermit = ['BZBZ 403-NOT-AUTHORIZED', 'BZBZ DOES-NOT-COMPUTE',
2016-04-14 21:27:05 +02:00
'BZBZ ADMIN-NOT-DETECTED', 'BZBZ IS-NOT-AUTHORIZED',
'BZBZ ACCESS-DENIED']
isFromAdmin = (res) ->
res.robot.adapterName is 'shell' or
2017-02-22 22:11:20 +01:00
res.message.room.id is process.env.ADMIN_ROOM
2016-04-14 21:27:05 +02:00
# 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"
2016-04-14 21:41:16 +02:00
res.send res.random ignored
2016-04-14 21:27:05 +02:00
else
robot.logger.info "no known user matches #{name}"
res.send res.random notFound
2017-02-22 22:11:20 +01:00
2016-04-14 21:27:05 +02:00
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
2016-04-14 21:41:16 +02:00
list.splice index, 1
2016-04-14 21:27:05 +02:00
robot.brain.set 'blacklist', list
robot.logger.info "user #{user.name} restored"
2016-04-14 21:41:16 +02:00
res.send res.random restored
2016-04-14 21:27:05 +02:00
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