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 eu.siacs.conversations.utils;
31
32import android.annotation.TargetApi;
33import android.content.Context;
34import android.os.Build;
35import android.support.annotation.ColorInt;
36import android.text.Spannable;
37import android.text.SpannableString;
38import android.text.SpannableStringBuilder;
39import android.text.style.ForegroundColorSpan;
40import android.util.LruCache;
41
42import java.util.ArrayList;
43import java.util.Collections;
44import java.util.HashMap;
45import java.util.HashSet;
46import java.util.List;
47import java.util.Map;
48import java.util.Set;
49import java.util.regex.Matcher;
50import java.util.regex.Pattern;
51
52import eu.siacs.conversations.R;
53import eu.siacs.conversations.ui.util.StyledAttributes;
54import rocks.xmpp.addr.Jid;
55
56public class IrregularUnicodeDetector {
57
58 private static final Map<Character.UnicodeBlock, Character.UnicodeBlock> NORMALIZATION_MAP;
59 private static final LruCache<Jid, PatternTuple> CACHE = new LruCache<>(4096);
60
61 static {
62 Map<Character.UnicodeBlock, Character.UnicodeBlock> temp = new HashMap<>();
63 temp.put(Character.UnicodeBlock.LATIN_1_SUPPLEMENT, Character.UnicodeBlock.BASIC_LATIN);
64 NORMALIZATION_MAP = Collections.unmodifiableMap(temp);
65 }
66
67 private static Character.UnicodeBlock normalize(Character.UnicodeBlock in) {
68 if (NORMALIZATION_MAP.containsKey(in)) {
69 return NORMALIZATION_MAP.get(in);
70 } else {
71 return in;
72 }
73 }
74
75 public static Spannable style(Context context, Jid jid) {
76 return style(jid, StyledAttributes.getColor(context, R.attr.color_warning));
77 }
78
79 private static Spannable style(Jid jid, @ColorInt int color) {
80 PatternTuple patternTuple = find(jid);
81 SpannableStringBuilder builder = new SpannableStringBuilder();
82 if (jid.getLocal() != null && patternTuple.local != null) {
83 SpannableString local = new SpannableString(jid.getLocal());
84 colorize(local, patternTuple.local, color);
85 builder.append(local);
86 builder.append('@');
87 }
88 if (jid.getDomain() != null) {
89 String[] labels = jid.getDomain().split("\\.");
90 for (int i = 0; i < labels.length; ++i) {
91 SpannableString spannableString = new SpannableString(labels[i]);
92 colorize(spannableString, patternTuple.domain.get(i), color);
93 if (i != 0) {
94 builder.append('.');
95 }
96 builder.append(spannableString);
97 }
98 }
99 if (builder.length() != 0 && jid.getResource() != null) {
100 builder.append('/');
101 builder.append(jid.getResource());
102 }
103 return builder;
104 }
105
106 private static void colorize(SpannableString spannableString, Pattern pattern, @ColorInt int color) {
107 Matcher matcher = pattern.matcher(spannableString);
108 while (matcher.find()) {
109 if (matcher.start() < matcher.end()) {
110 spannableString.setSpan(new ForegroundColorSpan(color), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
111 }
112 }
113 }
114
115 private static Map<Character.UnicodeBlock, List<String>> mapCompat(String word) {
116 Map<Character.UnicodeBlock, List<String>> map = new HashMap<>();
117 final int length = word.length();
118 for (int offset = 0; offset < length; ) {
119 final int codePoint = word.codePointAt(offset);
120 offset += Character.charCount(codePoint);
121 if (!Character.isLetter(codePoint)) {
122 continue;
123 }
124 Character.UnicodeBlock block = normalize(Character.UnicodeBlock.of(codePoint));
125 List<String> codePoints;
126 if (map.containsKey(block)) {
127 codePoints = map.get(block);
128 } else {
129 codePoints = new ArrayList<>();
130 map.put(block, codePoints);
131 }
132 codePoints.add(String.copyValueOf(Character.toChars(codePoint)));
133 }
134 return map;
135 }
136
137 @TargetApi(Build.VERSION_CODES.N)
138 private static Map<Character.UnicodeScript, List<String>> map(String word) {
139 Map<Character.UnicodeScript, List<String>> map = new HashMap<>();
140 final int length = word.length();
141 for (int offset = 0; offset < length; ) {
142 final int codePoint = word.codePointAt(offset);
143 Character.UnicodeScript script = Character.UnicodeScript.of(codePoint);
144 if (script != Character.UnicodeScript.COMMON) {
145 List<String> codePoints;
146 if (map.containsKey(script)) {
147 codePoints = map.get(script);
148 } else {
149 codePoints = new ArrayList<>();
150 map.put(script, codePoints);
151 }
152 codePoints.add(String.copyValueOf(Character.toChars(codePoint)));
153 }
154 offset += Character.charCount(codePoint);
155 }
156 return map;
157 }
158
159 private static Set<String> eliminateFirstAndGetCodePointsCompat(Map<Character.UnicodeBlock, List<String>> map) {
160 return eliminateFirstAndGetCodePoints(map, Character.UnicodeBlock.BASIC_LATIN);
161 }
162
163 @TargetApi(Build.VERSION_CODES.N)
164 private static Set<String> eliminateFirstAndGetCodePoints(Map<Character.UnicodeScript, List<String>> map) {
165 return eliminateFirstAndGetCodePoints(map, Character.UnicodeScript.COMMON);
166 }
167
168 private static <T> Set<String> eliminateFirstAndGetCodePoints(Map<T, List<String>> map, T defaultPick) {
169 T pick = defaultPick;
170 int size = 0;
171 for (Map.Entry<T, List<String>> entry : map.entrySet()) {
172 if (entry.getValue().size() > size) {
173 size = entry.getValue().size();
174 pick = entry.getKey();
175 }
176 }
177 map.remove(pick);
178 Set<String> all = new HashSet<>();
179 for (List<String> codePoints : map.values()) {
180 all.addAll(codePoints);
181 }
182 return all;
183 }
184
185 private static Set<String> findIrregularCodePoints(String word) {
186 Set<String> codePoints;
187 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
188 codePoints = eliminateFirstAndGetCodePointsCompat(mapCompat(word));
189 } else {
190 codePoints = eliminateFirstAndGetCodePoints(map(word));
191 }
192 return codePoints;
193 }
194
195 private static PatternTuple find(Jid jid) {
196 synchronized (CACHE) {
197 PatternTuple pattern = CACHE.get(jid);
198 if (pattern != null) {
199 return pattern;
200 }
201 ;
202 pattern = PatternTuple.of(jid);
203 CACHE.put(jid, pattern);
204 return pattern;
205 }
206 }
207
208 private static Pattern create(Set<String> codePoints) {
209 final StringBuilder pattern = new StringBuilder();
210 for (String codePoint : codePoints) {
211 if (pattern.length() != 0) {
212 pattern.append('|');
213 }
214 pattern.append(Pattern.quote(codePoint));
215 }
216 return Pattern.compile(pattern.toString());
217 }
218
219 private static class PatternTuple {
220 private final Pattern local;
221 private final List<Pattern> domain;
222
223 private PatternTuple(Pattern local, List<Pattern> domain) {
224 this.local = local;
225 this.domain = domain;
226 }
227
228 private static PatternTuple of(Jid jid) {
229 final Pattern localPattern;
230 if (jid.getLocal() != null) {
231 localPattern = create(findIrregularCodePoints(jid.getLocal()));
232 } else {
233 localPattern = null;
234 }
235 String domain = jid.getDomain();
236 final List<Pattern> domainPatterns = new ArrayList<>();
237 if (domain != null) {
238 for (String label : domain.split("\\.")) {
239 domainPatterns.add(create(findIrregularCodePoints(label)));
240 }
241 }
242 return new PatternTuple(localPattern, domainPatterns);
243 }
244 }
245}