1 import ../make-test-python.nix ({ pkgs, ... }:
3 homeserverUrl = "http://homeserver:8008";
6 name = "matrix-appservice-irc";
8 maintainers = pkgs.matrix-appservice-irc.meta.maintainers;
12 homeserver = { pkgs, ... }: {
13 # We'll switch to this once the config is copied into place
14 specialisation.running.configuration = {
15 services.matrix-synapse = {
18 database.name = "sqlite3";
19 app_service_config_files = [ "/registration.yml" ];
21 enable_registration = true;
23 # don't use this in production, always use some form of verification
24 enable_registration_without_verification = true;
27 # The default but tls=false
34 "names" = [ "client" ];
37 "names" = [ "federation" ];
45 networking.firewall.allowedTCPPorts = [ 8008 ];
49 ircd = { pkgs, ... }: {
55 Info = Server Info Text
66 networking.firewall.allowedTCPPorts = [ 6667 ];
69 appservice = { pkgs, ... }: {
70 services.matrix-appservice-irc = {
72 registrationUrl = "http://appservice:8009";
75 homeserver.url = homeserverUrl;
76 homeserver.domain = "homeserver";
78 ircService.servers."ircd" = {
83 aliasTemplate = "#irc_$CHANNEL";
89 networking.firewall.allowedTCPPorts = [ 8009 ];
92 client = { pkgs, ... }: {
93 environment.systemPackages = [
94 (pkgs.writers.writePython3Bin "do_test"
96 libraries = [ pkgs.python3Packages.matrix-nio ];
98 # We don't live in the dark ages anymore.
99 # Languages like Python that are whitespace heavy will overrun
107 from time import sleep
110 from nio import AsyncClient, RoomMessageText, JoinResponse
113 async def matrix_room_message_text_callback(matrix: AsyncClient, msg: str, _r, e):
114 print("Received matrix text message: ", e)
116 print("Received hi from IRC")
118 exit(0) # Actual exit point
123 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
124 sock.connect(("ircd", 6667))
125 sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
126 sock.send(b"USER bob bob bob :bob\n")
127 sock.send(b"NICK bob\n")
130 def join(self, room: str):
131 self.sock.send(f"JOIN {room}\n".encode())
133 def privmsg(self, room: str, msg: str):
134 self.sock.send(f"PRIVMSG {room} :{msg}\n".encode())
136 def expect_msg(self, body: str):
139 buf = self.sock.recv(1024).decode()
145 async def run(homeserver: str):
148 matrix = AsyncClient(homeserver)
149 response = await matrix.register("alice", "foobar")
150 print("Matrix register response: ", response)
152 response = await matrix.join("#irc_#test:homeserver")
153 print("Matrix join room response:", response)
154 assert isinstance(response, JoinResponse)
155 room_id = response.room_id
158 # FIXME: what are we waiting on here? Matrix? IRC? Both?
159 # 10s seem bad for busy hydra machines.
163 print("Sending text message to matrix room")
164 response = await matrix.room_send(
166 message_type="m.room.message",
167 content={"msgtype": "m.text", "body": "hi from matrix"},
169 print("Matrix room send response: ", response)
170 irc.privmsg("#test", "hi from irc")
172 print("Waiting for the matrix message to appear on the IRC side...")
173 irc.expect_msg("hi from matrix")
175 callback = functools.partial(
176 matrix_room_message_text_callback, matrix, "hi from irc"
178 matrix.add_event_callback(callback, RoomMessageText)
180 print("Waiting for matrix message...")
181 await matrix.sync_forever()
183 exit(1) # Unreachable
186 if __name__ == "__main__":
187 asyncio.run(run(sys.argv[1]))
200 ircd.wait_for_unit("ngircd.service")
201 ircd.wait_for_open_port(6667)
203 with subtest("start the appservice"):
204 appservice.wait_for_unit("matrix-appservice-irc.service")
205 appservice.wait_for_open_port(8009)
207 with subtest("copy the registration file"):
208 appservice.copy_from_vm("/var/lib/matrix-appservice-irc/registration.yml")
209 homeserver.copy_from_host(
210 str(pathlib.Path(os.environ.get("out", os.getcwd())) / "registration.yml"), "/"
212 homeserver.succeed("chmod 444 /registration.yml")
214 with subtest("start the homeserver"):
216 "/run/current-system/specialisation/running/bin/switch-to-configuration test >&2"
219 homeserver.wait_for_unit("matrix-synapse.service")
220 homeserver.wait_for_open_port(8008)
222 with subtest("ensure messages can be exchanged"):
223 client.succeed("do_test ${homeserverUrl} >&2")