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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
using ArcaneLibs.Extensions;
using LibMatrix.EventTypes.Common;
using LibMatrix.EventTypes.Spec.State.RoomInfo;
using LibMatrix.Helpers;
using LibMatrix.Responses;
using LibMatrix.RoomTypes;
using LibMatrix.Utilities.Bot.Interfaces;
namespace MiniUtils.Commands;
public class MakeRoomCommand() : ICommand {
public string Name => "make room";
public string[]? Aliases => ["makeroom", "create room", "createroom"];
public string Description => "Make a new room";
public bool Unlisted => false;
public async Task Invoke(CommandContext ctx) {
if (ctx.Args.Length == 0) {
await ctx.Room.SendMessageEventAsync(
new MessageBuilder()
.WithTable(tb => {
tb.WithTitle("~create room", 3);
tb.WithRow(rb => {
rb.WithCell("Argument")
.WithCell("Alternatives")
.WithCell("Description");
});
tb.WithRow(rb => {
rb.WithCell("--alias <localpart>")
.WithCell("")
.WithCell("Set the room alias");
});
tb.WithRow(rb => {
rb.WithCell("--avatar-url <url>")
.WithCell("")
.WithCell("Set the room avatar URL");
});
tb.WithRow(rb => {
rb.WithCell("--copy-avatar [room]")
.WithCell("")
.WithCell("Copy the avatar from another room (or current room if unspecified)");
});
tb.WithRow(rb => {
rb.WithCell("--copy-powerlevels [room]")
.WithCell("")
.WithCell("Copy the power levels from another room (or current room if unspecified)");
});
tb.WithRow(rb => {
rb.WithCell("--invite-admin <user>")
.WithCell("")
.WithCell("Invite a user as an admin");
});
tb.WithRow(rb => {
rb.WithCell("--invite <user>")
.WithCell("")
.WithCell("Invite a user");
});
tb.WithRow(rb => {
rb.WithCell("--name <name>")
.WithCell("")
.WithCell("Set the room name");
});
tb.WithRow(rb => {
rb.WithCell("--topic <topic>")
.WithCell("")
.WithCell("Set the room topic");
});
tb.WithRow(rb => {
rb.WithCell("--federate <true|false>")
.WithCell("")
.WithCell("Set whether the room is federatable");
});
tb.WithRow(rb => {
rb.WithCell("--join-rule <rule>")
.WithCell("""
--public
--invite-only
--knock
--restricted
--knock_restricted
--private
""")
.WithCell("Set the room join rule to public, invite-only, knock, restricted, knock-restricted or private");
});
tb.WithRow(rb => {
rb.WithCell("--history-visibility <visibility>")
.WithCell("""
--shared
--invited
--joined
--world_readable
""")
.WithCell("Set the room history visibility to shared, invited, joined or world_readable");
});
})
.Build()
);
return;
}
var rb = new RoomBuilder() { };
for (int i = 0; i < ctx.Args.Length; i++) {
switch (ctx.Args[i]) {
case "--alias":
rb.AliasLocalPart = ctx.Args[++i];
break;
case "--avatar-url":
rb.Avatar!.Url = ctx.Args[++i];
break;
case "--copy-avatar": {
var room = await GetRoomByArgument(ctx, ctx.Args[i + 1]);
if (room != ctx.Room) i++;
rb.Avatar = await room.GetAvatarUrlAsync() ?? throw new ArgumentException($"Room {room.RoomId} does not have an avatar");
break;
}
case "--copy-powerlevels": {
var room = await GetRoomByArgument(ctx, ctx.Args[i + 1]);
if (room != ctx.Room) i++;
rb.PowerLevels = await room.GetPowerLevelsAsync() ?? throw new ArgumentException($"Room {room.RoomId} does not have power levels???");
break;
}
case "--invite-admin":
var inviteAdmin = ctx.Args[++i];
if (!inviteAdmin.StartsWith('@')) {
throw new ArgumentException("Invalid user reference: " + inviteAdmin);
}
rb.Invites.Add(inviteAdmin, "Marked explicitly as admin to be invited");
break;
case "--invite":
var inviteUser = ctx.Args[++i];
if (!inviteUser.StartsWith('@')) {
throw new ArgumentException("Invalid user reference: " + inviteUser);
}
rb.Invites.Add(inviteUser, "Marked explicitly to be invited");
break;
case "--name":
var nameEvt = rb.Name = new() { Name = "" };
while (i + 1 < ctx.Args.Length && !ctx.Args[i + 1].StartsWith("--")) {
nameEvt.Name += (nameEvt.Name.Length > 0 ? " " : "") + ctx.Args[++i];
}
break;
case "--topic":
var topicEvt = rb.Topic = new() { Topic = "" };
while (i + 1 < ctx.Args.Length && !ctx.Args[i + 1].StartsWith("--")) {
topicEvt.Topic += (topicEvt.Topic.Length > 0 ? " " : "") + ctx.Args[++i];
}
break;
case "--federate":
rb.IsFederatable = bool.Parse(ctx.Args[++i]);
break;
case "--public":
case "--invite-only":
case "--knock":
case "--restricted":
case "--knock_restricted":
case "--private":
rb.JoinRules.JoinRule = ctx.Args[i].Replace("--", "").ToLowerInvariant() switch {
"public" => RoomJoinRulesEventContent.JoinRules.Public,
"invite-only" => RoomJoinRulesEventContent.JoinRules.Invite,
"knock" => RoomJoinRulesEventContent.JoinRules.Knock,
"restricted" => RoomJoinRulesEventContent.JoinRules.Restricted,
"knock_restricted" => RoomJoinRulesEventContent.JoinRules.KnockRestricted,
"private" => RoomJoinRulesEventContent.JoinRules.Private,
_ => throw new ArgumentException("Unknown join rule: " + ctx.Args[i])
};
break;
case "--join-rule":
if (i + 1 >= ctx.Args.Length || !ctx.Args[i + 1].StartsWith("--")) {
throw new ArgumentException("Expected join rule after --join-rule");
}
rb.JoinRules.JoinRule = ctx.Args[++i].ToLowerInvariant() switch {
"public" => RoomJoinRulesEventContent.JoinRules.Public,
"invite" => RoomJoinRulesEventContent.JoinRules.Invite,
"knock" => RoomJoinRulesEventContent.JoinRules.Knock,
"restricted" => RoomJoinRulesEventContent.JoinRules.Restricted,
"knock_restricted" => RoomJoinRulesEventContent.JoinRules.KnockRestricted,
"private" => RoomJoinRulesEventContent.JoinRules.Private,
_ => throw new ArgumentException("Unknown join rule: " + ctx.Args[i])
};
break;
case "--history-visibility":
rb.HistoryVisibility = new RoomHistoryVisibilityEventContent {
HistoryVisibility = ctx.Args[++i].ToLowerInvariant() switch {
"shared" => RoomHistoryVisibilityEventContent.HistoryVisibilityTypes.Shared,
"invited" => RoomHistoryVisibilityEventContent.HistoryVisibilityTypes.Invited,
"joined" => RoomHistoryVisibilityEventContent.HistoryVisibilityTypes.Joined,
"world_readable" => RoomHistoryVisibilityEventContent.HistoryVisibilityTypes.WorldReadable,
_ => throw new ArgumentException("Unknown history visibility: " + ctx.Args[i])
}
};
break;
default:
throw new ArgumentException("Unknown argument: " + ctx.Args[i]);
}
}
// await ctx.Room.SendMessageEventAsync(
// new MessageBuilder()
// .WithCodeBlock(rb.ToJson(), "json")
// .Build()
// );
// var result = await ctx.Homeserver.CreateRoom(creationContent);
var result = await rb.Create(ctx.Homeserver);
await ctx.Room.SendMessageEventAsync(new MessageBuilder()
.WithMention($"{result.RoomId}?via={ctx.Homeserver.ServerName}", rb.CanonicalAlias.Alias)
.Build());
}
private async Task<GenericRoom> GetRoomByArgument(CommandContext ctx, string roomReference, bool defaultToCurrent = true) {
if (roomReference.StartsWith("--")) {
return defaultToCurrent ? ctx.Room : throw new ArgumentException("Invalid room reference: " + roomReference);
}
if (roomReference.StartsWith('!')) {
return ctx.Homeserver.GetRoom(roomReference);
}
if (roomReference.StartsWith('#')) {
var resolvedAlias = await ctx.Homeserver.ResolveRoomAliasAsync(roomReference);
return ctx.Homeserver.GetRoom(resolvedAlias.RoomId);
}
throw new ArgumentException("Invalid room reference: " + roomReference);
}
}
|