2014-10-22 16:38:44 +00:00
|
|
|
package eu.siacs.conversations.parser;
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
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;
|
2016-05-17 12:25:58 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.InvalidJidException;
|
2014-11-05 20:55:47 +00:00
|
|
|
import eu.siacs.conversations.xmpp.jid.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-05-30 06:39:04 +00:00
|
|
|
long min = Long.MAX_VALUE;
|
|
|
|
boolean returnDefault = true;
|
|
|
|
for(Element child : element.getChildren()) {
|
|
|
|
if ("delay".equals(child.getName()) && "urn:xmpp:delay".equals(child.getNamespace())) {
|
|
|
|
String stamp = child.getAttribute("stamp");
|
|
|
|
if (stamp != null) {
|
|
|
|
try {
|
|
|
|
min = Math.min(min,AbstractParser.parseTimestamp(stamp));
|
|
|
|
returnDefault = false;
|
|
|
|
} catch (ParseException e) {
|
|
|
|
//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;
|
|
|
|
if (timestamp.charAt(19) == '.' && timestamp.length() >= 25) {
|
|
|
|
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);
|
2016-07-28 20:58:37 +00:00
|
|
|
contact.setLastResource(from.isBareJid() ? "" : from.getResourcepart());
|
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) {
|
2016-05-17 12:25:58 +00:00
|
|
|
final String local = conference.getJid().getLocalpart();
|
|
|
|
final String domain = conference.getJid().getDomainpart();
|
|
|
|
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 {
|
|
|
|
fullJid = Jid.fromParts(local, domain, nick);
|
|
|
|
} catch (InvalidJidException e) {
|
|
|
|
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);
|
2016-05-17 12:25:58 +00:00
|
|
|
user.setRealJid(realJid);
|
|
|
|
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
|
|
|
}
|