Пример #1
0
        /// <summary>
        /// Invoked when an IQ stanza is being received.
        /// </summary>
        /// <param name="stanza">The stanza which is being received.</param>
        /// <returns>true to intercept the stanza or false to pass the stanza
        /// on to the next handler.</returns>
        public bool Input(Iq stanza)
        {
            if (stanza.Type != IqType.Get)
            {
                return(false);
            }
            var e = stanza.Data["data"];

            if (e == null || e.NamespaceURI != "urn:xmpp:bob")
            {
                return(false);
            }
            string cid = e.GetAttribute("cid");

            if (cache.ContainsKey(cid))
            {
                BobData b    = cache[cid];
                var     data = Xml.Element("data", "urn:xmpp:bob").Attr("cid", cid)
                               .Attr("type", b.Type)
                               .Text(Convert.ToBase64String(b.Data));
                im.IqResult(stanza, data);
            }
            else
            {
                // If we don't have the item, return an 'item not found' error.
                im.IqError(stanza, ErrorType.Cancel, ErrorCondition.ItemNotFound);
            }
            // We took care of this IQ request, so intercept it and don't pass it
            // on to other handlers.
            return(true);
        }
Пример #2
0
        public bool Input(Iq stanza)
        {
            if (stanza.Type != IqType.Get)
            {
                return(false);
            }
            XmlElement element = stanza.Data["data"];

            if ((element == null) || (element.NamespaceURI != "urn:xmpp:bob"))
            {
                return(false);
            }
            string attribute = element.GetAttribute("cid");

            if (this.cache.ContainsKey(attribute))
            {
                BobData    data     = this.cache[attribute];
                XmlElement element2 = Xml.Element("data", "urn:xmpp:bob").Attr("cid", attribute).Attr("type", data.Type).Text(Convert.ToBase64String(data.Data));
                base.im.IqResult(stanza, element2);
            }
            else
            {
                base.im.IqError(stanza, ErrorType.Cancel, ErrorCondition.ItemNotFound, null, new XmlElement[0]);
            }
            return(true);
        }
Пример #3
0
        public static BobData Parse(XmlElement data)
        {
            BobData data2;

            if (data.NamespaceURI != "urn:xmpp:bob")
            {
                throw new ArgumentException("Invalid namespace attribute.");
            }
            string attribute = data.GetAttribute("type");

            if (string.IsNullOrEmpty(attribute))
            {
                throw new ArgumentException("The type attribute is missing.");
            }
            string str2 = data.GetAttribute("cid");

            if (string.IsNullOrEmpty(str2))
            {
                throw new ArgumentException("The cid attribute is missing.");
            }
            try
            {
                data2 = new BobData(Convert.FromBase64String(data.InnerText), attribute, str2);
            }
            catch (FormatException exception)
            {
                throw new ArgumentException("Invalid Base64 data.", exception);
            }
            return(data2);
        }
Пример #4
0
        public void Register(RegistrationCallback callback)
        {
            callback.ThrowIfNull <RegistrationCallback>("callback");
            Iq errorIq = base.im.IqRequest(IqType.Get, null, null, Xml.Element("query", "jabber:iq:register"), null, -1, "");

            if (errorIq.Type == IqType.Error)
            {
                throw new NotSupportedException("The XMPP server does not support the 'In-Band Registration' extension.");
            }
            XmlElement query = errorIq.Data["query"];

            if ((query == null) || (query.NamespaceURI != "jabber:iq:register"))
            {
                throw new XmppException("Erroneous server response: " + errorIq);
            }
            if (query["registered"] != null)
            {
                throw new XmppException("The XMPP entity is already registered.");
            }
            XmlElement element2 = query["data"];

            if ((element2 != null) && (element2.NamespaceURI == "urn:xmpp:bob"))
            {
                BobData bob = BobData.Parse(element2);
                this.bob.Add(bob);
            }
            RequestForm form = null;
            bool        flag = query["x"] != null;

            if (flag)
            {
                form = DataFormFactory.Create(query["x"]) as RequestForm;
            }
            else
            {
                form = this.CreateDataForm(query);
            }
            SubmitForm form2 = callback(form);
            XmlElement e     = Xml.Element("query", "jabber:iq:register");

            if (flag)
            {
                e.Child(form2.ToXmlElement());
            }
            else
            {
                foreach (DataField field in form2.Fields)
                {
                    e.Child(Xml.Element(field.Name, null).Text(field.Values.FirstOrDefault <string>()));
                }
            }
            errorIq = base.im.IqRequest(IqType.Set, null, null, e, null, -1, "");
            if (errorIq.Type == IqType.Error)
            {
                throw Util.ExceptionFromError(errorIq, "The registration could not be completed.");
            }
        }
