Ejemplo n.º 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)
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 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)
Ejemplo n.º 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)
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 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))
Ejemplo n.º 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")
Ejemplo n.º 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("")
Ejemplo n.º 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))
Ejemplo n.º 14
0
def test_ignore_case():
    ''' input case doesn't matter '''
    assert ("PING", {"message": "m"}) == unpack_command("pInG :m")
Ejemplo n.º 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")
Ejemplo n.º 16
0
def test_bad_command():
    ''' raise when command is incorrectly formatted '''
    with pytest.raises(ValueError):
        unpack_command(":prefix_only")
Ejemplo n.º 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("")
Ejemplo n.º 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))
Ejemplo n.º 19
0
def test_ignore_case():
    """ input case doesn't matter """
    assert ("PING", {"message": "m"}) == unpack_command("pInG :m")
Ejemplo n.º 20
0
def test_bad_command():
    """ raise when command is incorrectly formatted """
    with pytest.raises(ValueError):
        unpack_command(":prefix_only")