aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/utils/XmppUri.java
blob: 51f2b808a8509c01ac45f243b4718a491295376d (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package de.pixart.messenger.utils;

import android.net.Uri;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import de.pixart.messenger.xmpp.jid.InvalidJidException;
import de.pixart.messenger.xmpp.jid.Jid;

public class XmppUri {

    protected String jid;
    protected boolean muc;
    protected String fingerprint;
    protected List<Fingerprint> fingerprints = new ArrayList<>();
    public static final String OMEMO_URI_PARAM = "omemo-sid-";
    public static final String OTR_URI_PARAM = "otr-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);
    }

    public static boolean isXmppUri(String uri) {
        String scheme = Uri.parse(uri).getScheme();
        return "xmpp".equalsIgnoreCase(scheme);
    }

    protected void parse(Uri uri) {
        String scheme = uri.getScheme();
        String host = uri.getHost();
        List<String> segments = uri.getPathSegments();
        if ("https".equalsIgnoreCase(scheme) && "jabber.pix-art.de".equalsIgnoreCase(host)) {
            if (segments.size() >= 2 && segments.get(1).contains("@")) {
                // sample : https://conversations.im/i/foo@bar.com
                try {
                    jid = Jid.fromString(segments.get(1)).toString();
                } catch (Exception e) {
                    jid = null;
                }
            } else if (segments.size() >= 3) {
                // sample : https://conversations.im/i/foo/bar.com
                jid = segments.get(1) + "@" + segments.get(2);
            }
            muc = segments.size() > 1 && "j".equalsIgnoreCase(segments.get(0));
        } else if ("xmpp".equalsIgnoreCase(scheme)) {
            // sample: xmpp:foo@bar.com
            muc = "join".equalsIgnoreCase(uri.getQuery());
            if (uri.getAuthority() != null) {
                jid = uri.getAuthority();
            } else {
                jid = uri.getSchemeSpecificPart().split("\\?")[0];
            }
            this.fingerprints = parseFingerprints(uri.getQuery());
        } else if ("imto".equalsIgnoreCase(scheme)) {
            // sample: imto://xmpp/foo@bar.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 List<Fingerprint> parseFingerprints(String query) {
        List<Fingerprint> fingerprints = new ArrayList<>();
        String[] pairs = query == null ? new String[0] : query.split(";");
        for (String pair : pairs) {
            String[] parts = pair.split("=", 2);
            if (parts.length == 2) {
                String key = parts[0].toLowerCase(Locale.US);
                String value = parts[1];
                if (OTR_URI_PARAM.equals(key)) {
                    fingerprints.add(new Fingerprint(FingerprintType.OTR, value));
                }
                if (key.startsWith(OMEMO_URI_PARAM)) {
                    try {
                        int id = Integer.parseInt(key.substring(OMEMO_URI_PARAM.length()));
                        fingerprints.add(new Fingerprint(FingerprintType.OMEMO, value, id));
                    } catch (Exception e) {
                        //ignoring invalid device id
                    }
                }
                return null;
            }
        }
        return fingerprints;
    }

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

    public List<Fingerprint> getFingerprints() {
        return this.fingerprints;
    }

    public boolean hasFingerprints() {
        return fingerprints.size() > 0;
    }

    public enum FingerprintType {
        OMEMO,
        OTR
    }

    public static class Fingerprint {
        public final FingerprintType type;
        public final String fingerprint;
        public final int deviceId;

        public Fingerprint(FingerprintType type, String fingerprint) {
            this(type, fingerprint, 0);
        }

        public Fingerprint(FingerprintType type, String fingerprint, int deviceId) {
            this.type = type;
            this.fingerprint = fingerprint;
            this.deviceId = deviceId;
        }

        @Override
        public String toString() {
            return type.toString() + ": " + fingerprint + (deviceId != 0 ? " " + String.valueOf(deviceId) : "");
        }
    }
}