From 1c0330a9311fdcd3bb163ba130fa6c3114348100 Mon Sep 17 00:00:00 2001 From: io42630 Date: Wed, 24 Jan 2024 23:44:13 +0100 Subject: [PATCH] + use web controller --- README.md | 4 +- pom.xml | 8 ++++ push.sh | 7 --- .../burnsmail/BurnsMailApplication.java | 22 +--------- .../olexyn/burnsmail/mail/EmailService.java | 27 +++++++++--- .../com/olexyn/burnsmail/web/Controller.java | 44 +++++++++++++++++++ src/test/resources/expunge.http | 6 +++ src/test/resources/read-mail.http | 6 +++ 8 files changed, 89 insertions(+), 35 deletions(-) delete mode 100644 push.sh create mode 100644 src/main/java/com/olexyn/burnsmail/web/Controller.java create mode 100644 src/test/resources/expunge.http create mode 100644 src/test/resources/read-mail.http diff --git a/README.md b/README.md index 528f6d1..4648f75 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ## Goals -* connect to mailbox +* connect to mailbox (OK) * connect to ollama -* check mail +* check mail (OK) * move to burn folder \ No newline at end of file diff --git a/pom.xml b/pom.xml index 360426f..474bd90 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,14 @@ + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-autoconfigure + org.springframework.boot spring-boot-starter-mail diff --git a/push.sh b/push.sh deleted file mode 100644 index 11ceb27..0000000 --- a/push.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -docker push io42630/burns-mail:0.1 - - - - diff --git a/src/main/java/com/olexyn/burnsmail/BurnsMailApplication.java b/src/main/java/com/olexyn/burnsmail/BurnsMailApplication.java index c2edd11..e7142bf 100644 --- a/src/main/java/com/olexyn/burnsmail/BurnsMailApplication.java +++ b/src/main/java/com/olexyn/burnsmail/BurnsMailApplication.java @@ -1,9 +1,7 @@ package com.olexyn.burnsmail; -import com.olexyn.burnsmail.mail.EmailService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.EnableAspectJAutoProxy; @SpringBootApplication @@ -11,24 +9,8 @@ import org.springframework.context.annotation.EnableAspectJAutoProxy; @EnableAspectJAutoProxy(proxyTargetClass = true) public class BurnsMailApplication { - public static void main(String[] args) throws InterruptedException { - ConfigurableApplicationContext context = SpringApplication.run(BurnsMailApplication.class, args); - - Thread.sleep(10000L); - // Retrieve the EmailService bean - EmailService emailService = context.getBean(EmailService.class); - - // Call the readEmails method - try { - emailService.readEmails(); - } catch (Exception e) { - e.printStackTrace(); - // Handle exceptions as needed - } - - // Close the application context - context.close(); - + public static void main(String[] args) { + SpringApplication.run(BurnsMailApplication.class, args); } } diff --git a/src/main/java/com/olexyn/burnsmail/mail/EmailService.java b/src/main/java/com/olexyn/burnsmail/mail/EmailService.java index e345b5f..db6a9bc 100644 --- a/src/main/java/com/olexyn/burnsmail/mail/EmailService.java +++ b/src/main/java/com/olexyn/burnsmail/mail/EmailService.java @@ -18,8 +18,8 @@ import java.util.Properties; public class EmailService { - public void readEmails() throws MessagingException, IOException { - Properties properties = new Properties(); + private Store connect() throws MessagingException { + var properties = new Properties(); properties.setProperty("mail.store.protocol", "imaps"); Session session = Session.getDefaultInstance(properties, null); @@ -29,6 +29,12 @@ public class EmailService { System.getenv("spring.mail.username"), System.getenv("spring.mail.password") ); + return store; + } + + + public void readEmails() throws MessagingException, IOException { + Store store = connect(); Folder inbox = store.getFolder("INBOX"); inbox.open(Folder.READ_ONLY); @@ -37,14 +43,23 @@ public class EmailService { Message[] messages = inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false)); for (Message message : messages) { - // Process each email message - System.out.println("Subject: " + message.getSubject()); - System.out.println("From: " + message.getFrom()[0]); - System.out.println("Body: " + message.getContent()); + //TODO: do something with the message } // Close resources inbox.close(false); store.close(); } + + public void expunge() throws MessagingException { + Store store = connect(); + Folder trash = store.getFolder("INBOX").getFolder("Trash"); + trash.open(Folder.READ_WRITE); + for (Message message : trash.getMessages()) { + message.setFlag(Flags.Flag.DELETED, true); + } + trash.close(true); // expunge + store.close(); + } + } \ No newline at end of file diff --git a/src/main/java/com/olexyn/burnsmail/web/Controller.java b/src/main/java/com/olexyn/burnsmail/web/Controller.java new file mode 100644 index 0000000..5a38456 --- /dev/null +++ b/src/main/java/com/olexyn/burnsmail/web/Controller.java @@ -0,0 +1,44 @@ +package com.olexyn.burnsmail.web; + + +import com.olexyn.burnsmail.mail.EmailService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class Controller { + + private final EmailService emailService; + + @Autowired + public Controller( + EmailService emailService + ){ + this.emailService = emailService; + } + + @PostMapping("/read") + public ResponseEntity postConfig() { + try { + emailService.readEmails(); + } catch (Exception e) { + return ResponseEntity.badRequest().build(); + } + return ResponseEntity.ok().build(); + } + + + @PostMapping("/expunge") + public ResponseEntity expunge() { + try { + emailService.expunge(); + } catch (Exception e) { + return ResponseEntity.badRequest().build(); + } + return ResponseEntity.ok().build(); + } + + +} diff --git a/src/test/resources/expunge.http b/src/test/resources/expunge.http new file mode 100644 index 0000000..c44fd9f --- /dev/null +++ b/src/test/resources/expunge.http @@ -0,0 +1,6 @@ +POST http://localhost:42003/expunge +Content-Type: application/json + +{ + +} diff --git a/src/test/resources/read-mail.http b/src/test/resources/read-mail.http new file mode 100644 index 0000000..f3fe1b5 --- /dev/null +++ b/src/test/resources/read-mail.http @@ -0,0 +1,6 @@ +POST http://localhost:42003/read +Content-Type: application/json + +{ + +}