Exemple #1
0
        /// <summary>
        /// Request the Search Form
        /// </summary>
        /// <returns>DataForm for avaible fields search</returns>
        public DataForm RequestSearchForm()
        {
            Jid searchDomain = SearchDomain();

            if (searchDomain == null)
            {
                throw new Exception("Feauture not supported");
            }

            Iq iq = im.IqRequest(IqType.Get, searchDomain, im.Jid, Xml.Element("query", xmlns));

            if (iq.Type == IqType.Result)
            {
                var query = iq.Data["query"];
                if (query == null || query.NamespaceURI != xmlns)
                {
                    throw new XmppException("Erroneous server response.");
                }

                return(DataFormFactory.Create(query["x"]));
            }
            else
            {
                var error = iq.Data["error"];
                throw new Exception(error["text"].InnerText);
            }
        }
Exemple #2
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.");
            }
        }
Exemple #3
0
 public static DataForm Parse(XmlElement feature)
 {
     feature.ThrowIfNull <XmlElement>("feature");
     if (((feature.Name != "feature") || (feature.NamespaceURI != "http://jabber.org/protocol/feature-neg")) || (feature["x"] == null))
     {
         throw new ArgumentException("Invalid XML 'feature' element.");
     }
     return(DataFormFactory.Create(feature["x"]));
 }
 /// <summary>
 /// Parses the the specified negotiation offer or result.
 /// </summary>
 /// <param name="feature">The 'feature' element containing the data-form</param>
 /// <returns>An initialized data-form instance.</returns>
 /// <exception cref="ArgumentNullException">The feature parameter is
 /// null.</exception>
 /// <exception cref="ArgumentException">The feature parameter is not a
 /// valid 'feature' XML element, or the feature element contains invalid
 /// data.</exception>
 public static DataForm Parse(XElement feature)
 {
     feature.ThrowIfNull("feature");
     if (feature.Name != "feature" || feature.GetDefaultNamespace().NamespaceName !=
         "http://jabber.org/protocol/feature-neg" || feature.Element("x") == null)
     {
         throw new ArgumentException("Invalid XML 'feature' element.");
     }
     return(DataFormFactory.Create(feature.Element("x")));
 }
Exemple #5
0
        private RequestForm RequestRoomConfigForm(Jid room)
        {
            Iq iq = im.IqRequest(IqType.Get, room, im.Jid, Xml.Element("query", MucNs.NsOwner));

            if (iq.Type != IqType.Result)
            {
                throw new NotSupportedException("Could not query features: " + iq);
            }

            // Parse the result.
            var query = iq.Data["query"];

            if (query == null || query.NamespaceURI != MucNs.NsOwner)
            {
                throw new NotSupportedException("Erroneous response: " + iq);
            }
            return(DataFormFactory.Create(query["x"]) as RequestForm);
        }
Exemple #6
0
        public DataForm RequestRegistration(Jid room)
        {
            Iq iq = im.IqRequest(IqType.Get, room, im.Jid, Xml.Element("query", "jabber:iq:register"));

            if (iq.Type != IqType.Result)
            {
                throw new NotSupportedException("Could not query features: " + iq);
            }

            // Parse the result.
            var query = iq.Data["query"];

            if (query == null || query.NamespaceURI != "jabber:iq:register")
            {
                throw new NotSupportedException("Erroneous response: " + iq);
            }

            return(DataFormFactory.Create(query["x"]));
        }
        /// <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.
        }
        /// <summary>
        /// Fetch a page of archived messages
        /// </summary>
        /// <param name="pageRequest">Paging options</param>
        /// <param name="with">Optional filter to only return messages if they match the supplied JID</param>
        /// <param name="roomId">Optional filter to only return messages if they match the supplied JID</param>
        /// <param name="start">Optional filter to only return messages whose timestamp is equal to or later than the given timestamp.</param>
        /// <param name="end">Optional filter to only return messages whose timestamp is equal to or earlier than the timestamp given in the 'end' field.</param>
        internal Task <XmppPage <Im.Message> > GetArchivedMessages(XmppPageRequest pageRequest, Jid with = null, Jid roomId = null, DateTimeOffset?start = null, DateTimeOffset?end = null)
        {
            Core.Iq iq = im.IqRequest(Core.IqType.Get, null, im.Jid, Xml.Element("query", xmlns));
            if (iq.Type == Core.IqType.Result)
            {
                var query = iq.Data["query"];
                if (query == null || query.NamespaceURI != xmlns)
                {
                    throw new XmppException("Erroneous server response.");
                }

                DataForm form = DataFormFactory.Create(query["x"]);

                string queryId = Guid.NewGuid().ToString().Replace("-", "");

                var request = Xml.Element("query", xmlns)
                              .Attr("queryid", queryId)
                              .Child(pageRequest.ToXmlElement());
                var filterForm = new SubmitForm();

                foreach (var campo in form.Fields)
                {
                    if (campo.Type == DataFieldType.Hidden)
                    {
                        filterForm.Fields.Add(campo);
                    }
                    if (campo.Name == "with" && with != null)
                    {
                        filterForm.AddUntypedValue("with", with);
                    }
                    if (campo.Name == "start" && start.HasValue)
                    {
                        filterForm.AddUntypedValue("start", DateTimeProfiles.ToXmppDateTimeString(start.Value));
                    }
                    if (campo.Name == "end" && end.HasValue)
                    {
                        filterForm.AddUntypedValue("end", DateTimeProfiles.ToXmppDateTimeString(end.Value));
                    }
                }

                request.Child(filterForm.ToXmlElement());


                var tcs       = new TaskCompletionSource <XmppPage <Im.Message> >();
                var queryTask = pendingQueries[queryId] = new ArchiveQueryTask(tcs);

                im.IqRequestAsync(Net.Xmpp.Core.IqType.Set, roomId, null, request, null,
                                  (string id, Core.Iq response) =>
                {
                    if (response.Type == Core.IqType.Error)
                    {
                        queryTask.SetException(Util.ExceptionFromError(response, "Failed to get archived messages"));
                    }
                    else
                    {
                        TryFinaliseQuery(response.Data);
                    }
                });

                return(tcs.Task);
            }
            else
            {
                var error = iq.Data["error"];
                throw new Exception(error["text"].InnerText);
            }
        }