working on forwarding mail
This commit is contained in:
@@ -40,6 +40,9 @@ public class MailingList implements MessageHandler {
|
||||
private static final int STATE_PENDING = 0;
|
||||
private static final int STATE_ENABLED = 1;
|
||||
private static final int STATE_PUBLIC = 2;
|
||||
public static final int STATE_FORWARD_FROM = 4;
|
||||
private static final int VISIBLE = 1;
|
||||
private static final int HIDDEN = 0;
|
||||
private final String name;
|
||||
private final String email;
|
||||
private int state;
|
||||
@@ -143,7 +146,8 @@ public class MailingList implements MessageHandler {
|
||||
private void forward(Message message) throws MessagingException {
|
||||
try {
|
||||
var emails = members().stream().map(User::email).toList();
|
||||
smtp.bccForward(email(),message,emails);
|
||||
String sender = (state & STATE_FORWARD_FROM) > 0 ? message.getFrom()[0].toString() : email();
|
||||
smtp.bccForward(sender,message,emails);
|
||||
} catch (SQLException e) {
|
||||
LOG.error("Failed to read list members of {} from database. Cannot forward message!",email(),e);
|
||||
}
|
||||
@@ -175,6 +179,10 @@ public class MailingList implements MessageHandler {
|
||||
return ml;
|
||||
}
|
||||
|
||||
public boolean hasState(int test){
|
||||
return (state & test) > 0;
|
||||
}
|
||||
|
||||
public void hide(boolean hide) throws SQLException {
|
||||
state = hide ? state ^ (state & STATE_PUBLIC) : state | STATE_PUBLIC;
|
||||
Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run();
|
||||
@@ -231,7 +239,7 @@ public class MailingList implements MessageHandler {
|
||||
String[] parts = email.split("@", 2);
|
||||
map.put(EMAIL,Map.of(PREFIX,parts[0],DOMAIN,parts[1]));
|
||||
map.put(NAME, name);
|
||||
map.put(STATE, stateString(state));
|
||||
map.put(STATE, stateMap());
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -280,7 +288,7 @@ public class MailingList implements MessageHandler {
|
||||
if (smtp.host() != null) map.put(SMTP_HOST, smtp.host());
|
||||
if (smtp.port() != 0) map.put(SMTP_PORT, smtp.port());
|
||||
if (smtp.username() != null) map.put(SMTP_USER, smtp.username());
|
||||
map.put(STATE,stateString(state));
|
||||
map.put(STATE,stateMap());
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -325,16 +333,12 @@ public class MailingList implements MessageHandler {
|
||||
smtp.login().send(email(),name(),user.email(),subject,text);
|
||||
}
|
||||
|
||||
/**
|
||||
* convert state to readable string
|
||||
* @param state
|
||||
* @return
|
||||
*/
|
||||
private static String stateString(int state) {
|
||||
var states = new ArrayList<String>();
|
||||
states.add((state & STATE_ENABLED) == STATE_ENABLED ? "enabled" : "disabled");
|
||||
states.add((state & STATE_PUBLIC) == STATE_PUBLIC ? "public" : "hidden");
|
||||
return String.join(", ", states);
|
||||
public Map<String,Integer> stateMap(){
|
||||
var map = new HashMap<String,Integer>();
|
||||
if (hasState(STATE_ENABLED)) map.put("enabled",VISIBLE);
|
||||
if (hasState(STATE_PUBLIC)) map.put("public",VISIBLE);
|
||||
if (hasState(STATE_FORWARD_FROM)) map.put("original_from",HIDDEN);
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,6 +19,7 @@ public class SmtpClient {
|
||||
private static final String UTF8 = "UTF-8";
|
||||
private final String host,password,username;
|
||||
private final int port;
|
||||
private boolean forwardUsingListAddress = true;
|
||||
|
||||
private Session session;
|
||||
|
||||
@@ -32,18 +33,18 @@ public class SmtpClient {
|
||||
public void bccForward(String from, Message message, List<String> emails) throws MessagingException {
|
||||
if (session == null) login();
|
||||
MimeMessage forward = new MimeMessage(session);
|
||||
var addresses = InternetAddress.parse(String.join(", ",emails));
|
||||
var senders = message.getFrom();
|
||||
forward.setFrom(senders[0]);
|
||||
//forward.setFrom(InternetAddress.parse(from)[0]);
|
||||
forward.setRecipients(Message.RecipientType.BCC,addresses);
|
||||
forward.setFrom(from);
|
||||
forward.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(String.join(", ",emails)));
|
||||
forward.setSubject(message.getSubject());
|
||||
MimeBodyPart body = new MimeBodyPart();
|
||||
body.setContent(message,"message/rfc822");
|
||||
Multipart multipart = new MimeMultipart();
|
||||
multipart.addBodyPart(body);
|
||||
|
||||
MimeMultipart multipart = new MimeMultipart();
|
||||
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
||||
|
||||
messageBodyPart.setDataHandler(message.getDataHandler());
|
||||
multipart.addBodyPart(messageBodyPart);
|
||||
|
||||
forward.setContent(multipart);
|
||||
forward.saveChanges();
|
||||
|
||||
send(forward);
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -27,6 +28,7 @@ public class Rest extends HttpServlet {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Rest.class);
|
||||
private static final String LIST_DISABLE = "list/disable";
|
||||
private static final String LIST_EDITABLE = "list/editable";
|
||||
private static final String LIST_DETAIL = "list/detail";
|
||||
private static final String LIST_ENABLE = "list/enable";
|
||||
private static final String LIST_HIDE = "list/hide";
|
||||
private static final String LIST_MEMBERS = "list/members";
|
||||
@@ -104,6 +106,7 @@ public class Rest extends HttpServlet {
|
||||
break;
|
||||
case LIST_SUBSCRIBABLE:
|
||||
json.put("lists", MailingList.subscribable(user).stream().map(MailingList::minimalMap).toList());
|
||||
break;
|
||||
default:
|
||||
json.put(ERROR,t("No handler for path '{}'!",path));
|
||||
break;
|
||||
@@ -139,6 +142,9 @@ public class Rest extends HttpServlet {
|
||||
var userEmail = req.getParameter(EMAIL);
|
||||
var permissions = req.getParameter(PERMISSIONS);
|
||||
switch (path) {
|
||||
case LIST_DETAIL:
|
||||
json.putAll(listDetail(listEmail,user));
|
||||
break;
|
||||
case LIST_DISABLE:
|
||||
json.putAll(enableList(listEmail,user,false));
|
||||
break;
|
||||
@@ -183,6 +189,14 @@ public class Rest extends HttpServlet {
|
||||
}
|
||||
}
|
||||
|
||||
private Map listDetail(String listEmail, User user) {
|
||||
var ml = MailingList.load(listEmail);
|
||||
if (ml == null) return Map.of(ERROR,t("Mailinglist {} unknown",listEmail));
|
||||
var map = new HashMap<>();
|
||||
if (ml.hasState(MailingList.STATE_FORWARD_FROM)) map.put("forward_from",true);
|
||||
return map;
|
||||
}
|
||||
|
||||
private Map<String, Object> listMembers(String listEmail, User user) {
|
||||
if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!");
|
||||
if (user.hashPermission(User.PERMISSION_ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)) {
|
||||
|
||||
Reference in New Issue
Block a user