async def _internal_cap_handler(conn: 'IrcProtocol', message: 'Message'): caplist = [] if len(message.parameters) > 2: caplist = CapList.parse(message.parameters[-1]) if message.parameters[1] == 'LS': for cap in caplist: if cap.name in conn.cap_handlers: conn.server.caps[cap.name] = (cap, None) if message.parameters[2] != '*': for cap in conn.server.caps: conn.send("CAP REQ :{}".format(cap)) if not conn.server.caps: conn.send( "CAP END" ) # We haven't request any CAPs, send a CAP END to end negotiation elif message.parameters[1] in ('ACK', 'NAK'): enabled = message.parameters[1] == 'ACK' for cap in caplist: current = conn.server.caps[cap.name][0] conn.server.caps[cap.name] = (current, enabled) if enabled: handlers = filter(None, conn.cap_handlers[cap.name]) await asyncio.gather(*[func(conn, cap) for func in handlers]) if all(val[1] is not None for val in conn.server.caps.values()): conn.send("CAP END") elif message.parameters[1] == 'LIST': if conn.logger: conn.logger.info("Current Capabilities: %s", caplist) elif message.parameters[1] == 'NEW': if conn.logger: conn.logger.info("New capabilities advertised: %s", caplist) for cap in caplist: if cap.name in conn.cap_handlers: conn.server.caps[cap.name] = (cap, None) if message.parameters[2] != '*': for cap in conn.server.caps: conn.send("CAP REQ :{}".format(cap)) elif message.parameters[1] == 'DEL': if conn.logger: conn.logger.info("Capabilities removed: %s", caplist) for cap in caplist: current = conn.server.caps[cap.name][0] conn.server.caps[cap.name] = (current, False)
def send_cap_ls(conn): conn.cmd("CAP", "LS", "302") conn.memory.setdefault("available_caps", CapList()).clear() conn.memory.setdefault("cap_queue", {}).clear()
async def on_cap(irc_paramlist, event): args = {} if len(irc_paramlist) > 2: args["caplist"] = CapList.parse(irc_paramlist[-1]) await _launch_handler(irc_paramlist[1], event, **args)
def test_ne_str(self, caps, text): """Test not-equals strings""" b = CapList(caps) != text assert not b b1 = text != CapList(caps) assert not b1
def test_eq_str(self, caps, text): """Test equals strings""" assert CapList(caps) == text assert text == CapList(caps)
def test_ne_list(self, caps): """Test not-equals list""" b = CapList(caps) != caps assert not b b1 = caps != CapList(caps) assert not b1
def test_eq_list(self, caps): """Test equals list""" assert CapList(caps) == caps assert caps == CapList(caps)
class TestCapList: """Test parsing a list of CAPs""" @pytest.mark.parametrize( "text,expected", [ ( "blah blah-blah cap-1 test-cap=value-data", ( ("blah", None), ("blah-blah", None), ("cap-1", None), ("test-cap", "value-data"), ), ), ( "blah blah-blah cap-1 test-cap=value-data ", ( ("blah", None), ("blah-blah", None), ("cap-1", None), ("test-cap", "value-data"), ), ), ( ":blah blah-blah cap-1 test-cap=value-data", ( ("blah", None), ("blah-blah", None), ("cap-1", None), ("test-cap", "value-data"), ), ), ( ":blah blah-blah cap-1 test-cap=value-data ", ( ("blah", None), ("blah-blah", None), ("cap-1", None), ("test-cap", "value-data"), ), ), ( "", (), ), ], ) def test_parse(self, text, expected): """Test string parsing""" parsed = CapList.parse(text) assert len(parsed) == len(expected) for (name, value), actual in zip(expected, parsed): assert actual.name == name assert actual.value == value @pytest.mark.parametrize("caps", [[], [Cap("a"), Cap("b", "c")]]) def test_eq_list(self, caps): """Test equals list""" assert CapList(caps) == caps assert caps == CapList(caps) @pytest.mark.parametrize( "caps", [ [], [Cap("a"), Cap("b", "c")], ], ) def test_ne_list(self, caps): """Test not-equals list""" b = CapList(caps) != caps assert not b b1 = caps != CapList(caps) assert not b1 @pytest.mark.parametrize( "caps,text", [ ([], ""), ([Cap("a"), Cap("b", "c")], "a b=c"), ([Cap("a"), Cap("b", "c")], "a b=c "), ], ) def test_eq_str(self, caps, text): """Test equals strings""" assert CapList(caps) == text assert text == CapList(caps) @pytest.mark.parametrize( "caps,text", [ ([], ""), ([Cap("a"), Cap("b", "c")], "a b=c"), ([Cap("a"), Cap("b", "c")], "a b=c "), ], ) def test_ne_str(self, caps, text): """Test not-equals strings""" b = CapList(caps) != text assert not b b1 = text != CapList(caps) assert not b1 @pytest.mark.parametrize( "obj,other", [ (CapList(), None), (CapList(), 0), ], ) def test_no_cmp(self, obj, other): """Test not-equals""" assert obj != other assert other != obj assert not obj == other assert not other == obj @pytest.mark.parametrize( "obj,text", [ (CapList([Cap("foo")]), "foo"), (CapList([Cap("foo"), Cap("bar")]), "foo bar"), (CapList([Cap("foo"), Cap("bar=baz")]), "foo bar=baz"), ], ) def test_str(self, obj, text): """Test string conversion""" assert str(obj) == text assert text == str(obj)
class TestCapList: @pytest.mark.parametrize('text,expected', [("blah blah-blah cap-1 test-cap=value-data", (("blah", None), ("blah-blah", None), ("cap-1", None), ("test-cap", "value-data"))), ("blah blah-blah cap-1 test-cap=value-data ", (("blah", None), ("blah-blah", None), ("cap-1", None), ("test-cap", "value-data"))), (":blah blah-blah cap-1 test-cap=value-data", (("blah", None), ("blah-blah", None), ("cap-1", None), ("test-cap", "value-data"))), (":blah blah-blah cap-1 test-cap=value-data ", (("blah", None), ("blah-blah", None), ("cap-1", None), ("test-cap", "value-data"))), ( "", (), )]) def test_parse(self, text, expected): parsed = CapList.parse(text) assert len(parsed) == len(expected) for (name, value), actual in zip(expected, parsed): assert actual.name == name assert actual.value == value @pytest.mark.parametrize('caps', [[], [Cap('a'), Cap('b', 'c')]]) def test_eq_list(self, caps): assert CapList(caps) == caps assert caps == CapList(caps) @pytest.mark.parametrize('caps', [ [], [Cap('a'), Cap('b', 'c')], ]) def test_ne_list(self, caps): assert not (CapList(caps) != caps) assert not (caps != CapList(caps)) @pytest.mark.parametrize('caps,text', [ ([], ''), ([Cap('a'), Cap('b', 'c')], 'a b=c'), ([Cap('a'), Cap('b', 'c')], 'a b=c '), ]) def test_eq_str(self, caps, text): assert CapList(caps) == text assert text == CapList(caps) @pytest.mark.parametrize('caps,text', [ ([], ''), ([Cap('a'), Cap('b', 'c')], 'a b=c'), ([Cap('a'), Cap('b', 'c')], 'a b=c '), ]) def test_ne_str(self, caps, text): assert not (CapList(caps) != text) assert not (text != CapList(caps)) @pytest.mark.parametrize('obj,other', [ (CapList(), None), (CapList(), 0), ]) def test_no_cmp(self, obj, other): assert obj != other assert other != obj assert not (obj == other) assert not (other == obj) @pytest.mark.parametrize('obj,text', [ (CapList([Cap('foo')]), 'foo'), (CapList([Cap('foo'), Cap('bar')]), 'foo bar'), (CapList([Cap('foo'), Cap('bar=baz')]), 'foo bar=baz'), ]) def test_str(self, obj, text): assert str(obj) == text assert text == str(obj)
def test_ne_str(self, caps, text): assert not (CapList(caps) != text) assert not (text != CapList(caps))
def test_eq_str(self, caps, text): assert CapList(caps) == text assert text == CapList(caps)
def test_ne_list(self, caps): assert not (CapList(caps) != caps) assert not (caps != CapList(caps))
def test_eq_list(self, caps): assert CapList(caps) == caps assert caps == CapList(caps)
def test_parse(self, text, expected): parsed = CapList.parse(text) assert len(parsed) == len(expected) for (name, value), actual in zip(expected, parsed): assert actual.name == name assert actual.value == value