Skip to content

Commit

Permalink
Code Refactor, Clean-up; Handle multiple domains
Browse files Browse the repository at this point in the history
This is heavy commit as in it chages lot
of stuff in the code. Changes are:
- Code refactor
- Code clean-up and used Lombok for
removing boilerplate getter/setter code.
- Support for config based max
emailIds per user
- version change from 1.0.0 -> 1.1.0
- Config restructuring.
- And support for multiple domains is
added. Now this bot can interact with
multiple domains and can offer temp-mails
from various domains. Config driven.
- Update README to reflect new config.

Fix #20
  • Loading branch information
rosehgal committed Jun 1, 2020
1 parent 396fea4 commit fcb1903
Show file tree
Hide file tree
Showing 21 changed files with 251 additions and 534 deletions.
29 changes: 17 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,27 @@ Dev config may look like this:
# Email Server IMAP and SMTP configuration
# SMTP server should support Alias creation and deletion
# IMAP server should support IDLE
emailServer:
host: trashemail.in
admin:
email: contact@trashemail.in
password: changeme
api:
aliases:
add_url: https://trashemail.in/admin/mail/aliases/add
remove_url: https://trashemail.in/admin/mail/aliases/remove

email-server:
hosts:
- trashemail.in
- thromail.com
- humblemail.com
admin-email: contact@trashemail.in
admin-password: changeme
add-url: https://trashemail.in/admin/mail/aliases/add
remove-url: https://trashemail.in/admin/mail/aliases/remove
imap:
host: trashemail.in
port: 993
email: contact@trashemail.in
password: changeme


# Telegram bot specific settings
telegram:
url: https://api.telegram.org/bot
botToken: telegram-bot-token
botToken: xxxxxxxxxxxxxxxxxxxxxx
size: 4096

# For development purpose, H2 is used.
# I prefer H2 persistent in file.
Expand All @@ -90,20 +91,24 @@ spring:
path: /h2-console
settings:
web-allow-others: true
enabled: true
application:
name: Trashemail

# Tomcat server settings
server:
port: 9090

# Trashemail app server settings
trashemail:
max-emails-per-user: 4

# Logger settings
logging:
level:
io:
github:
trashemail: debug

```
1. This code will spin up the service at `localhost:9090/telegram/new-message` endpoint.
Expand Down
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
</parent>
<groupId>io.github</groupId>
<artifactId>trashemail</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<name>Trashemail</name>
<description>Sprinboot miscroservice heart of Telegram @trashemail_bot</description>
<description>Springboot miscroservice - heart of Telegram @trashemail_bot</description>
<distributionManagement>
<repository>
<id>github</id>
Expand Down Expand Up @@ -82,6 +82,11 @@
<artifactId>jsoup</artifactId>
<version>1.13.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.trashemail.Configurations;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
@ConfigurationProperties(prefix = "email-server")
@Getter @Setter @NoArgsConstructor
public class EmailServerConfig {

private List<String> hosts;
private String adminEmail;
private String adminPassword;
private String addUrl;
private String removeUrl;
private Imap imap;


@Configuration
@ConfigurationProperties(prefix = "imap")
@Getter @Setter @NoArgsConstructor
public static class Imap{
private String host;
private String port;
private String email;
private String password;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
package io.github.trashemail.Configurations;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix="telegram")
@Getter
@Setter
@NoArgsConstructor
public class TelegramConfg{
private String botToken;
private String url;
private int size;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getBotToken() {
return botToken;
}

public void setBotToken(String botToken) {
this.botToken = botToken;
}

public int getSize() {
return size;
}

public void setSize(int size) {
this.size = size;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.trashemail.Configurations;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Getter
@Setter
@NoArgsConstructor
@Configuration
@ConfigurationProperties(prefix = "trashemail")
public class TrashemailConfig {
private Integer maxEmailsPerUser;

@Override
public String toString() {
return Integer.toString(maxEmailsPerUser);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.github.trashemail.EmailInteraction;

import io.github.trashemail.Telegram.TelegramRequestHandler;
import io.github.trashemail.Configurations.EmailServerConfig;
import io.github.trashemail.utils.exceptions.EmailAliasNotCreatedExecption;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -12,15 +12,14 @@
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import io.github.trashemail.Configurations.EmailServerConfiguration;
import io.github.trashemail.models.User;


@Component("EmailServerInteraction")
public class EmailServerInteraction {

@Autowired
private EmailServerConfiguration emailServerConfig;
private EmailServerConfig emailServerConfig;

@Autowired
RestTemplate restTemplate;
Expand All @@ -31,20 +30,20 @@ public String createEmailId(User user) throws HttpClientErrorException, EmailAli

HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(
emailServerConfig.getAdminemail(),
emailServerConfig.getAdminEmail(),
emailServerConfig.getAdminPassword()
);
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

MultiValueMap<String, String> data = new LinkedMultiValueMap<String, String>();

data.add("address", user.getEmailId());
data.add("forwards_to", emailServerConfig.getEmailServerImapTaregtUsername());
data.add("forwards_to", emailServerConfig.getImap().getEmail());

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(data, headers);

ResponseEntity response = restTemplate.postForEntity(
emailServerConfig.getEmailServerApiAddAliasesUrl(),
emailServerConfig.getAddUrl(),
request,
String.class);

Expand All @@ -60,7 +59,7 @@ public String createEmailId(User user) throws HttpClientErrorException, EmailAli
public boolean deleteEmailId(User user) {
HttpHeaders headers = new HttpHeaders();
headers.setBasicAuth(
emailServerConfig.getAdminemail(),
emailServerConfig.getAdminEmail(),
emailServerConfig.getAdminPassword()
);
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
Expand All @@ -73,7 +72,7 @@ public boolean deleteEmailId(User user) {

RestTemplate restTemplate = new RestTemplate();
ResponseEntity response = restTemplate.postForEntity(
emailServerConfig.getEmailServerApiRemoveAliasUrl(),
emailServerConfig.getRemoveUrl(),
request,
String.class);

Expand Down
Loading

0 comments on commit fcb1903

Please sign in to comment.