/// <summary> /// Initializes a new instance of the <see cref="Parser" /> class /// </summary> /// <param name="xmpp">XMPP instance</param> public Parser(XmppBase xmpp) { _xmpp = xmpp; _dataQueue = new Queue <string>(); _xmpp.ClientSocket.Data += ClientSocket_Data; _logger.Log(LogLevel.Debug, $"{typeof(Parser)} created"); }
/// <summary> /// Execute the the state /// </summary> /// <param name="xmpp">Xmpp client to use</param> /// <param name="tag">Tag from the server</param> /// <inheritdoc /> public void Execute(XmppBase xmpp, Tag tag = null) { if (xmpp is XmppClient client) { var result = xmpp.SaslProcessor.Step(tag); switch (result) { case Success _: xmpp.ClientSocket.SetReadClear(); client.Authenticated = true; client.State = new ConnectedState(); client.State.Execute(client); break; case Failure _: client.State = new DisconnectState(); client.State.Execute(client); break; default: xmpp.ClientSocket.SetReadClear(); client.ClientSocket.Send(result); break; } } }
/// <summary> /// Check if SSL is required or requested /// </summary> /// <param name="features">Current <see cref="Features"/> from the server</param> /// <param name="xmpp">Current <see cref="XmppBase"/> instance</param> /// <returns>A value indicating whether to secure the socket or not</returns> public static bool CheckSsl(this Features features, XmppBase xmpp) { if (features.StartTls != null && (xmpp.UseSsl || features.FeatureCount == 1 || features.StartTls.Required)) { return(true); } return(false); }
/// <inheritdoc /> public void Execute(XmppBase xmpp, Tag tag = null) { if (xmpp is XmppClient client) { Logger.Log(LogLevel.Debug, "Connecting to server"); client.ClientSocket.Connect(client.Id); client.ClientSocket.SetReadClear(); } }
/// <inheritdoc /> public void Execute(XmppBase xmpp, Tag tag = null) { Features features; xmpp.ClientSocket.SetReadClear(); Logger.Log(LogLevel.Debug, "Starting to parse features"); switch (tag) { case Stream s when s.Version.StartsWith("1."): features = s.Features; break; case Features f: features = f; break; default: Logger.Log(LogLevel.Error, "Unexpected tag. Wrong state executed"); return; } if (features.StartTls != null && (xmpp.UseSsl || features.FeatureCount == 1 || features.StartTls.Required)) { Logger.Log(LogLevel.Debug, "SSL/TLS is required or it is supported and we want to use it"); xmpp.State = new StartTlsState(); xmpp.State.Execute(xmpp); return; } if (xmpp is XmppClient client && !client.Authenticated) { Logger.Log(LogLevel.Debug, "Starting authentication"); client.SaslProcessor = SaslProcessor.CreateProcessor( features.Mechanisms.SupportedTypes, MechanismTypes.Default, client); if (client.SaslProcessor is null) { client.State = new DisconnectState(); client.State.Execute(client); return; } client.ClientSocket.Send(client.SaslProcessor.Initialize(client.Id, client.Password)); client.State = new SaslState(); } }
/// <inheritdoc /> public void Execute(XmppBase xmpp, Tag tag = null) { if (xmpp is XmppClient client) { var stream = xmpp.Registry.GetTag <Stream>(Stream.XmlName); stream.Version = "1.0"; stream.To = client.Id.Server; stream.Namespace = Namespaces.Client; client.ClientSocket.Send(stream.StartTag); client.ClientSocket.SetReadClear(); xmpp.State = new StreamFeaturesState(); } }
/// <inheritdoc /> public void Execute(XmppBase xmpp, Tag tag = null) { if (tag is Proceed) { Logger.Log(LogLevel.Debug, "Clear to start SSL/TLS connection"); xmpp.State = new ConnectedState(); xmpp.ClientSocket.StartSsl(); return; } Logger.Log(LogLevel.Debug, "Sending starttls"); var starttls = xmpp.Registry.GetTag <StartTls>(StartTls.XmlName); xmpp.ClientSocket.Send(starttls); }
/// <inheritdoc /> public void Execute(XmppBase xmpp, Tag tag = null) { Features features; xmpp.ClientSocket.SetReadClear(); Logger.Log(LogLevel.Debug, "Starting to parse features"); switch (tag) { case Stream s when s.Version.StartsWith("1."): features = s.Features; break; case Features f: features = f; break; default: Logger.Log(LogLevel.Error, "Unexpected tag. Wrong state executed"); throw new InvalidStateException("Received tag that is not valid for the current state"); } if (!xmpp.ClientSocket.Secure) { Logger.Log(LogLevel.Debug, "Socket is not secure. Checking if we should use SSL"); if (features.CheckSsl(xmpp)) { Logger.Log(LogLevel.Debug, "Initializing security..."); xmpp.State = new StartTlsState(); xmpp.State.Execute(xmpp); return; } } if (xmpp is XmppClient client && !client.Authenticated) { Logger.Log(LogLevel.Debug, "Authenticating the user"); features.AuthenticateUser(client); } Logger.Log(LogLevel.Debug, "Starting resource binding"); xmpp.State = new BindingState(); xmpp.State.Execute(xmpp); }
/// <inheritdoc /> public void Execute(XmppBase xmpp, Tag tag = null) { var client = xmpp as XmppClient; if (tag is null) { var bind = xmpp.Registry.GetTag <Bind>(XName.Get("bind", Namespaces.Bind)); var iq = xmpp.Registry.GetTag <Iq>(XName.Get("iq", Namespaces.Client)); if (!string.IsNullOrEmpty(client.Resource)) { } iq.IqType = IqType.Set; iq.Add(bind); xmpp.ClientSocket.SetReadClear(); xmpp.ClientSocket.Send(iq); } else { } }
/// <inheritdoc /> public void Execute(XmppBase xmpp, Tag tag = null) { Logger.Log(LogLevel.Debug, "Disconnecting from the server"); xmpp.ClientSocket.Disconnect(); xmpp.State = new DisconnectedState(); }
/// <inheritdoc /> public void Execute(XmppBase xmpp, Tag tag = null) { // Disconnected from a server - nothing to do }