AbstractParser.java

  1package eu.siacs.conversations.parser;
  2
  3import java.text.ParseException;
  4import java.text.SimpleDateFormat;
  5
  6import java.util.Locale;
  7
  8import eu.siacs.conversations.entities.Account;
  9import eu.siacs.conversations.entities.Contact;
 10import eu.siacs.conversations.entities.Conversation;
 11import eu.siacs.conversations.entities.MucOptions;
 12import eu.siacs.conversations.services.XmppConnectionService;
 13import eu.siacs.conversations.xml.Element;
 14import eu.siacs.conversations.xmpp.stanzas.AbstractStanza;
 15import rocks.xmpp.addr.Jid;
 16
 17public abstract class AbstractParser {
 18
 19	protected XmppConnectionService mXmppConnectionService;
 20
 21	protected AbstractParser(XmppConnectionService service) {
 22		this.mXmppConnectionService = service;
 23	}
 24
 25	public static Long parseTimestamp(Element element, Long d) {
 26		return parseTimestamp(element,d,false);
 27	}
 28
 29	public static Long parseTimestamp(Element element, Long d, boolean ignoreCsiAndSm) {
 30		long min = Long.MAX_VALUE;
 31		boolean returnDefault = true;
 32		final Jid to;
 33		if (ignoreCsiAndSm && element instanceof AbstractStanza) {
 34			to = ((AbstractStanza) element).getTo();
 35		} else {
 36			to = null;
 37		}
 38		for(Element child : element.getChildren()) {
 39			if ("delay".equals(child.getName()) && "urn:xmpp:delay".equals(child.getNamespace())) {
 40				final Jid f = to == null ? null : child.getAttributeAsJid("from");
 41				if (f != null && (to.asBareJid().equals(f) || to.getDomain().equals(f.toString()))) {
 42					continue;
 43				}
 44				final String stamp = child.getAttribute("stamp");
 45				if (stamp != null) {
 46					try {
 47						min = Math.min(min,AbstractParser.parseTimestamp(stamp));
 48						returnDefault = false;
 49					} catch (Throwable t) {
 50						//ignore
 51					}
 52				}
 53			}
 54		}
 55		if (returnDefault) {
 56			return d;
 57		} else {
 58			return min;
 59		}
 60	}
 61
 62	public static long parseTimestamp(Element element) {
 63		return parseTimestamp(element, System.currentTimeMillis());
 64	}
 65
 66	public static long parseTimestamp(String timestamp) throws ParseException {
 67		timestamp = timestamp.replace("Z", "+0000");
 68		SimpleDateFormat dateFormat;
 69		long ms;
 70		if (timestamp.length() >= 25 && timestamp.charAt(19) == '.') {
 71			String millis = timestamp.substring(19,timestamp.length() - 5);
 72			try {
 73				double fractions = Double.parseDouble("0" + millis);
 74				ms = Math.round(1000 * fractions);
 75			} catch (NumberFormatException e) {
 76				ms = 0;
 77			}
 78		} else {
 79			ms = 0;
 80		}
 81		timestamp = timestamp.substring(0,19)+timestamp.substring(timestamp.length() -5,timestamp.length());
 82		dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ",Locale.US);
 83		return Math.min(dateFormat.parse(timestamp).getTime()+ms, System.currentTimeMillis());
 84	}
 85
 86	protected void updateLastseen(final Account account, final Jid from) {
 87		final Contact contact = account.getRoster().getContact(from);
 88		contact.setLastResource(from.isBareJid() ? "" : from.getResource());
 89	}
 90
 91	protected String avatarData(Element items) {
 92		Element item = items.findChild("item");
 93		if (item == null) {
 94			return null;
 95		}
 96		return item.findChildContent("data", "urn:xmpp:avatar:data");
 97	}
 98
 99	public static MucOptions.User parseItem(Conversation conference, Element item) {
100		return parseItem(conference,item, null);
101	}
102
103	public static MucOptions.User parseItem(Conversation conference, Element item, Jid fullJid) {
104		final String local = conference.getJid().getLocal();
105		final String domain = conference.getJid().getDomain();
106		String affiliation = item.getAttribute("affiliation");
107		String role = item.getAttribute("role");
108		String nick = item.getAttribute("nick");
109		if (nick != null && fullJid == null) {
110			try {
111				fullJid = Jid.of(local, domain, nick);
112			} catch (IllegalArgumentException e) {
113				fullJid = null;
114			}
115		}
116		Jid realJid = item.getAttributeAsJid("jid");
117		MucOptions.User user = new MucOptions.User(conference.getMucOptions(), fullJid);
118		user.setRealJid(realJid);
119		user.setAffiliation(affiliation);
120		user.setRole(role);
121		return user;
122	}
123
124	public static String extractErrorMessage(Element packet) {
125		final Element error = packet.findChild("error");
126		if (error != null && error.getChildren().size() > 0) {
127			final String text = error.findChildContent("text");
128			if (text != null && !text.trim().isEmpty()) {
129				return text;
130			} else {
131				return error.getChildren().get(0).getName().replace("-"," ");
132			}
133		} else {
134			return null;
135		}
136	}
137}