public void Send(Node node, Encoding encoding)
        {
            var elem = node as DirectionalElement;

            if (elem != null && elem.To != null)
            {
                var type = node.GetType();

                if (type == typeof(Message))
                {
                    try
                    {
                        string nameFrom = elem.From.User.ToLowerInvariant();
                        var message = (Message)node;

                        if (message.Body != null)
                        {
                            signalrServiceClient.SendMessage(nameFrom, message.To.User.ToLowerInvariant(),
                                message.Body, -1, elem.From.Server);
                        }
                        else if (message.FirstChild.HasTag(typeof(Invite)))
                        {
                            signalrServiceClient.SendInvite(nameFrom, message.To.User.ToLowerInvariant(), elem.To.Server);
                        }
                    }
                    catch (Exception ex)
                    {
                        _log.ErrorFormat("Unexpected error, connectionId = {0}, {1}, {2}, {3}", Id,
                            ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : string.Empty);
                    }
                }
            }
        }
Exemplo n.º 2
0
		public XmppStreamEventArgs(string connectionId, Node node)
		{
			if (string.IsNullOrEmpty(connectionId)) throw new ArgumentNullException("connectionId");
			if (node == null) throw new ArgumentNullException("node");

			ConnectionId = connectionId;
			Node = node;
		}
Exemplo n.º 3
0
        /// <summary>
        /// </summary>
        /// <param name="e"> </param>
        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;
            }

            List.Add(e);
        }
