Hermod
A cross-platform, modular and fully GDPR-compliant email archival solution!
Loading...
Searching...
No Matches
PluginRegistry.Delegation.cs
Go to the documentation of this file.
1using System;
2
4
5 using Core.Commands.Results;
6 using Core.Delegation;
7 using Core.Exceptions;
8 using System.Text.RegularExpressions;
9
10 partial class PluginRegistry {
11
15 internal Dictionary<string, List<IPlugin>> TopicSubscriptions { get; } = new Dictionary<string, List<IPlugin>>();
16
23 internal void AddSubscription(IPlugin plugin, string topic) {
24 if (!TopicIsValid(ref topic)) {
25 throw new MalformedTopicException(topic, "The topic is invalid!");
26 }
27
28 if (!TopicSubscriptions.ContainsKey(topic)) {
29 TopicSubscriptions.Add(topic, new List<IPlugin>());
30 }
31
32 var subList = TopicSubscriptions[topic];
33 if (subList.Contains(plugin)) { return; }
34
35 subList.Add(plugin);
36 }
37
44 internal void RemoveSubscription(IPlugin plugin, string topic) {
45 if (!TopicIsValid(ref topic)) {
46 throw new MalformedTopicException(topic, "The topic is invalid!");
47 }
48
49 if (!TopicSubscriptions.ContainsKey(topic)) { return; }
50
51 var subList = TopicSubscriptions[topic];
52 if (!subList.Contains(plugin)) { return; }
53
54 subList.Remove(plugin);
55 }
56
57 [GeneratedRegex("^[A-z0-9-_Ä-ü]+$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
58 public static partial Regex TopicRegex();
59
65 bool TopicIsValid(ref string topic) {
66 if (
67 string.IsNullOrEmpty(topic) ||
68 string.IsNullOrWhiteSpace(topic) ||
69 topic[0] != '/' ||
70 topic.EndsWith('/')
71 ) {
72 return false;
73 }
74
75 var splitTopic = topic.Split('/').Where(x => x != "/").Where(x => !TopicRegex().IsMatch(x));
76
77 return splitTopic.Count() > 0;
78 }
79
80 internal void OnMessagePublished(string topic, object? message) {
81 if (!TopicIsValid(ref topic)) { throw new MalformedTopicException(topic, "The given topic is invalid!"); }
82
83 if (!TopicSubscriptions.ContainsKey(topic)) {
84 TopicSubscriptions.Add(topic, new List<IPlugin>());
85 return; // this may change in the future.
86 }
87
88 var eventArgs = new MessageReceivedEventArgs(topic, message);
89 foreach (var subscriber in TopicSubscriptions[topic]) {
90 PluginDelegators.FirstOrDefault(p => p.Plugin == subscriber)?.OnMessageReceived(eventArgs);
91 }
92 }
93
94 internal ICommandResult ExecuteCommand(params string[] commands) {
95 throw new NotImplementedException();
96 }
97
98 }
99}
100
Exception class that is thrown when a topic for IPC is malformed.
Dictionary< string, List< IPlugin > > TopicSubscriptions
The topic subscription list.
void AddSubscription(IPlugin plugin, string topic)
Adds a plugin to a topic subscription list.
void RemoveSubscription(IPlugin plugin, string topic)
Removes a plugin from a topic subscription list.
List< PluginDelegator > PluginDelegators
Basic contract between Hermod and any plugins.
Definition: IPlugin.cs:14