コード例 #1
0
ファイル: inf.py プロジェクト: romkabursa/vkbot
def reply(m):
    #friendship request
    m['user_id'] = str(m['user_id'])
    if 'chat_id' in m:
        m['chat_id'] = str(m['chat_id'])
        if m['chat_id'] in banned:
            return None
    elif m['user_id'] in banned:
        return None
  #  elif not isFriend(m['user_id']):
  #      return ('', 0)
    if 'body' not in m:
        m['body'] = ''
    if m['user_id'] in ignored:
        return ('', 0)
    if 'chat_id' in m:
        m['chat_id'] = str(m['chat_id'])
        if m['chat_id'] in ignored:
            return ('', 0)
    if 'id' not in m:
        return (getBotReply(m['user_id'], m['message'], 0), 2)
    m['body'] = preprocessMessage(m)
    if m['body']:
        if m['body'].startswith('\\') and len(m['body']) > 1:
            cmd = m['body'][1:].split()
            if cmd:
                if reset_command and cmd[0] == reset_command:
                    cmd = cmd[1:]
                    vk.sendMessage(admin, '%s from %s' % (cmd, m['user_id']))
                    return (processCommand(*cmd), 1)
                elif m['user_id'] == admin:
                    return (processCommand(*cmd), 1)
        t = evalExpression(m['body'])
        if t:
            print(m['body'], '=', t, '(calculated)')
            log.write('calc', '"{}" = {}'.format(m['body'], t))
            return (t, 0)
        if bot_msg.match(m['body'].strip()):
            print(m['body'], '- ignored (bot message)')
            return ('', 0)
    if m['body'] and m['body'].upper() == m['body'] and len([i for i in m['body'] if i.isalpha()]) > 1:
        print(m['body'], '- ignored (caps)')
        return ('', 0)
    return (getBotReply(m['user_id'], m['body'] , 'chat_id' in m), 0)
コード例 #2
0
ファイル: inf.py プロジェクト: team55/vkbot
def reply(message):
    if vk.getSender(message) in banign.banned:
        return None
    if vk.getSender(message) < 0:
        return None
    if vk.getSender(message) in check_friend.noadd or message[
            'user_id'] in check_friend.noadd:
        return ('', 0)
    if 'deactivated' in vk.users[message['user_id']] or vk.users[
            message['user_id']]['blacklisted'] or vk.users[
                message['user_id']]['blacklisted_by_me']:
        return ('', 0)

    if 'body' not in message:
        message['body'] = ''

    if 'id' not in message:  # friendship request
        return (getBotReply(message['user_id'], message['message'], 0,
                            'friendship request'), 2)
    message['body'] = preprocessMessage(message)

    if message['body']:
        if message['body'].startswith('\\') and len(message['body']) > 1:
            cmd = message['body'][1:].split()
            if cmd and message['user_id'] == vk.admin:
                return (processCommand(*cmd), 1)

        if isBotMessage(message['body']):
            text_msg = '({}) {} - ignored (bot message)'.format(
                vk.printableSender(message, False), message['body'])
            html_msg = '({}) {} - ignored (bot message)'.format(
                vk.printableSender(message, True), message['body'])
            log.info((text_msg, html_msg))
            if 'chat_id' in message:
                bot_users[message['user_id']] = bot_users.get(
                    message['user_id'], 0) + 1
                if bot_users[message['user_id']] >= 3:
                    log.info('Too many bot messages')
                    log.write(
                        'conf',
                        str(message['user_id']) + ' ' +
                        str(message['chat_id']) + ' (bot messages)')
                    vk.leaveConf(message['chat_id'])
            return ('', 0)
        elif message['user_id'] in bot_users:
            del bot_users[message['user_id']]

        if message['body'] == last_message_text.get(
                message['user_id'], (0, 0, 0))[0] and message['body'] != '..':
            last_message_text[message['user_id']][2] += 1
            if last_message_text[message['user_id']][2] >= 5:
                noaddUsers([message['user_id']], reason='flood')
            else:
                text_msg = '({}) {} - ignored (repeated)'.format(
                    vk.printableSender(message, False), message['body'])
                html_msg = '({}) {} - ignored (repeated)'.format(
                    vk.printableSender(message, True), message['body'])
                log.info((text_msg, html_msg))
            return ('', 0)

        if message['body'].strip().upper() == last_message_text.get(
                message['user_id'], (0, 0, 0))[1]:
            text_msg = '({}) {} - ignored (my reply)'.format(
                vk.printableSender(message, False), message['body'])
            html_msg = '({}) {} - ignored (my reply)'.format(
                vk.printableSender(message, True), message['body'])
            log.info((text_msg, html_msg))
            return ('', 0)

        t = evalExpression(message['body'])
        if t:
            if getBotReply(None, message['body'], -2):
                return ('', 0)
            text_msg = '({}) {} = {} (calculated)'.format(
                vk.printableSender(message, False), message['body'], t)
            html_msg = '({}) {} = {} (calculated)'.format(
                vk.printableSender(message, True), message['body'], t)
            log.info((text_msg, html_msg))
            log.write(
                'calc', '{}: "{}" = {}'.format(message['user_id'],
                                               message['body'], t))
            return (t, 0)
    if message['body']:
        message['body'] = message['body'].replace('<br>', '<BR>')
    if message['body'] and message['body'].upper() == message['body'] and len(
        [i for i in message['body'] if i.isalpha()]) > 1:
        text_msg = '({}) {} - ignored (caps)'.format(
            vk.printableSender(message, False), message['body'])
        html_msg = '({}) {} - ignored (caps)'.format(
            vk.printableSender(message, True), message['body'])
        log.info((text_msg, html_msg))
        return ('', 0)

    reply = getBotReply(message['user_id'], message['body'],
                        message.get('chat_id', 0), message.get('_method', ''))
    if reply is not None:
        last_message_text[message['user_id']] = [
            message['body'], reply.strip().upper(), 1
        ]
    return (reply, 0)
