diff --git a/src/main/java/com/olexyn/burnsmail/MiscU.java b/src/main/java/com/olexyn/burnsmail/MiscU.java new file mode 100644 index 0000000..9bb647d --- /dev/null +++ b/src/main/java/com/olexyn/burnsmail/MiscU.java @@ -0,0 +1,49 @@ +package com.olexyn.burnsmail; + +import lombok.experimental.UtilityClass; + +import javax.mail.Folder; +import javax.mail.MessagingException; +import javax.mail.Store; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static javax.mail.Folder.READ_WRITE; + +@UtilityClass +public class MiscU { + + private static final String EMAIL_REGEX = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z]{2,6}\\b"; + private static final Pattern PATTERN = Pattern.compile(EMAIL_REGEX, Pattern.CASE_INSENSITIVE); + + public static String extractEmail(String rawFrom) { + Matcher matcher = PATTERN.matcher(rawFrom); + if (matcher.find()) { + return matcher.group(); + } + return null; + } + + + private static Folder expand(Store store, Folder folder, List folderPath) throws MessagingException { + if (folderPath.isEmpty()) { + return folder; + } + if (folder == null) { + folder = store.getFolder(folderPath.remove(0)); + } else { + folder = folder.getFolder(folderPath.remove(0)); + } + return expand(store, folder, folderPath); + + } + + public static Folder open(Store store, String folderPath) throws MessagingException { + var arrList = new ArrayList<>(List.of(folderPath.split("/"))); + Folder targetFolder = expand(store, null, arrList); + targetFolder.open(READ_WRITE); + return targetFolder; + } +}