Exemplo n.º 4
0
        public void SendTo(XmppStream to, Node node)
        {
            if (to == null) throw new ArgumentNullException("to");
            if (node == null) throw new ArgumentNullException("node");

            var connection = GetXmppConnection(to.ConnectionId);
            if (connection != null)
            {
                _log.DebugFormat(SEND_FORMAT, to.ConnectionId, to.Namespace, node.ToString(Formatting.Indented));
                connection.Send(node, Encoding.UTF8);
            }
        }
        public void ProcessStreamStart(Node node, string ns, XmppStream xmppStream)
        {
            try
            {
                var stream = node as Stream;
                if (stream == null)
                {
                    sender.SendToAndClose(xmppStream, XmppStreamError.BadFormat);
                    return;
                }
                if (!stream.HasTo)
                {
                    sender.SendToAndClose(xmppStream, XmppStreamError.ImproperAddressing);//TODO: Return something more correct^)
                    return;
                }
                if (!stream.To.IsServer)
                {
                    sender.SendToAndClose(xmppStream, XmppStreamError.ImproperAddressing);
                    return;
                }

                var handlers = handlerStorage.GetStreamStartHandlers(stream.To);
                if (handlers.Count == 0)
                {
                    sender.SendToAndClose(xmppStream, XmppStreamError.HostUnknown);
                    return;
                }

                var handler = handlers.Find(h => h.Namespace == ns);
                if (handler == null)
                {
                    sender.SendToAndClose(xmppStream, XmppStreamError.BadNamespacePrefix);
                    return;
                }

                xmppStream.Namespace = ns;
                xmppStream.Domain = stream.To.Server;
                xmppStream.Language = stream.Language;

                handler.StreamStartHandle(xmppStream, stream, context);
            }
            catch (Exception ex)
            {
                ProcessException(ex, node, xmppStream);
            }
        }
        /// <summary>
        /// </summary>
        /// <param name="e"> </param>
        public override void Parse(Node e)
        {
            if (e.GetType() == typeof (Challenge))
            {
                var c = e as Challenge;

                var step1 = new Step1(c.TextBase64);
                if (step1.Rspauth == null)
                {
                    // response xmlns="urn:ietf:params:xml:ns:xmpp-sasl">dXNlcm5hbWU9ImduYXVjayIscmVhbG09IiIsbm9uY2U9IjM4MDQzMjI1MSIsY25vbmNlPSIxNDE4N2MxMDUyODk3N2RiMjZjOWJhNDE2ZDgwNDI4MSIsbmM9MDAwMDAwMDEscW9wPWF1dGgsZGlnZXN0LXVyaT0ieG1wcC9qYWJiZXIucnUiLGNoYXJzZXQ9dXRmLTgscmVzcG9uc2U9NDcwMTI5NDU4Y2EwOGVjYjhhYTIxY2UzMDhhM2U5Nzc
                    var s2 = new Step2(step1, base.Username, base.Password, base.Server);
                    var r = new Response(s2.ToString());
                    //base.XmppClientConnection.Send(r);
                }
                else
                {
                    // SEND: <response xmlns="urn:ietf:params:xml:ns:xmpp-sasl"/>
                    //base.XmppClientConnection.Send(new Response());
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// </summary>
        /// <param name="e"> </param>
        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;

            List.Add(e);
        }
        private int ValidateNodeInternal(Node node)
        {
            int[] errorcount = {0};
            try
            {

                if (node is Element)
                {
                    List<XmlSchema> schemasUsed = new List<XmlSchema>();
                    AddSchemas(node as Element, schemasUsed);
                    if (schemasUsed.Count > 0)
                    {
                        using (StringReader reader = new StringReader(node.ToString()))
                        {
                            StringBuilder validationErrors = new StringBuilder();
                            XmlReaderSettings xmppSettings = new XmlReaderSettings
                                                                 {ValidationType = ValidationType.Schema};
                            xmppSettings.ValidationEventHandler += (x, y) =>
                                                                       {
                                                                           validationErrors.AppendLine(
                                                                               GetErrorString(x, y));
                                                                           errorcount[0]++;
                                                                       };
                        
                        
                            foreach (XmlSchema schema in schemasUsed)
                            {
                                xmppSettings.Schemas.Add(schema);
                            }


                            XmlReader validator = XmlReader.Create(reader, xmppSettings);
                            bool bContinue = true;
                            while (bContinue)
                            {
                                try
                                {

                                    bContinue = validator.Read();
                                }
                                catch (Exception ex)
                                {
                                    errorcount[0]++;
                                    validationErrors.AppendLine(ex.Message);
                                }
                            }
                            if (validationErrors.Length > 0)
                            {
                                log.DebugFormat("validation summary:{1} {0}", validationErrors.ToString(), Environment.NewLine);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return errorcount[0];
        }
Exemplo n.º 9
0
        /// <summary>
        /// </summary>
        /// <param name="buf"> </param>
        /// <param name="offset"> </param>
        /// <param name="ct"> </param>
        /// <param name="tok"> </param>
        private void StartTag(byte[] buf, int offset, ContentToken ct, TOK tok)
        {
            m_Depth++;
            int colon;
            string name;
            string prefix;
            var 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 = string.Empty;
            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, m_ns.DefaultNamespace ?? "");
                }
            }
            else
            {
                if (current != null)
                {
                    current.AddChild(newel);
                }

                current = newel;
            }
        }
Exemplo n.º 10
0
        /// <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();
        }
Exemplo n.º 11
0
        public void Send(Node node, Encoding encoding)
        {
            if (closed) return;
            lock (sendBuffer)
            {
                var text = node.ToString();
                if (log.IsDebugEnabled) log.DebugFormat("Add node {0} to send buffer connection {1}", 200 < text.Length ? text.Substring(0, 195) + " ... " : text, Id);

                sendBuffer.Enqueue(node);
                waitAnswer.Set();
            }
        }
Exemplo n.º 12
0
 private void StreamParserOnStreamElement(object sender, Node e)
 {
     packetSize = 0;
     var handler = XmppStreamElement;
     if (handler != null) handler(this, new XmppStreamEventArgs(Id, e));
 }
Exemplo n.º 13
0
		public XmppStreamStartEventArgs(string connectionId, Node node, string ns)
			: base(connectionId, node)
		{
			Namespace = ns;
		}
Exemplo n.º 14
0
 public bool Broadcast(ICollection<XmppSession> sessions, Node node)
 {
     if (sessions == null) throw new ArgumentNullException("sessions");
     foreach (var session in sessions)
     {
         try
         {
             SendTo(session, node);
         }
         catch (Exception ex)
         {
             log.ErrorFormat("Can not send to {0} in broadcast: {1}", session, ex);
         }
     }
     return 0 < sessions.Count;
 }
Exemplo n.º 15
0
 public void SendTo(XmppSession to, Node node)
 {
     if (to == null) throw new ArgumentNullException("to");
     SendTo(to.Stream, node);
 }
Exemplo n.º 16
0
        /// <summary>
        /// </summary>
        /// <param name="e"> </param>
        /// <param name="tw"> </param>
        /// <param name="parent"> </param>
        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"?> ...
                var 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)
            {
                var 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();
                }
            }
        }
