POP3: block senders

pull/207/head
M66B 3 years ago
parent c156fce9d9
commit 91dd224c87

@ -2047,6 +2047,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
boolean pop = (message.accountProtocol == EntityAccount.TYPE_POP);
boolean imap = (message.accountProtocol == EntityAccount.TYPE_IMAP);
boolean inInbox = EntityFolder.INBOX.equals(message.folderType);
boolean inArchive = EntityFolder.ARCHIVE.equals(message.folderType);
boolean inSent = EntityFolder.SENT.equals(message.folderType);
boolean inTrash = EntityFolder.TRASH.equals(message.folderType);
@ -2068,7 +2069,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
boolean delete = (inTrash || !hasTrash || inJunk || outbox || message.uid == null || pop);
boolean forever = (delete && (!pop || !message.accountLeaveDeleted));
boolean report = ((!inJunk && move) || (pop && inInbox));
boolean headers = (message.uid != null || (pop && message.headers != null));
evalProperties(message); // TODO: done again in bindBody
@ -2139,7 +2140,7 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
ibMove.setVisibility(tools && button_move && move ? View.VISIBLE : View.GONE);
ibArchive.setVisibility(tools && button_archive && archive ? View.VISIBLE : View.GONE);
ibTrash.setVisibility(outbox || (tools && button_trash && trash) ? View.VISIBLE : View.GONE);
ibJunk.setVisibility(tools && button_junk && !inJunk && move ? View.VISIBLE : View.GONE);
ibJunk.setVisibility(tools && button_junk && report ? View.VISIBLE : View.GONE);
ibInbox.setVisibility(tools && inbox ? View.VISIBLE : View.GONE);
ibMore.setVisibility(tools && !outbox ? View.VISIBLE : View.GONE);
ibTools.setImageLevel(tools ? 0 : 1);
@ -5348,7 +5349,6 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
aargs.putLong("folder", message.folder);
aargs.putString("type", message.folderType);
aargs.putString("from", DB.Converters.encodeAddresses(message.from));
aargs.putBoolean("inJunk", EntityFolder.JUNK.equals(message.folderType));
FragmentDialogJunk ask = new FragmentDialogJunk();
ask.setArguments(aargs);

