|
|
@ -1,15 +1,23 @@
|
|
|
|
package com.olexyn.burnsmail.mail;
|
|
|
|
package com.olexyn.burnsmail.mail;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.olexyn.burnsmail.flow.Flow;
|
|
|
|
|
|
|
|
import com.olexyn.burnsmail.flow.action.CopyToDst;
|
|
|
|
|
|
|
|
import com.olexyn.burnsmail.flow.cleanup.FlushFolders;
|
|
|
|
|
|
|
|
import com.olexyn.burnsmail.flow.filter.AnyFromContains;
|
|
|
|
|
|
|
|
import com.olexyn.burnsmail.flow.map.FullToAMailMapper;
|
|
|
|
|
|
|
|
import com.olexyn.burnsmail.flow.map.LazyToAMailMapper;
|
|
|
|
|
|
|
|
import com.olexyn.burnsmail.flow.search.FetchAll;
|
|
|
|
|
|
|
|
import com.olexyn.burnsmail.flow.search.SearchAnySubject;
|
|
|
|
|
|
|
|
import com.olexyn.burnsmail.flow.transform.SetOpenAiScore;
|
|
|
|
|
|
|
|
import com.olexyn.burnsmail.model.AMail;
|
|
|
|
|
|
|
|
import com.olexyn.min.log.LogU;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.mail.Flags;
|
|
|
|
|
|
|
|
import javax.mail.Folder;
|
|
|
|
|
|
|
|
import javax.mail.Message;
|
|
|
|
|
|
|
|
import javax.mail.MessagingException;
|
|
|
|
import javax.mail.MessagingException;
|
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.Session;
|
|
|
|
import javax.mail.Store;
|
|
|
|
import javax.mail.Store;
|
|
|
|
import javax.mail.search.FlagTerm;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -17,6 +25,7 @@ import java.util.Properties;
|
|
|
|
@Service
|
|
|
|
@Service
|
|
|
|
public class EmailService {
|
|
|
|
public class EmailService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String INBOX = "INBOX";
|
|
|
|
|
|
|
|
|
|
|
|
private Store connect() throws MessagingException {
|
|
|
|
private Store connect() throws MessagingException {
|
|
|
|
var properties = new Properties();
|
|
|
|
var properties = new Properties();
|
|
|
@ -32,34 +41,54 @@ public class EmailService {
|
|
|
|
return store;
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void process(Flow flow) {
|
|
|
|
|
|
|
|
try (Store store = connect()) {
|
|
|
|
|
|
|
|
// SEARCH
|
|
|
|
|
|
|
|
var messages = flow.getSearchFolder().search(store);
|
|
|
|
|
|
|
|
// MAP TO AMail
|
|
|
|
|
|
|
|
List<AMail> mappeds = new ArrayList<>();
|
|
|
|
|
|
|
|
for (var message : messages) {
|
|
|
|
|
|
|
|
mappeds.add(flow.getToAMailMapper().map(message));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// FILTER & TRANSFORM & ACTION
|
|
|
|
|
|
|
|
mappeds.stream()
|
|
|
|
|
|
|
|
.filter(flow.getFilterAMail()::doKeep)
|
|
|
|
|
|
|
|
.map(m -> flow.getTransformAMail().transform(m))
|
|
|
|
|
|
|
|
.forEach(m -> flow.getAction().execute(m, store));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (MessagingException | NullPointerException e) {
|
|
|
|
|
|
|
|
LogU.warnPlain("Could not process." + e.getMessage());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void readEmails() throws MessagingException, IOException {
|
|
|
|
|
|
|
|
Store store = connect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Folder inbox = store.getFolder("INBOX");
|
|
|
|
|
|
|
|
inbox.open(Folder.READ_ONLY);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Fetch unread messages
|
|
|
|
|
|
|
|
Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (Message message : messages) {
|
|
|
|
|
|
|
|
//TODO: do something with the message
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Close resources
|
|
|
|
public void doSbbTriage() {
|
|
|
|
inbox.close(false);
|
|
|
|
var flow = new Flow(
|
|
|
|
store.close();
|
|
|
|
new SearchAnySubject(INBOX, "Your online purchase from SBB", "Your online purchase from SBB"),
|
|
|
|
|
|
|
|
new LazyToAMailMapper(),
|
|
|
|
|
|
|
|
new AnyFromContains("sbbclient@sbb.ch", "sbbclient@order.info.sbb.ch"),
|
|
|
|
|
|
|
|
(AMail a) -> a,
|
|
|
|
|
|
|
|
new CopyToDst(INBOX, "INBOX/TRIAGE/SBB"),
|
|
|
|
|
|
|
|
new FlushFolders(INBOX)
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
process(flow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void expunge() throws MessagingException {
|
|
|
|
public void classifyAsSpam() {
|
|
|
|
Store store = connect();
|
|
|
|
var flow = new Flow(
|
|
|
|
Folder trash = store.getFolder("INBOX").getFolder("Trash");
|
|
|
|
new FetchAll(INBOX),
|
|
|
|
trash.open(Folder.READ_WRITE);
|
|
|
|
new FullToAMailMapper(),
|
|
|
|
for (Message message : trash.getMessages()) {
|
|
|
|
(AMail a) -> true,
|
|
|
|
message.setFlag(Flags.Flag.DELETED, true);
|
|
|
|
new SetOpenAiScore(),
|
|
|
|
}
|
|
|
|
new CopyToDst(INBOX, "INBOX/TRIAGE/SPAM"),
|
|
|
|
trash.close(true); // expunge
|
|
|
|
new FlushFolders(INBOX)
|
|
|
|
store.close();
|
|
|
|
);
|
|
|
|
|
|
|
|
process(flow);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|