Created
April 19, 2019 14:18
-
-
Save nathan130200/356f5f969268d6caa63280e22076ae06 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Globalization; | |
using System.Reflection; | |
using System.Text; | |
using agsXMPP; | |
using agsXMPP.protocol.client; | |
using agsXMPP.Xml.Dom; | |
using U = agsXMPP.Uri; | |
namespace WarfaceEmulator.Extensions | |
{ | |
public static class AgsxmppExtensions | |
{ | |
public static readonly NumberFormatInfo DEFAULT_NUMBER_FORMAT = new NumberFormatInfo | |
{ | |
CurrencyDecimalSeparator = ".", | |
CurrencyGroupSeparator = ".", | |
NumberDecimalSeparator = ".", | |
NumberGroupSeparator = ".", | |
PercentDecimalSeparator = ".", | |
PercentGroupSeparator = ".", | |
}; | |
public static Element C(this Element parent, string name, string text = null, string ns = null) | |
{ | |
var child = new Element(name, text, ns); | |
parent.AddChild(child); | |
return child; | |
} | |
public static Element Q(this Element element, string name, Action<Element> builder) | |
{ | |
var query = new Element("query", null, "urn:cryonline:k01"); | |
var tag = new Element(name); | |
builder(tag); | |
return element; | |
} | |
public static IQ ToError(this IQ iq, ErrorType type = ErrorType.wait, ErrorCondition condition = ErrorCondition.FeatureNotImplemented, string text = "Custom query error.") | |
{ | |
iq.SwitchDirection(); | |
iq.Type = IqType.error; | |
var error = iq.C("error") | |
.A("type", type.ToString()); | |
var tag = ""; | |
switch (condition) | |
{ | |
case ErrorCondition.BadRequest: tag = "bad-request"; break; | |
case ErrorCondition.Conflict: tag = "conflict"; break; | |
case ErrorCondition.FeatureNotImplemented: tag = "feature-not-implemented"; break; | |
case ErrorCondition.Forbidden: tag = "forbidden"; break; | |
case ErrorCondition.Gone: tag = "gone"; break; | |
case ErrorCondition.InternalServerError: tag = "internal-server-error"; break; | |
case ErrorCondition.ItemNotFound: tag = "item-not-found"; break; | |
case ErrorCondition.JidMalformed: tag = "jid-malformed"; break; | |
case ErrorCondition.NotAcceptable: tag = "not-acceptable"; break; | |
case ErrorCondition.NotAllowed: tag = "not-allowed"; break; | |
case ErrorCondition.NotAuthorized: tag = "not-authorized"; break; | |
case ErrorCondition.NotModified: tag = "not-modified"; break; | |
case ErrorCondition.PaymentRequired: tag = "payment-required"; break; | |
case ErrorCondition.RecipientUnavailable: tag = "recipient-unavailable"; break; | |
case ErrorCondition.Redirect: tag = "redirect"; break; | |
case ErrorCondition.RegistrationRequired: tag = "registration-required"; break; | |
case ErrorCondition.RemoteServerNotFound: tag = "remote-server-not-found"; break; | |
case ErrorCondition.RemoteServerTimeout: tag = "remote-server-timeout"; break; | |
case ErrorCondition.ResourceConstraint: tag = "resource-constraint"; break; | |
case ErrorCondition.ServiceUnavailable: tag = "service-unavailable"; break; | |
case ErrorCondition.SubscriptionRequired: tag = "subscription-required"; break; | |
case ErrorCondition.UndefinedCondition: tag = "undefined-condition"; break; | |
case ErrorCondition.UnexpectedRequest: tag = "unexpected-request"; break; | |
default: tag = "service-unavailable"; break; | |
} | |
error.C(tag, ns: U.STANZAS); | |
return iq; | |
} | |
public static IQ ToError(this IQ iq, int code = 8, int custom_code = -1, string text = "Custom query error.") | |
{ | |
iq.SwitchDirection(); | |
iq.Type = IqType.error; | |
iq.C("error", U.STANZAS) | |
.A("code", code) | |
.A("custom_code", custom_code) | |
.C("text", text, U.STANZAS); | |
return iq; | |
} | |
public static Element C(this Element element, Element child) | |
{ | |
element.AddChild(child); | |
return child; | |
} | |
public static Element A(this Element element, string name, string value) | |
{ | |
element.SetAttribute(name, value); | |
return element; | |
} | |
public static Element A(this Element element, string name, bool value, bool number = true) | |
{ | |
element.SetAttribute(name, number ? (value ? "1" : "0") : (value ? "true" : "false")); | |
return element; | |
} | |
public static Element A(this Element element, string name, double value, IFormatProvider provider = null) | |
{ | |
if (provider == null) | |
provider = DEFAULT_NUMBER_FORMAT; | |
element.SetAttribute(name, value, provider); | |
return element; | |
} | |
public static Element A(this Element element, string name, long value) | |
{ | |
element.SetAttribute(name, value); | |
return element; | |
} | |
public static Element A(this Element element, string name, Jid value) | |
{ | |
element.SetAttribute(name, value); | |
return element; | |
} | |
public static Element P(this Element element) | |
{ | |
var field = typeof(Element) | |
.GetField("Parent", BindingFlags.NonPublic | BindingFlags.Instance); | |
var obj = field.GetValue(element); | |
if (obj == null) | |
return element; | |
return (Element)obj; | |
} | |
public static Element R(this Element element) | |
{ | |
object parent = null; | |
object instance = element; | |
var field = typeof(Element) | |
.GetField("Parent", BindingFlags.NonPublic | BindingFlags.Instance); | |
while ((instance = field.GetValue(instance)) != null) | |
parent = instance; | |
return (Element)parent; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment