Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NotificationConfiguration.cs
1using System;
2using System.Collections.Generic;
3using System.Net.Mail;
4using System.Text;
5using System.Threading.Tasks;
12
14{
19 {
20 private static NotificationConfiguration instance = null;
21 private HttpResource testAddresses = null;
22
23 private CaseInsensitiveString[] addresses = new CaseInsensitiveString[0];
24
29 : base()
30 {
31 }
32
36 public static NotificationConfiguration Instance => instance;
37
41 [DefaultValueNull]
43 {
44 get => this.addresses;
45 set => this.addresses = value;
46 }
47
51 public string AddressesString
52 {
53 get
54 {
55 StringBuilder sb = new StringBuilder();
56 bool First = true;
57
58 foreach (string s in this.addresses)
59 {
60 if (First)
61 First = false;
62 else
63 sb.Append("; ");
64
65 sb.Append(s);
66 }
67
68 return sb.ToString();
69 }
70 }
71
75 public override string Resource => "/Settings/Notification.md";
76
80 public override int Priority => 600;
81
87 public override Task<string> Title(Language Language)
88 {
89 return Language.GetStringAsync(typeof(Gateway), 2, "Notification");
90 }
91
95 public override Task ConfigureSystem()
96 {
97 return Task.CompletedTask;
98 }
99
104 public override void SetStaticInstance(ISystemConfiguration Configuration)
105 {
106 instance = Configuration as NotificationConfiguration;
107 }
108
113 public override Task InitSetup(HttpServer WebServer)
114 {
115 this.testAddresses = WebServer.Register("/Settings/TestNotificationAddresses", null, this.TestNotificationAddresses, true, false, true);
116
117 return base.InitSetup(WebServer);
118 }
119
124 public override Task UnregisterSetup(HttpServer WebServer)
125 {
126 WebServer.Unregister(this.testAddresses);
127
128 return base.UnregisterSetup(WebServer);
129 }
130
134 protected override string ConfigPrivilege => "Admin.Communication.Notification";
135
136 private async Task TestNotificationAddresses(HttpRequest Request, HttpResponse Response)
137 {
138 Gateway.AssertUserAuthenticated(Request, this.ConfigPrivilege);
139
140 if (!Request.HasData)
141 throw new BadRequestException();
142
143 object Obj = await Request.DecodeDataAsync();
144 if (!(Obj is string Address))
145 throw new BadRequestException();
146
147 string TabID = Request.Header["X-TabID"];
148
149 List<CaseInsensitiveString> Addresses = new List<CaseInsensitiveString>();
150
151 Response.StatusCode = 200;
152
153 try
154 {
155 foreach (string Part in Address.Split(';'))
156 {
157 string s = Part.Trim();
158 if (string.IsNullOrEmpty(s))
159 continue;
160
161 if (string.Compare(s, Gateway.XmppClient.BareJID, true) == 0)
162 continue;
163
164 MailAddress Addr = new MailAddress(s);
165 Addresses.Add(Addr.Address);
166 }
167
168 this.addresses = Addresses.ToArray();
169
170 await Database.Update(this);
171
172 if (this.addresses.Length > 0)
173 {
174 await Gateway.SendNotification("Test\r\n===========\r\n\r\nThis message was generated to test the notification feature of **" +
176 }
177
178 if (!string.IsNullOrEmpty(TabID))
179 await Response.Write(1);
180 }
181 catch (Exception ex)
182 {
183 if (!string.IsNullOrEmpty(TabID))
184 await Response.Write(0);
185 else
186 throw new BadRequestException(ex.Message);
187 }
188
189 await Response.SendResponse();
190 }
191
196 public override Task<bool> SimplifiedConfiguration()
197 {
198 return Task.FromResult(true);
199 }
200
205
210 public override Task<bool> EnvironmentConfiguration()
211 {
212 CaseInsensitiveString Value = Environment.GetEnvironmentVariable(GATEWAY_NOTIFICATION_JIDS);
214 return Task.FromResult(false);
215
216 CaseInsensitiveString[] Jids = Value.Split(',');
217 foreach (CaseInsensitiveString Jid in Jids)
218 {
219 if (!XmppClient.BareJidRegEx.IsMatch(Jid))
220 {
221 this.LogEnvironmentError("Invalid JID.", GATEWAY_NOTIFICATION_JIDS, Jid);
222 return Task.FromResult(false);
223 }
224 }
225
226 this.addresses = Jids;
227
228 return Task.FromResult(true);
229 }
230
231 }
232}
Contains a markdown document. This markdown document class supports original markdown,...
static string Encode(string s)
Encodes all special characters in a string so that it can be included in a markdown document without ...
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static IUser AssertUserAuthenticated(HttpRequest Request, string Privilege)
Makes sure a request is being made from a session with a successful user login.
Definition: Gateway.cs:3041
static Task SendNotification(Graph Graph)
Sends a graph as a notification message to configured notification recipients.
Definition: Gateway.cs:3826
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:3187
static string ApplicationName
Application Name.
Definition: Gateway.cs:2390
override Task< bool > SimplifiedConfiguration()
Simplified configuration by configuring simple default values.
override Task ConfigureSystem()
Is called during startup to configure the system.
override Task InitSetup(HttpServer WebServer)
Initializes the setup object.
override string ConfigPrivilege
Minimum required privilege for a user to be allowed to change the configuration defined by the class.
override Task< bool > EnvironmentConfiguration()
Environment configuration by configuring values available in environment variables.
override string Resource
Resource to be redirected to, to perform the configuration.
string AddressesString
A string containing all addresses, delimited by "; ".
override int Priority
Priority of the setting. Configurations are sorted in ascending order.
override Task UnregisterSetup(HttpServer WebServer)
Unregisters the setup object.
static NotificationConfiguration Instance
Current instance of configuration.
const string GATEWAY_NOTIFICATION_JIDS
JIDs of operators of gateway.
override void SetStaticInstance(ISystemConfiguration Configuration)
Sets the static instance of the configuration.
override Task< string > Title(Language Language)
Gets a title for the system configuration.
CaseInsensitiveString[] Addresses
Notification addresses.
Abstract base class for system configurations.
void LogEnvironmentError(string EnvironmentVariable, object Value)
Logs an error to the event log, telling the operator an environment variable value contains an error.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repe...
Represents an HTTP request.
Definition: HttpRequest.cs:18
HttpRequestHeader Header
Request header.
Definition: HttpRequest.cs:134
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Base class for all HTTP resources.
Definition: HttpResource.cs:23
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task SendResponse()
Sends the response back to the client. If the resource is synchronous, there's no need to call this m...
async Task Write(byte[] Data)
Returns binary data in the response.
Implements an HTTP server.
Definition: HttpServer.cs:36
HttpResource Register(HttpResource Resource)
Registers a resource with the server.
Definition: HttpServer.cs:1287
bool Unregister(HttpResource Resource)
Unregisters a resource from the server.
Definition: HttpServer.cs:1438
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
static readonly Regex BareJidRegEx
Regular expression for Bare JIDs
Definition: XmppClient.cs:188
Represents a case-insensitive string.
int Length
Gets the number of characters in the current CaseInsensitiveString object.
static bool IsNullOrEmpty(CaseInsensitiveString value)
Indicates whether the specified string is null or an CaseInsensitiveString.Empty string.
CaseInsensitiveString[] Split(params char[] separator)
Returns a string array that contains the substrings in this instance that are delimited by elements o...
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
Contains information about a language.
Definition: Language.cs:17
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 ...
Definition: Language.cs:209
Interface for system configurations. The gateway will scan all module for system configuration classe...