Пример #1
0
        /// <summary>
        /// Fetch message history from the server.
        ///
        /// The 'start' and 'end' attributes MAY be specified to indicate a date range.
        ///
        /// If the 'with' attribute is omitted then collections with any JID are returned.
        ///
        /// If only 'start' is specified then all collections on or after that date should be returned.
        ///
        /// If only 'end' is specified then all collections prior to that date should be returned.
        /// </summary>
        /// <param name="pageRequest">Paging options</param>
        /// <param name="start">Optional start date range to query</param>
        /// <param name="end">Optional enddate range to query</param>
        /// <param name="with">Optional JID to filter archive results by</param>
        public XmppPage <ArchivedChatId> GetArchivedChatIds(XmppPageRequest pageRequest, DateTimeOffset?start = null, DateTimeOffset?end = null, Jid with = null)
        {
            pageRequest.ThrowIfNull();

            var request = Xml.Element("list", xmlns);

            if (with != null)
            {
                request.Attr("with", with.ToString());
            }

            if (start != null)
            {
                request.Attr("start", start.Value.ToXmppDateTimeString());
            }

            if (end != null)
            {
                request.Attr("end", end.Value.ToXmppDateTimeString());
            }

            var setNode = pageRequest.ToXmlElement();

            request.Child(setNode);

            var response = IM.IqRequest(IqType.Get, null, null, request);

            if (response.Type == IqType.Error)
            {
                throw Util.ExceptionFromError(response, "Failed to get archived chat ids");
            }

            return(new XmppPage <ArchivedChatId>(response.Data["list"], GetChatIdsFromStanza));
        }
Пример #2
0
        public List <AdHocCommand> GetAdHocCommands()
        {
            var query    = Xml.Element("query", "http://jabber.org/protocol/disco#items").Attr("node", "http://jabber.org/protocol/commands");
            var response = IM.IqRequest(IqType.Get, IM.Hostname, IM.Jid, query);
            var commands = response.Data["query"].GetElementsByTagName("item").Cast <XmlElement>().Select(e => new AdHocCommand(e)).ToList();

            return(commands);
        }
Пример #3
0
        public void DeleteUser(Jid userId)
        {
            // declare namespaces
            XNamespace commandNamespace = "http://jabber.org/protocol/commands";
            XNamespace xNamespace       = "jabber:x:data";

            // request to delete a user
            var command1 = new XElement(commandNamespace + "command",
                                        new XAttribute("action", "execute"),
                                        new XAttribute("node", "http://jabber.org/protocol/admin#delete-user"));

            var response1 = IM.IqRequest(IqType.Set, IM.Hostname, IM.Jid, command1.ToXmlElement());

            response1.ThrowIfError();

            // delete user
            var sessionId = response1.Data["command"].Attributes["sessionid"].Value;
            var command2  = new XElement(commandNamespace + "command",
                                         new XAttribute("node", "http://jabber.org/protocol/admin#delete-user"),
                                         new XAttribute("sessionid", sessionId),
                                         new XElement(xNamespace + "x",
                                                      new XAttribute("type", "submit"),
                                                      new XElement(xNamespace + "field",
                                                                   new XAttribute("type", "hidden"),
                                                                   new XAttribute("var", "FORM_TYPE"),
                                                                   new XElement(xNamespace + "value", "http://jabber.org/protocol/admin")),
                                                      new XElement(xNamespace + "field",
                                                                   new XAttribute("var", "accountjids"),
                                                                   new XElement(xNamespace + "value", userId))));

            var response2 = IM.IqRequest(IqType.Set, IM.Hostname, IM.Jid, command2.ToXmlElement());

            response2.ThrowIfError();

            // validate response
            var noteElement = response2.ToXDocument().Root.Element(commandNamespace + "command").Element(commandNamespace + "note");
            var noteText    = noteElement == null ? null : noteElement.Value;

            if (noteText != null)
            {
                // todo: this is weak but no other way to identify
                if (noteText.StartsWith("The following accounts could not be deleted:"))
                {
                    throw new Exception("The user could not be deleted");
                }
            }
        }