@ -3024,6 +3024,23 @@ class Core {
// No MX check
// No blocklist
if (message.from != null) {
boolean blocked = false;
for (Address from : message.from) {
String email = ((InternetAddress) from).getAddress();
if (TextUtils.isEmpty(email))
continue;
EntityContact badboy = db.contact().getContact(message.account, EntityContact.TYPE_JUNK, email);
if (badboy != null) {
blocked = true;
break;
}
}
if (blocked)
continue;
}
boolean needsHeaders = EntityRule.needsHeaders(message, rules);
List<Header> headers = (needsHeaders ? helper.getAllHeaders() : null);
String body = parts.getHtml(context, download_plain);

@ -76,7 +76,8 @@ public class FragmentDialogJunk extends FragmentDialogBase {
final long folder = args.getLong("folder");
final String type = args.getString("type");
final Address[] froms = DB.Converters.decodeAddresses(args.getString("from"));
final boolean inJunk = args.getBoolean("inJunk");
boolean imap = (protocol == EntityAccount.TYPE_IMAP);
final Context context = getContext();
final View view = LayoutInflater.from(context).inflate(R.layout.dialog_junk, null);
@ -94,8 +95,9 @@ public class FragmentDialogJunk extends FragmentDialogBase {
final Button btnClear = view.findViewById(R.id.btnClear);
final ImageButton ibRules = view.findViewById(R.id.ibRules);
final ImageButton ibManage = view.findViewById(R.id.ibManage);
final Group grpInJunk = view.findViewById(R.id.grpInJunk);
final Group grpMore = view.findViewById(R.id.grpMore);
final Group grpBlockDomain = view.findViewById(R.id.grpBlockDomain);
final Group grpFilter = view.findViewById(R.id.grpFilter);
final Group grpManage = view.findViewById(R.id.grpManage);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean block_sender = prefs.getBoolean("block_sender", true);
@ -127,12 +129,14 @@ public class FragmentDialogJunk extends FragmentDialogBase {
View.OnClickListener onMore = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (grpMore.getVisibility() == View.VISIBLE) {
if (grpManage.getVisibility() == View.VISIBLE) {
ibMore.setImageLevel(1);
grpMore.setVisibility(View.GONE);
grpFilter.setVisibility(View.GONE);
grpManage.setVisibility(View.GONE);
} else {
ibMore.setImageLevel(0);
grpMore.setVisibility(View.VISIBLE);
grpFilter.setVisibility(imap ? View.VISIBLE : View.GONE);
grpManage.setVisibility(View.VISIBLE);
}
}
};
@ -345,21 +349,17 @@ public class FragmentDialogJunk extends FragmentDialogBase {
}
// Initialize
if (inJunk)
tvMessage.setText(R.string.title_folder_junk);
else {
String who = MessageHelper.formatAddresses(froms);
String title = getString(R.string.title_ask_spam_who, who);
SpannableStringBuilder ssb = new SpannableStringBuilderEx(title);
if (who.length() > 0) {
int start = title.indexOf(who);
if (start > 0) {
int textColorPrimary = Helper.resolveColor(context, android.R.attr.textColorPrimary);
ssb.setSpan(new ForegroundColorSpan(textColorPrimary), start, start + who.length(), 0);
}
String who = MessageHelper.formatAddresses(froms);
String title = getString(R.string.title_ask_spam_who, who);
SpannableStringBuilder ssb = new SpannableStringBuilderEx(title);
if (who.length() > 0) {
int start = title.indexOf(who);
if (start > 0) {
int textColorPrimary = Helper.resolveColor(context, android.R.attr.textColorPrimary);
ssb.setSpan(new ForegroundColorSpan(textColorPrimary), start, start + who.length(), 0);
}
tvMessage.setText(ssb);
}
tvMessage.setText(ssb);
cbBlockSender.setEnabled(canBlock);
cbBlockDomain.setEnabled(false);
@ -376,12 +376,14 @@ public class FragmentDialogJunk extends FragmentDialogBase {
cbBlockDomain.setCompoundDrawableTintList(ColorStateList.valueOf(colorWarning));
}
cbBlockDomain.setVisibility(domains.size() > 0 ? View.VISIBLE : View.GONE);
ibMore.setImageLevel(1);
cbBlocklist.setChecked(check_blocklist && use_blocklist);
tvBlocklist.setText(TextUtils.join(", ", DnsBlockList.getNamesEnabled(context)));
grpInJunk.setVisibility(inJunk ? View.GONE : View.VISIBLE);
grpMore.setVisibility(inJunk ? View.VISIBLE : View.GONE);
cbBlockSender.setVisibility(imap ? View.VISIBLE : View.GONE);
grpBlockDomain.setVisibility(domains.size() > 0 && imap ? View.VISIBLE : View.GONE);
grpFilter.setVisibility(View.GONE);
grpManage.setVisibility(View.GONE);
new SimpleTask<Boolean>() {
@Override
@ -429,16 +431,15 @@ public class FragmentDialogJunk extends FragmentDialogBase {
.setView(view)
.setNegativeButton(android.R.string.cancel, null);
if (!inJunk)
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
prefs.edit().putBoolean("block_sender", cbBlockSender.isChecked()).apply();
getArguments().putBoolean("block_sender", cbBlockSender.isChecked());
getArguments().putBoolean("block_domain", cbBlockDomain.isChecked());
sendResult(Activity.RESULT_OK);
}
});
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
prefs.edit().putBoolean("block_sender", cbBlockSender.isChecked()).apply();
getArguments().putBoolean("block_sender", cbBlockSender.isChecked() || !imap);
getArguments().putBoolean("block_domain", cbBlockDomain.isChecked() && imap);
sendResult(Activity.RESULT_OK);
}
});
return builder.create();
}

