Add command to ban user of latest message

This commit is contained in:
rnhmjoj 2015-10-31 02:40:35 +01:00
parent 3a943afbc1
commit 6e9a0b2e37

View File

@ -25,6 +25,17 @@ module.exports = (robot) ->
# format a name as telegram-cli likes it
norm = (x) -> x.replace /\ /g, '_'
# return a list of every match for a regex
match_all = (regex, str) ->
matches = []
str.replace regex, ->
arr = [].slice.call arguments, 0
extras = arr.splice -2
arr.index = extras[0]
arr.input = extras[1]
matches.push arr
matches
# directly run a command in telegram-cli and return its output
# (a list of strings)
run_command = (command, callback) ->
@ -107,6 +118,17 @@ module.exports = (robot) ->
return callback reply[1..]
callback()
# get the messages history for a chat group
get_history = (chat, size, callback) ->
regex = /\[(.+)\] (.+) [>«»]+ ((?:(?!\n(\[|\d))[\s\S])+)/g
parse_line = (x) ->
date: x[1]
peer: x[2].replace(chat, '').trim()
text: x[3].trim().replace '\n', ' '
run_command "history #{norm chat} #{size}", (lines) ->
callback ((match_all regex, lines.join '\n').map parse_line)
robot.respond /chi sei/, (res) ->
robot_info (user) ->
username = if user.username? then " noto anche come #{user.username}" else ''
@ -133,7 +155,7 @@ module.exports = (robot) ->
else
res.send "fatto!"
robot.respond /banna (.+)/, (res) ->
robot.respond /banna(lo| (.+))/, (res) ->
ok = [', ora non sarai più un problema', ' terminato',
' non farti più vedere', ', questa è la tua fine'
unk = ['non so chi sia', 'chi?', 'mai sentito', 'sicuro? non lo trovo']
@ -141,36 +163,57 @@ module.exports = (robot) ->
nope = ['eh, mi piacerebbe molto ma non posso', 'i have no powers here',
'nope, non l\'invitato io', 'non sono admin qui, sorry']
resolve_name = (name, chat, history) ->
if name?
# find the closest match
dists = [].concat.apply [], (
for user in chat.members
for word in [user].concat user.split ' '
[(lev.get name, word), user] )
[dist, name] = dists.reduce (x, y) ->
if x[0] < y[0] then x else y
return if dist < 4 then name else null
else
# use the latest message peer
return history[0].peer
delete_user = (user, chat) ->
run_command "chat_del_user #{chat.id} #{user.id}", (reply) ->
err = (reply[0].split ' ')[-1..][0]
robot.logger.info "delete user #{user.name} from chat #{chat.name}"
if err is 'SUCCESS'
robot.logger.info 'user deleted'
res.send user.name + res.random ok
else if err is 'CHAT_ADMIN_REQUIRED'
res.send res.random nope
robot.logger.warning 'cannot delete user: not authorized'
else
robot.logger.error 'error:\n' + reply.join '\n'
res.send 'oh no: c\'è stato un errore, non so'
if res.message.room.match /^user/
return res.send 'non è un gruppo questo'
chat_info res.message.room, (chat) ->
dists = [].concat.apply [], (
for user in chat.members
for word in [user].concat user.split ' '
[(lev.get res.match[1], word), user])
[dist, name] = dists.reduce (x, y) -> if x[0] < y[0] then x else y
get_history chat.name, 2, (history) ->
robot_info (self) ->
name = resolve_name(res.match[2], chat, history)
if dist < 4
user_info name, (user) ->
run_command "chat_del_user #{chat.id} #{user.id}", (reply) ->
err = (reply[0].split ' ')[-1..][0]
robot.logger.info "delete user #{user.name} from chat #{chat.name}"
if not name?
return res.send res.random unk
if name in [res.message.user.name, self.name]
return res.send 'ma sei scemo o cosa?'
if err is 'SUCCESS'
robot.logger.info 'user deleted'
res.send user.name + res.random ok
else if err is 'CHAT_ADMIN_REQUIRED'
res.send res.random nope
robot.logger.warning 'cannot delete user: not authorized'
else
robot.logger.error 'error:\n' + reply.join '\n'
res.send 'oh no: c\'è stato un errore, non so'
else
res.send res.random unk
user_info name, (user) ->
delete_user user, chat
robot.respond /(mi )?invit(i|ami) (in|nel gruppo) ([^?]+)\??/, (res) ->
denied = ['BZBZ ADMIN-NOT-DETECTED', 'BZBZ IS-NOT-AUTHORIZED', 'BZBZ ACCESS-DENIED']
nope = ['ahahah NO', 'mai sentito questo', 'invita anche me magari', 'che?']
denied = ['BZBZ ADMIN-NOT-DETECTED', 'BZBZ IS-NOT-AUTHORIZED',
'BZBZ ACCESS-DENIED']
nope = ['ahahah NO', 'mai sentito questo', 'invita anche me magari',
'che?']
ok = ['provvedo subito', 'ok', 'certo', 'va bene']
done = ['fatto', 'ecco qui', 'ecco fatto']