Repository: thundernest/k-9
Issue: Sender name is not displayed

Please search to check for an existing issue (including closed issues, for which the fix may not have yet been released) before opening a new issue: https://github.com/k9mail/k-9/issues?q=is%3Aissue

If you are unsure whether or not you have found a bug please post to the support forum instead: https://forum.k9mail.app

**Describe the bug**
I see some issue about showing address instead of name but I can only see the address on some emails. I checked the email header and there is `From: "linsui (@linsui)" <gitlab@mg.gitlab.com>` but only gitlab@mg.gitlab.com is displayed though the contact picture is an L. When the header is `GitLab <gitlab@mg.gitlab.com>` the name is displayed though.

**To Reproduce**
Steps to reproduce the behavior:
1. Receive an email from gitlab
4. See error

**Expected behavior**
The contact name is displayed.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Environment (please complete the following information):**
 - K-9 Mail version: 5.804
 - Android version: 11
 - Device: Mi 6
 - Account type: IMAP

**Additional context**
Add any other context about the problem here.

**Logs**
Please take some time to [retrieve logs](https://github.com/k9mail/k-9/wiki/LoggingErrors) and attach them here:



Changed files:
app/core/src/test/java/com/fsck/k9/helper/MessageHelperTest.java, app/core/src/main/java/com/fsck/k9/helper/MessageHelper.java

diff --git a/app/core/src/main/java/com/fsck/k9/helper/MessageHelper.java b/app/core/src/main/java/com/fsck/k9/helper/MessageHelper.java
index 5595d47f8..cb9e865da 100644
--- a/app/core/src/main/java/com/fsck/k9/helper/MessageHelper.java
+++ b/app/core/src/main/java/com/fsck/k9/helper/MessageHelper.java
@@ -1,6 +1,8 @@
 package com.fsck.k9.helper;
 
 
+import java.util.regex.Pattern;
+
 import android.content.Context;
 import android.text.Spannable;
 import android.text.SpannableString;
@@ -27,6 +29,7 @@ public class MessageHelper {
      * @see #toFriendly(Address[], com.fsck.k9.helper.Contacts)
      */
     private static final int TOO_MANY_ADDRESSES = 50;
+    private static final Pattern SPOOF_ADDRESS_PATTERN = Pattern.compile("[^(]@");
 
     private static MessageHelper sInstance;
 
@@ -143,6 +146,6 @@ public class MessageHelper {
     }
 
     private static boolean isSpoofAddress(String displayName) {
-        return displayName.contains("@");
+        return displayName.contains("@") && SPOOF_ADDRESS_PATTERN.matcher(displayName).find();
     }
 }
diff --git a/app/core/src/test/java/com/fsck/k9/helper/MessageHelperTest.java b/app/core/src/test/java/com/fsck/k9/helper/MessageHelperTest.java
index f5b7cc0eb..85347a322 100644
--- a/app/core/src/test/java/com/fsck/k9/helper/MessageHelperTest.java
+++ b/app/core/src/test/java/com/fsck/k9/helper/MessageHelperTest.java
@@ -94,6 +94,20 @@ public class MessageHelperTest extends RobolectricTest {
         assertEquals("test@testor.com", friendly.toString());
     }
 
+    @Test
+    public void toFriendly_atPrecededByOpeningParenthesisShouldNotTriggerSpoofPrevention() {
+        Address address = new Address("gitlab@gitlab.example", "username (@username)");
+        CharSequence friendly = MessageHelper.toFriendly(address, contacts);
+        assertEquals("username (@username)", friendly.toString());
+    }
+
+    @Test
+    public void toFriendly_nameStartingWithAtShouldNotTriggerSpoofPrevention() {
+        Address address = new Address("address@domain.example", "@username");
+        CharSequence friendly = MessageHelper.toFriendly(address, contacts);
+        assertEquals("@username", friendly.toString());
+    }
+
     @Test
     public void toFriendly_spoofPreventionDoesntOverrideContact() {
         Address address = new Address("test@testor.com", "Tim Testor");