Mercurial Hosting > nabble
view src/nabble/view/web/mailing_list/MailingListNamespace.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
line wrap: on
line source
package nabble.view.web.mailing_list; import fschmidt.db.DbDatabase; import fschmidt.util.mail.MailAddress; import nabble.model.Db; import nabble.model.ListServer; import nabble.model.MailingList; import nabble.model.ModelException; import nabble.model.Node; import nabble.naml.compiler.Command; import nabble.naml.compiler.CommandSpec; import nabble.naml.compiler.IPrintWriter; import nabble.naml.compiler.Interpreter; import nabble.naml.compiler.Namespace; import nabble.naml.compiler.ScopedInterpreter; import nabble.naml.namespaces.ListSequence; import nabble.naml.namespaces.TemplateException; import nabble.view.web.template.NodeNamespace; import nabble.view.web.template.ServletNamespace; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.Arrays; import java.util.List; @Namespace ( name = "mailing_list", global = true ) public class MailingListNamespace { private Node node; private MailingList mailingList; public MailingListNamespace(Node node) { this.node = node; this.mailingList = node.getMailingList(); } public MailingListNamespace(MailingList mailingList) { this.node = mailingList.getForum(); this.mailingList = mailingList; } private DbDatabase db() { return node.getSite().getDb(); } public static final CommandSpec mailing_list_node = CommandSpec.DO; @Command public void mailing_list_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) { NodeNamespace ns = new NodeNamespace(node); out.print( interp.getArg(ns,"do") ); } @Command public void mailing_list_address(IPrintWriter out,Interpreter interp) { out.print( mailingList.getListAddress() ); } @Command public void mailing_list_url(IPrintWriter out,Interpreter interp) { out.print( interp.encode( mailingList.getUrl() ) ); } @Command public void mailing_list_server(IPrintWriter out,Interpreter interp) { out.print( mailingList.getListServer().getType() ); } @Command public void mailing_list_prefix(IPrintWriter out,Interpreter interp) { out.print( mailingList.getListName() ); } @Command public void plain_text_only(IPrintWriter out,Interpreter interp) { out.print( mailingList.plainTextOnly() ); } @Command public void ignore_xnoarchive(IPrintWriter out,Interpreter interp) { out.print( mailingList.ignoreNoArchive() ); } @Command public void mailing_list_options_path(IPrintWriter out,Interpreter interp) { out.print( interp.encode( "/mailing_list/MailingListOptions.jtp?forum=" + node.getId() ) ); } @Command public void can_subscribe(IPrintWriter out,Interpreter interp) { out.print( mailingList.getListServer().canSubscribe() ); } @Command public void subscriber_address(IPrintWriter out,Interpreter interp) { out.print( mailingList.getSubscriberAddress().getAddrSpec() ); } public static final CommandSpec available_servers = CommandSpec.DO; @Command public void available_servers(IPrintWriter out,ScopedInterpreter<Servers> interp) throws IOException, ServletException { String[] serverTypes = ListServer.getAllServerTypes(); Object block = interp.getArg(new Servers(Arrays.asList(serverTypes)),"do"); out.print(block); } @Namespace ( name = "server_list", global = false ) public static final class Servers extends ListSequence<String> { Servers(List<String> serverTypes) { super(serverTypes); } @Command public void current_server(IPrintWriter out,Interpreter interp) { out.print(ListServer.getServer(get()).getType()); } @Command public void current_server_name(IPrintWriter out,Interpreter interp) { out.print(ListServer.getServer(get()).getViewName()); } } public static final CommandSpec save = new CommandSpec.Builder() .dotParameter("do") .parameters("address","url","server","plain_text_only","ignore_xnoarchive") .optionalParameters("prefix") .requiredInStack(ServletNamespace.class) .build() ; @Command public void save(IPrintWriter out,Interpreter interp) throws TemplateException { ServletNamespace servletNs = interp.getFromStack(ServletNamespace.class); HttpServletRequest request = servletNs.request; if( !"POST".equals(request.getMethod()) ) return; String address = interp.getArgString("address"); if (address == null) throw ModelException.newInstance("invalid_list_address"); address = address.trim().toLowerCase(); if (!new MailAddress(address).isValid()) throw ModelException.newInstance("invalid_list_address"); String url = interp.getArgString("url"); if (url == null) throw ModelException.newInstance("invalid_list_url"); try { new URL(url); } catch (MalformedURLException e) { throw ModelException.newInstance("invalid_list_url"); } String server = interp.getArgString("server"); ListServer listServerType = ListServer.getServer(server); String prefix = interp.getArgString("prefix"); if (prefix != null && prefix.trim().length() == 0) prefix = null; boolean isPlainTextOnly = interp.getArgAsBoolean("plain_text_only",false); boolean isIgnoreXNoArchive = interp.getArgAsBoolean("ignore_xnoarchive",false); db().beginTransaction(); try { node = node.getGoodCopy(); // In case the node needs to be updated in the "do" block. mailingList = node.getMailingList(); if (mailingList != null) { mailingList.setListAddress(address); mailingList.setUrl(url); mailingList.setListServer(listServerType); } else { mailingList = node.newMailingList(listServerType, address, url); } mailingList.setListName(prefix); mailingList.setPlainTextOnly(isPlainTextOnly); mailingList.setIgnoreNoArchive(isIgnoreXNoArchive); interp.getArgString("do"); mailingList.update(); db().commitTransaction(); } finally { db().endTransaction(); } } @Command public void remove(IPrintWriter out,Interpreter interp) throws TemplateException { db().beginTransaction(); try { node.getGoodCopy().deleteMailingList(); db().commitTransaction(); } finally { db().endTransaction(); } } }