forked from pgstath/Sharp.Xmpp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCustomIqExtension.cs
214 lines (196 loc) · 8.71 KB
/
CustomIqExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using Net.Xmpp.Core;
using Net.Xmpp.Im;
using System;
using System.Collections.Generic;
using System.Xml;
namespace Net.Xmpp.Extensions
{
/// <summary>
/// Implements Mechanism for providing Custom IQ Extensions
/// </summary>
internal class CustomIqExtension : XmppExtension, IInputFilter<Iq>
{
/// <summary>
/// A reference to the 'Entity Capabilities' extension instance.
/// </summary>
private EntityCapabilities ecapa;
/// <summary>
/// An enumerable collection of XMPP namespaces the extension implements.
/// </summary>
/// <remarks>This is used for compiling the list of supported extensions
/// advertised by the 'Service Discovery' extension.</remarks>
public override IEnumerable<string> Namespaces
{
get
{
return new string[] { "urn:sharp.xmpp:customiq" };
}
}
/// <summary>
/// The named constant of the Extension enumeration that corresponds to this
/// extension.
/// </summary>
public override Extension Xep
{
get
{
return Extension.CustomIqExtension;
}
}
/// <summary>
/// Invoked after all extensions have been loaded.
/// </summary>
public override void Initialize()
{
ecapa = im.GetExtension<EntityCapabilities>();
}
/// <summary>
/// Invoked when an IQ stanza is being received.
/// If the Iq is correctly received a Result response is included
/// with extension specific metadata included.
/// If the Iq is not correctly received an error is returned
/// Semantics of error on the response refer only to the XMPP level
/// and not the application specific level
/// </summary>
/// <param name="stanza">The stanza which is being received.</param>
/// <returns>true to intercept the stanza or false to pass the stanza
/// on to the next handler.</returns>
public bool Input(Iq stanza)
{
string response = null;
//if (stanza.Type != IqType.Get)
// return false;
//get,set, result are supported
var customIqStanza = stanza.Data["customiq"];
if (customIqStanza == null || customIqStanza.NamespaceURI != "urn:sharp.xmpp:customiq")
return false;
//Result indicates that the request has been received.
//It has not to do with the semantics of the message
XmlElement query = stanza.Data["customiq"];
XmlDocument targetDocument = new XmlDocument();
CopyNodes(targetDocument, targetDocument, query.FirstChild);
var xmlresponse = Xml.Element("customiq", "urn:sharp.xmpp:customiq");
try
{
//call the callback for receiving a relevant stanza
//and wait for answer in order provide it
response = im.CustomIqDelegate.Invoke(stanza.From, targetDocument.InnerXml);
if (response != null && response != "")
{
xmlresponse.Text(response);
}
im.IqResult(stanza, xmlresponse);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Send back an error response" + e.StackTrace + e.ToString());
// Send back an error response in case the callback method threw
// an exception.
im.IqError(stanza, ErrorType.Modify, ErrorCondition.BadRequest);
}
// We took care of this IQ request, so intercept it and don't pass it
// on to other handlers.
//Also send a void acknowledgement
return true;
}
public void CopyNodes(XmlDocument targetDocument, XmlNode targetNode, XmlNode source)
{
XmlNode targetChildNode = targetDocument.CreateNode(source.NodeType, source.Name, "");
if (!source.HasChildNodes)
targetChildNode.InnerText = source.InnerText;
targetNode.AppendChild(targetChildNode);
foreach (XmlNode childNode in source.ChildNodes)
{
CopyNodes(targetDocument, targetChildNode, childNode);
}
}
/// <summary>
/// Requests the XMPP entity with the specified JID a GET command.
/// When the Result is received and it not not an error
/// if fires the callback function
/// </summary>
/// <param name="jid">The JID of the XMPP entity to get.</param>
/// <exception cref="ArgumentNullException">The jid parameter
/// is null.</exception>
/// <exception cref="NotSupportedException">The XMPP entity with
/// the specified JID does not support the 'Ping' XMPP extension.</exception>
/// <exception cref="XmppErrorException">The server returned an XMPP error code.
/// Use the Error property of the XmppErrorException to obtain the specific
/// error condition.</exception>
/// <exception cref="XmppException">The server returned invalid data or another
/// unspecified XMPP error occurred.</exception>
public void RequestCustomIqAsync(Jid jid, string request, Action callback)
{
jid.ThrowIfNull("jid");
request.ThrowIfNull("str");
//First check if the Jid entity supports the namespace
if (!ecapa.Supports(jid, Extension.CustomIqExtension))
{
throw new NotSupportedException("The XMPP entity does not support the " +
"'CustomIqExtension' extension.");
}
var xml = Xml.Element("customiq", "urn:sharp.xmpp:customiq").Text(request);
//The Request is Async
im.IqRequestAsync(IqType.Get, jid, im.Jid, xml, null, (id, iq) =>
{
//For any reply we execute the callback
if (iq.Type == IqType.Error)
throw Util.ExceptionFromError(iq, "Could not Send Object to XMPP entity.");
if (iq.Type == IqType.Result)
{
try
{
//An empty response means the message was received
if (callback != null)
{
callback.Invoke();
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Not correctly formated response to RequestCustomIqAsync" + e.StackTrace + e.ToString());
throw Util.ExceptionFromError(iq, "Not correctly formated response to RequestCustomIqAsync, " + e.Message);
}
}
});
}
/// <summary>
/// Requests the XMPP entity with the specified JID a GET command.
/// When the Result is received and it not not an error
/// if fires the callback function
/// </summary>
/// <param name="jid">The JID of the XMPP entity to get.</param>
/// <exception cref="ArgumentNullException">The jid parameter
/// is null.</exception>
/// <exception cref="NotSupportedException">The XMPP entity with
/// the specified JID does not support the 'Ping' XMPP extension.</exception>
/// <exception cref="XmppErrorException">The server returned an XMPP error code.
/// Use the Error property of the XmppErrorException to obtain the specific
/// error condition.</exception>
/// <exception cref="XmppException">The server returned invalid data or another
/// unspecified XMPP error occurred.</exception>
public void RequestCustomIq(Jid jid, string request)
{
jid.ThrowIfNull("jid");
request.ThrowIfNull("str");
//First check if the Jid entity supports the namespace
if (!ecapa.Supports(jid, Extension.CustomIqExtension))
{
throw new NotSupportedException("The XMPP entity does not support the " +
"'CustomIqExtension' extension.");
}
var xml = Xml.Element("customiq", "urn:sharp.xmpp:customiq").Text(request);
//The Request is Async
im.IqRequest(IqType.Get, jid, im.Jid, xml);
}
/// <summary>
/// Initializes a new instance of the CustomIq class.
/// </summary>
/// <param name="im">A reference to the XmppIm instance on whose behalf this
/// instance is created.</param>
public CustomIqExtension(XmppIm im)
: base(im)
{
}
}
}