AbstractParser.java

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