diff --git a/config/logback.xml b/config/logback.xml new file mode 100644 index 0000000..49a6adb --- /dev/null +++ b/config/logback.xml @@ -0,0 +1,16 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5}: %msg%n + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index e9a82b6..0aad4b6 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,13 @@ 1.3.0-alpha13 + + ch.qos.logback + logback-core + 1.3.0-alpha13 + + + com.googlecode.json-simple json-simple diff --git a/src/main/java/de/srsoftware/widerhall/Application.java b/src/main/java/de/srsoftware/widerhall/Application.java index cbd6686..2b00e35 100644 --- a/src/main/java/de/srsoftware/widerhall/Application.java +++ b/src/main/java/de/srsoftware/widerhall/Application.java @@ -14,6 +14,8 @@ import org.json.simple.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.nio.file.Path; + public class Application { private static final Logger LOG = LoggerFactory.getLogger(Application.class); diff --git a/src/main/java/de/srsoftware/widerhall/Constants.java b/src/main/java/de/srsoftware/widerhall/Constants.java index 8db0b68..190cd86 100644 --- a/src/main/java/de/srsoftware/widerhall/Constants.java +++ b/src/main/java/de/srsoftware/widerhall/Constants.java @@ -2,7 +2,9 @@ package de.srsoftware.widerhall; public class Constants { public static final String ADMIN = "Admin"; + public static final String BASE = "base"; public static final String BASE_URL = "base_url"; + public static final String DB = "database"; public static final String EMAIL = "email"; public static final String ERROR = "error"; public static final String HOST = "host"; @@ -10,6 +12,7 @@ public class Constants { public static final String INBOX = "inbox"; public static final String INDEX = "index"; public static final String INT = "INT"; + public static final String LIST = "list"; public static final String NAME = "name"; public static final String NOTES = "notes"; public static final String PASSWORD = "password"; @@ -20,8 +23,6 @@ public class Constants { public static final String VARCHAR = "VARCHAR(255)"; - public static final String DB = "database"; - public static final String BASE = "base"; public static final String CONFIG = "configuration"; public static final String LOCATIONS = "locations"; } diff --git a/src/main/java/de/srsoftware/widerhall/data/Database.java b/src/main/java/de/srsoftware/widerhall/data/Database.java index 98b8381..4ef857b 100644 --- a/src/main/java/de/srsoftware/widerhall/data/Database.java +++ b/src/main/java/de/srsoftware/widerhall/data/Database.java @@ -80,6 +80,20 @@ public class Database { var marks = String.join(", ",arr); sb.append("(").append(marks).append(")"); } + if (!where.isEmpty()){ + var clauses = new ArrayList(); + sb.append(" WHERE "); + + for (var entry : where.entrySet()){ + var arr = new String[entry.getValue().size()]; + Arrays.fill(arr,"?"); + var marks = String.join(", ",arr); + clauses.add("("+entry.getKey()+" IN ("+marks+"))"); + args.addAll(entry.getValue()); + } + sb.append(String.join(" AND ",clauses)); + + } var sql = sb.toString(); LOG.debug(sql); try { diff --git a/src/main/java/de/srsoftware/widerhall/data/ListMember.java b/src/main/java/de/srsoftware/widerhall/data/ListMember.java index 840b6a7..0b225e1 100644 --- a/src/main/java/de/srsoftware/widerhall/data/ListMember.java +++ b/src/main/java/de/srsoftware/widerhall/data/ListMember.java @@ -43,7 +43,7 @@ public class ListMember { Database.open().query(sql.toString()).run(); } - public static List listsOf(User user) { + public static List listsOwnedBy(User user) { var list = new ArrayList(); try { var rs = Database.open() diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index 3d7cc14..1582701 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -25,10 +25,10 @@ public class MailingList { 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; + private final String imapPass, smtpPass, imapHost, smtpHost, imapUser, smtpUser; + private final int imapPort, smtpPort, state; - public MailingList(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass, int state){ + 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; @@ -43,7 +43,7 @@ public class MailingList { } 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(); + return new MailingList(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass, ENABLED).save(); } public static void createTable() throws SQLException { @@ -65,18 +65,22 @@ public class MailingList { Database.open().query(sql.toString()).run(); } + public static void hide(String listEmail, boolean hide) throws SQLException { + // https://stackoverflow.com/questions/16440831/bitwise-xor-in-sqlite-bitwise-not-not-working-as-i-expect + String expression = hide ? "state = (~(state & "+PUBLIC+"))&(state|"+PUBLIC+")" : ("state = state | "+PUBLIC); + Database.open().query("UPDATE " + TABLE_NAME + " SET "+expression).where(EMAIL, listEmail).run(); + } + public static List listsOf(User user) { - List keys = (user.is(ADMIN)) ? null : ListMember.listsOf(user); + List keys = (user.is(ADMIN)) ? null : ListMember.listsOwnedBy(user); var list = new ArrayList(); + if (keys != null && keys.isEmpty()) return list; try { - Database.Request q = Database.open().query("SELECT * FROM " + TABLE_NAME); - if (keys != null){ - if (keys.isEmpty()) return list; - q.where(EMAIL,keys); - } - var rs = q.exec(); - while (rs.next()){ + Database.Request query = Database.open().query("SELECT * FROM " + TABLE_NAME); + if (keys != null) query.where(EMAIL, keys); + var rs = query.exec(); + while (rs.next()) { var email = rs.getString(EMAIL); var name = rs.getString(NAME); var imapHost = rs.getString(IMAP_HOST); @@ -88,46 +92,60 @@ public class MailingList { 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)); + 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); + LOG.warn("Listing mailing lists failed: ", e); } return list; } + public static List openLists() { + var list = new ArrayList(); + try { + var rs = Database.open().query("SELECT *, (" + STATE + " & " + PUBLIC + ") as test FROM " + TABLE_NAME).where("test", PUBLIC).exec(); + while (rs.next()) { + var email = rs.getString(EMAIL); + var name = rs.getString(NAME); + var state = rs.getInt(STATE); + list.add(new MailingList(email, name, null, 0, null, null, null, 0, null, null, state)); + } + } catch (SQLException e) { + LOG.warn("Listing mailing lists failed: ", e); + } + return list; + } - public Map 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)); + public Map 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, stateString(state)); } - private static String stateName(int state) { - switch (state){ - case ENABLED: - return "enabled"; - default: - return "disabled"; - } + private static String stateString(int state) { + var states = new ArrayList(); + states.add((state & ENABLED) == ENABLED ? "enabled" : "disabled"); + states.add((state & PUBLIC) == PUBLIC ? "public" : "hidden"); + return String.join(", ", states); } private MailingList save() throws SQLException { Database.open().insertInto(TABLE_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))) + 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; } + } diff --git a/src/main/java/de/srsoftware/widerhall/web/Rest.java b/src/main/java/de/srsoftware/widerhall/web/Rest.java index aa3700f..0329d32 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Rest.java +++ b/src/main/java/de/srsoftware/widerhall/web/Rest.java @@ -1,5 +1,6 @@ package de.srsoftware.widerhall.web; +import de.srsoftware.widerhall.data.ListMember; import de.srsoftware.widerhall.data.MailingList; import de.srsoftware.widerhall.data.User; import org.json.simple.JSONObject; @@ -11,7 +12,9 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.sql.SQLException; import java.util.List; +import java.util.Map; import static de.srsoftware.widerhall.Constants.*; import static de.srsoftware.widerhall.Util.t; @@ -19,20 +22,29 @@ 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 LIST_HIDE = "list/hide"; + private static final String LIST_SHOW = "list/show"; private static final String USER_LIST = "user/list"; + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String error = handleGet(req, resp); if (error != null) resp.sendError(400,error); } + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String error = handlePost(req, resp); + if (error != null) resp.sendError(400,error); + } + public String handleGet(HttpServletRequest req, HttpServletResponse resp){ Object o = req.getSession().getAttribute(USER); JSONObject json = new JSONObject(); + var path = req.getPathInfo(); + path = path == null ? INDEX : path.substring(1); if (o instanceof User user){ - var path = req.getPathInfo(); 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(User::safeMap).toList()); @@ -44,6 +56,42 @@ public class Rest extends HttpServlet { json.put(ERROR,t("No handler for path '{}'!",path)); break; } + } else { + switch (path) { + case LIST_LIST: + json.put("lists", MailingList.openLists().stream().map(MailingList::safeMap).toList()); + break; + default: + json.put(ERROR,"Not logged in!"); + } + } + try { + resp.setContentType("application/json"); + resp.getWriter().println(json.toJSONString()); + return null; + } catch (IOException e) { + return t("Failed to handle request: {}",e.getMessage()); + } + } + + public String handlePost(HttpServletRequest req, HttpServletResponse resp){ + Object o = req.getSession().getAttribute(USER); + JSONObject json = new JSONObject(); + var path = req.getPathInfo(); + path = path == null ? INDEX : path.substring(1); + if (o instanceof User user){ + json.put(USER,user.safeMap()); + switch (path) { + case LIST_HIDE: + json.putAll(hideList(req,user,true)); + break; + case LIST_SHOW: + json.putAll(hideList(req,user,false)); + break; + default: + json.put(ERROR,t("No handler for path '{}'!",path)); + break; + } } else { json.put(ERROR,"Not logged in!"); } @@ -55,4 +103,22 @@ public class Rest extends HttpServlet { return t("Failed to handle request: {}",e.getMessage()); } } + + private Map hideList(HttpServletRequest req, User user, boolean hide) { + var listEmail = req.getParameter(LIST); + if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!"); + if (user.is(ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)){ + try { + MailingList.hide(listEmail,hide); + return Map.of("success",t("Mailing list '{}' was {}!",listEmail,hide ? "hidden" : "made public")); + } catch (SQLException e) { + LOG.error("Failed to (un)hide mailing list: ",e); + return Map.of("error",t("Failed to update list '{}'",listEmail)); + } + + } else { + return Map.of("error",t("You are not allowed to edit '{}'",listEmail)); + } + + } } diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml new file mode 100644 index 0000000..49a6adb --- /dev/null +++ b/src/main/resources/logback.xml @@ -0,0 +1,16 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{5}: %msg%n + + + + + + + + + + + diff --git a/static/templates/admin.st b/static/templates/admin.st index c657e55..d2fb414 100644 --- a/static/templates/admin.st +++ b/static/templates/admin.st @@ -12,6 +12,6 @@

Widerhall Administration

«messages()» «userlist()» - «listlist()» + «listadminlist()» \ No newline at end of file diff --git a/static/templates/index.st b/static/templates/index.st index 40ebffc..c88b4b3 100644 --- a/static/templates/index.st +++ b/static/templates/index.st @@ -11,5 +11,7 @@ «userinfo()» «messages()»

Widerhall Index page

+ «messages()» + «listlist()» \ No newline at end of file diff --git a/static/templates/js.st b/static/templates/js.st index 5b228a6..e49fb06 100644 --- a/static/templates/js.st +++ b/static/templates/js.st @@ -11,22 +11,47 @@ function enableList(listEmail){ } function hideList(listEmail){ - console.log('hideList('+listEmail+')'); + $.post('/api/list/hide',{list:listEmail},showListResult,'json'); +} + +function loadListAdminList(){ + $.getJSON('/api/list/list', showListAdminList); } function loadListList(){ - $.getJSON("/api/list/list", showListList); + $.getJSON('/api/list/list', showListAdminList); } function loadUserList(){ - $.getJSON("/api/user/list", showUserList); + $.getJSON('/api/user/list', showUserList); +} + +function reload(){ + window.location.reload(true); } function showList(listEmail){ - console.log('showList('+listEmail+')'); + $.post('/api/list/show',{list:listEmail},showListResult,'json'); +} + +function showListResult(result){ + console.log(result); + if ('error' in result){ + alert(result.error); + return; + } + if ('success' in result){ + alert(result.success); + reload(); + return; + } + alert("Api call did not return result"); +} + +function showListAdminList(data){ } -function showListList(data){ +function showListAdminList(data){ for (let i in data.lists){ let list = data.lists[i]; let row = $(''); diff --git a/static/templates/listadminlist.st b/static/templates/listadminlist.st new file mode 100644 index 0000000..f9d199b --- /dev/null +++ b/static/templates/listadminlist.st @@ -0,0 +1,26 @@ +
+ List of mailinglists + + + + + + + + + + + + + + + + + + +
ListIMAPSMTP
NameAddressStateActionsHostPortUserHostPortUser
+ Add new mailing list + +
\ No newline at end of file diff --git a/static/templates/listlist.st b/static/templates/listlist.st index 55501a1..2b4c6de 100644 --- a/static/templates/listlist.st +++ b/static/templates/listlist.st @@ -2,24 +2,11 @@ List of mailinglists - - - - - - - - - - - - - - - + + +
ListIMAPSMTP
NameAddressStateActionsHostPortUserHostPortUserList NameList AddressActions
- Add new mailing list