Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SendOperatorMessage.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Content;
8using Waher.Events;
12using Waher.Security;
13
15{
17 {
18 public SendOperatorMessage()
19 : base("/SendOperatorMessage")
20 {
21 }
22
23 public override bool HandlesSubPaths => false;
24 public override bool UserSessions => true;
25 public bool AllowsPOST => true;
26
27 public async Task POST(HttpRequest Request, HttpResponse Response)
28 {
29 IUser User = Gateway.AssertUserAuthenticated(Request, "Admin.Notarius.Contracts");
30
31 if (!Request.HasData)
32 throw new BadRequestException();
33
34 object Obj = await Request.DecodeDataAsync();
35 if (!(Obj is Dictionary<string, object> Form))
36 throw new BadRequestException();
37
38 if (!Form.TryGetValue("message", out Obj) || !(Obj is string Message) ||
39 !Form.TryGetValue("from", out Obj) || !(Obj is string From) ||
40 !Form.TryGetValue("to", out Obj) || !(Obj is string To) ||
41 !Form.TryGetValue("logObject", out Obj) || !(Obj is string LogObject))
42 {
43 throw new BadRequestException();
44 }
45
46 bool ValidFrom = XmppServerModule.Server.IsServerDomain(From, true);
47
48 if (!ValidFrom)
49 {
51 {
52 if (Component.IsComponentDomain(From, true))
53 {
54 ValidFrom = true;
55 break;
56 }
57 }
58
59 if (!ValidFrom)
60 throw new BadRequestException("Invalid from address.");
61 }
62
63 StringBuilder Content = new StringBuilder();
64
65 Content.Append("<subject>Message from operator.</subject>");
66 Content.Append("<body>");
67 Content.Append(XML.Encode(Message));
68 Content.Append("</body>");
69
70 await XmppServerModule.Server.SendMessage("chat", string.Empty, new XmppAddress(From), new XmppAddress(To),
71 string.Empty, Content.ToString());
72
73 DateTime Now = DateTime.Now;
74
75 Log.Informational(Message, LogObject, User.UserName,
76 new KeyValuePair<string, object>("From", From),
77 new KeyValuePair<string, object>("To", To));
78
79 Response.StatusCode = 200;
80 Response.ContentType = JsonCodec.DefaultContentType;
81 await Response.Write(JSON.Encode(new Dictionary<string, object>()
82 {
83 { "date", Now.ToShortDateString() },
84 { "time", Now.ToLongTimeString() },
85 { "type", EventType.Informational },
86 { "object", LogObject },
87 { "actor", User.UserName },
88 { "message", XML.HtmlValueEncode(Message).Replace("\r\n","\n").Replace("\n","<br/>") }
89 }, false));
90 }
91 }
92}
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
const string DefaultContentType
application/json
Definition: JsonCodec.cs:19
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Definition: Log.cs:334
Base class for objects that can log events.
Definition: LogObject.cs:10
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
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
bool HasData
If the request has data.
Definition: HttpRequest.cs:74
async Task< object > DecodeDataAsync()
Decodes data sent in request.
Definition: HttpRequest.cs:95
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Write(byte[] Data)
Returns binary data in the response.
Base class for all synchronous HTTP resources. A synchronous resource responds within the method hand...
Base class for components.
Definition: Component.cs:16
bool IsComponentDomain(CaseInsensitiveString Domain, bool IncludeAlternativeDomains)
Checks if a domain is the component domain, or optionally, an alternative component domain.
Definition: Component.cs:123
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
override string ToString()
object.ToString()
Definition: XmppAddress.cs:190
Task< bool > SendMessage(string Type, string Id, string From, string To, string Language, string ContentXml)
Sends a Message stanza to a recipient.
Definition: XmppServer.cs:3412
bool IsServerDomain(CaseInsensitiveString Domain, bool IncludeAlternativeDomains)
Checks if a domain is the server domain, or optionally, an alternative domain.
Definition: XmppServer.cs:861
IComponent[] Components
Registered components
Definition: XmppServer.cs:1063
async Task POST(HttpRequest Request, HttpResponse Response)
Executes the POST method on the resource.
Service Module hosting the XMPP broker and its components.
POST Interface for HTTP resources.
Interface for components.
Definition: IComponent.cs:10
Basic interface for a user.
Definition: IUser.cs:7
string UserName
User Name.
Definition: IUser.cs:12