Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SendPresenceSubscription.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
13using Waher.Script;
16
18{
23 {
28 : base("Xmpp/SendPresenceSubscription",
29 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
30 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
31 {
32 }
33
34 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(SendPresenceSubscription).Namespace + ".JSON.SendPresenceSubscription.req");
35 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SendPresenceSubscription).Namespace + ".XML.SendPresenceSubscription.req");
36
45 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
46 {
48
49 string To = (string)Parameters["PTo"].AssociatedObjectValue;
50
51 if (string.IsNullOrEmpty(To))
52 throw new BadRequestException("Missing recipient JID.");
53
54 if (!IsValidBareJID(To))
55 throw new BadRequestException("Recpient must be a Bare JID.");
56
57 string CustomXml = (string)Parameters["PCustomXml"]?.AssociatedObjectValue;
58 string Language = (string)Parameters["PLanguage"]?.AssociatedObjectValue;
59 string Id = (string)Parameters["PId"]?.AssociatedObjectValue;
60
61 if (string.IsNullOrEmpty(Id))
62 Id = Guid.NewGuid().ToString();
63
64 if (string.IsNullOrEmpty(CustomXml))
65 CustomXml = await GetApprovedLegalIDXml(User.UserName);
66 else
67 {
68 if (!XML.IsValidXml(CustomXml, true, true, true, true, false, false))
69 throw new BadRequestException("Not valid XML.");
70
71 CustomXml += await GetApprovedLegalIDXml(User.UserName);
72 }
73
74 Dictionary<string, object> NewItem;
75 bool Result;
76
77 if (XmppServerModule.Server is null)
78 {
80
81 if (Client is null)
82 {
83 if (Types.TryGetModuleParameter("XMPP", out XmppClient Client2))
84 Client = Client2;
85 }
86
87 if (Client is null)
88 throw new ServiceUnavailableException("XMPP Client not available.");
89
90 await Client.RequestPresenceSubscription(To, CustomXml);
91 Result = true;
92
93 Networking.XMPP.RosterItem Item = Client[To];
94
95 NewItem = new Dictionary<string, object>()
96 {
97 { "bareJid", Item?.BareJid ?? To },
98 { "pendingSubscription", (Item?.PendingSubscription ?? PendingSubscription.Subscribe) == PendingSubscription.Subscribe },
99 { "status", Item?.State ?? SubscriptionState.None },
100 { "name", Item?.Name ?? string.Empty },
101 { "Groups", Item?.Groups ?? Array.Empty<string>() }
102 };
103 }
104 else
105 {
106 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
107 IRosterItem Item = await PersistenceLayer.GetRosterItem(User.UserName, To);
108
110 Item?.Name ?? string.Empty, null, null, null);
111
112 Result = await XmppServerModule.Server.Presence("subscribe", Id,
113 new XmppAddress(To),
114 new XmppAddress(User.UserName + "@" + XmppServerModule.Server.Domain),
115 Language, CustomXml, XmppServerModule.Server);
116
117 Item = await PersistenceLayer.GetRosterItem(User.UserName, To);
118 NewItem = new Dictionary<string, object>()
119 {
120 { "bareJid", Item.BareJid.Value },
121 { "pendingSubscription", Item.PendingSubscription },
122 { "status", Item.Subscription },
123 { "name", Item.Name },
124 { "Groups", Item.Groups }
125 };
126 }
127
128 await Response.Return(new NamedDictionary<string, object>("PresenceSubscriptionResult", AgentNamespace)
129 {
130 { "sent", Result },
131 { "id", Id },
132 { "RosterItem", NewItem }
133 });
134 }
135 }
136}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
Helps with common XML-related tasks.
Definition: XML.cs:21
static bool IsValidXml(string Xml)
Checks if a string is valid XML
Definition: XML.cs:1397
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static XmppClient XmppClient
XMPP Client connection of gateway.
Definition: Gateway.cs:4038
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:22
Represets a response of an HTTP client request.
Definition: HttpResponse.cs:23
Task Return(Exception ex)
Returns an error to the client.
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Maintains information about an item in the roster.
Definition: RosterItem.cs:75
SubscriptionState State
roup Current subscription state.
Definition: RosterItem.cs:268
string[] Groups
Any groups the roster item belongs to.
Definition: RosterItem.cs:186
string BareJid
Bare JID of the roster item.
Definition: RosterItem.cs:276
PendingSubscription PendingSubscription
If there's a pending unanswered presence subscription or unsubscription request made to the contact.
Definition: RosterItem.cs:291
string Name
Name of the roster item.
Definition: RosterItem.cs:282
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
CaseInsensitiveString Domain
Domain name.
Definition: XmppServer.cs:922
async Task< bool > Presence(string Type, string Id, XmppAddress From, string Language, Stanza Stanza, ISender Sender)
Presence stanza.
Definition: XmppServer.cs:2997
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:58
Task RequestPresenceSubscription(string BareJid)
Requests subscription of presence information from a contact.
Definition: XmppClient.cs:4969
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:13
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:55
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
static bool TryGetModuleParameter(string Name, out object Value)
Tries to get a module parameter value.
Definition: Types.cs:607
Class managing a script expression.
Definition: Expression.cs:41
async Task< IRosterItem > GetRosterItem(CaseInsensitiveString UserName, CaseInsensitiveString Jid)
Gets a roster item for an account.
async Task< IRosterItem > SetRosterItem(CaseInsensitiveString UserName, CaseInsensitiveString Jid, string Name, SubscriptionStatus? Subscription, bool? PendingSubscription, string[] Groups)
Sets a roster item in a users roster.
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.
static async Task< string > GetApprovedLegalIDXml(string Account)
Gets XML containing the latest approvied Legal ID for an account, for inclusion in requests to peers.
Sends a presence subscription request to an XMPP account.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
SendPresenceSubscription()
Sends a presence subscription request to an XMPP account.
Service Module hosting the XMPP broker and its components.
Interface for roster items.
Definition: IRosterItem.cs:43
string Name
Name of roster item.
Definition: IRosterItem.cs:56
PendingSubscription
Pending subscription states.
Definition: RosterItem.cs:54