Exemple #1
0
        public void Add(Node e)
        {
            // can't add a empty node, so return immediately
            // Some people tried dthis which caused an error
            if (e == null)
                return;

            this.List.Add(e);
        }
Exemple #2
0
        public void Add(Node e)
        {
            // can't add a empty node, so return immediately
            // Some people tried this which caused an error
            if (e == null)
                return;

            if (m_Owner != null)
            {
                e.Parent = m_Owner;
                if (e.Namespace == null)
                    e.Namespace = m_Owner.Namespace;
            }

            e.m_Index = Count;

            this.List.Add(e);
        }
        public override void Parse(Node e)
        {
            if ( e.GetType() == typeof(Protocol.sasl.Challenge) )
            {
                Protocol.sasl.Challenge c = e as Protocol.sasl.Challenge;

                Sasl.DigestMD5.Step1 step1 = new Sasl.DigestMD5.Step1(c.TextBase64);
                if (step1.Rspauth == null)
                {
                    //response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">dXNlcm5hbWU9ImduYXVjayIscmVhbG09IiIsbm9uY2U9IjM4MDQzMjI1MSIsY25vbmNlPSIxNDE4N2MxMDUyODk3N2RiMjZjOWJhNDE2ZDgwNDI4MSIsbmM9MDAwMDAwMDEscW9wPWF1dGgsZGlnZXN0LXVyaT0ieG1wcC9qYWJiZXIucnUiLGNoYXJzZXQ9dXRmLTgscmVzcG9uc2U9NDcwMTI5NDU4Y2EwOGVjYjhhYTIxY2UzMDhhM2U5Nzc
                    Sasl.DigestMD5.Step2 s2 = new XMPPProtocol.Sasl.DigestMD5.Step2(step1, base.Username, base.Password, base.Server);
                    Protocol.sasl.Response r = new XMPPProtocol.Protocol.sasl.Response(s2.ToString());
                    base.XmppClientConnection.Send(r);
                }
                else
                {
                    // SEND: <response xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
                    base.XmppClientConnection.Send(new Protocol.sasl.Response());
                }
            }
        }
Exemple #4
0
        private void WriteTree(Node e, XmlTextWriter tw, Node parent)
        {
            if (e.NodeType == NodeType.Document)
            {
                //Write the ProcessingInstruction node.
                // <?xml version="1.0" encoding="windows-1252"?> ...
                Document doc = e as Document;
                string pi = null;

                if (doc.Version != null)
                    pi += "version='" + doc.Version + "'";

                if (doc.Encoding != null)
                {
                    if (pi != null)
                        pi += " ";

                    pi += "encoding='" + doc.Encoding + "'";
                }

                if (pi != null)
                    tw.WriteProcessingInstruction("xml", pi);

                foreach(Node n in e.ChildNodes)
                {
                    WriteTree(n, tw, e);
                }
            }
            else if (e.NodeType == NodeType.Text)
            {
                tw.WriteString(e.Value);
            }
            else if (e.NodeType == NodeType.Comment)
            {
                tw.WriteComment(e.Value);
            }
            else if (e.NodeType == NodeType.Element)
            {
                Element el = e as Element;

                if (el.Prefix==null)
                    tw.WriteStartElement( el.TagName );
                else
                    tw.WriteStartElement( el.Prefix + ":" + el.TagName );

                // Write Namespace
                if ( (parent == null || parent.Namespace != el.Namespace)
                    && el.Namespace != null
                    && el.Namespace.Length !=0
                    )
                {
                    if (el.Prefix==null)
                        tw.WriteAttributeString("xmlns", el.Namespace);
                    else
                        tw.WriteAttributeString("xmlns:" + el.Prefix , el.Namespace);
                }

                foreach (string attName in el.Attributes.Keys)
                {
                    tw.WriteAttributeString(attName, el.Attribute(attName));
                }

                //tw.WriteString(el.Value);

                if (el.ChildNodes.Count > 0)
                {
                    foreach(Node n in el.ChildNodes)
                    {
                        WriteTree(n, tw, e);
                    }
                    tw.WriteEndElement();
                }
                else
                {
                    tw.WriteEndElement();
                }
            }
        }