Пример #5
0
        public BobData Create(byte[] data, string type, bool cache = true)
        {
            data.ThrowIfNull <byte[]>("data");
            type.ThrowIfNull <string>("type");
            BobData data2 = new BobData(data, type);

            if (cache)
            {
                this.cache[data2.Cid] = data2;
            }
            return(data2);
        }
Пример #6
0
        /// <summary>
        /// Creates a new bit of binary data.
        /// </summary>
        /// <param name="data">The data to embed.</param>
        /// <param name="type">The content-type of the data.</param>
        /// <param name="cache">true to cache the newly created bit of binary
        /// data.</param>
        /// <returns>An initialized instance of the BobData class representing the
        /// bit of binary data.</returns>
        /// <exception cref="ArgumentNullException">The data parameter or the type
        /// parameter is null.</exception>
        public BobData Create(byte[] data, string type, bool cache = true)
        {
            data.ThrowIfNull("data");
            type.ThrowIfNull("type");
            BobData b = new BobData(data, type);

            if (cache)
            {
                this.cache[b.Cid] = b;
            }
            return(b);
        }
Пример #7
0
        /// <summary>
        /// Retrieves the data-item with the specified CID from the XMPP entity
        /// with the specified JID.
        /// </summary>
        /// <param name="cid">The CID of the binary data to retrieve.</param>
        /// <param name="from">The JID of the XMPP entity to request the data
        /// from.</param>
        /// <param name="cache">true to store the requested item in the local
        /// cache for future references.</param>
        /// <returns>The data-item with the specified CID.</returns>
        /// <exception cref="ArgumentNullException">The cid parameter or the from
        /// parameter is null.</exception>
        /// <exception cref="NotSupportedException">The XMPP entity with
        /// the specified JID does not support the 'Bits of Binary' XMPP
        /// extension.</exception>
        /// <exception cref="XmppErrorException">The server returned an XMPP error code.
        /// Use the Error property of the XmppErrorException to obtain the specific
        /// error condition.</exception>
        /// <exception cref="XmppException">The server returned invalid data or another
        /// unspecified XMPP error occurred.</exception>
        public BobData Get(string cid, Jid from, bool cache = true)
        {
            cid.ThrowIfNull("cid");
            from.ThrowIfNull("from");
            // If the data is already in the cache, return it.
            if (this.cache.ContainsKey(cid))
            {
                return(this.cache[cid]);
            }
            if (!ecapa.Supports(from, Extension.BitsOfBinary))
            {
                throw new NotSupportedException("The XMPP entity does not support " +
                                                "the 'Bits of Binary' extension.");
            }
            // Request the data.
            Iq iq = im.IqRequest(IqType.Get, from, im.Jid,
                                 Xml.Element("data", "urn:xmpp:bob").Attr("cid", cid));

            if (iq.Type == IqType.Error)
            {
                throw Util.ExceptionFromError(iq, "The data-item with the specified " +
                                              "CID could not be retrieved.");
            }
            var data = iq.Data["data"];

            if (data == null || data.NamespaceURI != "urn:xmpp:bob")
            {
                throw new XmppException("Erroneous response.");
            }
            try {
                // Parse the response 'data' element.
                BobData b = BobData.Parse(data);
                if (cache)
                {
                    this.cache[cid] = b;
                }
                return(b);
            } catch (ArgumentException e) {
                throw new XmppException("The retrieved data-item could not be " +
                                        "processed.", e);
            }
        }
