feat: add multi-server support

Connect to multiple IRC servers concurrently from a single config file.
Plugins are loaded once and shared; per-server state is isolated via
separate SQLite databases and per-bot runtime state (bot._pstate).

- Add build_server_configs() for [servers.*] config layout
- Bot.__init__ gains name parameter, _pstate dict for plugin isolation
- cli.py runs multiple bots via asyncio.gather
- 9 stateful plugins migrated from module-level dicts to _ps(bot) pattern
- Backward compatible: legacy [server] config works unchanged

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
user
2026-02-21 19:04:20 +01:00
parent e9528bd879
commit 073659607e
27 changed files with 987 additions and 735 deletions

View File

@@ -18,18 +18,16 @@ _spec.loader.exec_module(_mod)
from plugins.youtube import ( # noqa: E402
_MAX_ANNOUNCE,
_channels,
_compact_num,
_delete,
_derive_name,
_errors,
_extract_channel_id,
_format_duration,
_is_youtube_url,
_load,
_parse_feed,
_poll_once,
_pollers,
_ps,
_restore,
_save,
_start_poller,
@@ -163,6 +161,7 @@ class _FakeBot:
self.sent: list[tuple[str, str]] = []
self.replied: list[str] = []
self.state = _FakeState()
self._pstate: dict = {}
self._admin = admin
async def send(self, target: str, text: str) -> None:
@@ -195,13 +194,7 @@ def _pm(text: str, nick: str = "alice") -> Message:
def _clear() -> None:
"""Reset module-level state between tests."""
for task in _pollers.values():
if task and not task.done():
task.cancel()
_pollers.clear()
_channels.clear()
_errors.clear()
"""No-op -- state is per-bot now, each _FakeBot starts fresh."""
def _fake_fetch_ok(url, etag="", last_modified=""):
@@ -491,8 +484,8 @@ class TestCmdYtFollow:
assert data["name"] == "3b1b"
assert data["channel"] == "#test"
assert len(data["seen"]) == 3
assert "#test:3b1b" in _pollers
_stop_poller("#test:3b1b")
assert "#test:3b1b" in _ps(bot)["pollers"]
_stop_poller(bot, "#test:3b1b")
await asyncio.sleep(0)
asyncio.run(inner())
@@ -679,7 +672,7 @@ class TestCmdYtUnfollow:
await cmd_yt(bot, _msg("!yt unfollow delfeed"))
assert "Unfollowed 'delfeed'" in bot.replied[0]
assert _load(bot, "#test:delfeed") is None
assert "#test:delfeed" not in _pollers
assert "#test:delfeed" not in _ps(bot)["pollers"]
await asyncio.sleep(0)
asyncio.run(inner())
@@ -876,7 +869,7 @@ class TestPollOnce:
}
key = "#test:f304"
_save(bot, key, data)
_channels[key] = data
_ps(bot)["channels"][key] = data
async def inner():
with patch.object(_mod, "_fetch_feed", _fake_fetch_304):
@@ -896,13 +889,13 @@ class TestPollOnce:
}
key = "#test:ferr"
_save(bot, key, data)
_channels[key] = data
_ps(bot)["channels"][key] = data
async def inner():
with patch.object(_mod, "_fetch_feed", _fake_fetch_error):
await _poll_once(bot, key)
await _poll_once(bot, key)
assert _errors[key] == 2
assert _ps(bot)["errors"][key] == 2
updated = _load(bot, key)
assert updated["last_error"] == "Connection refused"
@@ -939,7 +932,7 @@ class TestPollOnce:
}
key = "#test:big"
_save(bot, key, data)
_channels[key] = data
_ps(bot)["channels"][key] = data
async def inner():
with (
@@ -964,7 +957,7 @@ class TestPollOnce:
}
key = "#test:quiet"
_save(bot, key, data)
_channels[key] = data
_ps(bot)["channels"][key] = data
async def inner():
with patch.object(_mod, "_fetch_feed", _fake_fetch_ok):
@@ -987,7 +980,7 @@ class TestPollOnce:
}
key = "#test:etag"
_save(bot, key, data)
_channels[key] = data
_ps(bot)["channels"][key] = data
async def inner():
with patch.object(_mod, "_fetch_feed", _fake_fetch_ok):
@@ -1015,10 +1008,10 @@ class TestRestore:
async def inner():
_restore(bot)
assert "#test:restored" in _pollers
task = _pollers["#test:restored"]
assert "#test:restored" in _ps(bot)["pollers"]
task = _ps(bot)["pollers"]["#test:restored"]
assert not task.done()
_stop_poller("#test:restored")
_stop_poller(bot, "#test:restored")
await asyncio.sleep(0)
asyncio.run(inner())
@@ -1035,9 +1028,9 @@ class TestRestore:
async def inner():
dummy = asyncio.create_task(asyncio.sleep(9999))
_pollers["#test:active"] = dummy
_ps(bot)["pollers"]["#test:active"] = dummy
_restore(bot)
assert _pollers["#test:active"] is dummy
assert _ps(bot)["pollers"]["#test:active"] is dummy
dummy.cancel()
await asyncio.sleep(0)
@@ -1056,12 +1049,12 @@ class TestRestore:
async def inner():
done_task = asyncio.create_task(asyncio.sleep(0))
await done_task
_pollers["#test:done"] = done_task
_ps(bot)["pollers"]["#test:done"] = done_task
_restore(bot)
new_task = _pollers["#test:done"]
new_task = _ps(bot)["pollers"]["#test:done"]
assert new_task is not done_task
assert not new_task.done()
_stop_poller("#test:done")
_stop_poller(bot, "#test:done")
await asyncio.sleep(0)
asyncio.run(inner())
@@ -1073,7 +1066,7 @@ class TestRestore:
async def inner():
_restore(bot)
assert "#test:bad" not in _pollers
assert "#test:bad" not in _ps(bot)["pollers"]
asyncio.run(inner())
@@ -1090,8 +1083,8 @@ class TestRestore:
async def inner():
msg = _msg("", target="botname")
await on_connect(bot, msg)
assert "#test:conn" in _pollers
_stop_poller("#test:conn")
assert "#test:conn" in _ps(bot)["pollers"]
_stop_poller(bot, "#test:conn")
await asyncio.sleep(0)
asyncio.run(inner())
@@ -1112,16 +1105,17 @@ class TestPollerManagement:
}
key = "#test:mgmt"
_save(bot, key, data)
_channels[key] = data
_ps(bot)["channels"][key] = data
async def inner():
_start_poller(bot, key)
assert key in _pollers
assert not _pollers[key].done()
_stop_poller(key)
ps = _ps(bot)
assert key in ps["pollers"]
assert not ps["pollers"][key].done()
_stop_poller(bot, key)
await asyncio.sleep(0)
assert key not in _pollers
assert key not in _channels
assert key not in ps["pollers"]
assert key not in ps["channels"]
asyncio.run(inner())
@@ -1135,21 +1129,23 @@ class TestPollerManagement:
}
key = "#test:idem"
_save(bot, key, data)
_channels[key] = data
_ps(bot)["channels"][key] = data
async def inner():
_start_poller(bot, key)
first = _pollers[key]
ps = _ps(bot)
first = ps["pollers"][key]
_start_poller(bot, key)
assert _pollers[key] is first
_stop_poller(key)
assert ps["pollers"][key] is first
_stop_poller(bot, key)
await asyncio.sleep(0)
asyncio.run(inner())
def test_stop_nonexistent(self):
_clear()
_stop_poller("#test:nonexistent")
bot = _FakeBot()
_stop_poller(bot, "#test:nonexistent")
# ---------------------------------------------------------------------------