コード例 #1
0
ファイル: BitsOfBinary.cs プロジェクト: luz-sf/Net.Xmpp
        /// <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);
            }
        }
コード例 #2
0
ファイル: InBandRegistration.cs プロジェクト: luz-sf/Net.Xmpp
        /// <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.
        }