Пример #8
0
        public BobData Get(string cid, Jid from, bool cache = true)
        {
            BobData data2;

            cid.ThrowIfNull <string>("cid");
            from.ThrowIfNull <Jid>("from");
            if (this.cache.ContainsKey(cid))
            {
                return(this.cache[cid]);
            }
            if (!this.ecapa.Supports(from, new Extension[] { Extension.BitsOfBinary }))
            {
                throw new NotSupportedException("The XMPP entity does not support the 'Bits of Binary' extension.");
            }
            Iq errorIq = base.im.IqRequest(IqType.Get, from, base.im.Jid, Xml.Element("data", "urn:xmpp:bob").Attr("cid", cid), null, -1, "");

            if (errorIq.Type == IqType.Error)
            {
                throw Util.ExceptionFromError(errorIq, "The data-item with the specified CID could not be retrieved.");
            }
            XmlElement element = errorIq.Data["data"];

            if ((element == null) || (element.NamespaceURI != "urn:xmpp:bob"))
            {
                throw new XmppException("Erroneous response.");
            }
            try
            {
                BobData data = BobData.Parse(element);
                if (cache)
                {
                    this.cache[cid] = data;
                }
                data2 = data;
            }
            catch (ArgumentException exception)
            {
                throw new XmppException("The retrieved data-item could not be processed.", exception);
            }
            return(data2);
        }
Пример #9
0
        /// <summary>
        /// Registers a new XMPP account on the connected XMPP server.
        /// </summary>
        /// <param name="callback">A callback method invoked during the registration
        /// process to gather user information.</param>
        /// <exception cref="ArgumentNullException">The callback parameter is
        /// null.</exception>
        /// <exception cref="NotSupportedException">The XMPP entity with
        /// the specified JID does not support the 'In-Band Registration' XMPP
        /// extension.</exception>
        /// <exception cref="XmppErrorException">The server returned an XMPP error code.
        /// Use the Error property of the XmppErrorException to obtain the specific
        /// error condition.</exception>
        /// <exception cref="XmppException">The server returned invalid data or another
        /// unspecified XMPP error occurred.</exception>
        public void Register(RegistrationCallback callback)
        {
            callback.ThrowIfNull("callback");
            Iq iq = im.IqRequest(IqType.Get, null, null,
                                 Xml.Element("query", "jabber:iq:register"));

            if (iq.Type == IqType.Error)
            {
                throw new NotSupportedException("The XMPP server does not support the " +
                                                "'In-Band Registration' extension.");
            }
            var query = iq.Data["query"];

            if (query == null || query.NamespaceURI != "jabber:iq:register")
            {
                throw new XmppException("Erroneous server response: " + iq);
            }
            if (query["registered"] != null)
            {
                throw new XmppException("The XMPP entity is already registered.");
            }
            // If the IQ contains binary data, cache it.
            var data = query["data"];

            if (data != null && data.NamespaceURI == "urn:xmpp:bob")
            {
                BobData bobData = BobData.Parse(data);
                bob.Add(bobData);
            }
            RequestForm form  = null;
            bool        xdata = query["x"] != null;

            if (xdata)
            {
                form = DataFormFactory.Create(query["x"]) as RequestForm;
            }
            // "Traditional" registration, create a data-form off the provided fields.
            else
            {
                form = CreateDataForm(query);
            }
            // Hand the data-form to the caller to have it filled-out.
            var submit = callback.Invoke(form);
            // Construct the response element.
            var xml = Xml.Element("query", "jabber:iq:register");

            // Convert the data-form back to traditional fields if needed.
            if (xdata)
            {
                xml.Child(submit.ToXmlElement());
            }
            else
            {
                foreach (var field in submit.Fields)
                {
                    xml.Child(Xml.Element(field.Name).Text(
                                  field.Values.FirstOrDefault()));
                }
            }
            iq = im.IqRequest(IqType.Set, null, null, xml);
            if (iq.Type == IqType.Error)
            {
                throw Util.ExceptionFromError(iq, "The registration could not be " +
                                              "completed.");
            }
            // Reconnect.
        }
Пример #10
0
 public void Add(BobData bob)
 {
     bob.ThrowIfNull <BobData>("bob");
     this.cache[bob.Cid] = bob;
 }
Пример #11
0
 /// <summary>
 /// Adds the specified BobData object to the cache of cached bits-of-binary
 /// data.
 /// </summary>
 /// <param name="bob">The BobData object to add to the cache.</param>
 /// <exception cref="ArgumentNullException">The bob parameter is
 /// null.</exception>
 public void Add(BobData bob)
 {
     bob.ThrowIfNull("bob");
     cache[bob.Cid] = bob;
 }