Пример #4
0
        /// <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 <Message> > GetArchivedMessages(XmppPageRequest pageRequest, Jid with = null, Jid roomId = null, DateTimeOffset?start = null, DateTimeOffset?end = null)
        {
            string queryId = Guid.NewGuid().ToString().Replace("-", "");

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

            if (with != null || start != null || end != null)
            {
                var filterForm = new SubmitForm();

                filterForm.AddValue("FORM_TYPE", DataFieldType.Hidden, xmlns);

                if (with != null)
                {
                    filterForm.AddUntypedValue("with", with);
                }

                if (start != null)
                {
                    filterForm.AddUntypedValue("start", DateTimeProfiles.ToXmppDateTimeString(start.Value));
                }

                if (end != null)
                {
                    filterForm.AddUntypedValue("end", DateTimeProfiles.ToXmppDateTimeString(end.Value));
                }

                request.Child(filterForm.ToXmlElement());
            }

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

            var response = IM.IqRequest(Sharp.Xmpp.Core.IqType.Set, roomId, null, request);

            if (response.Type == Core.IqType.Error)
            {
                queryTask.SetException(Util.ExceptionFromError(response, "Failed to get archived messages"));
            }

            return(tcs.Task);
        }
Пример #5
0
        /// <summary>
        /// Fetch a page of archived messages from a chat
        /// </summary>
        /// <param name="pageRequest">Paging options</param>
        /// <param name="with">The id of the entity that the chat was with</param>
        /// <param name="start">The start time of the chat</param>
        public ArchivedChatPage GetArchivedChat(XmppPageRequest pageRequest, Jid with, DateTimeOffset start)
        {
            pageRequest.ThrowIfNull();
            with.ThrowIfNull();

            var request = Xml.Element("retrieve", xmlns);

            request.Attr("with", with.ToString());
            request.Attr("start", start.ToXmppDateTimeString());

            var setNode = pageRequest.ToXmlElement();

            request.Child(setNode);

            var response = IM.IqRequest(IqType.Get, null, null, request);

            if (response.Type == IqType.Error)
            {
                throw Util.ExceptionFromError(response, "Failed to get archived chat messages");
            }

            return(new ArchivedChatPage(response.Data["chat"]));
        }
Пример #6
0
        public void AddUser(Jid userId, string password, string verifiedPassword, string email, string firstName, string lastName)
        {
            // validate
            userId.ThrowIfNull("user ID");
            userId.Node.ThrowIfNullOrEmpty("user node");
            userId.Domain.ThrowIfNullOrEmpty("user domain");
            password.ThrowIfNullOrEmpty("password");
            email.ThrowIfNullOrEmpty("email");
            firstName.ThrowIfNullOrEmpty("first name");
            lastName.ThrowIfNullOrEmpty("last name");

            if (password != verifiedPassword)
            {
                throw new Exception("passwords do not match");
            }

            // declare namespaces
            XNamespace commandNamespace = "http://jabber.org/protocol/commands";
            XNamespace xNamespace       = "jabber:x:data";

            // request to add a user
            var command1 = new XElement(commandNamespace + "command",
                                        new XAttribute("action", "execute"),
                                        new XAttribute("node", "http://jabber.org/protocol/admin#add-user"));

            var response1 = IM.IqRequest(IqType.Set, IM.Hostname, IM.Jid, command1.ToXmlElement());

            response1.ThrowIfError();

            // add user
            var sessionId = response1.Data["command"].Attributes["sessionid"].Value;
            var command2  = new XElement(commandNamespace + "command",
                                         new XAttribute("node", "http://jabber.org/protocol/admin#add-user"),
                                         new XAttribute("sessionid", sessionId),
                                         new XElement(xNamespace + "x",
                                                      new XAttribute("type", "submit"),
                                                      new XElement(xNamespace + "field",
                                                                   new XAttribute("type", "hidden"),
                                                                   new XAttribute("var", "FORM_TYPE"),
                                                                   new XElement(xNamespace + "value", "http://jabber.org/protocol/admin")),
                                                      new XElement(xNamespace + "field",
                                                                   new XAttribute("var", "accountjid"),
                                                                   new XElement(xNamespace + "value", userId)),
                                                      new XElement(xNamespace + "field",
                                                                   new XAttribute("var", "password"),
                                                                   new XElement(xNamespace + "value", password)),
                                                      new XElement(xNamespace + "field",
                                                                   new XAttribute("var", "password-verify"),
                                                                   new XElement(xNamespace + "value", verifiedPassword)),
                                                      new XElement(xNamespace + "field",
                                                                   new XAttribute("var", "email"),
                                                                   new XElement(xNamespace + "value", email)),
                                                      new XElement(xNamespace + "field",
                                                                   new XAttribute("var", "given_name"),
                                                                   new XElement(xNamespace + "value", firstName)),
                                                      new XElement(xNamespace + "field",
                                                                   new XAttribute("var", "surname"),
                                                                   new XElement(xNamespace + "value", lastName))));

            var response2 = IM.IqRequest(IqType.Set, IM.Hostname, IM.Jid, command2.ToXmlElement());

            response2.ThrowIfError();
        }