2021-07-02 05:32:15 +00:00
|
|
|
package eu.faircode.email;
|
|
|
|
|
|
|
|
/*
|
|
|
|
This file is part of FairEmail.
|
|
|
|
|
|
|
|
FairEmail is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
FairEmail is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2018-2021 by Marcel Bokhorst (M66B)
|
|
|
|
*/
|
|
|
|
|
2021-07-20 20:16:20 +00:00
|
|
|
import android.content.Context;
|
2021-09-02 18:28:27 +00:00
|
|
|
import android.net.Uri;
|
2021-07-20 20:16:20 +00:00
|
|
|
import android.text.TextUtils;
|
2021-09-02 18:28:27 +00:00
|
|
|
import android.webkit.URLUtil;
|
2021-07-20 20:16:20 +00:00
|
|
|
|
2021-08-17 08:39:27 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2021-09-09 06:23:55 +00:00
|
|
|
import androidx.core.util.PatternsCompat;
|
2021-08-17 08:39:27 +00:00
|
|
|
|
2021-07-20 20:16:20 +00:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.util.HashSet;
|
2021-08-17 06:44:44 +00:00
|
|
|
import java.util.Locale;
|
2021-07-20 20:16:20 +00:00
|
|
|
|
2021-07-02 05:32:15 +00:00
|
|
|
public class UriHelper {
|
2021-07-20 20:16:20 +00:00
|
|
|
// https://publicsuffix.org/
|
|
|
|
private static final HashSet<String> suffixList = new HashSet<>();
|
|
|
|
|
2021-08-07 07:26:44 +00:00
|
|
|
// https://raw.githubusercontent.com/publicsuffix/list/master/public_suffix_list.dat
|
|
|
|
private static final String SUFFIX_LIST_NAME = "public_suffix_list.dat";
|
2021-07-20 20:16:20 +00:00
|
|
|
|
|
|
|
static String getParentDomain(Context context, String host) {
|
2021-07-02 05:32:15 +00:00
|
|
|
if (host == null)
|
|
|
|
return null;
|
2021-08-17 06:44:44 +00:00
|
|
|
String parent = _getSuffix(context, host);
|
|
|
|
return (parent == null ? host : parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
static boolean hasParentDomain(Context context, String host) {
|
|
|
|
return (host != null && _getSuffix(context, host) != null);
|
|
|
|
}
|
2021-07-02 05:32:15 +00:00
|
|
|
|
2021-08-17 08:39:27 +00:00
|
|
|
private static String _getSuffix(Context context, @NonNull String host) {
|
2021-07-20 20:16:20 +00:00
|
|
|
ensureSuffixList(context);
|
|
|
|
|
2021-08-17 06:44:44 +00:00
|
|
|
String h = host.toLowerCase(Locale.ROOT);
|
2021-07-20 20:16:20 +00:00
|
|
|
while (true) {
|
|
|
|
int dot = h.indexOf('.');
|
|
|
|
if (dot < 0)
|
2021-08-17 06:44:44 +00:00
|
|
|
return null;
|
|
|
|
|
2021-07-20 20:16:20 +00:00
|
|
|
String prefix = h.substring(0, dot);
|
|
|
|
h = h.substring(dot + 1);
|
2021-07-02 05:32:15 +00:00
|
|
|
|
2021-07-20 20:16:20 +00:00
|
|
|
int d = h.indexOf('.');
|
|
|
|
String w = (d < 0 ? null : '*' + h.substring(d));
|
|
|
|
|
|
|
|
synchronized (suffixList) {
|
2021-08-17 06:44:44 +00:00
|
|
|
if (!suffixList.contains('!' + h) &&
|
|
|
|
(suffixList.contains(h) || suffixList.contains(w))) {
|
2021-07-20 20:16:20 +00:00
|
|
|
String parent = prefix + "." + h;
|
2021-08-11 13:42:07 +00:00
|
|
|
Log.d("Host=" + host + " parent=" + parent);
|
2021-07-20 20:16:20 +00:00
|
|
|
return parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 05:32:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 07:51:56 +00:00
|
|
|
static String getEmailUser(String address) {
|
|
|
|
if (address == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
int at = address.indexOf('@');
|
|
|
|
if (at > 0)
|
|
|
|
return address.substring(0, at);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-07-02 05:32:15 +00:00
|
|
|
static String getEmailDomain(String address) {
|
|
|
|
if (address == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
int at = address.indexOf('@');
|
|
|
|
if (at > 0)
|
|
|
|
return address.substring(at + 1);
|
|
|
|
|
2021-07-02 07:51:56 +00:00
|
|
|
return null;
|
2021-07-02 05:32:15 +00:00
|
|
|
}
|
2021-07-20 20:16:20 +00:00
|
|
|
|
2021-09-02 18:28:27 +00:00
|
|
|
static @NonNull
|
|
|
|
Uri guessScheme(@NonNull Uri uri) {
|
|
|
|
if (uri.getScheme() != null)
|
|
|
|
return uri;
|
|
|
|
|
|
|
|
String url = uri.toString();
|
|
|
|
if (Helper.EMAIL_ADDRESS.matcher(url).matches())
|
|
|
|
return Uri.parse("mailto:" + url);
|
2021-09-09 06:23:55 +00:00
|
|
|
else if (PatternsCompat.IP_ADDRESS.matcher(url).matches())
|
|
|
|
return Uri.parse("https://" + url);
|
2021-09-02 18:28:27 +00:00
|
|
|
else if (android.util.Patterns.PHONE.matcher(url).matches())
|
2021-09-09 06:23:55 +00:00
|
|
|
// Patterns.PHONE (\+[0-9]+[\- \.]*)?(\([0-9]+\)[\- \.]*)?([0-9][0-9\- \.]+[0-9])
|
|
|
|
// PhoneNumberUtils.isGlobalPhoneNumber() [\+]?[0-9.-]+
|
2021-09-02 18:28:27 +00:00
|
|
|
return Uri.parse("tel:" + url);
|
|
|
|
else {
|
|
|
|
Uri g = Uri.parse(URLUtil.guessUrl(url));
|
|
|
|
String scheme = g.getScheme();
|
|
|
|
if (scheme == null)
|
|
|
|
return uri;
|
|
|
|
else if ("http".equals(scheme))
|
|
|
|
scheme = "https";
|
|
|
|
return Uri.parse(scheme + "://" + url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 20:16:20 +00:00
|
|
|
static void ensureSuffixList(Context context) {
|
|
|
|
synchronized (suffixList) {
|
|
|
|
if (suffixList.size() > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Log.i("Reading " + SUFFIX_LIST_NAME);
|
|
|
|
try (InputStream is = context.getAssets().open(SUFFIX_LIST_NAME)) {
|
|
|
|
BufferedReader br = new BufferedReader(new InputStreamReader((is)));
|
|
|
|
String line;
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
|
|
line = line.trim();
|
|
|
|
if (TextUtils.isEmpty(line))
|
|
|
|
continue;
|
|
|
|
if (line.startsWith("//"))
|
|
|
|
continue;
|
|
|
|
suffixList.add(line);
|
|
|
|
}
|
|
|
|
Log.i(SUFFIX_LIST_NAME + "=" + suffixList.size());
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
Log.e(ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-02 05:32:15 +00:00
|
|
|
}
|