Exemple #5
0
        internal void OnStreamElement(object sender, XMPPProtocol.Xml.Dom.Node e)
        {
            if (m_XmppClient.XmppConnectionState == XmppConnectionState.Securing ||
                m_XmppClient.XmppConnectionState == XmppConnectionState.StartCompression)
            {
                return;
            }

            if (e.GetType() == typeof(Protocol.stream.Features))
            {
                Features f = e as Features;
                if (!m_XmppClient.Authenticated)
                {
                    // RECV: <stream:features><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
                    //			<mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism>
                    //			</mechanisms>
                    //			<register xmlns='http://jabber.org/features/iq-register'/>
                    //		</stream:features>
                    // SENT: <auth mechanism="DIGEST-MD5" xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
                    // Select a SASL mechanism

                    SaslEventArgs args = new SaslEventArgs(f.Mechanisms);

                    if (OnSaslStart != null)
                    {
                        OnSaslStart(this, args);
                    }

                    if (args.Auto == true)
                    {
                        // Library handles the Sasl stuff
                        if (f.Mechanisms != null)
                        {
                            if (m_XmppClient.UseStartTLS == false && m_XmppClient.UseSSL == false &&
                                f.Mechanisms.SupportsMechanism(MechanismType.X_GOOGLE_TOKEN))
                            {
                                // This is the only way to connect to GTalk on a unsecure Socket for now
                                // Secure authentication is done over https requests to pass the
                                // authentication credentials on a secure connection
                                args.Mechanism = XMPPProtocol.Protocol.sasl.Mechanism.GetMechanismName(MechanismType.X_GOOGLE_TOKEN);
                            }
                            else if (f.Mechanisms.SupportsMechanism(MechanismType.DIGEST_MD5))
                            {
                                args.Mechanism = XMPPProtocol.Protocol.sasl.Mechanism.GetMechanismName(MechanismType.DIGEST_MD5);
                            }
                            else if (f.Mechanisms.SupportsMechanism(MechanismType.PLAIN))
                            {
                                args.Mechanism = XMPPProtocol.Protocol.sasl.Mechanism.GetMechanismName(MechanismType.PLAIN);
                            }
                            else
                            {
                                args.Mechanism = null;
                            }
                        }
                        else
                        {
                            // Hack for Google
                            // TODO: i don't think we need this anymore. This was in an very early version of the gtalk server.
                            args.Mechanism = null;
                            //args.Mechanism = XMPPProtocol.Protocol.sasl.Mechanism.GetMechanismName(XMPPProtocol.Protocol.sasl.MechanismType.PLAIN);
                        }
                    }
                    if (args.Mechanism != null)
                    {
                        m_Mechanism = Factory.SaslFactory.GetMechanism(args.Mechanism);
                        // Set properties for the SASL mechanism
                        m_Mechanism.Username = m_XmppClient.Username;
                        m_Mechanism.Password = m_XmppClient.Password;
                        m_Mechanism.Server   = m_XmppClient.Server;
                        // Call Init Method on the mechanism
                        m_Mechanism.Init(m_XmppClient);
                    }
                    else
                    {
                        m_XmppClient.RequestLoginInfo();
                    }
                }
                else if (!m_XmppClient.Binded)
                {
                    if (f.SupportsBind)
                    {
                        m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Binding);

                        BindIq bIq;
                        if (m_XmppClient.Resource == null || m_XmppClient.Resource.Length == 0)
                        {
                            bIq = new BindIq(IqType.set, new Jid(m_XmppClient.Server));
                        }
                        else
                        {
                            bIq = new BindIq(IqType.set, new Jid(m_XmppClient.Server), m_XmppClient.Resource);
                        }

                        m_XmppClient.IqGrabber.SendIq(bIq, new IqCB(BindResult), null);
                    }
                }
            }
            else if (e.GetType() == typeof(Protocol.sasl.Challenge))
            {
                if (m_Mechanism != null && !m_XmppClient.Authenticated)
                {
                    m_Mechanism.Parse(e);
                }
            }
            else if (e.GetType() == typeof(Protocol.sasl.Success))
            {
                // SASL authentication was successfull
                if (OnSaslEnd != null)
                {
                    OnSaslEnd(this);
                }

                m_XmppClient.DoChangeXmppConnectionState(XmppConnectionState.Authenticated);

                m_Mechanism = null;

                m_XmppClient.Reset();
            }
            else if (e.GetType() == typeof(Protocol.sasl.Failure))
            {
                // Authentication failure
                m_XmppClient.FireOnAuthError(e as Element);
            }
        }
