Skip to content
Permalink
Browse files
267: Be more tolerant when parsing mbox files
Reviewed-by: ehelin
  • Loading branch information
rwestberg committed Feb 6, 2020
1 parent feb2fcc commit 6e9e58b7776dd3fa94baab9fac8f5eadfc1eb1f3
Showing 2 changed files with 17 additions and 6 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -55,9 +55,10 @@ private EmailAddress(String fullName, String localPart, String domain) {
this.domain = domain;
}

private static Pattern decoratedAddressPattern = Pattern.compile("(?<name>.*?)(?:\\s*)<(?<local>.*)@(?<domain>.*?)>");
private final static Pattern decoratedAddressPattern = Pattern.compile("(?<name>.*?)(?:\\s*)<(?<local>.*)@(?<domain>.*?)>");
private final static Pattern obfuscatedPattern = Pattern.compile("(?<local>.*) at (?<domain>.*) \\((?<name>.*)\\)");
private static Pattern plainAddressPattern = Pattern.compile("(?<name>)(?<local>.*)@(?<domain>.*?)");
private final static Pattern plainAddressPattern = Pattern.compile("(?<name>)(?<local>.*)@(?<domain>.*?)");
private final static Pattern unqualifiedDecoratedAddressPattern = Pattern.compile("(?<name>.*?)(?:\\s*)<(?<local>.*)(?<domain>)>");

public static EmailAddress parse(String address) {
var matcher = decoratedAddressPattern.matcher(address);
@@ -66,7 +67,10 @@ public static EmailAddress parse(String address) {
if (!matcher.matches()) {
matcher = plainAddressPattern.matcher(address);
if (!matcher.matches()) {
throw new IllegalArgumentException("Cannot parse email address: " + address);
matcher = unqualifiedDecoratedAddressPattern.matcher(address);
if (!matcher.matches()) {
throw new IllegalArgumentException("Cannot parse email address: " + address);
}
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -27,7 +27,6 @@
import static org.junit.jupiter.api.Assertions.*;

class EmailAddressTests {

@Test
void simple() {
var address = EmailAddress.parse("Full Name <full@name.com>");
@@ -55,4 +54,12 @@ void noBrackets() {
assertEquals("no", address.localPart());
}

@Test
void noDomain() {
var address = EmailAddress.parse("<noone.ever.>");
assertFalse(address.fullName().isPresent());
assertEquals("noone.ever.@", address.address());
assertEquals("", address.domain());
assertEquals("noone.ever.", address.localPart());
}
}

0 comments on commit 6e9e58b

Please sign in to comment.