Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SendSubscriptionAccepted.cs
1using System;
3using System.Threading.Tasks;
4using System.Xml;
5using Waher.Content;
12using Waher.Script;
15
17{
22 {
27 : base("Xmpp/SendSubscriptionAccepted",
28 new KeyValuePair<Type, Expression>(typeof(Dictionary<string, object>), new Expression(jsonPattern)),
29 new KeyValuePair<Type, Expression>(typeof(XmlDocument), new Expression(xmlPattern)))
30 {
31 }
32
33 private static readonly string jsonPattern = Resources.LoadResourceAsText(typeof(SendSubscriptionAccepted).Namespace + ".JSON.SendSubscriptionAccepted.req");
34 private static readonly string xmlPattern = Resources.LoadResourceAsText(typeof(SendSubscriptionAccepted).Namespace + ".XML.SendSubscriptionAccepted.req");
35
44 public override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary<string, IElement> Parameters)
45 {
47
48 string To = (string)Parameters["PTo"].AssociatedObjectValue;
49
50 if (string.IsNullOrEmpty(To))
51 throw new BadRequestException("Missing recipient JID.");
52
53 if (!IsValidBareJID(To))
54 throw new BadRequestException("Recpient must be a Bare JID.");
55 string Id = (string)Parameters["PId"]?.AssociatedObjectValue;
56
57 if (string.IsNullOrEmpty(Id))
58 Id = Guid.NewGuid().ToString();
59
60 Dictionary<string, object> NewItem;
61 bool Result;
62
63 if (XmppServerModule.Server is null)
64 {
66
67 if (Client is null)
68 {
69 if (Types.TryGetModuleParameter("XMPP", out XmppClient Client2))
70 Client = Client2;
71 }
72
73 if (Client is null)
74 throw new ServiceUnavailableException("XMPP Client not available.");
75
77 ?? throw new NotFoundException("Presence subscription request not found.");
78
79 Id = e.Id;
80
81 await e.Accept();
82 Result = true;
83
84 Networking.XMPP.RosterItem Item = Client[To];
85
86 NewItem = new Dictionary<string, object>()
87 {
88 { "bareJid", Item?.BareJid ?? To },
89 { "pendingSubscription", (Item?.PendingSubscription ?? PendingSubscription.Subscribe) == PendingSubscription.Subscribe },
90 { "status", Item?.State ?? SubscriptionState.None },
91 { "name", Item?.Name ?? string.Empty },
92 { "Groups", Item?.Groups ?? Array.Empty<string>() }
93 };
94 }
95 else
96 {
97 PersistenceLayer PersistenceLayer = XmppServerModule.PersistenceLayer ?? new PersistenceLayer();
100
101 switch (State)
102 {
103 case SubscriptionStatus.none:
104 State = SubscriptionStatus.from;
105 break;
106
107 case SubscriptionStatus.to:
108 State = SubscriptionStatus.both;
109 break;
110 }
111
113 Item?.Name ?? string.Empty, State, null, null);
114
115 Result = await XmppServerModule.Server.Presence("subscribed", Id,
116 new XmppAddress(To),
117 new XmppAddress(User.UserName + "@" + XmppServerModule.Server.Domain),
118 string.Empty, string.Empty, XmppServerModule.Server);
119
120 Item = await PersistenceLayer.GetRosterItem(User.UserName, To);
121 NewItem = new Dictionary<string, object>()
122 {
123 { "bareJid", Item.BareJid.Value },
124 { "pendingSubscription", Item.PendingSubscription },
125 { "status", Item.Subscription },
126 { "name", Item.Name },
127 { "Groups", Item.Groups }
128 };
129 }
130
131 await Response.Return(new NamedDictionary<string, object>("SubscriptionAcceptedResult", AgentNamespace)
132 {
133 { "sent", Result },
134 { "id", Id },
135 { "RosterItem", NewItem }
136 });
137 }
138 }
139}
A Named dictionary is a dictionary, with a local name and a namespace. Use it to return content that ...
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 has not found anything matching the Request-URI. No indication is given of whether the con...
The server is currently unable to handle the request due to a temporary overloading or maintenance of...
Event arguments for presence events.
async Task Accept()
Accepts a subscription or unsubscription request.
string Id
ID attribute of presence stanza.
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
static readonly XmppAddress Empty
Empty address.
Definition: XmppAddress.cs:31
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
PresenceEventArgs GetSubscriptionRequest(string BareJID)
Gets a presence subscription request
Definition: XmppClient.cs:4759
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.
Accepts a presence subscription request made to the XMPP account.
SendSubscriptionAccepted()
Accepts a presence subscription request made to the XMPP account.
override async Task POST(HttpRequest Request, HttpResponse Response, Dictionary< string, IElement > Parameters)
Executes the POST method on the resource.
Service Module hosting the XMPP broker and its components.
Interface for roster items.
Definition: IRosterItem.cs:43
SubscriptionStatus Subscription
Subscription status.
Definition: IRosterItem.cs:72
string Name
Name of roster item.
Definition: IRosterItem.cs:56
SubscriptionStatus
Roster item subscription status enumeration.
Definition: IRosterItem.cs:10
PendingSubscription
Pending subscription states.
Definition: RosterItem.cs:54