Exemple #6
0
 private void sp_OnStreamEnd(object sender, Node e)
 {
 }
Exemple #7
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="e"></param>
 public abstract void Parse(Node e);
Exemple #8
0
 public StreamInfo(Node node, ClientConnection client)
 {
     m_Node = node;
     m_Client = client;
 }
        /// <summary>
        /// Reset the XML Stream
        /// </summary>
        public void Reset()
        {
            m_Depth		= 0;
            m_root		= null;
            current		= null;
            m_cdata		= false;

            m_buf   = null;
            m_buf	= new BufferAggregate();

            //m_buf.Clear(0);
            m_ns.Clear();
        }
        public virtual void StreamParserOnStreamStart(object sender, Node e)
        {
            string xml = e.ToString().Trim();
            xml = xml.Substring(0, xml.Length - 2) + ">";

            this.FireOnReadXml(this, xml);

            Protocol.Stream st = (Protocol.Stream)e;
            if (st != null)
            {
                m_StreamId = st.StreamId;
                m_StreamVersion = st.Version;
            }
        }
        public virtual void StreamParserOnStreamEnd(object sender, Node e)
        {
            Element tag = e as Element;

            string qName;
            if (tag.Prefix == null)
                qName = tag.TagName;
            else
                qName = tag.Prefix + ":" + tag.TagName;

            string xml = "</" + qName + ">";

            this.FireOnReadXml(this, xml);
        }
 public virtual void StreamParserOnStreamElement(object sender, Node e)
 {
     this.FireOnReadXml(this, e.ToString());
 }
 public override void Parse(Node e)
 {
     // not needed here in X-GOOGLE-TOKEN mechanism
 }
