From 4504d69aaa91741ecdb48326c94196a83a0100a3 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 30 Jan 2020 12:48:50 +0100 Subject: [PATCH] Experimental: block list support --- app/src/main/java/eu/faircode/email/Core.java | 44 +++++++++++++++++++ .../java/eu/faircode/email/MessageHelper.java | 14 ++++++ 2 files changed, 58 insertions(+) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index fdb92ba7ef..5ded40184a 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -69,6 +69,9 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.Inet4Address; +import java.net.Inet6Address; +import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import java.text.DateFormat; @@ -2148,6 +2151,47 @@ class Core { message.warning = Log.formatThrowable(ex, false); } + boolean check_spam = prefs.getBoolean("check_spam", BuildConfig.DEBUG); + if (check_spam) + try { + String host = helper.getReceivedFromHost(); + if (host != null) { + InetAddress addr = InetAddress.getByName(host); + Log.i("Received from " + host + "=" + addr); + + StringBuilder lookup = new StringBuilder(); + if (addr instanceof Inet4Address) { + List a = Arrays.asList(addr.getHostAddress().split("\\.")); + Collections.reverse(a); + lookup.append(TextUtils.join(".", a)).append('.'); + } else if (addr instanceof Inet6Address) { + StringBuilder sb = new StringBuilder(); + byte[] a = addr.getAddress(); + for (int i = 0; i < 8; i++) + sb.append(String.format("%02x", + ((a[i << 1] << 8) & 0xff00) | (a[(i << 1) + 1] & 0xff))); + sb.reverse(); + for (char kar : sb.toString().toCharArray()) + lookup.append(kar).append('.'); + } + + lookup.append("zen.spamhaus.org"); + + try { + InetAddress.getByName(lookup.toString()); + if (message.warning == null) + message.warning = lookup.toString(); + else + message.warning += ", " + lookup; + } catch (UnknownHostException ignore) { + } + } + } catch (UnknownHostException ex) { + + } catch (Throwable ex) { + Log.w(folder.name, ex); + } + boolean check_reply = prefs.getBoolean("check_reply", false); if (check_reply && message.from != null && message.from.length > 0 && diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index aebc057566..5695863945 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -96,6 +96,8 @@ public class MessageHelper { static final int DEFAULT_ATTACHMENT_DOWNLOAD_SIZE = 256 * 1024; // bytes static final long ATTACHMENT_PROGRESS_UPDATE = 1500L; // milliseconds + // https://tools.ietf.org/html/rfc4021 + static void setSystemProperties(Context context) { System.setProperty("mail.mime.decodetext.strict", "false"); @@ -835,6 +837,18 @@ public class MessageHelper { return result; } + String getReceivedFromHost() throws MessagingException { + String[] received = imessage.getHeader("Received"); + if (received == null || received.length == 0) + return null; + + String[] h = MimeUtility.unfold(received[received.length - 1]).split("\\s+"); + if (h.length > 1 && h[0].equalsIgnoreCase("from")) + return h[1]; + + return null; + } + private Address[] getAddressHeader(String name) throws MessagingException { String header = imessage.getHeader(name, ","); if (header == null)