示例#1
0
        protected XmppConnection(string ns)
        {
            queries      = new Dictionary <string, Action <XMPPIq> >();
            _ns          = ns;
            Capabilities = new CapabilitiesManager
            {
                Identity = new Identity
                {
                    Category     = "client",
                    IdentityType = "mobile",
                    IdentityName = "SharpXMPP"
                },

                Node     = "http://bggg.net.ru/caps",
                Features = new List <string>
                {
                    Namespaces.DiscoInfo,
                    Namespaces.DiscoItems
                }
            };

            IqManager = new IqManager
            {
                PayloadHandlers =
                {
                    new InfoHandler(Capabilities),
                    new ItemsHandler()
                }
            };
            Iq += IqManager.Handle;
        }
示例#2
0
        public async Task ConnectSSL(string host, AuthCred cred)
        {
            TcpClient.OnStringReceived += ReadXMPPMessage;

            await TcpClient.ConnectSSL(host);

            Host = host;
            TcpClient.SendString(
                $"<stream:stream to=\"{host}\" xml:lang=\"*\" version=\"1.0\" xmlns:stream=\"http://etherx.jabber.org/streams\" xmlns=\"jabber:client\">");
            AuthCred = cred;
            IqManager.DoChatPing();
        }
示例#3
0
        /// <summary>
        /// The handler for when the string is received
        /// </summary>
        /// <param name="x">The received string</param>
        private bool ReadXMPPMessage(string x)
        {
            if (x.Contains("</stream:stream>"))
            {
                //TODO: Handle a disconnect
            }
            //Lazy hack, this converts the initial starting output to a valid XML doc
            else if (x.Contains("<stream:stream"))
            {
                x += "</stream:stream>";
            }
            //Lazy hack 2, This stops any stupid blah blah blah is not a part of this domain stuff
            x = x.Replace("stream:", "");


            try
            {
                //The XML Reader
                using (var reader = XmlReader.Create(new StringReader(x)))
                {
                    //Convert to XmlDocument for reading
                    var doc = new XmlDocument();
                    doc.Load(reader);
                    var el = doc.DocumentElement;

                    #region XmppPresenceHandler

                    if (el.Name == "presence" && el.HasChildNodes)
                    {
                        PresenceManager.HandleReceivedPresence(el);
                    }

                    #endregion XmppPresenceHandler

                    #region XmppMessageRecieveHandler

                    else if (el.Name == "message" && el.HasChildNodes)
                    {
                        try
                        {
                            if (el.Attributes["from"].Value == el.Attributes["to"].Value)
                            {
                                return(true);
                            }

                            //TODO: Add a message handler
                            //OnMessageRecieved?.Invoke(new Jid(el.Attributes["from"].Value), el.InnerText);
                        }
                        catch
                        {
                            //Ignore for now
                        }
                    }

                    #endregion XmppMessageRecieveHandler

                    else if (el.HasChildNodes)
                    {
                        foreach (var node in el.ChildNodes)
                        {
                            var xmlNode = (XmlNode)node;

                            //Handle RsoAuth

                            #region AuthHandler

                            if (xmlNode.Name == "mechanisms")
                            {
                                if (AuthHandler.HandleAuth(xmlNode))
                                {
                                    break;
                                }
                            }

                            #endregion AuthHandler

                            #region ConnectionSuccessHandler

                            if (el.Name == "success" && xmlNode.Name == "#text")
                            {
                                //I have no idea why I did this, but I did it. Don't question me Wild
                                if (int.TryParse(xmlNode.InnerText, out var output))
                                {
                                    Id = output;
                                }

                                //Say we logged in okay
                                OnSuccessLogin?.Invoke();

                                //Send another random static string
                                TcpClient.SendString(
                                    "<stream:stream " +
                                    "to=\"pvp.net\" " +
                                    "xml:lang=\"*\" " +
                                    "version=\"1.0\" " +
                                    "xmlns:stream=\"http://etherx.jabber.org/streams\" " +
                                    "xmlns=\"jabber:client\">");
                            }

                            #endregion ConnectionSuccessHandler

                            #region IDontKnowWtfThisIsButReturnBinding

                            if (xmlNode.Name == "rxep")
                            {
                                //This is sent from client to server after this is received. I honestly don't know this xmpp stuff so lets pretend we read that

                                TcpClient.SendString("<iq type=\"set\" id=\"0\"><bind xmlns=\"urn:ietf:params:xml:ns:xmpp-bind\"><resource>RC</resource></bind></iq>");

                                break;
                            }

                            #endregion IDontKnowWtfThisIsButReturnBinding

                            #region IQManager
                            if (el.Name == "iq")
                            {
                                IqManager.HandleIq(xmlNode, el);
                            }
                            #endregion IQManager
                        }
                    }
                    else
                    {
                        //TODO: <?xml version='1.0'?><stream:stream xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' id='3891861776' from='pvp.net' version='1.0'>
                    }
                }

                return(true);
            }
            catch
            {
                return(false);
            }
        }