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()); }
/// <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; } }
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); }
/// <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); }
/// <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); } }