aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/utils/XmppUri.java
blob: 0f1b18c39eddb233fa743c1f0225b6f1212b244e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package eu.siacs.conversations.utils;

import android.net.Uri;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import eu.siacs.conversations.xmpp.jid.InvalidJidException;
import eu.siacs.conversations.xmpp.jid.Jid;

public class XmppUri {

	protected String jid;
	protected boolean muc;
	protected String fingerprint;

	public XmppUri(String uri) {
		try {
			parse(Uri.parse(uri));
		} catch (IllegalArgumentException e) {
			try {
				jid = Jid.fromString(uri).toBareJid().toString();
			} catch (InvalidJidException e2) {
				jid = null;
			}
		}
	}

	public XmppUri(Uri uri) {
		parse(uri);
	}

	protected void parse(Uri uri) {
		String scheme = uri.getScheme();
		if ("xmpp".equals(scheme)) {
			// sample: xmpp:jid@foo.com
			muc = "join".equalsIgnoreCase(uri.getQuery());
			if (uri.getAuthority() != null) {
				jid = uri.getAuthority();
			} else {
				jid = uri.getSchemeSpecificPart().split("\\?")[0];
			}
			fingerprint = parseFingerprint(uri.getQuery());
		} else if ("imto".equals(scheme)) {
			// sample: imto://xmpp/jid@foo.com
			try {
				jid = URLDecoder.decode(uri.getEncodedPath(), "UTF-8").split("/")[1];
			} catch (final UnsupportedEncodingException ignored) {
				jid = null;
			}
		} else {
			try {
				jid = Jid.fromString(uri.toString()).toBareJid().toString();
			} catch (final InvalidJidException ignored) {
				jid = null;
			}
		}
	}

	protected  String parseFingerprint(String query) {
		if (query == null) {
			return null;
		} else {
			final String NEEDLE = "otr-fingerprint=";
			int index = query.indexOf(NEEDLE);
			if (index >= 0 && query.length() >= (NEEDLE.length() + index + 40)) {
				return query.substring(index + NEEDLE.length(), index + NEEDLE.length() + 40);
			} else {
				return null;
			}
		}
	}

	public Jid getJid() {
		try {
			return this.jid == null ? null :Jid.fromString(this.jid);
		} catch (InvalidJidException e) {
			return null;
		}
	}

	public String getFingerprint() {
		return this.fingerprint;
	}
}