Hermod
A cross-platform, modular and fully GDPR-compliant email archival solution!
Loading...
Searching...
No Matches
PluginDelegator.cs
Go to the documentation of this file.
1using System;
2
4
5 using Config;
6 using Core.Commands.Results;
7 using Core.Delegation;
8 using Serilog;
9
14
15 internal IPlugin Plugin { get; }
16 internal ILogger? Logger { get; }
17
22 public PluginDelegator(IPlugin plugin, ILogger? logger = null) {
23 Plugin = plugin;
24 Logger = logger;
25 }
26
27 #region IMessageReceived
30
31 internal void OnMessageReceived(MessageReceivedEventArgs e) => MessageReceived?.Invoke(this, e);
32 #endregion
33
35 public void SubscribeTopic(string topicName) => PluginRegistry.Instance.AddSubscription(Plugin, topicName);
36
38 public void SubscribeTopics(params string[] topics) {
39 foreach (var topic in topics) {
40 try { PluginRegistry.Instance.AddSubscription(Plugin, topic); } catch (Exception ex) {
41 Error($"Subscription failed: {ex.Message}");
42 Debug($"Stacktrace: {ex.StackTrace}");
43 }
44 }
45 }
46
48 public void UnsubscribeTopic(string topicName) => PluginRegistry.Instance.RemoveSubscription(Plugin, topicName);
49
51 public void PublishMessage(string topic, object? message) => PluginRegistry.Instance.OnMessagePublished(topic, message);
52
54 public ICommandResult ExecuteCommand(params string[] command) => PluginRegistry.Instance.ExecuteCommand(command);
55
57 public void Information(string? msg) => Logger?.Information($"[{ Plugin.PluginName }] {msg}");
58
60 public void Debug(string? msg) => Logger?.Debug($"[{ Plugin.PluginName }] {msg}");
61
63 public void Error(string? msg) => Logger?.Error($"[{ Plugin.PluginName }] {msg}");
64
66 public void Warning(string? msg) => Logger?.Warning($"[{ Plugin.PluginName }] {msg}");
67
69 public void Trace(string? msg) => Logger?.Verbose($"[{ Plugin.PluginName }] {msg}");
70
72 public T GetApplicationConfig<T>(string config) => ConfigManager.Instance.GetConfig<T>(config);
73
75 public bool TryGetApplicationConfig<T>(string config, out T? value) {
76 try {
77 value = GetApplicationConfig<T>(config);
78 } catch {
79 value = default;
80 return false;
81 }
82
83 return true;
84 }
85 }
86}
87
Provides an application-wide, thread safe way of getting and setting configurations relevant to the m...
static ConfigManager Instance
Gets the application-wide instance of the ConfigManager.
EventArgs-derived class containing the event arguments for when a message was received on a given top...
Custom logger class for Hermod; internally uses Serilog.
Definition: Logger.cs:12
Allows delegating topics and command execution requests from plugins through Hermod to other plugins.
void SubscribeTopic(string topicName)
Allows a plugin to subscribe to an individual topic.
void Information(string? msg)
Logs an information message to the logger.This will prefix the message with [name of plugin].
PluginDelegator(IPlugin plugin, ILogger? logger=null)
Instantiates a new instance of this class.
ICommandResult ExecuteCommand(params string[] command)
Executes a single command.If command execution has been disabled for this plugin, then a Exceptions....
void Trace(string? msg)
Logs a trace message to the logger.This will prefix the message with [name of plugin].
void Debug(string? msg)
Logs a debug message to the logger.This will prefix the message with [name of plugin].
T GetApplicationConfig< T >(string config)
Retrieves an application configuration value.The desired configuration.
void OnMessageReceived(MessageReceivedEventArgs e)
void Error(string? msg)
Logs an error message to the logger.This will prefix the message with [name of plugin].
void SubscribeTopics(params string[] topics)
Allows a plugin to subscribe to multiple individual topics.This method will fail silently and ignore ...
bool TryGetApplicationConfig< T >(string config, out T? value)
Tries to retrieve an application configuration value.true if retrieving the configuration was success...
void PublishMessage(string topic, object? message)
Allows a plugin to publish a message on a given topic.
void Warning(string? msg)
Logs a warning message to the logger.This will prefix the message with [name of plugin].
MessageReceivedEventHandler? MessageReceived
void UnsubscribeTopic(string topicName)
Allows a plugin to unsubscribe from a single topic.
An abstract class for plugins with all the main features already implemented.
Definition: Plugin.cs:13
Handles the loading, unloading, and general management of plugins.
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.
static PluginRegistry Instance
Gets the current instance of this object.
A generic, object-based variation of ICommandResult<T>.
Basic contract between Hermod and any laoded plugins which allows jobs to be delegated to other plugi...
Basic contract between Hermod and any plugins.
Definition: IPlugin.cs:14
delegate void MessageReceivedEventHandler(object? sender, MessageReceivedEventArgs e)
Event handler delegate for the MessageReceived event.