Browse Source

working on forwarding mail

drop_old_mail
Stephan Richter 3 years ago
parent
commit
b38b40a677
  1. 2
      pom.xml
  2. 30
      src/main/java/de/srsoftware/widerhall/data/MailingList.java
  3. 21
      src/main/java/de/srsoftware/widerhall/mail/SmtpClient.java
  4. 14
      src/main/java/de/srsoftware/widerhall/web/Rest.java
  5. 2
      static/templates/add_list.st
  6. 2
      static/templates/frontpage.st
  7. 2
      static/templates/index.st
  8. 15
      static/templates/inspect.st
  9. 20
      static/templates/js.st
  10. 2
      static/templates/login.st
  11. 2
      static/templates/register.st
  12. 2
      static/templates/subscribe.st
  13. 2
      static/templates/unsubscribe.st

2
pom.xml

@ -6,7 +6,7 @@
<groupId>org.example</groupId> <groupId>org.example</groupId>
<artifactId>Widerhall</artifactId> <artifactId>Widerhall</artifactId>
<version>0.0.15</version> <version>0.0.16</version>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>

30
src/main/java/de/srsoftware/widerhall/data/MailingList.java

@ -40,6 +40,9 @@ public class MailingList implements MessageHandler {
private static final int STATE_PENDING = 0; private static final int STATE_PENDING = 0;
private static final int STATE_ENABLED = 1; private static final int STATE_ENABLED = 1;
private static final int STATE_PUBLIC = 2; 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 name;
private final String email; private final String email;
private int state; private int state;
@ -143,7 +146,8 @@ public class MailingList implements MessageHandler {
private void forward(Message message) throws MessagingException { private void forward(Message message) throws MessagingException {
try { try {
var emails = members().stream().map(User::email).toList(); 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) { } catch (SQLException e) {
LOG.error("Failed to read list members of {} from database. Cannot forward message!",email(),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; return ml;
} }
public boolean hasState(int test){
return (state & test) > 0;
}
public void hide(boolean hide) throws SQLException { public void hide(boolean hide) throws SQLException {
state = hide ? state ^ (state & STATE_PUBLIC) : state | STATE_PUBLIC; state = hide ? state ^ (state & STATE_PUBLIC) : state | STATE_PUBLIC;
Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run(); 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); String[] parts = email.split("@", 2);
map.put(EMAIL,Map.of(PREFIX,parts[0],DOMAIN,parts[1])); map.put(EMAIL,Map.of(PREFIX,parts[0],DOMAIN,parts[1]));
map.put(NAME, name); map.put(NAME, name);
map.put(STATE, stateString(state)); map.put(STATE, stateMap());
return map; return map;
} }
@ -280,7 +288,7 @@ public class MailingList implements MessageHandler {
if (smtp.host() != null) map.put(SMTP_HOST, smtp.host()); if (smtp.host() != null) map.put(SMTP_HOST, smtp.host());
if (smtp.port() != 0) map.put(SMTP_PORT, smtp.port()); if (smtp.port() != 0) map.put(SMTP_PORT, smtp.port());
if (smtp.username() != null) map.put(SMTP_USER, smtp.username()); if (smtp.username() != null) map.put(SMTP_USER, smtp.username());
map.put(STATE,stateString(state)); map.put(STATE,stateMap());
return map; return map;
} }
@ -325,16 +333,12 @@ public class MailingList implements MessageHandler {
smtp.login().send(email(),name(),user.email(),subject,text); smtp.login().send(email(),name(),user.email(),subject,text);
} }
/** public Map<String,Integer> stateMap(){
* convert state to readable string var map = new HashMap<String,Integer>();
* @param state if (hasState(STATE_ENABLED)) map.put("enabled",VISIBLE);
* @return if (hasState(STATE_PUBLIC)) map.put("public",VISIBLE);
*/ if (hasState(STATE_FORWARD_FROM)) map.put("original_from",HIDDEN);
private static String stateString(int state) { return map;
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);
} }
/** /**

21
src/main/java/de/srsoftware/widerhall/mail/SmtpClient.java

@ -19,6 +19,7 @@ public class SmtpClient {
private static final String UTF8 = "UTF-8"; private static final String UTF8 = "UTF-8";
private final String host,password,username; private final String host,password,username;
private final int port; private final int port;
private boolean forwardUsingListAddress = true;
private Session session; private Session session;
@ -32,18 +33,18 @@ public class SmtpClient {
public void bccForward(String from, Message message, List<String> emails) throws MessagingException { public void bccForward(String from, Message message, List<String> emails) throws MessagingException {
if (session == null) login(); if (session == null) login();
MimeMessage forward = new MimeMessage(session); MimeMessage forward = new MimeMessage(session);
var addresses = InternetAddress.parse(String.join(", ",emails)); forward.setFrom(from);
var senders = message.getFrom(); forward.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(String.join(", ",emails)));
forward.setFrom(senders[0]);
//forward.setFrom(InternetAddress.parse(from)[0]);
forward.setRecipients(Message.RecipientType.BCC,addresses);
forward.setSubject(message.getSubject()); forward.setSubject(message.getSubject());
MimeBodyPart body = new MimeBodyPart();
body.setContent(message,"message/rfc822"); MimeMultipart multipart = new MimeMultipart();
Multipart multipart = new MimeMultipart(); MimeBodyPart messageBodyPart = new MimeBodyPart();
multipart.addBodyPart(body);
messageBodyPart.setDataHandler(message.getDataHandler());
multipart.addBodyPart(messageBodyPart);
forward.setContent(multipart); forward.setContent(multipart);
forward.saveChanges();
send(forward); send(forward);
} }

14
src/main/java/de/srsoftware/widerhall/web/Rest.java

@ -16,6 +16,7 @@ import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; 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 Logger LOG = LoggerFactory.getLogger(Rest.class);
private static final String LIST_DISABLE = "list/disable"; private static final String LIST_DISABLE = "list/disable";
private static final String LIST_EDITABLE = "list/editable"; 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_ENABLE = "list/enable";
private static final String LIST_HIDE = "list/hide"; private static final String LIST_HIDE = "list/hide";
private static final String LIST_MEMBERS = "list/members"; private static final String LIST_MEMBERS = "list/members";
@ -104,6 +106,7 @@ public class Rest extends HttpServlet {
break; break;
case LIST_SUBSCRIBABLE: case LIST_SUBSCRIBABLE:
json.put("lists", MailingList.subscribable(user).stream().map(MailingList::minimalMap).toList()); json.put("lists", MailingList.subscribable(user).stream().map(MailingList::minimalMap).toList());
break;
default: default:
json.put(ERROR,t("No handler for path '{}'!",path)); json.put(ERROR,t("No handler for path '{}'!",path));
break; break;
@ -139,6 +142,9 @@ public class Rest extends HttpServlet {
var userEmail = req.getParameter(EMAIL); var userEmail = req.getParameter(EMAIL);
var permissions = req.getParameter(PERMISSIONS); var permissions = req.getParameter(PERMISSIONS);
switch (path) { switch (path) {
case LIST_DETAIL:
json.putAll(listDetail(listEmail,user));
break;
case LIST_DISABLE: case LIST_DISABLE:
json.putAll(enableList(listEmail,user,false)); json.putAll(enableList(listEmail,user,false));
break; 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) { private Map<String, Object> listMembers(String listEmail, User user) {
if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!"); if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!");
if (user.hashPermission(User.PERMISSION_ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)) { if (user.hashPermission(User.PERMISSION_ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)) {

2
static/templates/add_list.st

@ -6,7 +6,7 @@
<script src="js"></script> <script src="js"></script>
<link rel="stylesheet" href="css" /> <link rel="stylesheet" href="css" />
</head> </head>
<body id="login"> <body>
«navigation()» «navigation()»
«userinfo()» «userinfo()»
<h1>Widerhall List Creation</h1> <h1>Widerhall List Creation</h1>

2
static/templates/frontpage.st

@ -4,7 +4,7 @@
<meta charset="utf-8" /> <meta charset="utf-8" />
<link rel="stylesheet" href="css" /> <link rel="stylesheet" href="css" />
</head> </head>
<body id="login"> <body>
<h1>Widerhall front page</h1> <h1>Widerhall front page</h1>
If you are looking for you mailing lists, <a href="web/index">Go to the /web page</a>... If you are looking for you mailing lists, <a href="web/index">Go to the /web page</a>...
<fieldset> <fieldset>

2
static/templates/index.st

@ -6,7 +6,7 @@
<script src="js"></script> <script src="js"></script>
<link rel="stylesheet" href="css" /> <link rel="stylesheet" href="css" />
</head> </head>
<body id="login"> <body>
«navigation()» «navigation()»
«userinfo()» «userinfo()»
«messages()» «messages()»

15
static/templates/inspect.st

@ -6,11 +6,24 @@
<script src="js"></script> <script src="js"></script>
<link rel="stylesheet" href="css" /> <link rel="stylesheet" href="css" />
</head> </head>
<body id="login"> <body>
«navigation()» «navigation()»
«userinfo()» «userinfo()»
«messages()» «messages()»
<h1>Widerhall '«data.list»' Details</h1> <h1>Widerhall '«data.list»' Details</h1>
«listmembers()» «listmembers()»
<form method="POST">
<fieldset>
<legend>Options for «data.list»</legend>
<label>
<input type="checkbox" name="forward_from">
Forward using original sender
</label>
<button type="submit">Save</button>
</fieldset>
</form>
<script type="text/javascript">
loadListDetail('«data.list»');
</script>
</body> </body>
</html> </html>

20
static/templates/js.st

@ -27,6 +27,10 @@ function hideList(listEmail){
$.post('/api/list/hide',{list:listEmail},showListResult,'json'); $.post('/api/list/hide',{list:listEmail},showListResult,'json');
} }
function loadListDetail(listEmail){
$.post('/api/list/detail',{list:listEmail},showListDetail,'json');
}
function loadListOfEditableLists(){ function loadListOfEditableLists(){
$.getJSON('/api/list/editable', showListOfEditableLists); $.getJSON('/api/list/editable', showListOfEditableLists);
} }
@ -51,6 +55,10 @@ function showList(listEmail){
$.post('/api/list/show',{list:listEmail},showListResult,'json'); $.post('/api/list/show',{list:listEmail},showListResult,'json');
} }
function showListDetail(data){
console.log(data);
}
function showListOfEditableLists(data){ function showListOfEditableLists(data){
for (let i in data.lists){ for (let i in data.lists){
let list = data.lists[i]; let list = data.lists[i];
@ -63,7 +71,11 @@ function showListOfEditableLists(data){
td = $('<td/>'); td = $('<td/>');
$('<a/>',{href:inspect}).text(addr).appendTo(td) $('<a/>',{href:inspect}).text(addr).appendTo(td)
td.appendTo(row); td.appendTo(row);
$('<td/>').text(list.state).appendTo(row); let states = [];
for (let state in list.state){
if (list.state[state] > 0) states.push(state);
}
$('<td/>').text(states.toString()).appendTo(row);
let select = $('<select/>',{name:addr}).change(function () { let select = $('<select/>',{name:addr}).change(function () {
let action = $(this).children("option:selected").val(); let action = $(this).children("option:selected").val();
@ -99,7 +111,11 @@ function showListList(data){
$('<td/>',{class:'right'}).text(list.email.prefix).appendTo(row); $('<td/>',{class:'right'}).text(list.email.prefix).appendTo(row);
$('<td/>',{class:'right'}).text('@').appendTo(row); $('<td/>',{class:'right'}).text('@').appendTo(row);
$('<td/>').text(list.email.domain).appendTo(row); $('<td/>').text(list.email.domain).appendTo(row);
$('<td/>').text(list.state).appendTo(row); let states = [];
for (let state in list.state){
if (list.state[state] > 0) states.push(state);
}
$('<td/>').text(states.toString()).appendTo(row);
let td = $('<td/>',{class:'actions'}); let td = $('<td/>',{class:'actions'});
$('<button/>',{onclick:"subscribeTo('"+list.email.domain+"', '"+list.email.prefix+"');"}).text('subscribe').appendTo(td); $('<button/>',{onclick:"subscribeTo('"+list.email.domain+"', '"+list.email.prefix+"');"}).text('subscribe').appendTo(td);
$('<button/>',{onclick:"unsubscribeFrom('"+list.email.domain+"', '"+list.email.prefix+"');"}).text('unsubcribe').appendTo(td); $('<button/>',{onclick:"unsubscribeFrom('"+list.email.domain+"', '"+list.email.prefix+"');"}).text('unsubcribe').appendTo(td);

2
static/templates/login.st

@ -6,7 +6,7 @@
<script src="js"></script> <script src="js"></script>
<link rel="stylesheet" href="css" /> <link rel="stylesheet" href="css" />
</head> </head>
<body id="login"> <body>
«navigation()» «navigation()»
«messages()» «messages()»
<h1>Widerhall login</h1> <h1>Widerhall login</h1>

2
static/templates/register.st

@ -6,7 +6,7 @@
<script src="js"></script> <script src="js"></script>
<link rel="stylesheet" href="css" /> <link rel="stylesheet" href="css" />
</head> </head>
<body id="login"> <body>
«navigation()» «navigation()»
«userinfo()» «userinfo()»
<h1>Widerhall user registration</h1> <h1>Widerhall user registration</h1>

2
static/templates/subscribe.st

@ -6,7 +6,7 @@
<script src="js"></script> <script src="js"></script>
<link rel="stylesheet" href="css" /> <link rel="stylesheet" href="css" />
</head> </head>
<body id="login"> <body>
«navigation()» «navigation()»
«userinfo()» «userinfo()»
«messages()» «messages()»

2
static/templates/unsubscribe.st

@ -6,7 +6,7 @@
<script src="js"></script> <script src="js"></script>
<link rel="stylesheet" href="css" /> <link rel="stylesheet" href="css" />
</head> </head>
<body id="login"> <body>
«navigation()» «navigation()»
«userinfo()» «userinfo()»
«messages()» «messages()»

Loading…
Cancel
Save