| 1 | /* | |
| 2 | MailClena - Copyright (C) 2018, Aiki IT | |
| 3 | <p> | |
| 4 | This program is free software: you can redistribute it and/or modify | |
| 5 | it under the terms of the GNU General Public License as published by | |
| 6 | the Free Software Foundation, either version 3 of the License, or | |
| 7 | (at your option) any later version. | |
| 8 | <p> | |
| 9 | This program is distributed in the hope that it will be useful, | |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | GNU General Public License for more details. | |
| 13 | <p> | |
| 14 | You should have received a copy of the GNU General Public License | |
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | */ | |
| 17 | package de.aikiit.mailclena.mail; | |
| 18 | ||
| 19 | import com.google.common.base.Strings; | |
| 20 | import de.aikiit.mailclena.MailConfiguration; | |
| 21 | import jakarta.mail.Flags; | |
| 22 | import jakarta.mail.Folder; | |
| 23 | import jakarta.mail.Message; | |
| 24 | import jakarta.mail.MessagingException; | |
| 25 | import jakarta.mail.Session; | |
| 26 | import jakarta.mail.Store; | |
| 27 | import lombok.extern.log4j.Log4j2; | |
| 28 | import me.tongfei.progressbar.DelegatingProgressBarConsumer; | |
| 29 | import me.tongfei.progressbar.ProgressBar; | |
| 30 | import me.tongfei.progressbar.ProgressBarBuilder; | |
| 31 | import me.tongfei.progressbar.ProgressBarStyle; | |
| 32 | import org.apache.commons.lang3.tuple.Pair; | |
| 33 | import org.assertj.core.util.VisibleForTesting; | |
| 34 | ||
| 35 | import java.time.ZoneId; | |
| 36 | import java.time.format.DateTimeFormatter; | |
| 37 | import java.util.Arrays; | |
| 38 | import java.util.Date; | |
| 39 | import java.util.List; | |
| 40 | import java.util.Locale; | |
| 41 | import java.util.Optional; | |
| 42 | import java.util.Properties; | |
| 43 | import java.util.concurrent.atomic.AtomicLong; | |
| 44 | ||
| 45 | import static de.aikiit.mailclena.mail.MailClient.MailClientCommands.LIST; | |
| 46 | import static de.aikiit.mailclena.mail.MailClient.MailClientCommands.parse; | |
| 47 | ||
| 48 | /** | |
| 49 | * Encapsulates technical access to mail inbox based on the given application/mail configuration. | |
| 50 | */ | |
| 51 | @Log4j2 | |
| 52 | public final class MailClient { | |
| 53 | ||
| 54 | private static final String INBOX = "INBOX"; | |
| 55 | private static final String POP3S = "pop3s"; | |
| 56 | private static final DateTimeFormatter SENT_DATE_FORMATTER = | |
| 57 | DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss", Locale.ROOT); | |
| 58 | ||
| 59 | private final MailConfiguration mailConfiguration; | |
| 60 | private final Session emailSession; | |
| 61 | ||
| 62 | /** | |
| 63 | * Creates a client using the default POP3S connection settings. | |
| 64 | * | |
| 65 | * @param mailConfiguration credentials and host of the mailbox. | |
| 66 | */ | |
| 67 | public MailClient(MailConfiguration mailConfiguration) { | |
| 68 | this(mailConfiguration, Session.getInstance(getProperties(mailConfiguration))); | |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * Creates a client with a given email session, useful for testing. | |
| 73 | * | |
| 74 | * @param mailConfiguration credentials and host of the mailbox. | |
| 75 | * @param emailSession email session to use for connecting to the mailbox. | |
| 76 | */ | |
| 77 | @VisibleForTesting | |
| 78 | MailClient(MailConfiguration mailConfiguration, Session emailSession) { | |
| 79 | this.mailConfiguration = mailConfiguration; | |
| 80 | this.emailSession = emailSession; | |
| 81 | } | |
| 82 | ||
| 83 | private static Properties getProperties(MailConfiguration mailConfiguration) { | |
| 84 | Properties properties = new Properties(); | |
| 85 | properties.put("mail.pop3s.host", mailConfiguration.host()); | |
| 86 | properties.put("mail.pop3.starttls.enable", "true"); | |
| 87 | properties.put("mail.pop3.starttls.required", "true"); | |
| 88 | properties.put("mail.pop3s.port", "995"); | |
| 89 | properties.put("mail.store.protocol", "pop3"); | |
| 90 | // in case of Goneo certificate errors: enable debug | |
| 91 | // properties.put("mail.debug", "true"); | |
| 92 |
1
1. getProperties : replaced return value with null for de/aikiit/mailclena/mail/MailClient::getProperties → KILLED |
return properties; |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Opens a mail folder in the given mode. | |
| 97 | * | |
| 98 | * @param mode see @{@link Folder#open(int)} for available options. | |
| 99 | * @return pair of @{@link Store} and @{@link Folder} if available. | |
| 100 | * @throws MessagingException if folder cannot be opened or store is inaccessible. | |
| 101 | */ | |
| 102 | @VisibleForTesting | |
| 103 | Optional<Pair<Store, Folder>> openFolder(int mode) throws MessagingException { | |
| 104 | Store store = emailSession.getStore(POP3S); | |
| 105 |
1
1. openFolder : removed call to jakarta/mail/Store::connect → KILLED |
store.connect(mailConfiguration.host(), mailConfiguration.username(), mailConfiguration.password()); |
| 106 | ||
| 107 | Folder emailFolder = store.getFolder(INBOX); | |
| 108 |
1
1. openFolder : removed call to jakarta/mail/Folder::open → KILLED |
emailFolder.open(mode); |
| 109 |
1
1. openFolder : replaced return value with Optional.empty for de/aikiit/mailclena/mail/MailClient::openFolder → KILLED |
return Optional.of(Pair.of(store, emailFolder)); |
| 110 | } | |
| 111 | ||
| 112 | /** | |
| 113 | * Wraps a list of messages into a progress bar if the application is running in an interactive terminal. | |
| 114 | * | |
| 115 | * @param messages list of messages to wrap. | |
| 116 | * @param task name of the task to display in the progress bar. | |
| 117 | * @param interactive {@code true} if the application is running in an interactive terminal, {@code false} otherwise. | |
| 118 | * @return iterable of messages with or without progress bar. | |
| 119 | */ | |
| 120 | @VisibleForTesting | |
| 121 | // Terminal detection can block on headless Windows runners while the POP3 session times out. | |
| 122 | static Iterable<Message> withProgress(List<Message> messages, String task, boolean interactive) { | |
| 123 |
1
1. withProgress : negated conditional → KILLED |
if (!interactive) { |
| 124 |
1
1. withProgress : replaced return value with Collections.emptyList for de/aikiit/mailclena/mail/MailClient::withProgress → KILLED |
return messages; |
| 125 | } | |
| 126 | // Cursor-based rendering would overwrite the last line of multiline mail logs. | |
| 127 |
1
1. withProgress : replaced return value with Collections.emptyList for de/aikiit/mailclena/mail/MailClient::withProgress → KILLED |
return ProgressBar.wrap(messages, new ProgressBarBuilder() |
| 128 | .setTaskName(task) | |
| 129 | .setStyle(ProgressBarStyle.ASCII) | |
| 130 | .setConsumer(new DelegatingProgressBarConsumer(log::info, 100))); | |
| 131 | } | |
| 132 | ||
| 133 | /** | |
| 134 | * Shows a list of messages in the mailbox root folder. It accesses the folder in read-only mode. | |
| 135 | * | |
| 136 | * @return messages in given folder, -1 in case of errors. | |
| 137 | */ | |
| 138 | @VisibleForTesting | |
| 139 | long list() { | |
| 140 | try { | |
| 141 | Optional<Pair<Store, Folder>> folder = openFolder(Folder.READ_ONLY); | |
| 142 | ||
| 143 |
1
1. list : negated conditional → KILLED |
if (folder.isEmpty()) { |
| 144 | log.error("Unable to open folder in read-only mode to list mails, will abort."); | |
| 145 |
1
1. list : replaced long return with 0 for de/aikiit/mailclena/mail/MailClient::list → SURVIVED |
return -1; |
| 146 | } | |
| 147 | ||
| 148 | Pair<Store, Folder> storeAndFolder = folder.get(); | |
| 149 | List<Message> messages = Arrays.asList(storeAndFolder.getRight().getMessages()); | |
| 150 | final int size = messages.size(); | |
| 151 | ||
| 152 |
1
1. list : negated conditional → KILLED |
if (size == 0) { |
| 153 | log.info("No messages found - nothing to be done here."); | |
| 154 | } else { | |
| 155 | ||
| 156 | log.info("Found {} messages.", size); | |
| 157 | ||
| 158 |
1
1. list : negated conditional → SURVIVED |
for (Message m : withProgress(messages, "Listing", System.console() != null)) { |
| 159 | try { | |
| 160 | Date sentDate = m.getSentDate(); | |
| 161 |
1
1. list : negated conditional → KILLED |
String formattedDate = sentDate == null ? null : SENT_DATE_FORMATTER |
| 162 | .format(sentDate.toInstant().atZone(ZoneId.systemDefault())); | |
| 163 | log.info("{} bytes / {}{}Subject: {} / From: {}", | |
| 164 | m.getSize(), formattedDate, System.lineSeparator(), m.getSubject(), Arrays.toString(m.getFrom())); | |
| 165 | } catch (MessagingException e) { | |
| 166 | log.error("Error while traversing messages", e); | |
| 167 | } | |
| 168 | } | |
| 169 | } | |
| 170 | ||
| 171 |
1
1. list : removed call to jakarta/mail/Store::close → KILLED |
storeAndFolder.getLeft().close(); |
| 172 |
1
1. list : replaced long return with 0 for de/aikiit/mailclena/mail/MailClient::list → KILLED |
return size; |
| 173 | } catch (MessagingException e) { | |
| 174 | log.error(e); | |
| 175 | } | |
| 176 |
1
1. list : replaced long return with 0 for de/aikiit/mailclena/mail/MailClient::list → SURVIVED |
return -1; |
| 177 | } | |
| 178 | ||
| 179 | /** | |
| 180 | * Application option to delete existing messages. | |
| 181 | * | |
| 182 | * @return number of messages deleted, if any. Empty otherwise. | |
| 183 | */ | |
| 184 | @VisibleForTesting | |
| 185 | Optional<Long> delete() { | |
| 186 | try { | |
| 187 | Optional<Pair<Store, Folder>> folder = openFolder(Folder.READ_WRITE); | |
| 188 | ||
| 189 |
1
1. delete : negated conditional → KILLED |
if (folder.isEmpty()) { |
| 190 | log.error("Unable to open folder in write mode to remove mails, will abort."); | |
| 191 | return Optional.empty(); | |
| 192 | } | |
| 193 | ||
| 194 | Pair<Store, Folder> storeAndFolder = folder.get(); | |
| 195 | final Folder f = storeAndFolder.getRight(); | |
| 196 | List<Message> messages = Arrays.asList(f.getMessages()); | |
| 197 | ||
| 198 | final int count = messages.size(); | |
| 199 | final AtomicLong mailSize = new AtomicLong(0L); | |
| 200 |
1
1. delete : negated conditional → KILLED |
if (count == 0) { |
| 201 | log.info("Folder is empty already - nothing to be done here."); | |
| 202 | } else { | |
| 203 | log.info("Starting to delete {} messages.", count); | |
| 204 | ||
| 205 |
1
1. delete : negated conditional → SURVIVED |
for (Message message : withProgress(messages, "Deleting", System.console() != null)) { |
| 206 | try { | |
| 207 | long messageSize = message.getSize(); | |
| 208 | log.info("Marking for deletion: {} bytes / {}{}Subject: {} / From: {}", | |
| 209 | messageSize, message.getSentDate(), System.lineSeparator(), message.getSubject(), Arrays.toString(message.getFrom())); | |
| 210 |
1
1. delete : removed call to jakarta/mail/Message::setFlag → KILLED |
message.setFlag(Flags.Flag.DELETED, true); |
| 211 | mailSize.addAndGet(messageSize); | |
| 212 | } catch (MessagingException e) { | |
| 213 | log.error("Error while traversing messages for deletion", e); | |
| 214 | } | |
| 215 | } | |
| 216 | ||
| 217 |
1
1. delete : removed call to jakarta/mail/Folder::close → KILLED |
f.close(true); |
| 218 | log.info("Expunge folder to actually remove messages."); | |
| 219 | log.info("Finished to delete {} messages, set {} bytes free", count, mailSize.get()); | |
| 220 | } | |
| 221 |
1
1. delete : removed call to jakarta/mail/Store::close → KILLED |
storeAndFolder.getLeft().close(); |
| 222 | ||
| 223 |
1
1. delete : replaced return value with Optional.empty for de/aikiit/mailclena/mail/MailClient::delete → KILLED |
return Optional.of(mailSize.longValue()); |
| 224 | } catch (MessagingException e) { | |
| 225 | log.error(e); | |
| 226 | } | |
| 227 | return Optional.empty(); | |
| 228 | } | |
| 229 | ||
| 230 | /** | |
| 231 | * Execute the given command or print an error message if the command is unknown. | |
| 232 | * | |
| 233 | * @param command command to execute, should be one of {@link MailClientCommands}. | |
| 234 | */ | |
| 235 | public void execute(String command) { | |
| 236 | Optional<MailClientCommands> cmd = parse(command); | |
| 237 |
1
1. execute : negated conditional → KILLED |
if (cmd.isEmpty()) { |
| 238 | log.info("No explicit command given, will fallback to 'list'"); | |
| 239 | cmd = Optional.of(LIST); | |
| 240 | } | |
| 241 | ||
| 242 | switch (cmd.get()) { | |
| 243 | case CLEAN: | |
| 244 | delete(); | |
| 245 | break; | |
| 246 | case LIST: | |
| 247 | list(); | |
| 248 | break; | |
| 249 | default: | |
| 250 | log.error("If you see this message, please report a bug since the CLI parser has new commands."); | |
| 251 | // NOOP: avoid findbugs warning | |
| 252 | break; | |
| 253 | } | |
| 254 | } | |
| 255 | ||
| 256 | /** | |
| 257 | * Encapsulates available application commands for MailClena. | |
| 258 | */ | |
| 259 | @VisibleForTesting | |
| 260 | enum MailClientCommands { | |
| 261 | /** | |
| 262 | * Option to list available mails. | |
| 263 | */ | |
| 264 | LIST, | |
| 265 | /** | |
| 266 | * Option to purge existing mails. | |
| 267 | */ | |
| 268 | CLEAN; | |
| 269 | ||
| 270 | /** | |
| 271 | * Tries to convert a given command into an available application command option. | |
| 272 | * | |
| 273 | * @param command alphanumeric representation of the command. | |
| 274 | * @return a valid application command or empty if invalid. | |
| 275 | */ | |
| 276 | static Optional<MailClientCommands> parse(String command) { | |
| 277 |
1
1. parse : negated conditional → KILLED |
if (!Strings.isNullOrEmpty(command)) { |
| 278 | String normalized = command.trim(); | |
| 279 | for (MailClientCommands cmd : values()) { | |
| 280 |
1
1. parse : negated conditional → KILLED |
if (normalized.equalsIgnoreCase(cmd.toString())) { |
| 281 |
1
1. parse : replaced return value with Optional.empty for de/aikiit/mailclena/mail/MailClient$MailClientCommands::parse → KILLED |
return Optional.of(cmd); |
| 282 | } | |
| 283 | } | |
| 284 | } | |
| 285 | ||
| 286 | return Optional.empty(); | |
| 287 | } | |
| 288 | ||
| 289 | } | |
| 290 | ||
| 291 | } | |
Mutations | ||
| 92 |
1.1 |
|
| 105 |
1.1 |
|
| 108 |
1.1 |
|
| 109 |
1.1 |
|
| 123 |
1.1 |
|
| 124 |
1.1 |
|
| 127 |
1.1 |
|
| 143 |
1.1 |
|
| 145 |
1.1 |
|
| 152 |
1.1 |
|
| 158 |
1.1 |
|
| 161 |
1.1 |
|
| 171 |
1.1 |
|
| 172 |
1.1 |
|
| 176 |
1.1 |
|
| 189 |
1.1 |
|
| 200 |
1.1 |
|
| 205 |
1.1 |
|
| 210 |
1.1 |
|
| 217 |
1.1 |
|
| 221 |
1.1 |
|
| 223 |
1.1 |
|
| 237 |
1.1 |
|
| 277 |
1.1 |
|
| 280 |
1.1 |
|
| 281 |
1.1 |