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
|
using System.Diagnostics;
using System.Text.Json.Nodes;
using ArcaneLibs.Attributes;
using ArcaneLibs.Extensions;
using LibMatrix;
using LibMatrix.EventTypes.Spec;
using LibMatrix.EventTypes.Spec.State.Policy;
using LibMatrix.Helpers;
using LibMatrix.Homeservers;
using LibMatrix.RoomTypes;
using LibMatrix.Utilities.Bot.Interfaces;
using MatrixAntiDmSpam.Core.Classes;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace MatrixAntiDmSpam.Core;
public class PolicyExecutor(
ILogger<PolicyExecutor> logger,
AntiDmSpamConfiguration config,
RoomInviteHandler roomInviteHandler,
PolicyStore policyStore,
AuthenticatedHomeserverGeneric homeserver) : IHostedService {
private readonly GenericRoom? _logRoom = string.IsNullOrWhiteSpace(config.LogRoom) ? null : homeserver.GetRoom(config.LogRoom);
public async Task StartAsync(CancellationToken cancellationToken) {
roomInviteHandler.OnInviteReceived.Add(CheckPoliciesAgainstInvite);
policyStore.OnPolicyAdded.Add(CheckPolicyAgainstOutstandingInvites);
if (config.IgnoreBannedUsers) {
var ignoreList = await homeserver.GetAccountDataOrNullAsync<IgnoredUserListEventContent>(IgnoredUserListEventContent.EventId);
if (ignoreList != null) {
ignoreList.IgnoredUsers.RemoveAll((id, meta) => {
if (meta.AdditionalData?.ContainsKey(MadsIgnoreMetadataContent.EventId) ?? false) {
var metadata = meta.GetAdditionalData<JsonObject>(MadsIgnoreMetadataContent.EventId);
if (metadata?["was_user_added"]?.GetValue<bool>() ?? false) {
return true;
}
}
return false;
});
}
policyStore.OnPoliciesChanged.Add(UpdateIgnoreList);
}
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
#region Feature: Manage ignore list
private async Task UpdateIgnoreList((
List<StateEventResponse> NewPolicies,
List<(StateEventResponse Old, StateEventResponse New)> UpdatedPolicies,
List<(StateEventResponse Old, StateEventResponse New)> RemovedPolicies) updates
) {
var ignoreListContent = await homeserver.GetIgnoredUserListAsync();
foreach (var newEvent in updates.NewPolicies) {
var content = newEvent.TypedContent as PolicyRuleEventContent;
if (content.Entity is null || content.IsGlobRule()) continue;
if (content.GetNormalizedRecommendation() != "m.ban") continue;
var policyEventReference = new MadsIgnoreMetadataContent.PolicyEventReference() {
Type = newEvent.Type,
RoomId = newEvent.RoomId ?? throw new InvalidOperationException("RoomId is null"),
StateKey = newEvent.StateKey!
};
if (ignoreListContent.IgnoredUsers.TryGetValue(content.Entity, out var existingRule)) {
if (existingRule.AdditionalData?.ContainsKey(MadsIgnoreMetadataContent.EventId) ?? false) {
var existingMetadata = existingRule.GetAdditionalData<MadsIgnoreMetadataContent>(MadsIgnoreMetadataContent.EventId);
existingMetadata.Policies.Add(policyEventReference);
}
else {
existingRule.AdditionalData ??= new();
existingRule.AdditionalData.Add(MadsIgnoreMetadataContent.EventId, new MadsIgnoreMetadataContent {
WasUserAdded = true,
Policies = [policyEventReference]
});
}
}
else {
ignoreListContent.IgnoredUsers[content.Entity] = new() {
AdditionalData = new() {
[MadsIgnoreMetadataContent.EventId] = new MadsIgnoreMetadataContent {
WasUserAdded = false,
Policies = [policyEventReference]
}
}
};
}
}
foreach (var (previousEvent, newEvent) in updates.RemovedPolicies) {
if (previousEvent.Type != UserPolicyRuleEventContent.EventId) continue;
var previousContent = previousEvent.ContentAs<UserPolicyRuleEventContent>();
if (previousContent.Entity is null || previousContent.IsGlobRule()) continue;
if (previousContent.GetNormalizedRecommendation() != "m.ban") continue;
var ignoreList = await homeserver.GetIgnoredUserListAsync();
if (ignoreList.IgnoredUsers.TryGetValue(previousContent.Entity, out var existingRule)) {
if (existingRule.AdditionalData?.ContainsKey(MadsIgnoreMetadataContent.EventId) ?? false) {
var existingMetadata = existingRule.GetAdditionalData<MadsIgnoreMetadataContent>(MadsIgnoreMetadataContent.EventId);
existingMetadata.Policies.RemoveAll(x => x.Type == previousEvent.Type && x.RoomId == previousEvent.RoomId && x.StateKey == previousEvent.StateKey);
if (!existingMetadata.WasUserAdded)
ignoreList.IgnoredUsers.Remove(previousContent.Entity);
}
}
}
await homeserver.SetAccountDataAsync(IgnoredUserListEventContent.EventId, ignoreListContent);
}
private async Task<IgnoredUserListEventContent> TryRecoverIgnoreList(IgnoredUserListEventContent content) {
}
#endregion
#region Feature: Report blocked invites
#endregion
#region Feature: Reject invites
private Task CheckPoliciesAgainstInvite(RoomInviteContext invite) {
logger.LogInformation("Checking policies against invite");
var sw = Stopwatch.StartNew();
// Technically not required, but helps with scaling against millions of policies
Parallel.ForEach(policyStore.AllPolicies.Values, (policy, loopState, idx) => {
if (CheckPolicyAgainstInvite(invite, policy) is not null) {
logger.LogInformation("Found matching policy after {} iterations ({})", idx, sw.Elapsed);
loopState.Break();
}
});
return Task.CompletedTask;
}
private async Task CheckPolicyAgainstOutstandingInvites(StateEventResponse newEvent) {
var tasks = roomInviteHandler.Invites
.Select(invite => CheckPolicyAgainstInvite(invite, newEvent))
.Where(x => x is not null)
.Cast<Task>() // from Task?
.ToList();
await Task.WhenAll(tasks);
}
private Task? CheckPolicyAgainstInvite(RoomInviteContext invite, StateEventResponse policyEvent) {
var policy = policyEvent.TypedContent as PolicyRuleEventContent ?? throw new InvalidOperationException("Policy is null");
if (policy.Recommendation != "m.ban") return null;
var policyMatches = false;
switch (policy) {
case UserPolicyRuleEventContent userPolicy:
policyMatches = userPolicy.EntityMatches(invite.MemberEvent.Sender!);
break;
case ServerPolicyRuleEventContent serverPolicy:
policyMatches = serverPolicy.EntityMatches(invite.MemberEvent.Sender!);
break;
case RoomPolicyRuleEventContent roomPolicy:
policyMatches = roomPolicy.EntityMatches(invite.RoomId);
break;
default:
if (_logRoom is not null)
_ = _logRoom.SendMessageEventAsync(new MessageBuilder().WithColoredBody("#FF0000", "Unknown policy type " + policy.GetType().FullName).Build());
break;
}
if (!policyMatches) return null;
logger.LogWarning("[{}] Rejecting invite to {}, matching {} {}", homeserver.WhoAmI.UserId, invite.RoomId, policy.GetType().GetFriendlyName(), policy.ToJson(ignoreNull: true));
return Task.Run(async () => {
if (_logRoom is not null) {
string roomName = await invite.TryGetRoomNameAsync();
await roomInviteHandler.RejectInvite(invite, new MessageBuilder()
.WithColoredBody("#FF0000",
cb => cb.WithBody("Rejecting invite to ").WithMention(invite.RoomId, roomName)
.WithBody($", matching {policy.GetType().GetFriendlyName().ToLowerInvariant()}.")
.WithNewline())
.WithCollapsibleSection("Policy JSON", cb => cb.WithCodeBlock(policy.ToJson(ignoreNull: true), "json"))
);
}
});
}
#endregion
}
|