Exemplo n.º 17
0
        /// <summary>
        /// </summary>
        /// <param name="e"> </param>
        /// <param name="format"> </param>
        /// <param name="indent"> </param>
        /// <param name="indentchar"> </param>
        /// <returns> </returns>
        private string BuildXml(Node e, Formatting format, int indent, char indentchar)
        {
            if (e != null)
            {
                var tw = new StringWriter();
                var w = new XmlTextWriter(tw);
                w.Formatting = format;
                w.Indentation = indent;
                w.IndentChar = indentchar;

                WriteTree(this, w, null);

                return tw.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
Exemplo n.º 18
0
 /// <summary>
 ///   Appends the given Element as child element
 /// </summary>
 /// <param name="e"> </param>
 public virtual void AddChild(Node e)
 {
     m_ChildNodes.Add(e);
 }
Exemplo n.º 19
0
 /// <summary>
 /// </summary>
 /// <param name="e"> </param>
 public abstract void Parse(Node e);
        public bool ValidateNode(Node node, XmppStream stream, XmppHandlerContext handlercontext)
        {
#if (SCHEME)
            var stopwatch = new Stopwatch();
            if (log.IsDebugEnabled)
            {
                stopwatch.Reset();
                stopwatch.Start();
            }
            int  result = ValidateNodeInternal(node);
            if (log.IsDebugEnabled)
            {
                stopwatch.Stop();
                log.DebugFormat("Node validated. Error count: {1}. time: {0}ms", stopwatch.ElapsedMilliseconds, result);
            }
            return result==0;
#else
            return true;
#endif
            
        }
Exemplo n.º 21
0
 /// <summary>
 /// </summary>
 /// <param name="e"> </param>
 public override void Parse(Node e)
 {
     // not needed here in PLAIN mechanism
 }
Exemplo n.º 22
0
 private void Send(byte[] buffer, Node node)
 {
     NetStatistics.WriteBytes(buffer.Length);
     sendStream.BeginWrite(buffer, 0, buffer.Length, BeginWriteCallback, new object[] { sendStream, node });
 }
Exemplo n.º 23
0
 public void SendToAndClose(XmppStream to, Node node)
 {
     SendTo(to, node);
     SendTo(to, string.Format("</stream:{0}>", Uri.PREFIX));
     CloseStream(to);
 }
Exemplo n.º 24
0
 /// <summary>
 /// </summary>
 /// <param name="sender"> </param>
 /// <param name="e"> </param>
 private void sp_OnStreamStart(object sender, Node e, string streamNamespace)
 {
     doc.ChildNodes.Add(e);
 }
Exemplo n.º 25
0
 public void Send(Node node, Encoding encoding)
 {
     if (node == null) throw new ArgumentNullException("node");
     if (encoding == null) throw new ArgumentNullException("encoding");
     Send(encoding.GetBytes(node.ToString(encoding)), node);
 }
Exemplo n.º 26
0
 /// <summary>
 /// </summary>
 /// <param name="sender"> </param>
 /// <param name="e"> </param>
 private void sp_OnStreamElement(object sender, Node e)
 {
     doc.RootElement.ChildNodes.Add(e);
 }
Exemplo n.º 27
0
 private void StreamParserOnStreamStart(object sender, Node e, string streamNamespace)
 {
     packetSize = 0;
     var handler = XmppStreamStart;
     if (handler != null) handler(this, new XmppStreamStartEventArgs(Id, e, streamNamespace));
 }
Exemplo n.º 28
0
 /// <summary>
 /// </summary>
 /// <param name="sender"> </param>
 /// <param name="e"> </param>
 private void sp_OnStreamEnd(object sender, Node e)
 {
 }
Exemplo n.º 29
0
 private void StreamParserOnStreamEnd(object sender, Node e)
 {
     packetSize = 0;
     OnStreamEnd();
 }
Exemplo n.º 30
0
 public bool Broadcast(ICollection<XmppSession> sessions, Node node)
 {
     if (sessions == null) throw new ArgumentNullException("sessions");
     foreach (var session in sessions)
     {
         if (!session.IsSignalRFake)
         {
             try
             {
                 SendTo(session, node);
             }
             catch (Exception ex)
             {
                 if (ex is IOException || ex is ObjectDisposedException)
                 {
                     // ignore
                 }
                 else
                 {
                     _log.ErrorFormat("Can not send to {0} in broadcast: {1}", session, ex);
                 }
             }
         }
     }
     return 0 < sessions.Count;
 }