Skip to content

Commit

Permalink
fix: Update delimiter fix-up for personal parts
Browse files Browse the repository at this point in the history
Updated the delimiter fix-up to take into account personal names in email addresses (e.g., Slide O Mix <someone@email.com>).

See JENKINS-69681
  • Loading branch information
slide committed Nov 16, 2024
1 parent d9e491c commit cd00349
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
61 changes: 56 additions & 5 deletions src/main/java/hudson/plugins/emailext/EmailRecipientUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,64 @@ public FormValidation validateFormRecipientList(String recipientList) {
}
}

private static String fixupDelimiters(String input) {
input = input.trim();
input = input.replaceAll("\\s+", " ");
if (input.contains(" ") && !input.contains(",")) {
input = input.replace(" ", ",");
static String fixupDelimiters(String input) {
// replace multiple whitespace with a single space
input = input.trim().replaceAll("\\s+", " ");

// we only need to do more fixup if there are spaces in the string
if (input.contains(" ")) {
if(!input.contains("<") && !input.contains(("("))) {
input = input.replace(" ", ", ");
} else {
StringBuilder builder = new StringBuilder(input);
boolean inQuote = false;
boolean sawAt = false;
for (int i = 0; i < builder.length(); i++) {
char curr = builder.charAt(i);
if (curr == '"') {
inQuote = !inQuote;
continue;
}

if (curr == '@') {
sawAt = true;
continue;
}

if (!inQuote && curr == ',') {
sawAt = false;
continue;
}

if (builder.charAt(i) == ' ' && !inQuote) {
if (builder.charAt(i - 1) == '>' || builder.charAt(i - 1) == ')') {
if (((i + 1) < builder.length()) && builder.charAt(i + 1) != ',') {
builder.setCharAt(i, ',');
if (builder.charAt(i + 1) != ' ') {
builder.insert(i + 1, ' ');
i++;
}
}
continue;
}
if (sawAt) {
if ((i + 1) < builder.length() && builder.charAt(i + 1) != ',') {
builder.setCharAt(i, ',');
if (builder.charAt(i + 1) != ' ') {
builder.insert(i + 1, ' ');
i++;
}
}
}
sawAt = false;
}
}
input = builder.toString();
}
}

// now that spaces and friends have been fixed, let's make sure we're
// using commas instead of semicolons as well.
input = input.replace(';', ',');
return input;
}
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/hudson/plugins/emailext/EmailRecipientUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package hudson.plugins.emailext;

import jakarta.mail.internet.InternetAddress;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class EmailRecipientUtilsTest {
@Test
public void testFixupDelimiters() throws Exception {
String output;
output = EmailRecipientUtils.fixupDelimiters(" Name Surname <n.Surname@mymail.com> ");
assertEquals("Name Surname <n.Surname@mymail.com>", output);
InternetAddress[] addresses = InternetAddress.parse(output);
assertEquals(1, addresses.length);
assertEquals("Name Surname", addresses[0].getPersonal());
assertEquals("n.Surname@mymail.com", addresses[0].getAddress());

output = EmailRecipientUtils.fixupDelimiters("user0, user1@email.com User Two <user2@email.com> user3@email.com ");
assertEquals("user0, user1@email.com, User Two <user2@email.com>, user3@email.com", output);
addresses = InternetAddress.parse(output);
assertEquals(4, addresses.length);

assertNull(addresses[0].getPersonal());
assertEquals("user0", addresses[0].getAddress());

assertNull(addresses[1].getPersonal());
assertEquals("user1@email.com", addresses[1].getAddress());

assertEquals("User Two", addresses[2].getPersonal());
assertEquals("user2@email.com", addresses[2].getAddress());

assertNull(addresses[3].getPersonal());
assertEquals("user3@email.com", addresses[3].getAddress());
}
}

0 comments on commit cd00349

Please sign in to comment.