From d06964b6a5bcc65583e0202cab30901e3b2e0401 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Wed, 24 Jun 2015 02:14:53 +0200 Subject: [PATCH] Parse chat info and invite via chat id (more secure) --- scripts/telegram.coffee | 65 +++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 25 deletions(-) diff --git a/scripts/telegram.coffee b/scripts/telegram.coffee index 6d1dee8..012951b 100644 --- a/scripts/telegram.coffee +++ b/scripts/telegram.coffee @@ -33,6 +33,20 @@ module.exports = (robot) -> callback (reply.split '\n')[1..-3] client.end() + # return an object with the known chat information + chat_info = (name, callback) -> + run_command "chat_info #{norm name}", (data) -> + match = data[0].match /// + Chat\ (\w+(\ ?\w+)*) # chat name + \ \(id\ (\d+)\) # chat id + /// + chat = + name: match[1] + id: 'chat#'+match[3] + members: data[1..].map (i) -> (i.split ' invited')[0].replace /\t/g, '' + robot.logger.info 'parsed chat data: ' + JSON.stringify chat, null, 2 + callback chat + # return an object with the known user information user_info = (name, callback) -> run_command "user_info #{norm name}", (data) -> @@ -120,29 +134,30 @@ module.exports = (robot) -> denied = ['BZBZ ADMIN-NOT-DETECTED', 'BZBZ IS-NOT-AUTHORIZED', 'BZBZ ACCESS-DENIED'] nope = ['ahahah NO', 'mai sentito questo?', 'invita anche me magari'] ok = ['provvedo subito', 'ok', 'certo', 'va bene'] - done = ['fatto', 'ecco qui', 'ecco'] - group_list (list) -> - chat = res.match[4] - user = res.message.user.name - if not chat in list - res.send res.random nope - return - if chat is process.env['ADMIN_ROOM_NAME'] - res.send res.random denied - return - res.send res.random ok - - norm = (x) -> x.replace /\ /g, '_' + done = ['fatto', 'ecco qui', 'ecco fatto'] - run_command "chat_add_user #{norm chat} #{norm user}", (reply) -> - robot.logger.info "add user #{user} to chat #{chat}" - robot.logger.info 'result: ' + reply[0] - err = (reply[0].split ' ')[-1..][0] - if err is 'SUCCESS' - res.send res.random done - else if err is 'USER_NOT_MUTUAL_CONTACT' - res.send 'aah, non posso: ho bisogno del tuo contatto' - else if err is 'USER_ALREADY_PARTICIPANT' - res.send 'ma sei già nel gruppo... [facepalm]' - else - res.send "non sono riuscito, c'è un errore: #{reply[0]}" + user = res.message.user + name = res.match[4] + admin_id = process.env['ADMIN_ROOM'].replace ':', '#' + + chat_list (list) -> + if not name in list + return res.send res.random nope + chat_info name, (chat) -> + if chat.id is admin_id + return res.send res.random denied + if user.name in chat.members + return res.send 'ma sei già nel gruppo... [facepalm]' + res.send res.random ok + + run_command "chat_add_user #{chat.id} #{user.id}", (reply) -> + robot.logger.info "add user #{user.name} to chat #{chat.name}" + robot.logger.info 'result: ' + reply[0] + err = (reply[0].split ' ')[-1..][0] + if err is 'SUCCESS' + robot.messageRoom chat.id, res.random done + robot.messageRoom chat.id, 'benvento' + else if err is 'USER_NOT_MUTUAL_CONTACT' + res.send 'aah, non posso: ho bisogno del tuo contatto' + else + res.send "non sono riuscito, c'è un errore: #{reply[0]}"