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 try {
61 final var uri = new URI(match);
62 if (uri.getScheme() == null) {
63 return false;
64 }
65 return switch (uri.getScheme()) {
66 case "tel" -> Patterns.URI_TEL.matcher(match).matches();
67 case "http", "https" -> Patterns.URI_HTTP.matcher(match).matches();
68 case "geo" -> Patterns.URI_GEO.matcher(match).matches();
69 case "xmpp" -> new XmppUri(Uri.parse(match)).isValidJid();
70 case "web+ap" -> {
71 if (Patterns.URI_WEB_AP.matcher(match).matches()) {
72 final var webAp = new MiniUri(match);
73 // TODO once we have fragment support check that there aren't any
74 yield Objects.nonNull(webAp.getAuthority()) && webAp.getParameter().isEmpty();
75 } else {
76 yield false;
77 }
78 }
79 default -> true;
80 };
81 } catch (final URISyntaxException e) {
82 return false;
83 }
84 }
85
86 public static void addLinks(final Spannable body) {
87 android.text.util.Linkify.addLinks(body, Patterns.URI_GENERIC, null, MATCH_FILTER, null);
88 }
89
90 public static void addLinks(final Editable body, final Account account, final Jid context) {
91 addLinks(body);
92 final var roster = account.getRoster();
93 urlspan:
94 for (final URLSpan urlspan : body.getSpans(0, body.length() - 1, URLSpan.class)) {
95 final var start = body.getSpanStart(urlspan);
96 if (start < 0) continue;
97 for (final var span : body.getSpans(start, start, Object.class)) {
98 // instanceof TypefaceSpan is to block in XHTML code blocks. Probably a bit heavy-handed but works for now
99 if ((body.getSpanFlags(span) & Spanned.SPAN_USER) >> Spanned.SPAN_USER_SHIFT == StylingHelper.NOLINKIFY || span instanceof TypefaceSpan) {
100 body.removeSpan(urlspan);
101 continue urlspan;
102 }
103 }
104 Uri uri = Uri.parse(urlspan.getURL());
105 if ("xmpp".equals(uri.getScheme())) {
106 try {
107 if (!body.subSequence(body.getSpanStart(urlspan), body.getSpanEnd(urlspan)).toString().startsWith("xmpp:")) {
108 // Already customized
109 continue;
110 }
111
112 XmppUri xmppUri = new XmppUri(uri);
113 Jid jid = xmppUri.getJid();
114 String display = xmppUri.toString();
115 if (jid.asBareJid().equals(context) && xmppUri.isAction("message") && xmppUri.getBody() != null) {
116 display = xmppUri.getBody();
117 } else if (jid.asBareJid().equals(context) && xmppUri.parameterString().length() > 0) {
118 display = xmppUri.parameterString();
119 } else {
120 ListItem item = account.getBookmark(jid);
121 if (item == null) item = roster.getContact(jid);
122 display = item.getDisplayName() + xmppUri.displayParameterString();
123 }
124 body.replace(
125 body.getSpanStart(urlspan),
126 body.getSpanEnd(urlspan),
127 display
128 );
129 } catch (final IllegalArgumentException | IndexOutOfBoundsException e) { /* bad JID or span gone */ }
130 }
131 }
132 }
133
134 public static List<MiniUri> getLinks(final String body) {
135 final var builder = new ImmutableList.Builder<MiniUri>();
136 final var matcher = Patterns.URI_GENERIC.matcher(body);
137 while (matcher.find()) {
138 final var match = matcher.group();
139 if (isPassAdditionalValidation(match)) {
140 builder.add(new MiniUri(match));
141 }
142 }
143 return builder.build();
144 }
145
146 public static List<String> extractLinks(final Editable body) {
147 addLinks(body);
148 final var spans =
149 Arrays.asList(body.getSpans(0, body.length() - 1, URLSpan.class));
150 final var urlWrappers =
151 Collections2.filter(
152 Collections2.transform(
153 spans,
154 s ->
155 s == null
156 ? null
157 : new UrlWrapper(body.getSpanStart(s), s.getURL())),
158 uw -> uw != null);
159 List<UrlWrapper> sorted = ImmutableList.sortedCopyOf(
160 (a, b) -> Integer.compare(a.position, b.position), urlWrappers);
161 return Lists.transform(sorted, uw -> uw.url);
162
163 }
164
165 private static class UrlWrapper {
166 private final int position;
167 private final String url;
168
169 private UrlWrapper(int position, String url) {
170 this.position = position;
171 this.url = url;
172 }
173 }
174}