working on IMAP listener
This commit is contained in:
129
src/main/java/de/srsoftware/widerhall/ImapClient.java
Normal file
129
src/main/java/de/srsoftware/widerhall/ImapClient.java
Normal file
@@ -0,0 +1,129 @@
|
||||
package de.srsoftware.widerhall;
|
||||
|
||||
import com.sun.mail.iap.ProtocolException;
|
||||
import com.sun.mail.imap.IMAPFolder;
|
||||
import com.sun.mail.imap.protocol.IMAPProtocol;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.mail.*;
|
||||
import java.util.HashSet;
|
||||
import java.util.Properties;
|
||||
|
||||
public class ImapClient {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ImapClient.class);
|
||||
private final JSONObject config;
|
||||
private boolean stopped = false;
|
||||
private IMAPFolder inbox;
|
||||
private HashSet<MessageHandler> listeners = new HashSet<>();
|
||||
|
||||
|
||||
|
||||
private class ListeningThread extends Thread {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class);
|
||||
private final JSONObject config;
|
||||
|
||||
ListeningThread(JSONObject config){
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stopped) {
|
||||
try {
|
||||
openInbox();
|
||||
} catch (MessagingException e){
|
||||
LOG.warn("Connection problem:",e);
|
||||
try {
|
||||
sleep(1000);
|
||||
} catch (InterruptedException interruptedException) {
|
||||
interruptedException.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void openInbox() throws MessagingException {
|
||||
String host = (String) config.get(Constants.HOST);
|
||||
String username = (String) config.get(Constants.USER);
|
||||
String password = (String) config.get(Constants.PASSWORD);
|
||||
LOG.debug("Connecting and logging in…");
|
||||
Properties props = imapProps();
|
||||
Session session = Session.getInstance(props);
|
||||
Store store = session.getStore(Constants.IMAPS);
|
||||
store.connect(host,username,password);
|
||||
LOG.debug("Connected, opening inbox:");
|
||||
inbox = (IMAPFolder)store.getFolder("INBOX");
|
||||
inbox.open(IMAPFolder.READ_WRITE);
|
||||
|
||||
while (!stopped){
|
||||
handleMessages();
|
||||
LOG.debug("Idling.");
|
||||
inbox.idle(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMessages() throws MessagingException {
|
||||
LOG.debug("reading email:");
|
||||
if (!inbox.isOpen()){
|
||||
inbox.open(IMAPFolder.READ_WRITE);
|
||||
}
|
||||
for (Message message : inbox.getMessages()){
|
||||
if (message.isSet(Flags.Flag.SEEN)) continue;
|
||||
handle(message);
|
||||
message.setFlag(Flags.Flag.SEEN,true);
|
||||
}
|
||||
}
|
||||
|
||||
private void handle(Message message) throws MessagingException {
|
||||
LOG.debug("Handling {}",message.getSubject());
|
||||
for (MessageHandler listener : listeners) listener.onMessageReceived(message);
|
||||
}
|
||||
|
||||
private Properties imapProps() {
|
||||
Properties props = new Properties();
|
||||
props.put(Constants.PROTOCOL,Constants.IMAPS);
|
||||
return props;
|
||||
}
|
||||
}
|
||||
|
||||
private class Heartbeat extends Thread{
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Heartbeat.class);
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stopped){
|
||||
try {
|
||||
sleep(300034);
|
||||
if (inbox != null) continue;
|
||||
LOG.debug("sending NOOP");
|
||||
inbox.doCommand(protocol -> {
|
||||
protocol.simpleCommand("NOOP",null);
|
||||
return null;
|
||||
});
|
||||
} catch (InterruptedException | MessagingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ImapClient(JSONObject config){
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
LOG.debug("Creating ListeningThread…");
|
||||
new ListeningThread(config).start();
|
||||
LOG.debug("Creating Heartbeat…");
|
||||
new Heartbeat().start();
|
||||
}
|
||||
|
||||
|
||||
public ImapClient addListener(MessageHandler messageHandler) {
|
||||
listeners.add(messageHandler);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user