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

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

@ -40,6 +40,9 @@ public class MailingList implements MessageHandler { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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;
}
/**

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

@ -19,6 +19,7 @@ public class SmtpClient { @@ -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 { @@ -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);
}

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

@ -16,6 +16,7 @@ import java.io.IOException; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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)) {

2
static/templates/add_list.st

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

2
static/templates/frontpage.st

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

2
static/templates/index.st

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

15
static/templates/inspect.st

@ -6,11 +6,24 @@ @@ -6,11 +6,24 @@
<script src="js"></script>
<link rel="stylesheet" href="css" />
</head>
<body id="login">
<body>
«navigation()»
«userinfo()»
«messages()»
<h1>Widerhall '«data.list»' Details</h1>
«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>
</html>

20
static/templates/js.st

@ -27,6 +27,10 @@ function hideList(listEmail){ @@ -27,6 +27,10 @@ function hideList(listEmail){
$.post('/api/list/hide',{list:listEmail},showListResult,'json');
}
function loadListDetail(listEmail){
$.post('/api/list/detail',{list:listEmail},showListDetail,'json');
}
function loadListOfEditableLists(){
$.getJSON('/api/list/editable', showListOfEditableLists);
}
@ -51,6 +55,10 @@ function showList(listEmail){ @@ -51,6 +55,10 @@ function showList(listEmail){
$.post('/api/list/show',{list:listEmail},showListResult,'json');
}
function showListDetail(data){
console.log(data);
}
function showListOfEditableLists(data){
for (let i in data.lists){
let list = data.lists[i];
@ -63,7 +71,11 @@ function showListOfEditableLists(data){ @@ -63,7 +71,11 @@ function showListOfEditableLists(data){
td = $('<td/>');
$('<a/>',{href:inspect}).text(addr).appendTo(td)
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 action = $(this).children("option:selected").val();
@ -99,7 +111,11 @@ function showListList(data){ @@ -99,7 +111,11 @@ function showListList(data){
$('<td/>',{class:'right'}).text(list.email.prefix).appendTo(row);
$('<td/>',{class:'right'}).text('@').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'});
$('<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);

2
static/templates/login.st

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

2
static/templates/register.st

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

2
static/templates/subscribe.st

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

2
static/templates/unsubscribe.st

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

Loading…
Cancel
Save