示例#1
0
文件: probot.py 项目: camconn/probot
def handle_incoming(line, shared_data):
    ''' Handles, and replies to incoming IRC messages

    line - the line to parse
    shared_data - the shared_data with literally everything in it
    '''
    config = shared_data['conf']
    reply = None  # Reset reply
    msg_packet = ircp.Packet(line)

    # Determine if prefix is at beginning of message
    # If it is, then parse for commands
    if msg_packet.msg_type == 'PRIVMSG':
        # TODO: Let use know they are being penalized for cooldown
        reply = handle_commands(msg_packet, shared_data)
        if reply is None:
            reply = handle_regexes(msg_packet, shared_data)
    elif msg_packet.msg_type == 'NUMERIC':
        if (config['password'] and not config['logged_in'] and
                msg_packet.numeric == ircp.numerics.RPL_ENDOFMOTD):
            reply = []  # pylint: disable=redefined-variable-type
            reply.append(ircp.make_message('identify {} {}'.format(config['bot_nick'],
                                                                   config['password']),
                                           'nickserv'))
            for channel in config['channels'].split(' '):
                reply.append(ircp.join_chan(channel))
            shared_data['conf']['logged_in'] = True  # Stop checking for login numerics
        elif msg_packet.numeric == ircp.numerics.RPL_ENDOFMOTD:
            reply = (ircp.join_chan(c) for c in shared_data['conf']['channels'].split(' '))

    elif msg_packet.msg_type == 'PING':
        reply = 'PONG {}'.format(msg_packet.host)

    elif msg_packet.msg_type == 'NICK':
        print('{} changed nick to {}'.format(msg_packet.sender, msg_packet.nick_to))
        if msg_packet.sender in shared_data['auth']:
            shared_data['auth'].remove(msg_packet.sender)
            shared_data['auth'].add(msg_packet.nick_to)
            print('moved {} to {} on auth list'.format(msg_packet.sender, msg_packet.nick_to))

    elif msg_packet.msg_type in ('PART', 'QUIT'):
        if msg_packet.sender in shared_data['auth']:
            shared_data['auth'].remove(msg_packet.sender)
            print('removed {} from auth list'.format(msg_packet.sender))

    elif msg_packet.msg_type == 'JOIN':
        if msg_packet.sender == shared_data['conf']['bot_nick']:
            shared_data['chan'].add(msg_packet.target)
            reply = ircp.make_message(shared_data['conf']['intro'], msg_packet.target)

    if isinstance(reply, int):
        flag = int(reply)
        reply = [ircp.make_message('kthxbai', c) for c in shared_data['chan']]
        reply.append(flag)  # Makes sure to close out.

    shared_data['recent_messages'].append(msg_packet)
    shared_data['stats']['num_messages'] += 1

    return reply
示例#2
0
def join_command(arg: tuple, packet: ircp.Packet, shared: dict):
    ''' Make the bot join a channel

        :join <channel> [channel [channel ...]]
    '''
    if len(arg) < 2:
        return packet.notice('You need to specify a channel for me to join.')

    output = []
    for c in arg[1:]:
        if c.find('#') == 0:
            output.append(ircp.join_chan(c))
        else:
            output.append(packet.notice('{} is not a valid channel'.format(c)))

    output.append(packet.notice('Joined!'))
    return output
示例#3
0
def join_command(arg: tuple, packet: ircp.Packet, shared: dict):
    ''' Make the bot join a channel

        :join <channel> [channel [channel ...]]
    '''
    if len(arg) < 2:
        return packet.notice('You need to specify a channel for me to join.')

    output = []
    for c in arg[1:]:
        if c.find('#') == 0:
            output.append(ircp.join_chan(c))
        else:
            output.append(packet.notice('{} is not a valid channel'.format(c)))

    output.append(packet.notice('Joined!'))
    return output
示例#4
0
文件: probot.py 项目: camconn/probot
def handle_incoming(line, shared_data):
    ''' Handles, and replies to incoming IRC messages

    line - the line to parse
    shared_data - the shared_data with literally everything in it
    '''
    config = shared_data['conf']
    reply = None  # Reset reply
    msg_packet = ircp.Packet(line)

    # Determine if prefix is at beginning of message
    # If it is, then parse for commands
    if msg_packet.msg_type == 'PRIVMSG':
        # TODO: Let use know they are being penalized for cooldown
        reply = handle_commands(msg_packet, shared_data)
        if reply is None:
            reply = handle_regexes(msg_packet, shared_data)
    elif msg_packet.msg_type == 'NUMERIC':
        if (config['password'] and not config['logged_in']
                and msg_packet.numeric == ircp.numerics.RPL_ENDOFMOTD):
            reply = []  # pylint: disable=redefined-variable-type
            reply.append(
                ircp.make_message(
                    'identify {} {}'.format(config['bot_nick'],
                                            config['password']), 'nickserv'))
            for channel in config['channels'].split(' '):
                reply.append(ircp.join_chan(channel))
            shared_data['conf'][
                'logged_in'] = True  # Stop checking for login numerics
        elif msg_packet.numeric == ircp.numerics.RPL_ENDOFMOTD:
            reply = (ircp.join_chan(c)
                     for c in shared_data['conf']['channels'].split(' '))

    elif msg_packet.msg_type == 'PING':
        reply = 'PONG {}'.format(msg_packet.host)

    elif msg_packet.msg_type == 'NICK':
        print('{} changed nick to {}'.format(msg_packet.sender,
                                             msg_packet.nick_to))
        if msg_packet.sender in shared_data['auth']:
            shared_data['auth'].remove(msg_packet.sender)
            shared_data['auth'].add(msg_packet.nick_to)
            print('moved {} to {} on auth list'.format(msg_packet.sender,
                                                       msg_packet.nick_to))

    elif msg_packet.msg_type in ('PART', 'QUIT'):
        if msg_packet.sender in shared_data['auth']:
            shared_data['auth'].remove(msg_packet.sender)
            print('removed {} from auth list'.format(msg_packet.sender))

    elif msg_packet.msg_type == 'JOIN':
        if msg_packet.sender == shared_data['conf']['bot_nick']:
            shared_data['chan'].add(msg_packet.target)
            reply = ircp.make_message(shared_data['conf']['intro'],
                                      msg_packet.target)

    if isinstance(reply, int):
        flag = int(reply)
        reply = [ircp.make_message('kthxbai', c) for c in shared_data['chan']]
        reply.append(flag)  # Makes sure to close out.

    shared_data['recent_messages'].append(msg_packet)
    shared_data['stats']['num_messages'] += 1

    return reply