コード例 #3
0
ファイル: inf.py プロジェクト: team55/vkbot
def reply(message):
    if vk.getSender(message) in banign.banned:
        return None
    if vk.getSender(message) < 0:
        return None
    if vk.getSender(message) in check_friend.noadd or message['user_id'] in check_friend.noadd:
        return ('', 0)
    if 'deactivated' in vk.users[message['user_id']] or vk.users[message['user_id']]['blacklisted'] or vk.users[message['user_id']]['blacklisted_by_me']:
        return ('', 0)

    if 'body' not in message:
        message['body'] = ''

    if 'id' not in message:  # friendship request
        return (getBotReply(message['user_id'], message['message'], 0, 'friendship request'), 2)
    message['body'] = preprocessMessage(message)

    if message['body']:
        if message['body'].startswith('\\') and len(message['body']) > 1:
            cmd = message['body'][1:].split()
            if cmd and message['user_id'] == vk.admin:
                return (processCommand(*cmd), 1)

        if isBotMessage(message['body']):
            text_msg = '({}) {} - ignored (bot message)'.format(vk.printableSender(message, False), message['body'])
            html_msg = '({}) {} - ignored (bot message)'.format(vk.printableSender(message, True), message['body'])
            log.info((text_msg, html_msg))
            if 'chat_id' in message:
                bot_users[message['user_id']] = bot_users.get(message['user_id'], 0) + 1
                if bot_users[message['user_id']] >= 3:
                    log.info('Too many bot messages')
                    log.write('conf', str(message['user_id']) + ' ' + str(message['chat_id']) + ' (bot messages)')
                    vk.leaveConf(message['chat_id'])
            return ('', 0)
        elif message['user_id'] in bot_users:
            del bot_users[message['user_id']]

        if message['body'] == last_message_text.get(message['user_id'], (0,0,0))[0] and message['body'] != '..':
            last_message_text[message['user_id']][2] += 1
            if last_message_text[message['user_id']][2] >= 5:
                noaddUsers([message['user_id']], reason='flood')
            else:
                text_msg = '({}) {} - ignored (repeated)'.format(vk.printableSender(message, False), message['body'])
                html_msg = '({}) {} - ignored (repeated)'.format(vk.printableSender(message, True), message['body'])
                log.info((text_msg, html_msg))
            return ('', 0)

        if message['body'].strip().upper() == last_message_text.get(message['user_id'], (0,0,0))[1]:
            text_msg = '({}) {} - ignored (my reply)'.format(vk.printableSender(message, False), message['body'])
            html_msg = '({}) {} - ignored (my reply)'.format(vk.printableSender(message, True), message['body'])
            log.info((text_msg, html_msg))
            return ('', 0)

        t = evalExpression(message['body'])
        if t:
            if getBotReply(None, message['body'], -2):
                return ('', 0)
            text_msg = '({}) {} = {} (calculated)'.format(vk.printableSender(message, False), message['body'], t)
            html_msg = '({}) {} = {} (calculated)'.format(vk.printableSender(message, True), message['body'], t)
            log.info((text_msg, html_msg))
            log.write('calc', '{}: "{}" = {}'.format(message['user_id'], message['body'], t))
            return (t, 0)
    if message['body']:
        message['body'] = message['body'].replace('<br>', '<BR>')
    if message['body'] and message['body'].upper() == message['body'] and len([i for i in message['body'] if i.isalpha()]) > 1:
        text_msg = '({}) {} - ignored (caps)'.format(vk.printableSender(message, False), message['body'])
        html_msg = '({}) {} - ignored (caps)'.format(vk.printableSender(message, True), message['body'])
        log.info((text_msg, html_msg))
        return ('', 0)

    reply = getBotReply(message['user_id'], message['body'] , message.get('chat_id', 0), message.get('_method', ''))
    if reply is not None:
        last_message_text[message['user_id']] = [message['body'], reply.strip().upper(), 1]
    return (reply, 0)
