Esempio n. 1
0
        private static IList <ArchivedMessage> GetChatMessagesFromStanza(XmlElement xml)
        {
            List <ArchivedMessage> messages = new List <ArchivedMessage>();

            DateTimeOffset startTime      = default(DateTimeOffset);
            var            startAttribute = xml.Attributes["start"];

            if (startAttribute != null)
            {
                startTime = DateTimeProfiles.FromXmppString(startAttribute.InnerText);
            }

            var messageNodes = xml.GetElementsByTagName("from");

            foreach (XmlElement node in messageNodes)
            {
                messages.Add(GetChatMessageFromNode(startTime, node));
            }

            messageNodes = xml.GetElementsByTagName("to");

            foreach (XmlElement node in messageNodes)
            {
                messages.Add(GetChatMessageFromNode(startTime, node));
            }

            return(messages.OrderBy(m => m.Timestamp).ToList());
        }
Esempio n. 2
0
        /// <summary>
        /// Create an archived chat page from an xml response
        /// </summary>
        /// <param name="xml"></param>
        internal ArchivedChatPage(XmlElement xml)
            : base(xml, GetChatMessagesFromStanza)
        {
            var withAttribute = xml.Attributes["with"];

            if (withAttribute != null)
            {
                With = withAttribute.InnerText;
            }

            var startAttribute = xml.Attributes["start"];

            if (startAttribute != null)
            {
                Start = DateTimeProfiles.FromXmppString(startAttribute.InnerText);
            }

            var subjectAttribute = xml.Attributes["subject"];

            if (subjectAttribute != null)
            {
                Subject = subjectAttribute.InnerText;
            }

            var versionAttribute = xml.Attributes["version"];

            if (versionAttribute != null)
            {
                int version = 0;

                int.TryParse(versionAttribute.InnerText, out version);

                Version = version;
            }
        }
Esempio n. 3
0
        private static IList <ArchivedChatId> GetChatIdsFromStanza(XmlElement xml)
        {
            List <ArchivedChatId> chats = new List <ArchivedChatId>();
            var chatNodes = xml.GetElementsByTagName("chat");

            foreach (XmlNode node in chatNodes)
            {
                string with = null;
                try
                {
                    with = node.Attributes["with"].InnerText;
                }
                catch
                {
                }

                DateTimeOffset start = default(DateTimeOffset);
                try
                {
                    string startText = node.Attributes["start"].InnerText;
                    start = DateTimeProfiles.FromXmppString(startText);
                }
                catch
                {
                }

                chats.Add(new ArchivedChatId(with, start));
            }

            return(chats);
        }
Esempio n. 4
0
        /// <summary>
        /// Try to get a timestamp from a delay node. If this fails, return the current UTC time.
        /// </summary>
        /// <param name="xml">The xml node that contains a delay node</param>
        public static DateTimeOffset GetDelayedTimestampOrNow(XmlElement xml)
        {
            DateTimeOffset timestamp = DateTimeOffset.UtcNow;

            // Refer to XEP-0203.
            var delay = xml["delay"];

            if (delay != null && delay.NamespaceURI == "urn:xmpp:delay")
            {
                var stampAttribute = delay.GetAttribute("stamp");
                if (stampAttribute != null)
                {
                    timestamp = DateTimeProfiles.FromXmppString(stampAttribute);
                }
            }

            return(timestamp);
        }
Esempio n. 5
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);
        }