2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.parser;
|
|
|
|
|
2019-02-20 20:44:06 +00:00
|
|
|
|
2014-10-22 16:38:44 +00:00
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
2019-02-20 20:44:06 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2014-10-22 16:38:44 +00:00
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
import eu.siacs.conversations.entities.Account;
|
|
|
|
import eu.siacs.conversations.entities.Contact;
|
2016-05-17 12:25:58 +00:00
|
|
|
import eu.siacs.conversations.entities.Conversation;
|
|
|
|
import eu.siacs.conversations.entities.MucOptions;
|
2014-10-22 16:38:44 +00:00
|
|
|
import eu.siacs.conversations.services.XmppConnectionService;
|
|
|
|
import eu.siacs.conversations.xml.Element;
|
2018-04-28 14:26:40 +00:00
|
|
|
import eu.siacs.conversations.xmpp.InvalidJid;
|
2020-05-15 15:06:16 +00:00
|
|
|
import eu.siacs.conversations.xmpp.Jid;
|
2021-01-23 08:25:34 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
|
2014-10-22 16:38:44 +00:00
|
|
|
|
|
|
|
public abstract class AbstractParser {
|
|
|
|
|
|
|
|
protected XmppConnectionService mXmppConnectionService;
|
|
|
|
|
|
|
|
protected AbstractParser(XmppConnectionService service) {
|
|
|
|
this.mXmppConnectionService = service;
|
|
|
|
}
|
|
|
|
|
2016-06-04 14:16:14 +00:00
|
|
|
public static Long parseTimestamp(Element element, Long d) {
|
2017-06-01 14:45:00 +00:00
|
|
|
return parseTimestamp(element,d,false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Long parseTimestamp(Element element, Long d, boolean ignoreCsiAndSm) {
|
2017-05-30 06:39:04 +00:00
|
|
|
long min = Long.MAX_VALUE;
|
|
|
|
boolean returnDefault = true;
|
2017-06-01 14:45:00 +00:00
|
|
|
final Jid to;
|
|
|
|
if (ignoreCsiAndSm && element instanceof AbstractStanza) {
|
|
|
|
to = ((AbstractStanza) element).getTo();
|
|
|
|
} else {
|
|
|
|
to = null;
|
|
|
|
}
|
2017-05-30 06:39:04 +00:00
|
|
|
for(Element child : element.getChildren()) {
|
|
|
|
if ("delay".equals(child.getName()) && "urn:xmpp:delay".equals(child.getNamespace())) {
|
2018-04-28 14:26:40 +00:00
|
|
|
final Jid f = to == null ? null : InvalidJid.getNullForInvalid(child.getAttributeAsJid("from"));
|
2020-05-21 05:57:57 +00:00
|
|
|
if (f != null && (to.asBareJid().equals(f) || to.getDomain().equals(f))) {
|
2017-06-01 14:45:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
final String stamp = child.getAttribute("stamp");
|
2017-05-30 06:39:04 +00:00
|
|
|
if (stamp != null) {
|
|
|
|
try {
|
|
|
|
min = Math.min(min,AbstractParser.parseTimestamp(stamp));
|
|
|
|
returnDefault = false;
|
2017-11-16 11:47:49 +00:00
|
|
|
} catch (Throwable t) {
|
2017-05-30 06:39:04 +00:00
|
|
|
//ignore
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2017-05-30 06:39:04 +00:00
|
|
|
if (returnDefault) {
|
|
|
|
return d;
|
|
|
|
} else {
|
|
|
|
return min;
|
|
|
|
}
|
2015-05-15 03:14:15 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 14:16:14 +00:00
|
|
|
public static long parseTimestamp(Element element) {
|
|
|
|
return parseTimestamp(element, System.currentTimeMillis());
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 14:16:14 +00:00
|
|
|
public static long parseTimestamp(String timestamp) throws ParseException {
|
2014-12-04 15:20:28 +00:00
|
|
|
timestamp = timestamp.replace("Z", "+0000");
|
|
|
|
SimpleDateFormat dateFormat;
|
2016-06-04 14:16:14 +00:00
|
|
|
long ms;
|
2017-07-16 19:03:59 +00:00
|
|
|
if (timestamp.length() >= 25 && timestamp.charAt(19) == '.') {
|
2016-06-04 14:16:14 +00:00
|
|
|
String millis = timestamp.substring(19,timestamp.length() - 5);
|
|
|
|
try {
|
|
|
|
double fractions = Double.parseDouble("0" + millis);
|
|
|
|
ms = Math.round(1000 * fractions);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
ms = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ms = 0;
|
|
|
|
}
|
2021-01-23 08:25:34 +00:00
|
|
|
timestamp = timestamp.substring(0,19)+timestamp.substring(timestamp.length() -5);
|
2015-02-10 10:11:01 +00:00
|
|
|
dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.US);
|
2016-06-04 14:16:14 +00:00
|
|
|
return Math.min(dateFormat.parse(timestamp).getTime()+ms, System.currentTimeMillis());
|
2014-12-04 15:20:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 14:16:14 +00:00
|
|
|
protected void updateLastseen(final Account account, final Jid from) {
|
2014-12-21 20:43:58 +00:00
|
|
|
final Contact contact = account.getRoster().getContact(from);
|
2018-03-05 17:30:40 +00:00
|
|
|
contact.setLastResource(from.isBareJid() ? "" : from.getResource());
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected String avatarData(Element items) {
|
|
|
|
Element item = items.findChild("item");
|
|
|
|
if (item == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-05-14 12:42:21 +00:00
|
|
|
return item.findChildContent("data", "urn:xmpp:avatar:data");
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2016-05-17 12:25:58 +00:00
|
|
|
|
|
|
|
public static MucOptions.User parseItem(Conversation conference, Element item) {
|
2016-11-13 18:25:58 +00:00
|
|
|
return parseItem(conference,item, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static MucOptions.User parseItem(Conversation conference, Element item, Jid fullJid) {
|
2018-03-05 17:30:40 +00:00
|
|
|
final String local = conference.getJid().getLocal();
|
2020-05-17 08:24:46 +00:00
|
|
|
final String domain = conference.getJid().getDomain().toEscapedString();
|
2016-05-17 12:25:58 +00:00
|
|
|
String affiliation = item.getAttribute("affiliation");
|
|
|
|
String role = item.getAttribute("role");
|
|
|
|
String nick = item.getAttribute("nick");
|
2016-11-13 18:25:58 +00:00
|
|
|
if (nick != null && fullJid == null) {
|
|
|
|
try {
|
2018-03-05 17:30:40 +00:00
|
|
|
fullJid = Jid.of(local, domain, nick);
|
|
|
|
} catch (IllegalArgumentException e) {
|
2016-11-13 18:25:58 +00:00
|
|
|
fullJid = null;
|
|
|
|
}
|
2016-05-17 12:25:58 +00:00
|
|
|
}
|
|
|
|
Jid realJid = item.getAttributeAsJid("jid");
|
2016-11-13 18:25:58 +00:00
|
|
|
MucOptions.User user = new MucOptions.User(conference.getMucOptions(), fullJid);
|
2018-04-28 14:26:40 +00:00
|
|
|
if (InvalidJid.isValid(realJid)) {
|
|
|
|
user.setRealJid(realJid);
|
|
|
|
}
|
2016-05-17 12:25:58 +00:00
|
|
|
user.setAffiliation(affiliation);
|
|
|
|
user.setRole(role);
|
|
|
|
return user;
|
|
|
|
}
|
2016-10-26 10:26:04 +00:00
|
|
|
|
2022-08-01 08:14:49 +00:00
|
|
|
public static String extractErrorMessage(final Element packet) {
|
2016-10-26 10:26:04 +00:00
|
|
|
final Element error = packet.findChild("error");
|
|
|
|
if (error != null && error.getChildren().size() > 0) {
|
2019-02-20 20:44:06 +00:00
|
|
|
final List<String> errorNames = orderedElementNames(error.getChildren());
|
2016-10-26 10:26:04 +00:00
|
|
|
final String text = error.findChildContent("text");
|
|
|
|
if (text != null && !text.trim().isEmpty()) {
|
2019-02-20 20:44:06 +00:00
|
|
|
return prefixError(errorNames)+text;
|
|
|
|
} else if (errorNames.size() > 0){
|
|
|
|
return prefixError(errorNames)+errorNames.get(0).replace("-"," ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-01 21:39:56 +00:00
|
|
|
public static String errorMessage(Element packet) {
|
|
|
|
final Element error = packet.findChild("error");
|
|
|
|
if (error != null && error.getChildren().size() > 0) {
|
|
|
|
final List<String> errorNames = orderedElementNames(error.getChildren());
|
|
|
|
final String text = error.findChildContent("text");
|
|
|
|
if (text != null && !text.trim().isEmpty()) {
|
|
|
|
return text;
|
|
|
|
} else if (errorNames.size() > 0){
|
|
|
|
return errorNames.get(0).replace("-"," ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-20 20:44:06 +00:00
|
|
|
private static String prefixError(List<String> errorNames) {
|
|
|
|
if (errorNames.size() > 0) {
|
|
|
|
return errorNames.get(0)+'\u001f';
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<String> orderedElementNames(List<Element> children) {
|
|
|
|
List<String> names = new ArrayList<>();
|
|
|
|
for(Element child : children) {
|
|
|
|
final String name = child.getName();
|
|
|
|
if (name != null && !name.equals("text")) {
|
|
|
|
if ("urn:ietf:params:xml:ns:xmpp-stanzas".equals(child.getNamespace())) {
|
|
|
|
names.add(name);
|
|
|
|
} else {
|
|
|
|
names.add(0, name);
|
|
|
|
}
|
2016-10-26 10:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-20 20:44:06 +00:00
|
|
|
return names;
|
2016-10-26 10:26:04 +00:00
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|