XmppUri.java

  1package eu.siacs.conversations.utils;
  2
  3import android.net.Uri;
  4
  5import java.io.UnsupportedEncodingException;
  6import java.net.URLDecoder;
  7import java.util.List;
  8
  9import eu.siacs.conversations.xmpp.jid.InvalidJidException;
 10import eu.siacs.conversations.xmpp.jid.Jid;
 11
 12public class XmppUri {
 13
 14	protected String jid;
 15	protected boolean muc;
 16	protected String fingerprint;
 17
 18	public XmppUri(String uri) {
 19		try {
 20			parse(Uri.parse(uri));
 21		} catch (IllegalArgumentException e) {
 22			try {
 23				jid = Jid.fromString(uri).toBareJid().toString();
 24			} catch (InvalidJidException e2) {
 25				jid = null;
 26			}
 27		}
 28	}
 29
 30	public XmppUri(Uri uri) {
 31		parse(uri);
 32	}
 33
 34	protected void parse(Uri uri) {
 35		String scheme = uri.getScheme();
 36		String host = uri.getHost();
 37		List<String> segments = uri.getPathSegments();
 38		if ("https".equalsIgnoreCase(scheme) && "conversations.im".equalsIgnoreCase(host)) {
 39			if (segments.size() >= 2 && segments.get(1).contains("@")) {
 40				// sample : https://conversations.im/i/foo@bar.com
 41				try {
 42					jid = Jid.fromString(segments.get(1)).toString();
 43				} catch (Exception e) {
 44					jid = null;
 45				}
 46			} else if (segments.size() >= 3) {
 47				// sample : https://conversations.im/i/foo/bar.com
 48				jid = segments.get(1) + "@" + segments.get(2);
 49			}
 50			muc = segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0));
 51		} else if ("xmpp".equalsIgnoreCase(scheme)) {
 52			// sample: xmpp:foo@bar.com
 53			muc = "join".equalsIgnoreCase(uri.getQuery());
 54			if (uri.getAuthority() != null) {
 55				jid = uri.getAuthority();
 56			} else {
 57				jid = uri.getSchemeSpecificPart().split("\\?")[0];
 58			}
 59			fingerprint = parseFingerprint(uri.getQuery());
 60		} else if ("imto".equalsIgnoreCase(scheme)) {
 61			// sample: imto://xmpp/foo@bar.com
 62			try {
 63				jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1];
 64			} catch (final UnsupportedEncodingException ignored) {
 65				jid = null;
 66			}
 67		} else {
 68			try {
 69				jid = Jid.fromString(uri.toString()).toBareJid().toString();
 70			} catch (final InvalidJidException ignored) {
 71				jid = null;
 72			}
 73		}
 74	}
 75
 76	protected  String parseFingerprint(String query) {
 77		if (query == null) {
 78			return null;
 79		} else {
 80			final String NEEDLE = "otr-fingerprint=";
 81			int index = query.indexOf(NEEDLE);
 82			if (index >= 0 && query.length() >= (NEEDLE.length() + index + 40)) {
 83				return query.substring(index + NEEDLE.length(), index + NEEDLE.length() + 40);
 84			} else {
 85				return null;
 86			}
 87		}
 88	}
 89
 90	public Jid getJid() {
 91		try {
 92			return this.jid == null ? null :Jid.fromString(this.jid.toLowerCase());
 93		} catch (InvalidJidException e) {
 94			return null;
 95		}
 96	}
 97
 98	public String getFingerprint() {
 99		return this.fingerprint;
100	}
101}