Example #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));
        }
Example #2
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"]));
        }
        /// <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);
            }
        }