コード例 #4
0
ファイル: inf.py プロジェクト: rezvorck/vkbot
def reply(message):
    if getSender(message) in banign.banned or getSender(message) < 0:
        vk.banned_list.append(getSender(message))
        return None
    uid = message['user_id']
    if getSender(message) in friend_controller.noadd or uid in friend_controller.noadd:
        return (None, False)
    if 'deactivated' in vk.users[uid] or vk.users[uid]['blacklisted'] or vk.users[uid]['blacklisted_by_me']:
        return (None, False)

    if 'body' not in message:
        message['body'] = ''
    if message['body'] is None:
        return (None, False)

    if 'id' not in message:  # friendship request
        message['body'] = message['message']
        message['_method'] = 'friendship request'
        return (getBotReply(message), True)

    if isBotMessage(message['body']):
        vk.logSender('(%sender%) {} - ignored (bot message)'.format(message['body']), message)
        if 'chat_id' in message and not vk.no_leave_conf:
            bot_users[uid] = bot_users.get(uid, 0) + 1
            if bot_users[uid] >= 3:
                logging.info('Too many bot messages')
                log.write('conf', vk.loggableConf(message['chat_id']) + ' (bot messages)')
                vk.leaveConf(message['chat_id'])
        return ('', False)
    elif uid in bot_users:
        del bot_users[uid]

    message['body'] = preprocessMessage(message)

    if message['body'] is None:
        return ('', False)

    if message['body']:
        if message.get('_is_sticker') and config.get('vkbot.ignore_stickers', 'b'):
            vk.logSender('(%sender%) {} - ignored'.format(message['body']), message)
            return ('', False)
        if message.get('_is_voice') and 'chat_id' in message:
            vk.logSender('(%sender%) {} - ignored'.format(message['body']), message)
            return ('', False)
        user_msg = vk.last_message.byUser(uid)
        if message['body'] == user_msg.get('text') and message['body'] != '..':
            user_msg['count'] = user_msg.get('count', 0) + 1  # this modifies the cache entry too
            if message.get('_is_voice') and user_msg == vk.last_message.bySender(getSender(message)) and not user_msg.get('resent'):
                vk.logSender('(%sender%) {} - voice again'.format(message['body']), message)
                return (user_msg.get('reply'), False)
            if message.get('_is_sticker'):
                return ('', False)
            if user_msg['count'] == 5:
                noaddUsers([uid], reason='flood')
            elif user_msg['count'] < 5:
                vk.logSender('(%sender%) {} - ignored (repeated)'.format(message['body']), message)
            return ('', False)

        if 'reply' in user_msg and message['body'].upper() == user_msg['reply'].upper() and len(message['body'].split()) > 1:
            vk.logSender('(%sender%) {} - ignored (my reply)'.format(message['body']), message)
            user_msg['text'] = user_msg['reply']  # this modifies the cache entry too
            # user_msg['count'] = 1  # do we need it?
            return ('', False)

        t = evalExpression(message['body'])
        if t:
            if getBotReplyFlat(message['body']):
                return ('', False)
            vk.logSender('(%sender%) {} = {} (calculated)'.format(message['body'], t), message)
            log.write('calc', '{}: "{}" = {}'.format(vk.loggableName(uid), message['body'], t))
            return (t, False)
        tbody = message['body'].replace('<br>', '')
        if tbody.upper() == tbody and sum(i.isalpha() for i in tbody) > 1 and config.get('vkbot.ignore_caps', 'b'):
            vk.logSender('(%sender%) {} - ignored (caps)'.format(message['body']), message)
            return ('', False)

    return (getBotReply(message), False)