1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
{
lib,
...
}:
{
name = "draupnir";
meta.maintainers = with lib.maintainers; [
RorySys
emilylange
];
nodes = {
homeserver =
{ pkgs, ... }:
{
services.matrix-synapse = {
enable = true;
log.root.level = "WARNING";
settings = {
database.name = "sqlite3";
registration_shared_secret = "supersecret-registration";
listeners = [
{
bind_addresses = [
"::"
];
port = 8008;
resources = [
{
compress = true;
names = [ "client" ];
}
{
compress = false;
names = [ "federation" ];
}
];
tls = false;
type = "http";
x_forwarded = false;
}
];
};
};
specialisation.draupnir = {
inheritParentConfig = true;
configuration.services.draupnir = {
enable = true;
settings = {
homeserverUrl = "http://localhost:8008";
managementRoom = "#moderators:homeserver";
};
secrets = {
accessToken = "/tmp/draupnir-access-token";
};
};
};
environment.systemPackages = with pkgs; [
curl
jq
(writers.writePython3Bin "test_draupnir_in_matrix"
{
libraries = [ python3Packages.matrix-nio ];
flakeIgnore = [ "E501" ];
}
''
import asyncio
import time
from nio import AsyncClient, MatrixRoom, RoomMemberEvent, RoomMessageNotice
async def main() -> None:
client = AsyncClient("http://localhost:8008", "moderator")
async def member_callback(room: MatrixRoom, event: RoomMemberEvent) -> None:
if event.membership == "join" and event.sender == "@draupnir:homeserver":
print("Got draupnir join event!")
async def message_callback(room: MatrixRoom, event: RoomMessageNotice) -> None:
print(f"{event.sender}:\n{event.body}" if "\n" in event.body else f"{event.sender}: {event.body}")
if event.sender == "@draupnir:homeserver" and "Startup complete." in event.body:
print("Draupnir reported ready, sending '!draupnir status'")
await client.room_send(
room_id=room.room_id,
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": "!draupnir status"
}
)
if event.sender == "@draupnir:homeserver" and "Repository: " in event.body and "Version: " in event.body:
print("Got status reply, creating policy list")
await client.room_send(
room_id=room.room_id,
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": "!draupnir list create test test"
}
)
time.sleep(3)
print("Surely it's done by now (couldnt find a good way to trigger the next step), protecting a room")
await client.room_send(
room_id=room.room_id,
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": "!draupnir rooms add #protected:homeserver"
}
)
time.sleep(3)
print("Surely it's done by now (there's no text reply), sending a ban command")
await client.room_send(
room_id=room.room_id,
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": "!draupnir ban @bad:homeserver test spam"
}
)
time.sleep(3)
print("Surely it's done by now (there's no text reply), sending a server ban command")
await client.room_send(
room_id=room.room_id,
message_type="m.room.message",
content={
"msgtype": "m.text",
"body": "!draupnir ban meow test spam"
}
)
time.sleep(3)
print("Surely it's done by now (there's no text reply), assuming all is good")
await client.close()
exit(0)
client.add_event_callback(member_callback, RoomMemberEvent)
client.add_event_callback(message_callback, RoomMessageNotice)
print(await client.login("password"))
room = await client.room_create(
name="Moderators",
alias="moderators",
invite=["@draupnir:homeserver"],
power_level_override={
"users": {
"@draupnir:homeserver": 100,
"@moderator:homeserver": 100,
}
}
)
print(room)
protectedRoom = await client.room_create(
name="Protected",
alias="protected",
invite=["@draupnir:homeserver"],
power_level_override={
"users": {
"@draupnir:homeserver": 100,
"@moderator:homeserver": 100,
}
}
)
print(protectedRoom)
print(await client.join(room.room_id))
await client.sync_forever(timeout=30000)
asyncio.run(main())
''
)
];
};
};
testScript =
{ nodes, ... }:
''
import json
homeserver.wait_for_unit("matrix-synapse.service")
homeserver.wait_until_succeeds("curl --fail -L http://localhost:8008/")
homeserver.succeed("matrix-synapse-register_new_matrix_user -u draupnir -p password --no-admin")
homeserver.succeed("matrix-synapse-register_new_matrix_user -u moderator -p password --no-admin")
# get draupnir access token
draupnir_login_payload = json.dumps({ "type": "m.login.password", "user": "draupnir", "password": "password" })
homeserver.succeed(
f"curl --fail --json '{draupnir_login_payload}' http://localhost:8008/_matrix/client/v3/login"
+ " | jq -r .access_token"
+ " | tee /tmp/draupnir-access-token"
)
homeserver.succeed("${nodes.homeserver.system.build.toplevel}/specialisation/draupnir/bin/switch-to-configuration test")
homeserver.wait_for_unit("draupnir.service")
print(homeserver.succeed("test_draupnir_in_matrix >&2", timeout=60))
'';
}
|