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) {
|
2015-05-15 04:31:27 +00:00
|
|
|
Element delay = element.findChild("delay","urn:xmpp:delay");
|
2015-05-15 03:14:15 +00:00
|
|
|
if (delay != null) {
|
|
|
|
String stamp = delay.getAttribute("stamp");
|
|
|
|
if (stamp != null) {
|
|
|
|
try {
|
2016-06-04 14:16:14 +00:00
|
|
|
return AbstractParser.parseTimestamp(delay.getAttribute("stamp"));
|
2015-05-15 03:14:15 +00:00
|
|
|
} catch (ParseException e) {
|
2016-06-04 14:16:14 +00:00
|
|
|
return d;
|
2015-05-15 03:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|
2016-06-04 14:16:14 +00:00
|
|
|
return d;
|
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-06-04 14:16:14 +00:00
|
|
|
contact.setLastPresence(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) {
|
|
|
|
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");
|
|
|
|
Jid fullJid;
|
|
|
|
try {
|
|
|
|
fullJid = nick != null ? Jid.fromParts(local, domain, nick) : null;
|
|
|
|
} catch (InvalidJidException e) {
|
|
|
|
fullJid = null;
|
|
|
|
}
|
|
|
|
Jid realJid = item.getAttributeAsJid("jid");
|
2016-05-17 13:01:56 +00:00
|
|
|
MucOptions.User user = new MucOptions.User(conference.getMucOptions(), nick == null ? null : fullJid);
|
2016-05-17 12:25:58 +00:00
|
|
|
user.setRealJid(realJid);
|
|
|
|
user.setAffiliation(affiliation);
|
|
|
|
user.setRole(role);
|
|
|
|
return user;
|
|
|
|
}
|
2014-10-22 16:38:44 +00:00
|
|
|
}
|