1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package de.gultsch.common;
31
32import android.net.Uri;
33import android.text.Editable;
34import android.text.Spanned;
35import android.text.style.TypefaceSpan;
36import android.text.style.URLSpan;
37import android.text.Spannable;
38import com.google.common.base.Splitter;
39import com.google.common.collect.Collections2;
40import com.google.common.collect.ImmutableList;
41import com.google.common.collect.Iterables;
42import com.google.common.collect.Lists;
43import eu.siacs.conversations.entities.Account;
44import eu.siacs.conversations.entities.ListItem;
45import eu.siacs.conversations.utils.StylingHelper;
46import eu.siacs.conversations.utils.XmppUri;
47import eu.siacs.conversations.xmpp.Jid;
48import java.net.URI;
49import java.net.URISyntaxException;
50import java.util.Arrays;
51import java.util.List;
52import java.util.Objects;
53
54public class Linkify {
55
56 private static final android.text.util.Linkify.MatchFilter MATCH_FILTER =
57 (s, start, end) -> isPassAdditionalValidation(s.subSequence(start, end).toString());
58
59 private static boolean isPassAdditionalValidation(final String match) {
60 final var scheme = Iterables.getFirst(Splitter.on(':').limit(2).splitToList(match), null);
61 if (scheme == null) {
62 return false;
63 }
64 if (!isValidUri(match)) {
65 return false;
66 }
67 return switch (scheme) {
68 case "tel" -> Patterns.URI_TEL.matcher(match).matches();
69 case "http", "https" -> Patterns.URI_HTTP.matcher(match).matches();
70 case "geo" -> Patterns.URI_GEO.matcher(match).matches();
71 case "xmpp" -> new XmppUri(Uri.parse(match)).isValidJid();
72 case "web+ap" -> {
73 if (Patterns.URI_WEB_AP.matcher(match).matches()) {
74 final var webAp = new MiniUri(match);
75 // TODO once we have fragment support check that there aren't any
76 yield Objects.nonNull(webAp.getAuthority()) && webAp.getParameter().isEmpty();
77 } else {
78 yield false;
79 }
80 }
81 default -> true;
82 };
83 }
84
85 private static boolean isValidUri(final String match) {
86 try {
87 new URI(match);
88 return true;
89 } catch (final URISyntaxException e) {
90 return false;
91 }
92 }
93
94 public static void addLinks(final Spannable body) {
95 android.text.util.Linkify.addLinks(body, Patterns.URI_GENERIC, null, MATCH_FILTER, null);
96 for (final URLSpan span : body.getSpans(0, body.length(), URLSpan.class)) {
97 try {
98 new java.net.URI(span.getURL());
99 } catch (final java.net.URISyntaxException e) {
100 body.removeSpan(span);
101 }
102 }
103 }
104
105 public static void addLinks(final Editable body, final Account account, final Jid context) {
106 addLinks(body);
107 final var roster = account.getRoster();
108 urlspan:
109 for (final URLSpan urlspan : body.getSpans(0, body.length() - 1, URLSpan.class)) {
110 final var start = body.getSpanStart(urlspan);
111 if (start < 0) continue;
112 for (final var span : body.getSpans(start, start, Object.class)) {
113 // instanceof TypefaceSpan is to block in XHTML code blocks. Probably a bit heavy-handed but works for now
114 if ((body.getSpanFlags(span) & Spanned.SPAN_USER) >> Spanned.SPAN_USER_SHIFT == StylingHelper.NOLINKIFY || span instanceof TypefaceSpan) {
115 body.removeSpan(urlspan);
116 continue urlspan;
117 }
118 }
119 Uri uri = Uri.parse(urlspan.getURL());
120 if ("xmpp".equals(uri.getScheme())) {
121 try {
122 if (!body.subSequence(body.getSpanStart(urlspan), body.getSpanEnd(urlspan)).toString().startsWith("xmpp:")) {
123 // Already customized
124 continue;
125 }
126
127 XmppUri xmppUri = new XmppUri(uri);
128 Jid jid = xmppUri.getJid();
129 String display = xmppUri.toString();
130 if (jid.asBareJid().equals(context) && xmppUri.isAction("message") && xmppUri.getBody() != null) {
131 display = xmppUri.getBody();
132 } else if (jid.asBareJid().equals(context) && xmppUri.parameterString().length() > 0) {
133 display = xmppUri.parameterString();
134 } else {
135 ListItem item = account.getBookmark(jid);
136 if (item == null) item = roster.getContact(jid);
137 display = item.getDisplayName() + xmppUri.displayParameterString();
138 }
139 body.replace(
140 body.getSpanStart(urlspan),
141 body.getSpanEnd(urlspan),
142 display
143 );
144 } catch (final IllegalArgumentException | IndexOutOfBoundsException e) { /* bad JID or span gone */ }
145 }
146 }
147 }
148
149 public static List<MiniUri> getLinks(final String body) {
150 final var builder = new ImmutableList.Builder<MiniUri>();
151 final var matcher = Patterns.URI_GENERIC.matcher(body);
152 while (matcher.find()) {
153 final var match = matcher.group();
154 if (isPassAdditionalValidation(match)) {
155 builder.add(new MiniUri(match));
156 }
157 }
158 return builder.build();
159 }
160
161 public static List<String> extractLinks(final Editable body) {
162 addLinks(body);
163 final var spans =
164 Arrays.asList(body.getSpans(0, body.length() - 1, URLSpan.class));
165 final var urlWrappers =
166 Collections2.filter(
167 Collections2.transform(
168 spans,
169 s ->
170 s == null
171 ? null
172 : new UrlWrapper(body.getSpanStart(s), s.getURL())),
173 uw -> uw != null);
174 List<UrlWrapper> sorted = ImmutableList.sortedCopyOf(
175 (a, b) -> Integer.compare(a.position, b.position), urlWrappers);
176 return Lists.transform(sorted, uw -> uw.url);
177
178 }
179
180 private static class UrlWrapper {
181 private final int position;
182 private final String url;
183
184 private UrlWrapper(int position, String url) {
185 this.position = position;
186 this.url = url;
187 }
188 }
189}