Пример #1
0
 async def handler(next_handler: Callable, message: str) -> None:
     try:
         event, kwargs = unpack_command(message)
         client.trigger(event, **kwargs)
     except ValueError:
         rfc2812_log.debug("Failed to parse line >>> {}".format(message))
     await next_handler(message)
Пример #2
0
def test_usermode():
    """ MODE command """
    command = "USERMODE"
    expected_kwargs = {"nick": "nck", "host": "", "modes": "+x"}
    message = "MODE nck +x"
    assert (command, expected_kwargs) == unpack_command(message)
    expected_kwargs['user'] = '******'
    assert set(expected_kwargs) == set(parameters(command))
Пример #3
0
def test_usermode():
    """ MODE command """
    command = "USERMODE"
    expected_kwargs = {"nick": "nck", "host": "",
                       "modes": "+x"}
    message = "MODE nck +x"
    assert (command, expected_kwargs) == unpack_command(message)
    expected_kwargs['user'] = '******'
    assert set(expected_kwargs) == set(parameters(command))
Пример #4
0
def test_channelmode_no_params():
    """ MODE command """
    command = "CHANNELMODE"
    expected_kwargs = {"channel": "#ch", "host": "",
                       "modes": "+m", "params": []}
    message = "MODE #ch +m"
    assert (command, expected_kwargs) == unpack_command(message)
    expected_kwargs['user'] = '******'
    expected_kwargs['nick'] = 'nck'
    assert set(expected_kwargs) == set(parameters(command))
Пример #5
0
def test_channelmode_plus():
    """ MODE command """
    command = "CHANNELMODE"
    expected_kwargs = {"channel": "+ch", "host": "",
                       "modes": "+o", "params": ['trget', 'trget2']}
    message = "MODE +ch +o trget trget2"
    assert (command, expected_kwargs) == unpack_command(message)
    expected_kwargs['user'] = '******'
    expected_kwargs['nick'] = 'nck'
    assert set(expected_kwargs) == set(parameters(command))
Пример #6
0
def test_notice():
    """ NOTICE command """
    command = "NOTICE"
    message = ":n!u@h NOTICE #t :m"
    expected_kwargs = {"nick": "n", "user": "******", "host": "h", "message": "m", "target": "#t"}
    validate(command, message, expected_kwargs)

    # server notice - can't use validate since not all params are defined
    message = ":some.host.edu NOTICE #t :m"
    expected_kwargs = {"host": "some.host.edu", "message": "m", "target": "#t"}
    assert (command, expected_kwargs) == unpack_command(message)
Пример #7
0
def test_notice():
    """ NOTICE command """
    command = "NOTICE"
    message = ":n!u@h NOTICE #t :m"
    expected_kwargs = {"nick": "n", "user": "******", "host": "h",
                       "message": "m", "target": "#t"}
    validate(command, message, expected_kwargs)

    # server notice - can't use validate since not all params are defined
    message = ":some.host.edu NOTICE #t :m"
    expected_kwargs = {"host": "some.host.edu", "message": "m", "target": "#t"}
    assert (command, expected_kwargs) == unpack_command(message)
Пример #8
0
 def data_received(self, data):
     self.buffer += data
     # All but the last result of split should be pushed into the
     # client.  The last will be b"" if the buffer ends on b"\n"
     *lines, self.buffer = self.buffer.split(DELIM_COMPAT)
     for line in lines:
         message = line.decode(self.client.encoding, "ignore").strip()
         try:
             event, kwargs = unpack_command(message)
             self.client.trigger(event, **kwargs)
         except ValueError:
             log.debug("Failed to parse line >>> {}".format(message))
Пример #9
0
def test_channelmode_no_params():
    """ MODE command """
    command = "CHANNELMODE"
    expected_kwargs = {
        "channel": "#ch",
        "host": "",
        "modes": "+m",
        "params": []
    }
    message = "MODE #ch +m"
    assert (command, expected_kwargs) == unpack_command(message)
    expected_kwargs['user'] = '******'
    expected_kwargs['nick'] = 'nck'
    assert set(expected_kwargs) == set(parameters(command))
Пример #10
0
def test_channelmode_plus():
    """ MODE command """
    command = "CHANNELMODE"
    expected_kwargs = {
        "channel": "+ch",
        "host": "",
        "modes": "+o",
        "params": ['trget', 'trget2']
    }
    message = "MODE +ch +o trget trget2"
    assert (command, expected_kwargs) == unpack_command(message)
    expected_kwargs['user'] = '******'
    expected_kwargs['nick'] = 'nck'
    assert set(expected_kwargs) == set(parameters(command))
Пример #11
0
def test_unknown_command():
    """ raise when command isn't known """
    with pytest.raises(ValueError):
        unpack_command("unknown_command")
    with pytest.raises(ValueError):
        parameters("unknown_command")
Пример #12
0
def test_no_command():
    ''' raise when command is None or empty '''
    with pytest.raises(AttributeError):
        unpack_command(None)
    with pytest.raises(ValueError):
        unpack_command("")
Пример #13
0
def validate(command, message, expected_kwargs):
    ''' Basic case - expected_kwargs expects all parameters of the command '''
    assert (command, expected_kwargs) == unpack_command(message)
    assert set(expected_kwargs) == set(parameters(command))
Пример #14
0
def test_ignore_case():
    ''' input case doesn't matter '''
    assert ("PING", {"message": "m"}) == unpack_command("pInG :m")
Пример #15
0
def test_unknown_command():
    ''' raise when command isn't known '''
    with pytest.raises(ValueError):
        unpack_command("unknown_command")
    with pytest.raises(ValueError):
        parameters("unknown_command")
Пример #16
0
def test_bad_command():
    ''' raise when command is incorrectly formatted '''
    with pytest.raises(ValueError):
        unpack_command(":prefix_only")
Пример #17
0
def test_no_command():
    """ raise when command is None or empty """
    with pytest.raises(AttributeError):
        unpack_command(None)
    with pytest.raises(ValueError):
        unpack_command("")
Пример #18
0
def validate(command, message, expected_kwargs):
    """ Basic case - expected_kwargs expects all parameters of the command """
    assert (command, expected_kwargs) == unpack_command(message)
    assert set(expected_kwargs) == set(parameters(command))
Пример #19
0
def test_ignore_case():
    """ input case doesn't matter """
    assert ("PING", {"message": "m"}) == unpack_command("pInG :m")
Пример #20
0
def test_bad_command():
    """ raise when command is incorrectly formatted """
    with pytest.raises(ValueError):
        unpack_command(":prefix_only")