2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.parser;
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
2017-06-01 14:45:00 +00:00
|
|
|
|
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;
|
2017-06-01 14:45:00 +00:00
|
|
|
import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
|
2018-03-05 17:30:40 +00:00
|
|
|
import rocks.xmpp.addr.Jid;
|
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"));
|
2018-03-05 17:30:40 +00:00
|
|
|
if (f != null && (to.asBareJid().equals(f) || to.getDomain().equals(f.toString()))) {
|
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;
|
|
|
|
}
|
2015-02-10 10:11:01 +00:00
|
|
|
timestamp = timestamp.substring(0,19)+timestamp.substring(timestamp.length() -5,timestamp.length());
|
|
|
|
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();
|
|
|
|
final String domain = conference.getJid().getDomain();
|
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
|
|
|
|
|
|
|
public static String extractErrorMessage(Element packet) {
|
|
|
|
final Element error = packet.findChild("error");
|
|
|
|
if (error != null && error.getChildren().size() > 0) {
|
|
|
|
final String text = error.findChildContent("text");
|
|
|
|
if (text != null && !text.trim().isEmpty()) {
|
|
|
|
return text;
|
|
|
|
} else {
|
|
|
|
return error.getChildren().get(0).getName().replace("-"," ");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|