@ -2684,7 +2684,6 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
aargs.putLong("folder", message.folder);
aargs.putString("type", message.folderType);
aargs.putString("from", DB.Converters.encodeAddresses(message.from));
aargs.putBoolean("inJunk", EntityFolder.JUNK.equals(message.folderType));
FragmentDialogJunk ask = new FragmentDialogJunk();
ask.setArguments(aargs);
@ -8497,30 +8496,37 @@ public class FragmentMessages extends FragmentBase implements SharedPreferences.
if (message == null)
return null;
EntityFolder junk = db.folder().getFolderByType(message.account, EntityFolder.JUNK);
if (junk == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_junk_folder));
if (!message.folder.equals(junk.id))
EntityOperation.queue(context, message, EntityOperation.MOVE, junk.id);
EntityAccount account = db.account().getAccount(message.account);
if (account == null)
return null;
if (block_sender)
EntityContact.update(context,
message.account, message.from,
EntityContact.TYPE_JUNK, message.received);
if (block_domain) {
List<EntityRule> rules = EntityRule.blockSender(context, message, junk, block_domain);
for (EntityRule rule : rules) {
if (message.folder.equals(junk.id)) {
EntityFolder inbox = db.folder().getFolderByType(message.account, EntityFolder.INBOX);
if (inbox == null)
continue;
rule.folder = inbox.id;
if (account.protocol == EntityAccount.TYPE_IMAP) {
EntityFolder junk = db.folder().getFolderByType(message.account, EntityFolder.JUNK);
if (junk == null)
throw new IllegalArgumentException(context.getString(R.string.title_no_junk_folder));
if (!message.folder.equals(junk.id))
EntityOperation.queue(context, message, EntityOperation.MOVE, junk.id);
if (block_domain) {
List<EntityRule> rules = EntityRule.blockSender(context, message, junk, block_domain);
for (EntityRule rule : rules) {
if (message.folder.equals(junk.id)) {
EntityFolder inbox = db.folder().getFolderByType(message.account, EntityFolder.INBOX);
if (inbox == null)
continue;
rule.folder = inbox.id;
}
rule.id = db.rule().insertRule(rule);
}
rule.id = db.rule().insertRule(rule);
}
}
} else
db.message().deleteMessage(message.id);
db.setTransactionSuccessful();
} finally {

@ -71,7 +71,7 @@
app:layout_constraintTop_toBottomOf="@id/cbBlockSender" />
<eu.faircode.email.FixedTextView
android:id="@+id/tvBlockHint"
android:id="@+id/tvBlockDomainHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
@ -89,7 +89,7 @@
android:background="?android:attr/selectableItemBackgroundBorderless"
android:padding="3dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvBlockHint"
app:layout_constraintTop_toBottomOf="@id/tvBlockDomainHint"
app:srcCompat="@drawable/expander" />
<eu.faircode.email.FixedTextView
@ -247,20 +247,25 @@
app:layout_constraintTop_toBottomOf="@id/btnClear" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpInJunk"
android:id="@+id/grpBlockDomain"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="
tvBlockHint,cbBlockSender,cbBlockDomain,
ibMore,tvMore" />
app:constraint_referenced_ids="cbBlockDomain,tvBlockDomainHint" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpMore"
android:id="@+id/grpFilter"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="
cbJunkFilter,tvJunkFilterHint,ibInfoFilter,
cbBlocklist,tvBlocklist,tvBlocklistHint,ibInfoBlocklist,
btnClear,ibRules,ibManage,tvClearHint" />
<androidx.constraintlayout.widget.Group
android:id="@+id/grpManage"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraint_referenced_ids="
btnClear,ibRules,ibManage,tvClearHint" />
</androidx.constraintlayout.widget.ConstraintLayout>
</eu.faircode.email.ScrollViewEx>

Loading…
Cancel
Save