Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Added XEP-0199 (XMPP Ping) support
Browse files Browse the repository at this point in the history
Fixes 129
  • Loading branch information
COM8 committed May 6, 2020
1 parent 331109b commit e711222
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Follow [@UWPX_APP](https://twitter.com/UWPX_APP) on ![Twitter](http://i.imgur.co
| Chat State | [XEP-0085](https://xmpp.org/extensions/xep-0085.html "XEP-0085") |
| Personal Eventing Protocol | [XEP-0163](https://xmpp.org/extensions/xep-0085.html "XEP-0163") |
| Message Delivery Receipts | [XEP-0184](https://xmpp.org/extensions/xep-0184.html "XEP-0184") |
| XMPP Ping | [XEP-0199](https://xmpp.org/extensions/xep-0199.html "XEP-0199") |
| Direct MUC Invitations | [XEP-0249](https://xmpp.org/extensions/xep-0249.html "XEP-0249") |
| Message Carbons | [XEP-0280](https://xmpp.org/extensions/xep-0280.html "XEP-0280") |
| Chat Markers | [XEP-0333](https://xmpp.org/extensions/xep-0333.html "XEP-0333") |
Expand Down
2 changes: 2 additions & 0 deletions XMPP_API/Classes/Consts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,7 @@ public static class Consts
public const string XML_XEP_0313_NAMESPACE = "urn:xmpp:mam:tmp";
// XEP-0359 (Unique and Stable Stanza IDs)
public const string XML_XEP_0359_NAMESPACE = "urn:xmpp:sid:0";
// XEP-0199 (XMPP Ping)
public const string XML_XEP_0199_NAMESPACE = "urn:xmpp:ping";
}
}
22 changes: 22 additions & 0 deletions XMPP_API/Classes/Network/XML/MessageParser2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using XMPP_API.Classes.Network.XML.Messages.XEP_0085;
using XMPP_API.Classes.Network.XML.Messages.XEP_0184;
using XMPP_API.Classes.Network.XML.Messages.XEP_0198;
using XMPP_API.Classes.Network.XML.Messages.XEP_0199;
using XMPP_API.Classes.Network.XML.Messages.XEP_0249;
using XMPP_API.Classes.Network.XML.Messages.XEP_0336;
using XMPP_API.Classes.Network.XML.Messages.XEP_0363;
Expand Down Expand Up @@ -369,6 +370,19 @@ private List<AbstractMessage> parseMessageInternal(ref string msg)
}
break;

case IQMessage.GET:
// XEP-0199 (XMPP Ping):
XmlNode ping = XMLUtils.getChildNode(n, "ping", Consts.XML_XMLNS, Consts.XML_XEP_0199_NAMESPACE);
if (ping != null)
{
messages.Add(new PingMessage(n));
break;
}

// Default to IQErrorMessage:
messages.Add(new IQErrorMessage(n));
break;

case IQMessage.ERROR:
// XEP-0363 (HTTP File Upload) request slot error:
XmlNode requestNode = XMLUtils.getChildNode(n, "request", Consts.XML_XMLNS, Consts.XML_XEP_0363_NAMESPACE);
Expand All @@ -378,6 +392,14 @@ private List<AbstractMessage> parseMessageInternal(ref string msg)
break;
}

// XEP-0199 (XMPP Ping) ping failed:
XmlNode pingError = XMLUtils.getChildNode(n, "ping", Consts.XML_XMLNS, Consts.XML_XEP_0199_NAMESPACE);
if (pingError != null)
{
messages.Add(new PingErrorMessage(n));
break;
}

// Default to IQErrorMessage:
messages.Add(new IQErrorMessage(n));
break;
Expand Down
111 changes: 109 additions & 2 deletions XMPP_API/Classes/Network/XML/Messages/Error.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Xml;
using System;
using System.Xml;
using System.Xml.Linq;

namespace XMPP_API.Classes.Network.XML.Messages
{
public class Error
public class Error: IXElementable
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--
Expand Down Expand Up @@ -70,6 +72,27 @@ private static ErrorType getErrorType(XmlNode n)
}
}

private static string errorTypeToString(ErrorType errorType)
{
switch (errorType)
{
case ErrorType.AUTH:
return "auth";

case ErrorType.WAIT:
return "wait";

case ErrorType.MODIFY:
return "modify";

case ErrorType.CANCEL:
return "cancel";

default: // Should not happen
throw new NotImplementedException("No string representation for ErrorType: " + errorType.ToString());
}
}

private static ErrorName getErrorName(XmlNode n)
{
foreach (XmlNode n1 in n.ChildNodes)
Expand Down Expand Up @@ -152,6 +175,90 @@ private static ErrorName getErrorName(XmlNode n)
return ErrorName.UNKNOWN;
}

private static string errorNameToString(ErrorName errorName)
{
switch (errorName)
{
case ErrorName.BAD_REQUEST:
return "bad-request";

case ErrorName.CONFLICT:
return "conflict";

case ErrorName.FEATURE_NOT_IMPLEMENTED:
return "feature-not-implemented";

case ErrorName.FORBIDDEN:
return "forbidden";

case ErrorName.GONE:
return "gone";

case ErrorName.INTERNAL_SERVER_ERROR:
return "internal-server-error";

case ErrorName.ITEM_NOT_FOUND:
return "item-not-found";

case ErrorName.JID_MALFORMED:
return "jid-malformed";

case ErrorName.NOT_ACCEPTABLE:
return "not-acceptable";

case ErrorName.NOT_ALLOWED:
return "not-allowed";

case ErrorName.NOT_AUTHORIZED:
return "not-authorized";

case ErrorName.PAYMENT_REQUIRED:
return "payment-required";

case ErrorName.RECIPIENT_UNAVAILABLE:
return "recipient-unavailable";

case ErrorName.REDIRECT:
return "redirect";

case ErrorName.REGISTRATION_REQUIRED:
return "registration-required";

case ErrorName.REMOTE_SERVER_NOT_FOUND:
return "remote-server-not-found";

case ErrorName.REMOTE_SERVER_TIMEOUT:
return "remote-server-timeout";

case ErrorName.RESOURCE_CONSTRAINT:
return "resource-constraint";

case ErrorName.SERVICE_UNAVAILABLE:
return "service-unavailable";

case ErrorName.SUBSCRIPTION_REQUIRED:
return "subscription-required";

case ErrorName.UNDEFINED_CONDITION:
return "undefined-condition";

case ErrorName.UNEXPECTED_REQUEST:
return "unexpected-request";

default: // Should not happen
throw new NotImplementedException("No string representation for ErrorName: " + errorName.ToString());
}
}

public XElement toXElement(XNamespace ns)
{
XElement error = new XElement(ns + "error");
error.Add(new XAttribute("type", errorTypeToString(ERROR_TYPE)));
XNamespace nsInner = Consts.XML_ERROR_NAMESPACE;
error.Add(new XElement(nsInner + errorNameToString(ERROR_NAME)));
return error;
}

#endregion

#region --Misc Methods (Protected)--
Expand Down
8 changes: 6 additions & 2 deletions XMPP_API/Classes/Network/XML/Messages/IQErrorMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ public IQErrorMessage(XmlNode n) : base(n)
}
}

public IQErrorMessage(string from, string to, string id) : base(from, to, ERROR, id) { }

#endregion
//--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
#region --Set-, Get- Methods--
protected override XElement getQuery()
public override XElement toXElement()
{
throw new System.NotImplementedException();
XElement iq = base.toXElement();
iq.Add(ERROR_OBJ.toXElement(""));
return iq;
}

#endregion
Expand Down
52 changes: 52 additions & 0 deletions XMPP_API/Classes/Network/XML/Messages/XEP-0199/PingErrorMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Xml;
using System.Xml.Linq;

namespace XMPP_API.Classes.Network.XML.Messages.XEP_0199
{
public class PingErrorMessage: IQErrorMessage
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--


#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
#region --Constructors--
public PingErrorMessage(XmlNode n) : base(n) { }

public PingErrorMessage(string from, string to, string id) : base(from, to, id) { }

#endregion
//--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
#region --Set-, Get- Methods--


#endregion
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
#region --Misc Methods (Public)--
public override XElement toXElement()
{
XElement error = base.toXElement();
XNamespace ns = Consts.XML_XEP_0199_NAMESPACE;
error.Add(new XElement(ns + "ping"));
return error;
}

#endregion

#region --Misc Methods (Private)--


#endregion

#region --Misc Methods (Protected)--


#endregion
//--------------------------------------------------------Events:---------------------------------------------------------------------\\
#region --Events--


#endregion
}
}
57 changes: 57 additions & 0 deletions XMPP_API/Classes/Network/XML/Messages/XEP-0199/PingMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Xml;
using System.Xml.Linq;

