Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SendPresenceUnsubscription.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
11using Waher.Script;
14
16{
21 {
26 : base("Xmpp/SendPresenceUnsubscription",
27 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
28 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
29 {
30 }
31
32 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(SendPresenceUnsubscription).Namespace + ".JSON.SendPresenceUnsubscription.req");
33 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SendPresenceUnsubscription).Namespace + ".XML.SendPresenceUnsubscription.req");
34
43 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
44 {
46
47 string To = (string)Parameters["PTo"].AssociatedObjectValue;
48
49 if (string.IsNullOrEmpty(To))
50 throw new BadRequestException("Missing recipient JID.");
51
52 if (!IsValidBareJID(To))
53 throw new BadRequestException("Recpient must be a Bare JID.");
54
55 string Id = (string)Parameters["PId"]?.AssociatedObjectValue;
56
57 if (string.IsNullOrEmpty(Id))
58 Id = Guid.NewGuid().ToString();
59
60 bool Result;
61
62 if (XmppServerModule.Server is null)
63 {
65
66 if (Client is null)
67 {
68 if (Types.TryGetModuleParameter("XMPP", out object Obj) && Obj is XmppClient Client2)
69 Client = Client2;
70 }
71
72 if (Client is null)
73 throw new ServiceUnavailableException("XMPP Client not available.");
74
75 await Client.RequestPresenceUnsubscription(To);
76 Result = true;
77 }
78 else
79 {
80 Result = await XmppServerModule.Server.Presence("unsubscribe", Id,
81 new XmppAddress(To),
82 new XmppAddress(User.UserName + "@" + XmppServerModule.Server.Domain),
83 string.Empty, string.Empty, XmppServerModule.Server);
84 }
85
86 await Response.Return(new NamedDictionary<string, object>("StanzaResult", AgentNamespace)
87 {
88 { "sent", Result },
89 { "id", Id }
90 });
91 }
92 }
93}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:96
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:3187
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
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:21
async Task Return(object Object)
Returns an object to the client. This method can only be called once per response,...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
static readonly XmppAddress Empty
Empty address.
Definition: XmppAddress.cs:31
CaseInsensitiveString Domain
Domain name.
Definition: XmppServer.cs:882
async Task< bool > Presence(string Type, string Id, XmppAddress From, string Language, Stanza Stanza, ISender Sender)
Presence stanza.
Definition: XmppServer.cs:2647
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
Task RequestPresenceUnsubscription(string BareJid)
Requests unssubscription of presence information from a contact.
Definition: XmppClient.cs:4980
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:583
Class managing a script expression.
Definition: Expression.cs:39
static AccountUser AssertUserAuthenticated(HttpRequest Request)
Makes sure the request is made by an authenticated API user.
const string AgentNamespace
https://waher.se/Schema/BrokerAgent.xsd
Abstract base class for XMPP resources.
static bool IsValidBareJID(string JID)
Checks if a string is a valid Bare JID.
Sends a presence unsubscription request to an XMPP account.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
SendPresenceUnsubscription()
Sends a presence unsubscription request to an XMPP account.
Service Module hosting the XMPP broker and its components.