Exemple #14
0
 public NodeList(Node owner)
 {
     m_Owner = owner;
 }
        public override void StreamParserOnStreamStart(object sender, Node e)
        {
            base.StreamParserOnStreamStart (sender, e);

            m_StreamStarted = true;

            Login();
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="e"></param>
 public override void Parse(Node e)
 {
     // *No Challenges* in SASL ANONYMOUS
 }
        public override void StreamParserOnStreamElement(object sender, Node e)
        {
            base.StreamParserOnStreamElement(sender, e);

            if ( e.GetType() == typeof(IQ) )
            {
                if (OnIq != null)
                    OnIq(this, e as IQ);

                IQ iq = e as IQ;
                if ( iq != null && iq.Query != null)
                {
                    // Roster
                    if (iq.Query is Roster)
                        OnRosterIQ(iq);
                }
            }
            else if ( e.GetType() == typeof(Message) )
            {
                if (OnMessage != null)
                    OnMessage(this, e as Message);
            }
            else if ( e.GetType() == typeof(Presence) )
            {
                if (OnPresence != null)
                    OnPresence(this, e as Presence);
            }
            else if (e.GetType() == typeof(XMPPProtocol.Protocol.stream.Features))
            {
                // Stream Features
                // StartTLS stuff
                XMPPProtocol.Protocol.stream.Features f = e as XMPPProtocol.Protocol.stream.Features;
                if (m_UseCompression &&
                    f.SupportsCompression &&
                    f.Compression.SupportsMethod(CompressionMethod.zlib))
                {
                    // Check for Stream Compression
                    // we support only ZLIB because its a free algorithm without patents
                    // yes ePatents suck
                    DoChangeXmppConnectionState(XmppConnectionState.StartCompression);
                    this.Send(new Compress(CompressionMethod.zlib));
                }
            #if SSL || MONOSSL || BCCRYPTO
                else if (f.SupportsStartTls && m_UseStartTLS)
                {
                    DoChangeXmppConnectionState(XmppConnectionState.Securing);
                    this.Send(new StartTls());
                }
            #endif
                else if (f.SupportsRegistration && m_RegisterAccount)
                {
                    // Do registration after TLS when possible
                    if (f.SupportsRegistration)
                        GetRegistrationFields(e);
                    else
                    {
                        // registration is not enabled on this server
                        FireOnError(this, new RegisterException("Registration is not allowed on this server"));
                        Close();
                        // Close the stream
                    }
                }
            }
            #if SSL || MONOSSL || BCCRYPTO
            else if ( e.GetType() == typeof (Proceed) )
            {
                StreamParser.Reset();
                ClientSocket.StartTls();
                SendStreamHeader(false);
                DoChangeXmppConnectionState(XmppConnectionState.Authenticating);
            }
            #endif
            else if ( e.GetType() == typeof (Compressed) )
            {
                //DoChangeXmppConnectionState(XmppConnectionState.StartCompression);
                StreamParser.Reset();
                ClientSocket.StartCompression();
                // Start new Stream Header compressed.
                SendStreamHeader(false);

                DoChangeXmppConnectionState(XmppConnectionState.Compressed);
            }
            else if (e is XMPPProtocol.Protocol.Error)
            {
                if (OnStreamError != null)
                    OnStreamError(this, e as Element);
            }
        }
Exemple #18
0
        private void StartTag(byte[] buf, int offset,
            ContentToken ct, TOK tok)
        {
            m_Depth++;
            int colon;
            string name;
            string prefix;
            Hashtable ht = new Hashtable();

            m_ns.PushScope();

            // if i have attributes
            if ((tok == TOK.START_TAG_WITH_ATTS) ||
                (tok == TOK.EMPTY_ELEMENT_WITH_ATTS))
            {
                int start;
                int end;
                string val;
                for (int i=0; i<ct.getAttributeSpecifiedCount(); i++)
                {
                    start =  ct.getAttributeNameStart(i);
                    end = ct.getAttributeNameEnd(i);
                    name = utf.GetString(buf, start, end - start);

                    start = ct.getAttributeValueStart(i);
                    end =  ct.getAttributeValueEnd(i);
                    //val = utf.GetString(buf, start, end - start);

                    val = NormalizeAttributeValue(buf, start, end - start);
                    // <foo b='&amp;'/>
                    // <foo b='&amp;amp;'
                    // TODO: if val includes &amp;, it gets double-escaped
                    if (name.StartsWith("xmlns:"))
                    {
                        colon = name.IndexOf(':');
                        prefix = name.Substring(colon+1);
                        m_ns.AddNamespace(prefix, val);
                    }
                    else if (name == "xmlns")
                    {
                        m_ns.AddNamespace(string.Empty, val);
                    }
                    else
                    {
                        ht.Add(name, val);
                    }
                }
            }

            name = utf.GetString(buf,
                offset + m_enc.MinBytesPerChar,
                ct.NameEnd - offset - m_enc.MinBytesPerChar);

            colon = name.IndexOf(':');
            string ns = "";
            prefix = null;
            if (colon > 0)
            {
                prefix = name.Substring(0, colon);
                name = name.Substring(colon + 1);
                ns = m_ns.LookupNamespace(prefix);
            }
            else
            {
                ns = m_ns.DefaultNamespace;
            }

            Element newel = ElementFactory.GetElement(prefix, name, ns);

            foreach (string attrname in ht.Keys)
            {
                newel.SetAttribute(attrname, (string)ht[attrname]);
            }

            if (m_root == null)
            {
                m_root = newel;
                //FireOnDocumentStart(m_root);
                if (OnStreamStart!=null)
                    OnStreamStart(this, m_root);
            }
            else
            {
                if (current != null)
                    current.AddChild(newel);
                current = newel;
            }
        }
        public override void StreamParserOnStreamStart(object sender, Node e)
        {
            base.StreamParserOnStreamStart(this, e);

            m_StreamStarted = true;

            //m_CleanUpDone = false; moved that to _Open();

            Protocol.Stream st = (Protocol.Stream)e;
            if (st == null)
                return;

            // Read the server language string
            m_ServerLanguage = st.Language;

            // Auth stuff
            if (!RegisterAccount)
            {
                if (this.StreamVersion != null && this.StreamVersion.StartsWith("1."))
                {
                    if (!Authenticated)
                    {
                        // we assume server supports SASL here, because it advertised a StreamVersion 1.X
                        // and wait for the stream features and initialize the SASL Handler
                        InitSaslHandler();
                    }
                }
                else
                {
                    // old auth stuff
                    RequestLoginInfo();
                }
            }
            else
            {
                // Register on "old" jabber servers without stream features
                if (this.StreamVersion == null)
                    GetRegistrationFields(null);
            }
        }
 public override void Parse(Node e)
 {
     // not needed here in PLAIN mechanism
 }
 void m_StreamParser_OnStreamElement(object sender, Node e)
 {
     //throw new NotImplementedException();
     StreamFactory.Add(new StreamInfo(e, this));
 }
Exemple #22
0
 private void sp_OnStreamElement(object sender, Node e)
 {
     doc.RootElement.ChildNodes.Add(e);
 }
 void m_StreamParser_OnStreamEnd(object sender, Node e)
 {
     //throw new NotImplementedException();
 }
Exemple #24
0
 // ya, the Streamparser is only usable for parsing xmpp stream.
 // it also does a very good job here
 private void sp_OnStreamStart(object sender, Node e)
 {
     doc.ChildNodes.Add(e);
 }
        void m_StreamParser_OnStreamStart(object sender, Node e)
        {
            //string ServerDomain = "dcboy.vicp.net";
            m_SessionID = Function.CreateSessionID();

            StringBuilder sb = new StringBuilder();
            sb.Append("<stream:stream from='");
            sb.Append(IMServer.SERVERNAME);
            sb.Append("' xmlns='");
            sb.Append(XMPPProtocol.Uri.CLIENT);
            sb.Append("' xmlns:stream='");
            sb.Append(XMPPProtocol.Uri.STREAM);
            sb.Append("' id='");
            sb.Append(m_SessionID);
            sb.Append("'>");
            Send(sb.ToString());
        }
Exemple #26
0
        private string BuildXml(Node e, Formatting format, int indent, char indentchar)
        {
            if ( e != null )
            {
                System.IO.StringWriter tw = new StringWriter();
                XmlTextWriter w = new XmlTextWriter(tw);
                w.Formatting	= format;
                w.Indentation	= indent;
                w.IndentChar	= indentchar;

                WriteTree(this, w, null);

                return tw.ToString();
            }
            else
            {
                return "";
            }
        }
        public override void StreamParserOnStreamElement(object sender, Node e)
        {
            base.StreamParserOnStreamElement (sender, e);

            if (e is Handshake)
            {
                m_Authenticated = true;

                if (OnLogin != null)
                    OnLogin(this);

                if (KeepAlive)
                    CreateKeepAliveTimer();
            }
            else if (e is Route)
            {
                if (OnRoute != null)
                    OnRoute(this, e as Route);
            }
            else if (e is Protocol.Error)
            {
                Protocol.Error streamErr = e as Protocol.Error;
                switch (streamErr.Condition)
                {
                    // Auth errors are important for the users here, so throw catch auth errors
                    // in a separate event here
                    case XMPPProtocol.Protocol.StreamErrorCondition.NotAuthorized:
                        // Authentication Error
                        if (OnAuthError != null)
                            OnAuthError(this, e as Element);
                        break;
                    default:
                        if (OnStreamError != null)
                            OnStreamError(this, e as Element);
                        break;
                }
            }
            else if (e is Message)
            {
                if (OnMessage != null)
                    OnMessage(this, e as Message);
            }
            else if (e is Presence)
            {
                if (OnPresence != null)
                    OnPresence(this, e as Presence);
            }
            else if (e is IQ)
            {
                if (OnIq != null)
                    OnIq(this, e as IQ);
            }
        }
Exemple #28
0
 /// <summary>
 /// Appends the given Element as child element
 /// </summary>
 /// <param name="e"></param>
 public virtual void AddChild(Node e)
 {
     this.m_ChildNodes.Add(e);
 }
        public override void StreamParserOnStreamEnd(object sender, Node e)
        {
            base.StreamParserOnStreamEnd (sender, e);

            if(!m_CleanUpDone)
                CleanupSession();
        }