namespace XMPP_API.Classes.Network.XML.Messages.XEP_0199
{
public class PingMessage: IQMessage
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--


#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
#region --Constructors--
public PingMessage(XmlNode answer) : base(answer) { }

public PingMessage(string from, string to) : base(from, to, GET, getRandomId()) { }

#endregion
//--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
#region --Set-, Get- Methods--


#endregion
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
#region --Misc Methods (Public)--
public override XElement toXElement()
{
XElement iq = base.toXElement();
XNamespace ns = Consts.XML_XEP_0199_NAMESPACE;
iq.Add(new XElement(ns + "ping"));
return iq;
}

public PongMessage generateResponse()
{
return new PongMessage(getTo(), getFrom(), ID);
}

#endregion

#region --Misc Methods (Private)--


#endregion

#region --Misc Methods (Protected)--


#endregion
//--------------------------------------------------------Events:---------------------------------------------------------------------\\
#region --Events--


#endregion
}
}
41 changes: 41 additions & 0 deletions XMPP_API/Classes/Network/XML/Messages/XEP-0199/PongMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace XMPP_API.Classes.Network.XML.Messages.XEP_0199
{
public class PongMessage: IQMessage
{
//--------------------------------------------------------Attributes:-----------------------------------------------------------------\\
#region --Attributes--


#endregion
//--------------------------------------------------------Constructor:----------------------------------------------------------------\\
#region --Constructors--
public PongMessage(string from, string to, string id) : base(from, to, RESULT, id) { }

#endregion
//--------------------------------------------------------Set-, Get- Methods:---------------------------------------------------------\\
#region --Set-, Get- Methods--


#endregion
//--------------------------------------------------------Misc Methods:---------------------------------------------------------------\\
#region --Misc Methods (Public)--


#endregion

#region --Misc Methods (Private)--


#endregion

#region --Misc Methods (Protected)--


#endregion
//--------------------------------------------------------Events:---------------------------------------------------------------------\\
#region --Events--


#endregion
}
}
8 changes: 8 additions & 0 deletions XMPP_API/Classes/Network/XmppConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using XMPP_API.Classes.Network.XML.Messages.Helper;
using XMPP_API.Classes.Network.XML.Messages.Processor;
using XMPP_API.Classes.Network.XML.Messages.XEP_0048;
using XMPP_API.Classes.Network.XML.Messages.XEP_0199;
using XMPP_API.Classes.Network.XML.Messages.XEP_0384;

namespace XMPP_API.Classes.Network
Expand Down Expand Up @@ -522,6 +523,13 @@ private async Task ParseMessageAsync(string data)
}
}

// Respond to XEP-0199 (XMPP Ping) messages:
if (msg is PingMessage pingMsg)
{
Logger.Debug("[XMPPConnection2]: XMPP ping received from " + pingMsg.getFrom());
await SendAsync(pingMsg.generateResponse(), true);
}

// Invoke message processors:
NewValidMessage?.Invoke(this, new NewValidMessageEventArgs(msg));

Expand Down
3 changes: 3 additions & 0 deletions XMPP_API/XMPP_API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@
<Compile Include="Classes\Network\XML\Messages\XEP-0198\SMEnableMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\XEP-0198\SMFailedMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\XEP-0198\SMRequestMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\XEP-0199\PingErrorMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\XEP-0199\PingMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\XEP-0199\PongMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\XEP-0249\DirectMUCInvitationMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\XEP-0280\CarbonsDisableMessage.cs" />
<Compile Include="Classes\Network\XML\Messages\XEP-0280\CarbonsEnableMessage.cs" />
Expand Down

0 comments on commit e711222

Please sign in to comment.