Browse Source

working in list management

drop_old_mail
Stephan Richter 3 years ago
parent
commit
9f128eab39
  1. 3
      src/main/java/de/srsoftware/widerhall/Constants.java
  2. 1
      src/main/java/de/srsoftware/widerhall/data/Database.java
  3. 33
      src/main/java/de/srsoftware/widerhall/data/ListMember.java
  4. 93
      src/main/java/de/srsoftware/widerhall/data/MailingList.java
  5. 8
      src/main/java/de/srsoftware/widerhall/data/User.java
  6. 19
      src/main/java/de/srsoftware/widerhall/web/Rest.java
  7. 86
      src/main/java/de/srsoftware/widerhall/web/Web.java
  8. 16
      static/templates/add_list.st
  9. 31
      static/templates/js.st
  10. 18
      static/templates/listlist.st

3
src/main/java/de/srsoftware/widerhall/Constants.java

@ -2,6 +2,7 @@ package de.srsoftware.widerhall; @@ -2,6 +2,7 @@ package de.srsoftware.widerhall;
public class Constants {
public static final String ADMIN = "Admin";
public static final String BASE_URL = "base_url";
public static final String EMAIL = "email";
public static final String ERROR = "error";
public static final String HOST = "host";
@ -14,11 +15,11 @@ public class Constants { @@ -14,11 +15,11 @@ public class Constants {
public static final String PASSWORD = "password";
public static final Object PORT = "port";
public static final String PROTOCOL = "mail.store.protocol";
public static final String STATE = "state";
public static final String USER = "user";
public static final String VARCHAR = "VARCHAR(255)";
public static final String BASE_URL = "base_url";
public static final String DB = "database";
public static final String BASE = "base";
public static final String CONFIG = "configuration";

1
src/main/java/de/srsoftware/widerhall/data/Database.java

@ -143,6 +143,7 @@ public class Database { @@ -143,6 +143,7 @@ public class Database {
private Database assertTables() throws SQLException {
if (!tableExists(User.TABLE_NAME)) User.createTable();
if (!tableExists(MailingList.TABLE_NAME)) MailingList.createTable();
if (!tableExists(ListMember.TABLE_NAME)) ListMember.createTable();
return this;
}

33
src/main/java/de/srsoftware/widerhall/data/ListMember.java

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
package de.srsoftware.widerhall.data;
import java.sql.SQLException;
import static de.srsoftware.widerhall.Constants.INT;
import static de.srsoftware.widerhall.Constants.VARCHAR;
public class ListMember {
public static final String TABLE_NAME = "ListMembers";
private static final String LIST_EMAIL = "list_email";
private static final String USER_EMAIL = "user_email";
private static final String STATE = "state";
private final String listEmail;
private final String userEmail;
private final int state;
public ListMember(String listEmail, String userEmail, int state){
this.listEmail = listEmail;
this.userEmail = userEmail;
this.state = state;
}
public static void createTable() throws SQLException {
var sql = new StringBuilder()
.append("CREATE TABLE ").append(TABLE_NAME)
.append(" (")
.append(LIST_EMAIL).append(" ").append(VARCHAR).append(", ")
.append(USER_EMAIL).append(" ").append(VARCHAR).append(", ")
.append(STATE).append(" ").append(INT).append(", ")
.append("PRIMARY KEY (").append(LIST_EMAIL).append(", ").append(USER_EMAIL).append("));");
Database.open().query(sql.toString()).run();
}
}

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

@ -4,6 +4,8 @@ import org.slf4j.Logger; @@ -4,6 +4,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static de.srsoftware.widerhall.Constants.*;
@ -18,17 +20,29 @@ public class MailingList { @@ -18,17 +20,29 @@ public class MailingList {
private static final String SMTP_PORT = "smtp_port";
private static final String SMTP_USER = "smtp_user";
private static final String SMTP_PASS = "smtp_pass";
private static final int ENABLED = 1;
private final String name;
private final String email;
public static final String TABLE_NAME = "Lists";
private final String imapPass,smtpPass,imapHost,smtpHost,imapUser,smtpUser;
private final int imapPort,smtpPort,state;
public MailingList(String email, String name){
public MailingList(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass, int state){
this.email = email;
this.name = name;
this.imapHost = imapHost;
this.imapPort = imapPort;
this.imapUser = imapUser;
this.imapPass = imapPass;
this.smtpHost = smtpHost;
this.smtpPort = smtpPort;
this.smtpUser = smtpUser;
this.smtpPass = smtpPass;
this.state = state;
}
public static MailingList create(String email, String name) throws SQLException {
return new MailingList(email,name).save();
public static MailingList create(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass) throws SQLException {
return new MailingList(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass,ENABLED).save();
}
public static void createTable() throws SQLException {
@ -36,22 +50,75 @@ public class MailingList { @@ -36,22 +50,75 @@ public class MailingList {
.append("CREATE TABLE ").append(TABLE_NAME)
.append(" (")
.append(EMAIL).append(" ").append(VARCHAR).append(" NOT NULL PRIMARY KEY, ")
.append(NAME).append(" ").append(VARCHAR)
.append(IMAP_HOST).append(" ").append(VARCHAR)
.append(IMAP_PORT).append(" ").append(INT)
.append(IMAP_USER).append(" ").append(VARCHAR)
.append(IMAP_PASS).append(" ").append(VARCHAR)
.append(SMTP_HOST).append(" ").append(VARCHAR)
.append(SMTP_PORT).append(" ").append(INT)
.append(SMTP_USER).append(" ").append(VARCHAR)
.append(SMTP_PASS).append(" ").append(VARCHAR)
.append(NAME).append(" ").append(VARCHAR).append(", ")
.append(IMAP_HOST).append(" ").append(VARCHAR).append(", ")
.append(IMAP_PORT).append(" ").append(INT).append(", ")
.append(IMAP_USER).append(" ").append(VARCHAR).append(", ")
.append(IMAP_PASS).append(" ").append(VARCHAR).append(", ")
.append(SMTP_HOST).append(" ").append(VARCHAR).append(", ")
.append(SMTP_PORT).append(" ").append(INT).append(", ")
.append(SMTP_USER).append(" ").append(VARCHAR).append(", ")
.append(SMTP_PASS).append(" ").append(VARCHAR).append(", ")
.append(STATE).append(" ").append(INT)
.append(");");
Database.open().query(sql.toString()).run();
}
public static List<MailingList> listsOf(User user) {
var list = new ArrayList<MailingList>();
try {
var rs = Database.open().query("SELECT * FROM "+TABLE_NAME).exec();
while (rs.next()){
var email = rs.getString(EMAIL);
var name = rs.getString(NAME);
var imapHost = rs.getString(IMAP_HOST);
var imapPort = rs.getInt(IMAP_PORT);
var imapUser = rs.getString(IMAP_USER);
var imapPass = rs.getString(IMAP_PASS);
var smtpHost = rs.getString(SMTP_HOST);
var smtpPort = rs.getInt(SMTP_PORT);
var smtpUser = rs.getString(SMTP_USER);
var smtpPass = rs.getString(SMTP_PASS);
var state = rs.getInt(STATE);
list.add(new MailingList(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass,state));
}
} catch (SQLException e) {
LOG.warn("Listing mailing lists failed: ",e);
}
return list;
}
public Map<String,Object> safeMap() {
return Map.of(EMAIL,email,NAME,name,
IMAP_HOST,imapHost,IMAP_PORT,imapPort,IMAP_USER,imapUser,
SMTP_HOST,smtpHost,SMTP_PORT,smtpPort,SMTP_USER,smtpUser,
STATE,stateName(state));
}
private static String stateName(int state) {
switch (state){
case ENABLED:
return "enabled";
default:
return "disabled";
}
}
private MailingList save() throws SQLException {
Database.open().insertInto(TABLE_NAME)
.values(Map.of(EMAIL,email,NAME,name))
.values(Map.ofEntries(
Map.entry(EMAIL,email),
Map.entry(NAME,name),
Map.entry(IMAP_HOST,imapHost),
Map.entry(IMAP_PORT,imapPort),
Map.entry(IMAP_USER,imapUser),
Map.entry(IMAP_PASS,imapPass),
Map.entry(SMTP_HOST,smtpHost),
Map.entry(SMTP_PORT,smtpPort),
Map.entry(SMTP_USER,smtpUser),
Map.entry(SMTP_PASS,smtpPass),
Map.entry(STATE,state)))
.run();
return this;
}

8
src/main/java/de/srsoftware/widerhall/data/User.java

@ -100,9 +100,6 @@ public class User { @@ -100,9 +100,6 @@ public class User {
throw new InvalidKeyException();
}
public Map<String,String> map() {
return Map.of(EMAIL,email,NAME,name);
}
private boolean matching(String password) {
return hashedPass.equals(Util.sha256(password+salt));
@ -124,11 +121,14 @@ public class User { @@ -124,11 +121,14 @@ public class User {
return false;
}
public Map<String,String> safeMap(){
return Map.of(NAME,name,EMAIL,email,PASSWORD,hashedPassword() == null ? "no" : "yes");
}
private User save() throws SQLException {
Database.open().insertInto(TABLE_NAME)
.values(Map.of(EMAIL,email,NAME,name,SALT,salt,HASHED_PASS,hashedPass))
.run();
return this;
}
}

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

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package de.srsoftware.widerhall.web;
import de.srsoftware.widerhall.data.MailingList;
import de.srsoftware.widerhall.data.User;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
@ -10,15 +11,14 @@ import javax.servlet.http.HttpServlet; @@ -10,15 +11,14 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static de.srsoftware.widerhall.Constants.*;
import static de.srsoftware.widerhall.Util.t;
public class Rest extends HttpServlet {
private static final Logger LOG = LoggerFactory.getLogger(Rest.class);
private static final String LIST_LIST = "list/list";
private static final String USER_LIST = "user/list";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
@ -31,13 +31,18 @@ public class Rest extends HttpServlet { @@ -31,13 +31,18 @@ public class Rest extends HttpServlet {
JSONObject json = new JSONObject();
if (o instanceof User user){
var path = req.getPathInfo();
json.put(USER,safeMapUser(user));
json.put(USER,user.safeMap());
path = path == null ? INDEX : path.substring(1);
switch (path) {
case USER_LIST:
json.put("users", (user.is(ADMIN) ? User.list() : List.of(user)).stream().map(Rest::safeMapUser).toList());
json.put("users", (user.is(ADMIN) ? User.list() : List.of(user)).stream().map(User::safeMap).toList());
break;
case LIST_LIST:
json.put("lists", MailingList.listsOf(user).stream().map(MailingList::safeMap).toList());
break;
default:
json.put(ERROR,t("No handler for path '{}'!",path));
break;
}
} else {
json.put(ERROR,"Not logged in!");
@ -49,8 +54,4 @@ public class Rest extends HttpServlet { @@ -49,8 +54,4 @@ public class Rest extends HttpServlet {
return t("Failed to handle request: {}",e.getMessage());
}
}
private static Map<String,String> safeMapUser(User user){
return Map.of(NAME,user.name(),EMAIL,user.email(),PASSWORD,user.hashedPassword() == null ? "no" : "yes");
}
}

86
src/main/java/de/srsoftware/widerhall/web/Web.java

@ -2,6 +2,7 @@ package de.srsoftware.widerhall.web; @@ -2,6 +2,7 @@ package de.srsoftware.widerhall.web;
import de.srsoftware.widerhall.Configuration;
import de.srsoftware.widerhall.Util;
import de.srsoftware.widerhall.data.MailingList;
import de.srsoftware.widerhall.data.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -23,11 +24,19 @@ import static de.srsoftware.widerhall.Constants.*; @@ -23,11 +24,19 @@ import static de.srsoftware.widerhall.Constants.*;
import static de.srsoftware.widerhall.Util.t;
public class Web extends HttpServlet {
private static final String ADD_LIST = "add_list";
private static final Logger LOG = LoggerFactory.getLogger(Web.class);
private static final String LOGIN = "login";
private static final String LOGOUT = "logout";
private static final String REGISTER = "register";
private static final String IMAP_HOST = "imap_host";
private static final String IMAP_PORT = "imap_port";
private static final String IMAP_USER = "imap_user";
private static final String IMAP_PASS = "imap_pass";
private static final String SMTP_HOST = "smtp_host";
private static final String SMTP_PORT = "smtp_port";
private static final String SMTP_USER = "smtp_user";
private static final String SMTP_PASS = "smtp_pass";
private final String baseDir;
private STGroup templates;
private static final String WEB_ROOT = "/web";
@ -38,6 +47,77 @@ public class Web extends HttpServlet { @@ -38,6 +47,77 @@ public class Web extends HttpServlet {
loadTemplates();
}
private String addList(HttpServletRequest req, HttpServletResponse resp) {
var user = req.getSession().getAttribute(USER);
if (user == null) return redirectTo(LOGIN,resp);
var data = new HashMap<String,Object>();
data.put(USER,user);
var name = req.getParameter(NAME);
data.put(NAME,name);
var email = req.getParameter(EMAIL);
data.put(EMAIL,email);
var imapHost = req.getParameter(IMAP_HOST);
data.put(IMAP_HOST,imapHost);
var imapUser = req.getParameter(IMAP_USER);
data.put(IMAP_USER,imapUser);
var imapPass = req.getParameter(IMAP_PASS);
var smtpHost = req.getParameter(SMTP_HOST);
data.put(SMTP_HOST,smtpHost);
var smtpUser = req.getParameter(SMTP_USER);
data.put(SMTP_USER,smtpUser);
var smtpPass = req.getParameter(SMTP_PASS);
Integer imapPort = 993;
data.put(IMAP_PORT,imapPort);
Integer smtpPort = 465;
data.put(SMTP_PORT,smtpPort);
if (name == null || name.isBlank() || email == null || email.isBlank()) {
data.put(ERROR,"List name and address are required!");
return loadTemplate(ADD_LIST,data,resp);
}
if (imapHost == null || imapHost.isBlank() || imapUser == null || imapUser.isBlank() || imapPass == null || imapPass.isBlank()) {
data.put(ERROR,"IMAP credentials are required!");
return loadTemplate(ADD_LIST,data,resp);
}
try {
imapPort = Integer.parseInt(req.getParameter(IMAP_PORT));
data.put(IMAP_PORT,imapPort);
} catch (NumberFormatException nfe){
data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(IMAP_PORT)));
return loadTemplate(ADD_LIST,data,resp);
}
if (smtpHost == null || smtpHost.isBlank() || smtpUser == null || smtpUser.isBlank() || smtpPass == null || smtpPass.isBlank()) {
data.put(ERROR,"SMTP credentials are required!");
return loadTemplate(ADD_LIST,data,resp);
}
try {
smtpPort = Integer.parseInt(req.getParameter(SMTP_PORT));
data.put(SMTP_PORT,smtpPort);
} catch (NumberFormatException nfe){
data.put(ERROR,t("'{}' is not a proper port number!",req.getParameter(SMTP_PORT)));
return loadTemplate(ADD_LIST,data,resp);
}
try {
MailingList.create(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass);
return redirectTo(INDEX,resp);
} catch (SQLException e) {
return t("Failed to create list '{}': {}",name,e.getMessage());
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String error = handleGet(req, resp);
@ -80,7 +160,7 @@ public class Web extends HttpServlet { @@ -80,7 +160,7 @@ public class Web extends HttpServlet {
var o = req.getSession().getAttribute("user");
if (o instanceof User user){
var data = new HashMap<String,Object>();
data.put(USER,user.map());
data.put(USER,user.safeMap());
data.put(NOTES,notes);
return loadTemplate(path,data,resp);
}
@ -116,6 +196,8 @@ public class Web extends HttpServlet { @@ -116,6 +196,8 @@ public class Web extends HttpServlet {
return handleLogin(req,resp);
case REGISTER:
return registerUser(req,resp);
case ADD_LIST:
return addList(req,resp);
}
return t("No handler for path {}!",path);

16
static/templates/add_list.st

@ -27,38 +27,38 @@ @@ -27,38 +27,38 @@
<fieldset>
<legend>IMAP protocol</legend>
<label>
<input type="text" name="imap_host" value="«data.imap.host»" id="imap_host" />
<input type="text" name="imap_host" value="«data.imap_host»" id="imap_host" />
Hostname
</label>
<label>
<input type="number" name="imap_port" value="«data.imap.port»" id="imap_port" />
<input type="number" name="imap_port" value="«data.imap_port»" id="imap_port" />
Port
</label>
<label>
<input type="text" name="imap_user" value="«data.imap.user»" id="imap_user" />
<input type="text" name="imap_user" value="«data.imap_user»" id="imap_user" />
Username
</label>
<label>
<input type="password" name="imap_pass" value="«data.imap.pass»" id="imap_pass" />
<input type="password" name="imap_pass" value="«data.imap_pass»" id="imap_pass" />
Password
</label>
</fieldset>
<fieldset>
<legend>SMTP protocol</legend>
<label>
<input type="text" name="smtp_host" value="«data.smtp.host»" id="smtp_host" />
<input type="text" name="smtp_host" value="«data.smtp_host»" id="smtp_host" />
Hostname
</label>
<label>
<input type="number" name="smtp_port" value="«data.smtp.port»" id="smtp_port" />
<input type="number" name="smtp_port" value="«data.smtp_port»" id="smtp_port" />
Port
</label>
<label>
<input type="text" name="smtp_user" value="«data.smtp.user»" id="smtp_user" />
<input type="text" name="smtp_user" value="«data.smtp_user»" id="smtp_user" />
Username
</label>
<label>
<input type="password" name="smtp_pass" value="«data.smtp.pass»" id="smtp_pass" />
<input type="password" name="smtp_pass" value="«data.smtp_pass»" id="smtp_pass" />
Password
</label>
</fieldset>

31
static/templates/js.st

@ -1,7 +1,38 @@ @@ -1,7 +1,38 @@
function dropList(email){
console.log('dopList('+email+')');
}
function loadListList(){
$.getJSON("/api/list/list", showListList);
}
function loadUserList(){
$.getJSON("/api/user/list", showUserList);
}
function showListList(data){
for (let i in data.lists){
let list = data.lists[i];
let row = $('<tr/>');
let drop = $('<button/>').text('drop list').click(() => dropList(list.email))
$('<td/>').text(list.name).appendTo(row);
$('<td/>').text(list.email).appendTo(row);
$('<td/>').text(list.state).appendTo(row);
drop.appendTo($('<td/>')).appendTo(row);
$('<td/>').text(list.imap_host).appendTo(row);
$('<td/>').text(list.imap_port).appendTo(row);
$('<td/>').text(list.imap_user).appendTo(row);
$('<td/>').text(list.smtp_host).appendTo(row);
$('<td/>').text(list.smtp_port).appendTo(row);
$('<td/>').text(list.smtp_user).appendTo(row);
row.appendTo('#listlist');
}
if (data.user.name == 'Admin'){
$('a[href=register]').show();
} else {
$('a[href=register]').hide();
}
}
function showUserList(data){
for (let i in data.users){
let user = data.users[i];

18
static/templates/listlist.st

@ -2,9 +2,21 @@ @@ -2,9 +2,21 @@
<legend>List of mailinglists</legend>
<table id="listlist">
<tr>
<th>List name</th>
<th>List email</th>
<th>Details</th>
<th colspan="4">List</th>
<th colspan="3">IMAP</th>
<th colspan="3">SMTP</th>
</tr>
<tr>
<th>Name</th>
<th>Address</th>
<th>State</th>
<th>Actions</th>
<th>Host</th>
<th>Port</th>
<th>User</th>
<th>Host</th>
<th>Port</th>
<th>User</th>
</tr>
</table>
<a href="add_list">Add new mailing list</a>

Loading…
Cancel
Save