예제 #1
0
파일: client.py 프로젝트: larsks/bottom
 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")