def do_unpin_filecomment(channel, filecomment, token=bottoken): url = ('https://slack.com/api/pins.remove?token=' + token + '&channel=' + channel + '&file_comment=' + filecomment) posted, retries = False, 0 while posted == False and retries < 1: result = create_request(url) posted, retries = result['ok'], retries + 1 if result['ok'] == False: do_send_botmessage( 'WARN:do_pin_filecomment, ' + str(result['error']), botroomid)
def do_kick_user(channel, user, token=admintoken): '''empties the channel fromt the user''' url = ('https://slack.com/api/channels.kick?token=' + token + '&channel=' + channel + '&user='******'ok'], retries + 1 if result['ok'] == False: do_send_botmessage( 'WARN:do_kick_user error, ' + str(result['error']), botroomid)
def do_set_purpose(channel, purpose, token=admintoken): purpose = urllib.parse.quote_plus(purpose) url = ('https://slack.com/api/channels.setPurpose?token=' + token + '&channel=' + channel + '&purpose=' + purpose) posted, retries = False, 0 while posted == False and retries < 2: result = create_request(url) posted, retries = result['ok'], retries + 1 if result['ok'] == False: do_send_botmessage( 'WARN:set_purpose error, ' + str(result['error']), botroomid)
def do_set_topic(channel, topic, token=admintoken, botroomid=botroomid): topic = urllib.parse.quote_plus(topic) url = ('https://slack.com/api/channels.setTopic?token=' + token + '&channel=' + channel + '&topic=' + topic) posted, retries = False, 0 while posted == False and retries < 2: result = create_request(url) posted, retries = result['ok'], retries + 1 if result['ok'] == False: do_send_botmessage('WARN:set_topic error, ' + str(result['error']), botroomid)
def delete_file(fileid, token=admintoken): 'deletes a specified file posted' url = ('https://slack.com/api/files.delete?token=' + token + '&file=' + fileid) posted, retries = False, 0 while posted == False and retries < 1: result = create_request(url) posted, retries = result['ok'], retries + 1 if result['ok'] == False: do_send_botmessage( 'WARN:do_delete_file error, ' + str(result['error']), botroomid)
def delete_text(channel, ts, token=admintoken): url = ('https://slack.com/api/chat.delete?token=' + token + '&channel=' + channel + '&ts=' + ts) '''deletes a message posted taking as input the channel and the timestamp''' posted, retries = False, 0 while posted == False and retries < 1: result = create_request(url) posted, retries = result['ok'], retries + 1 if result['ok'] == False: do_send_botmessage( 'WARN:do_delete_chat error, ' + str(result['error']), botroomid)
def do_send_botephemeral(message, room, user, token=bottoken): '''sends an ephemeral message as the bot user to the user in the room''' text = urllib.parse.quote_plus(message) url = ('https://slack.com/api/chat.postEphemeral?token=' + token + '&channel=' + room + '&text=' + text + '&user='******'ok'], retries + 1 if result['ok'] == False: do_send_botmessage( 'WARN:do_send_botmessage error, ' + str(result['error']), botroomid)
def send_messages(messages): if len(messages)>=1: timers={} for n,m in enumerate(messages): timers[str(n)]=Timer(m['offset']) while True: try: for n,m in enumerate( messages ): if timers[str(n)].get_time()>m['speriod']: timers[str(n)].reset_time() do_send_botmessage(m['msg'], m['room']) except: pass
def do_unpin_message(channel, timestamp, token=bottoken): 'pins a file' url = ('https://slack.com/api/pins.remove?token=' + token + '&channel=' + channel + '×tamp=' + timestamp) posted, retries = False, 0 while posted == False and retries < 1: result = create_request(url) posted, retries = result['ok'], retries + 1 if posted == True: break if result['ok'] == False: do_send_botmessage('WARN:do_pin_message, ' + str(result['error']), botroomid)
def get_channels(token): '''gets the rooms list''' url = ('https://slack.com/api/channels.list?token=' + token + '&exclude_members=true') posted, retries = False, 0 x = 0 while x < 2: try: while posted == False and retries < 2: result = create_request(url) posted, retries = result['ok'], retries + 1 if result['ok'] == False: do_send_botmessage( 'WARN:get_channels error, ' + str(result['error']), botroomid) return result except: x = x + 1
def check_rtm(response, alloweddomains, secfeaturesconf, secfeatures, seccommands, simplecommands, alloweduserids, channels, admins, lograte, botroomid=botroomid): '''Main function that reads the RTM API responses and acts accordingly''' if 'type' in response.keys(): if response['type'] == 'file_shared' and response[ 'user_id'] not in alloweduserids and ( secfeatures['filedisable']): delete_file(response['file_id']) do_send_botmessage( 'WARN: ' + str(get_user_info(response['user_id'])) + ' uploaded a file and it was deleted', botroomid) elif response['type'] == 'user_change' and response['user'][ 'deleted'] == False: if secfeatures['nameban']: print("Checking name ban") if response['user']['profile'][ 'display_name_normalized'] in admins[0] or ( response['user']['profile'] ['display_name_normalized'] in secfeaturesconf['namebanlist']): with requests.post(create_banurl(), data=create_data( response['user']['id']), headers=create_banheaders()) as request: request.close() do_send_botmessage( 'CRIT: username: '******'user']['name'] + ' with name: ' + response['user']['real_name'] + ' Changed his username to : ' + response['user'] ['profile']['display_name_normalized'] + ' and was banned!', botroomid) else: if lograte.check_msg( 'WARN: username: '******'user']['name'] + ' with name: ' + response['user']['real_name'] + ' became username: '******'user']['profile'] ['display_name_normalized'] + ' with name: ' + response['user']['profile']['real_name_normalized'] + ' first name: ' + response['user']['profile']['first_name'] + ' last name: ' + response['user']['profile']['last_name']): do_send_botmessage( 'WARN: username: '******'user']['name'] + ' with name: ' + response['user']['real_name'] + ' became username: '******'user']['profile'] ['display_name_normalized'] + ' with name: ' + response['user']['profile']['real_name_normalized'] + ' first name: ' + response['user']['profile']['first_name'] + ' last name: ' + response['user']['profile']['last_name'], botroomid) elif response['type'] == 'pin_removed' and response[ 'user'] not in alloweduserids and (secfeatures['pinsoft']): if response['channel_id'] in channels.keys(): if secfeatures['pinhard']: do_kick_user(response['channel_id'], response['user']) do_send_botmessage( 'WARN: ' + str(get_user_info(response['user'])) + ' was kicked from ' + channels[response['channel_id']]['name'] + ' because he removed a pinned message', botroomid) if response['item']['type'] == 'message': do_pin_message(response['channel_id'], response['item']['message']['ts']) elif response['item']['type'] == 'file': do_pin_file(response['channel_id'], response['item']['file']['id']) elif response['item']['type'] == 'file_comment': do_pin_filecomment(response['channel_id'], response['item']['comment']['id']) do_send_botmessage( 'WARN: ' + str(get_user_info(response['user'])) + ' unpinned an item from ' + channels[response['channel_id']]['name'], botroomid) else: do_send_botmessage( 'WARN: ' + str(get_user_info(response['user'])) + ' is unpinning items in private channel and he has invited the' + ' bot user in it! ', botroomid) elif response['type'] == 'pin_added' and response[ 'user'] not in alloweduserids and (secfeatures['pinsoft']): if response['channel_id'] in channels.keys(): if secfeatures['pinhard']: do_kick_user(response['channel_id'], response['user']) do_send_botmessage( 'WARN: ' + str(get_user_info(response['user'])) + ' was kicked from ' + channels[response['channel_id']]['name'] + ' because he added a pinned message', botroomid) if response['item']['type'] == 'message': do_unpin_message(response['channel_id'], response['item']['message']['ts']) elif response['item']['type'] == 'file': do_unpin_file(response['channel_id'], response['item']['file']['id']) elif response['item']['type'] == 'file_comment': do_unpin_filecomment(response['channel_id'], response['item']['comment']['id']) do_send_botmessage( 'WARN: ' + str(get_user_info(response['user'])) + ' pinned an item in ' + channels[response['channel_id']]['name'], botroomid) else: do_send_botmessage( 'WARN: ' + str(get_user_info(response['user'])) + ' is pinning items in private channel and he has invited the' + ' bot user in it! ', botroomid) if 'text' in response.keys(): if response['type'] == 'message' and 'subtype' in response.keys(): if response['subtype'] == 'reminder_add' and secfeatures[ 'slackbotchannel']: do_send_botmessage( 'CRIT: Reminder added in public channel ' + 'from user ' + str(get_user_info(response['user'])), botroomid) elif response['subtype'] == 'channel_purpose' and response[ 'user'] not in alloweduserids and ( response['channel'] in channels.keys() and response['purpose'] != (channels[response['channel']]['purpose']) and secfeatures['topicsoft']): if secfeatures['topichard']: do_kick_user(response['channel'], response['user']) do_set_purpose(response['channel'], channels[response['channel']]['purpose']) do_send_botmessage( 'CRIT : Purpose changed in ' + channels[response['channel']]['name'] + ' from user ' + str(get_user_info(response['user'])), botroomid) elif response['subtype'] == 'channel_topic' and response[ 'user'] not in alloweduserids and ( response['channel'] in channels.keys() and response['topic'] != (channels[response['channel']]['topic']) and secfeatures['topicsoft']): if secfeatures['topichard']: do_kick_user(response['channel'], response['user']) do_set_topic(response['channel'], channels[response['channel']]['topic']) do_send_botmessage( 'CRIT : Topic changed in ' + channels[response['channel']]['name'] + ' from user ' + str(get_user_info(response['user'])), botroomid) if response['type'] == 'message' and 'user' in response.keys(): if check_mute(response['text'], response['user']): do_mute(response['text']) elif check_unmute(response['text'], response['user']): do_unmute(response['text']) elif check_ban(response['text'], response['user']): do_ban_user(response['text']) elif check_unban(response['text'], response['user']): do_unban_user(response['text']) elif response['user'] == 'USLACKBOT' and secfeatures[ 'slackbotchannel']: delete_text(response['channel'], response['ts']) elif check_muted(response['user']): delete_text(response['channel'], response['ts']) do_kick_user(response['channel'], response['user']) elif response['type'] == 'message' and ( similar_eth(response['text']) or similar_btc(response['text']) or similar_prv( response['text'])) and (secfeatures['deleth']): delete_text(response['channel'], response['ts']) elif check_prohibited( response['text'], secfeaturesconf['forbidden']) and (response['user'] not in alloweduserids): delete_text(response['channel'], response['ts']) do_kick_user(response['channel'], response['user']) do_send_botmessage( 'WARN: ' + str(get_user_info(response['user'])) + ' was kicked from ' + channels[response['channel']]['name'] + ' because he typed: ' + response['text'], botroomid) elif (is_url(response['text'])[0] ) and response['user'] not in alloweduserids and (is_url( response['text'])[1] > is_allowedurl( response['text'], alloweddomains)[1]): if 'subtype' in response.keys(): if response['subtype'] != 'file_share' and secfeatures[ 'filedisable']: pass else: if response['channel'] in channels.keys(): delete_text(response['channel'], response['ts']) do_send_botmessage( 'WARN: ' + str(get_user_info(response['user'])) + ' sent the following text containing clickable URL in ' + channels[response['channel']]['name'] + ' : ' + response['text'] + '', botroomid) elif response['type'] == 'message' and len( response['text'].rsplit()) == 1: for i in simplecommands.keys(): if similar_word(response['text'], '.?' + i + '.?') and (response['channel'] in channels.keys()): do_send_botephemeral(simplecommands[i], response['channel'], response['user']) elif similar_command( response['text']) and secfeatures['forbidrogue']: if response['channel'] in channels.keys(): delete_text(response['channel'], response['ts'])