2using System.Collections.Generic;
4using System.Threading.Tasks;
28 private string host =
string.Empty;
29 private string userName =
string.Empty;
30 private string password =
string.Empty;
31 private string testMailRecipient =
string.Empty;
32 private string sender =
string.Empty;
33 private string[] relayDomains =
null;
34 private int port = 587;
35 private bool useRelayServer =
false;
39 [DefaultValueStringEmpty]
43 set => this.host = value;
50 set => this.port = value;
53 [DefaultValueStringEmpty]
54 public string UserName
57 set => this.userName = value;
60 [DefaultValueStringEmpty]
61 public string Password
64 set => this.password = value;
68 public bool UseRelayServer
70 get => this.useRelayServer;
71 set => this.useRelayServer = value;
74 [DefaultValueStringEmpty]
75 public string TestMailRecipient
77 get => this.testMailRecipient;
78 set => this.testMailRecipient = value;
81 [DefaultValueStringEmpty]
85 set => this.sender = value;
89 public string[] RelayDomains
91 get => this.relayDomains;
92 set => this.relayDomains = value;
95 public string RelayDomainsText
99 if (this.relayDomains is
null)
102 StringBuilder Result =
new StringBuilder();
104 foreach (
string Domain
in this.relayDomains)
105 Result.AppendLine(Domain);
107 return Result.ToString();
119 public override string Resource =>
"/Settings/Relay.md";
124 public override int Priority => 450;
142 this.userName, this.password, this.relayDomains,
true);
143 return Task.CompletedTask;
161 this.testMail = WebServer.
Register(
"/Settings/TestMail",
null, this.TestMail,
true,
false,
true);
163 return base.InitSetup(WebServer);
174 return base.UnregisterSetup(WebServer);
190 if (!(Obj is Dictionary<string, object> Form) ||
191 !Form.TryGetValue(
"useRelayServer", out Obj) || !(Obj is
bool UseRelayServer) ||
192 !Form.TryGetValue(
"testMailRecipient", out Obj) || !(Obj is
string TestMailRecipient) ||
193 !Form.TryGetValue(
"sender", out Obj) || !(Obj is
string Sender) ||
194 !Form.TryGetValue(
"relayDomains", out Obj) || !(Obj is
string RelayDomains) ||
195 !Form.TryGetValue(
"sendMail", out Obj) || !(Obj is
bool SendMail))
200 if (!Form.TryGetValue(
"hostName", out Obj) || !(Obj is
string HostName) ||
201 !Form.TryGetValue(
"portNumber", out Obj) || !(Obj is
int PortNumber) || PortNumber < 1 || PortNumber > 65535 ||
202 !Form.TryGetValue(
"userName", out Obj) || !(Obj is
string UserName) ||
203 !Form.TryGetValue(
"password", out Obj) || !(Obj is
string Password))
209 HostName =
string.Empty;
211 UserName =
string.Empty;
212 Password =
string.Empty;
216 string TabID = Request.
Header[
"X-TabID"];
217 if (
string.IsNullOrEmpty(TabID))
220 Response.StatusCode = 200;
222 this.useRelayServer = UseRelayServer;
223 this.host = HostName;
224 this.port = PortNumber;
225 this.userName = UserName;
226 this.password = Password;
227 this.testMailRecipient = TestMailRecipient;
228 this.sender = Sender;
229 this.relayDomains = RelayDomains.Trim().Split(
CommonTypes.
CRLF, StringSplitOptions.RemoveEmptyEntries);
233 Task _ = Task.Run(async () =>
239 string[] TabIDs =
new string[] { TabID };
240 string Message = await this.Test(TabIDs);
242 if (
string.IsNullOrEmpty(Message))
258 private async Task<string> Test(
string[] TabIDs)
263 if (
string.IsNullOrEmpty(DefaultDomain))
264 DefaultDomain =
"localhost";
266 string Sender = this.sender;
267 if (
string.IsNullOrEmpty(Sender))
270 await this.SendMessage(TabIDs, Sender, this.testMailRecipient,
273 "------------------\r\n\r\n" +
289 throw new ArgumentException(
"Invalid mail address: " + To, nameof(To));
297 if (this.useRelayServer)
301 UserName = this.userName;
302 Password = this.password;
307 if (Exchanges is
null || Exchanges.Length == 0)
308 throw new ArgumentException(
"No mail exchange at " + Domain +
".", nameof(To));
318 if (!(TabIDs is
null))
336 public RelayConfiguration Config;
337 public string[] TabIDs;
341 return ClientEvents.
PushEvent(this.TabIDs,
"ShowStatus",
"Rx: <" + Data.Length.ToString() +
" bytes>",
false,
"User");
346 return ClientEvents.
PushEvent(this.TabIDs,
"ShowStatus",
"Tx: <" + Data.Length.ToString() +
" bytes>",
false,
"User");
349 public override Task
ReceiveText(DateTime _,
string Text)
354 public override Task
TransmitText(DateTime _,
string Text)
359 public override Task
Information(DateTime _,
string Comment)
364 public override Task
Warning(DateTime _,
string Warning)
369 public override Task
Error(DateTime _,
string Error)
374 public override Task
Exception(DateTime _,
string Exception)
386 return Task.FromResult(
true);
435 if (!
string.IsNullOrEmpty(Value))
436 this.relayDomains = Value.Split(
',');
439 if (!
string.IsNullOrEmpty(Value))
442 if (this.useRelayServer)
456 Value = await this.Test(
null);
457 if (!
string.IsNullOrEmpty(Value))
Helps with parsing of commong data types.
static readonly char[] CRLF
Contains the CR LF character sequence.
Helps with common XML-related tasks.
static string HtmlValueEncode(string s)
Differs from Encode(String), in that it does not encode the aposotrophe or the quote.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
The ClientEvents class allows applications to push information asynchronously to web clients connecte...
static Task< int > PushEvent(string[] TabIDs, string Type, object Data)
Puses an event to a set of Tabs, given their Tab IDs.
Static class managing the runtime environment of the IoT Gateway.
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
static int NextInteger(int Max)
Returns a non-negative random integer that is less than the specified maximum.
static DomainConfiguration Instance
Current instance of configuration.
string Domain
Principal domain name
void LogEnvironmentError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value contains an error.
bool TryGetEnvironmentVariable(string VariableName, bool Required, out string Value)
Tries to get a string-valued environment variable.
Abstract base class for multi-step system configurations.
static XmppConfiguration Instance
Current instance of configuration.
virtual void Add(ISniffer Sniffer)
ICommunicationLayer.Add
DNS resolver, as defined in:
static async Task< string[]> LookupMailExchange(string DomainName)
Looks up the Mail Exchanges related to a given domain name.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
HttpRequestHeader Header
Request header.
bool HasData
If the request has data.
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Base class for all HTTP resources.
Represets a response of an HTTP client request.
Implements an HTTP server.
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Implements a simple SMTP Server, as defined in:
static async Task< string > GetSalutation(string DefaultDomain)
Gets the proper salutation name for the server.
void SetRelaySettings(bool UseRelayServer, string HostName, int PortNumber, string UserName, string Password, string[] RelayDomains, bool LockSettings)
Sets mail relay settings.
const int DefaultSmtpPort
Default SMTP Port (25).
async Task Connect()
Connects to the server.
async Task< string > EHLO(string Domain)
Sends the EHLO command.
async Task QUIT()
Executes the QUIT command.
async Task SendFormattedEMail(string Sender, string Recipient, string Subject, string MarkdownContent, params object[] Attachments)
Sends a formatted e-mail message.
Abstract base class for sniffers. Implements default method overloads.
virtual Task TransmitBinary(byte[] Data)
Called when binary data has been transmitted.
virtual Task Information(string Comment)
Called to inform the viewer of something.
virtual Task Error(string Error)
Called to inform the viewer of an error state.
virtual Task Exception(string Exception)
Called to inform the viewer of an exception state.
virtual Task ReceiveText(string Text)
Called when text has been received.
virtual Task Warning(string Warning)
Called to inform the viewer of a warning state.
virtual Task ReceiveBinary(byte[] Data)
Called when binary data has been received.
virtual Task TransmitText(string Text)
Called when text has been transmitted.
A sniffer that redirects incoming events to another sniffable object.
Represents a case-insensitive string.
int IndexOf(CaseInsensitiveString value, StringComparison comparisonType)
Reports the zero-based index of the first occurrence of the specified string in the current System....
CaseInsensitiveString Trim()
Removes all leading and trailing white-space characters from the current CaseInsensitiveString object...
CaseInsensitiveString Substring(int startIndex, int length)
Retrieves a substring from this instance. The substring starts at a specified character position and ...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static async Task Update(object Object)
Updates an object in the database.
Contains information about a language.
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Provides the user configuration options regarding use of SMTP Relay server to send mail.
override void SetStaticInstance(ISystemConfiguration Configuration)
Sets the static instance of the configuration.
override Task< bool > SimplifiedConfiguration()
Simplified configuration by configuring simple default values.
const string BROKER_RELAY_PORT
Port number to use when connecting relay server.
override async Task< bool > EnvironmentConfiguration()
Environment configuration by configuring values available in environment variables.
const string BROKER_RELAY_USE
If an SMTP relay server is to be used (true or 1), or if the broker should connect to the recipient m...
static RelayConfiguration Instance
Current instance of configuration.
override Task InitSetup(HttpServer WebServer)
Initializes the setup object.
override Task ConfigureSystem()
Is called during startup to configure the system.
const string BROKER_RELAY_PASSWORD
Password of account when authenticating access to the relay server.
const string BROKER_RELAY_HOST
Host(or domain) or the SMTP Relay server.
override Task UnregisterSetup(HttpServer WebServer)
Unregisters the setup object.
const string BROKER_RELAY_DOMAINS
Optional Comma-separated list of domain names for which the broker can act as an SMTP relay.
override string Resource
Resource to be redirected to, to perform the configuration.
const string BROKER_RELAY_USER
User account in the relay server.
override string ConfigPrivilege
Minimum required privilege for a user to be allowed to change the configuration defined by the class.
const string BROKER_RELAY_SENDER
Default sender of mail messages from broker.
override Task< string > Title(Language Language)
Gets a title for the system configuration.
Service Module hosting the XMPP broker and its components.
Interface for system configurations. The gateway will scan all module for system configuration classe...