6 changed files with 185 additions and 9 deletions
			
			
		| @ -0,0 +1,24 @@@@ -0,0 +1,24 @@ | ||||
| package de.srsoftware.widerhall; | ||||
| 
 | ||||
| import org.json.simple.JSONObject; | ||||
| import org.json.simple.parser.JSONParser; | ||||
| import org.json.simple.parser.ParseException; | ||||
| 
 | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.nio.file.Files; | ||||
| 
 | ||||
| public class Application { | ||||
| 
 | ||||
|     public static void main(String[] args) throws IOException, ParseException { | ||||
|         var parser = new JSONParser(); | ||||
|         var config = Files.readString(new File("/tmp/config.json").toPath()); | ||||
|         JSONObject json = (JSONObject) parser.parse(config); | ||||
| 
 | ||||
|         //String testSender = (String) json.get("sender");
 | ||||
|         //String testReceiver = (String) json.get("receiver");
 | ||||
|      //   new SmtpClient(json).send(json,testSender,"Stephan Richter",testReceiver,"Test","Dies ist ein Test");
 | ||||
|         MessageHandler forward = new Forwarder(); | ||||
|        new ImapClient(json).addListener(forward).start(); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,9 @@@@ -0,0 +1,9 @@ | ||||
| package de.srsoftware.widerhall; | ||||
| 
 | ||||
| public class Constants { | ||||
|     public static final String IMAPS = "imaps"; | ||||
|     public static final String PROTOCOL = "mail.store.protocol"; | ||||
|     public static final String HOST = "host"; | ||||
|     public static final String USER = "user"; | ||||
|     public static final String PASSWORD = "password"; | ||||
| } | ||||
| @ -0,0 +1,15 @@@@ -0,0 +1,15 @@ | ||||
| package de.srsoftware.widerhall; | ||||
| 
 | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| 
 | ||||
| import javax.mail.Message; | ||||
| import javax.mail.MessagingException; | ||||
| 
 | ||||
| public class Forwarder implements MessageHandler { | ||||
|     private static final Logger LOG = LoggerFactory.getLogger(Forwarder.class); | ||||
|     @Override | ||||
|     public void onMessageReceived(Message message) throws MessagingException { | ||||
|         LOG.debug("forwarding {}",message.getSubject()); | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,129 @@@@ -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; | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,8 @@@@ -0,0 +1,8 @@ | ||||
| package de.srsoftware.widerhall; | ||||
| 
 | ||||
| import javax.mail.Message; | ||||
| import javax.mail.MessagingException; | ||||
| 
 | ||||
| public interface MessageHandler { | ||||
|     public void onMessageReceived(Message message) throws MessagingException; | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue