comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1 package nabble.view.web.mailing_list;
2
3 import fschmidt.db.DbDatabase;
4 import fschmidt.util.mail.MailAddress;
5 import nabble.model.Db;
6 import nabble.model.ListServer;
7 import nabble.model.MailingList;
8 import nabble.model.ModelException;
9 import nabble.model.Node;
10 import nabble.naml.compiler.Command;
11 import nabble.naml.compiler.CommandSpec;
12 import nabble.naml.compiler.IPrintWriter;
13 import nabble.naml.compiler.Interpreter;
14 import nabble.naml.compiler.Namespace;
15 import nabble.naml.compiler.ScopedInterpreter;
16 import nabble.naml.namespaces.ListSequence;
17 import nabble.naml.namespaces.TemplateException;
18 import nabble.view.web.template.NodeNamespace;
19 import nabble.view.web.template.ServletNamespace;
20
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServletRequest;
23 import java.io.IOException;
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.util.Arrays;
27 import java.util.List;
28
29
30 @Namespace (
31 name = "mailing_list",
32 global = true
33 )
34 public class MailingListNamespace {
35 private Node node;
36 private MailingList mailingList;
37
38 public MailingListNamespace(Node node) {
39 this.node = node;
40 this.mailingList = node.getMailingList();
41 }
42
43 public MailingListNamespace(MailingList mailingList) {
44 this.node = mailingList.getForum();
45 this.mailingList = mailingList;
46 }
47
48 private DbDatabase db() {
49 return node.getSite().getDb();
50 }
51
52 public static final CommandSpec mailing_list_node = CommandSpec.DO;
53
54 @Command public void mailing_list_node(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) {
55 NodeNamespace ns = new NodeNamespace(node);
56 out.print( interp.getArg(ns,"do") );
57 }
58
59 @Command public void mailing_list_address(IPrintWriter out,Interpreter interp) {
60 out.print( mailingList.getListAddress() );
61 }
62
63 @Command public void mailing_list_url(IPrintWriter out,Interpreter interp) {
64 out.print( interp.encode( mailingList.getUrl() ) );
65 }
66
67 @Command public void mailing_list_server(IPrintWriter out,Interpreter interp) {
68 out.print( mailingList.getListServer().getType() );
69 }
70
71 @Command public void mailing_list_prefix(IPrintWriter out,Interpreter interp) {
72 out.print( mailingList.getListName() );
73 }
74
75 @Command public void plain_text_only(IPrintWriter out,Interpreter interp) {
76 out.print( mailingList.plainTextOnly() );
77 }
78
79 @Command public void ignore_xnoarchive(IPrintWriter out,Interpreter interp) {
80 out.print( mailingList.ignoreNoArchive() );
81 }
82
83 @Command public void mailing_list_options_path(IPrintWriter out,Interpreter interp) {
84 out.print( interp.encode( "/mailing_list/MailingListOptions.jtp?forum=" + node.getId() ) );
85 }
86
87 @Command public void can_subscribe(IPrintWriter out,Interpreter interp) {
88 out.print( mailingList.getListServer().canSubscribe() );
89 }
90
91 @Command public void subscriber_address(IPrintWriter out,Interpreter interp) {
92 out.print( mailingList.getSubscriberAddress().getAddrSpec() );
93 }
94
95 public static final CommandSpec available_servers = CommandSpec.DO;
96
97 @Command public void available_servers(IPrintWriter out,ScopedInterpreter<Servers> interp)
98 throws IOException, ServletException
99 {
100 String[] serverTypes = ListServer.getAllServerTypes();
101 Object block = interp.getArg(new Servers(Arrays.asList(serverTypes)),"do");
102 out.print(block);
103 }
104
105 @Namespace (
106 name = "server_list",
107 global = false
108 )
109 public static final class Servers extends ListSequence<String> {
110
111 Servers(List<String> serverTypes) {
112 super(serverTypes);
113 }
114
115 @Command public void current_server(IPrintWriter out,Interpreter interp) {
116 out.print(ListServer.getServer(get()).getType());
117 }
118
119 @Command public void current_server_name(IPrintWriter out,Interpreter interp) {
120 out.print(ListServer.getServer(get()).getViewName());
121 }
122 }
123
124 public static final CommandSpec save = new CommandSpec.Builder()
125 .dotParameter("do")
126 .parameters("address","url","server","plain_text_only","ignore_xnoarchive")
127 .optionalParameters("prefix")
128 .requiredInStack(ServletNamespace.class)
129 .build()
130 ;
131
132 @Command public void save(IPrintWriter out,Interpreter interp)
133 throws TemplateException
134 {
135 ServletNamespace servletNs = interp.getFromStack(ServletNamespace.class);
136 HttpServletRequest request = servletNs.request;
137 if( !"POST".equals(request.getMethod()) )
138 return;
139
140 String address = interp.getArgString("address");
141 if (address == null)
142 throw ModelException.newInstance("invalid_list_address");
143 address = address.trim().toLowerCase();
144 if (!new MailAddress(address).isValid())
145 throw ModelException.newInstance("invalid_list_address");
146
147 String url = interp.getArgString("url");
148 if (url == null)
149 throw ModelException.newInstance("invalid_list_url");
150 try {
151 new URL(url);
152 } catch (MalformedURLException e) {
153 throw ModelException.newInstance("invalid_list_url");
154 }
155
156 String server = interp.getArgString("server");
157 ListServer listServerType = ListServer.getServer(server);
158
159 String prefix = interp.getArgString("prefix");
160 if (prefix != null && prefix.trim().length() == 0)
161 prefix = null;
162
163 boolean isPlainTextOnly = interp.getArgAsBoolean("plain_text_only",false);
164 boolean isIgnoreXNoArchive = interp.getArgAsBoolean("ignore_xnoarchive",false);
165
166 db().beginTransaction();
167 try {
168 node = node.getGoodCopy(); // In case the node needs to be updated in the "do" block.
169 mailingList = node.getMailingList();
170 if (mailingList != null) {
171 mailingList.setListAddress(address);
172 mailingList.setUrl(url);
173 mailingList.setListServer(listServerType);
174 } else {
175 mailingList = node.newMailingList(listServerType, address, url);
176 }
177 mailingList.setListName(prefix);
178 mailingList.setPlainTextOnly(isPlainTextOnly);
179 mailingList.setIgnoreNoArchive(isIgnoreXNoArchive);
180
181 interp.getArgString("do");
182 mailingList.update();
183 db().commitTransaction();
184 } finally {
185 db().endTransaction();
186 }
187 }
188
189 @Command public void remove(IPrintWriter out,Interpreter interp)
190 throws TemplateException
191 {
192 db().beginTransaction();
193 try {
194 node.getGoodCopy().deleteMailingList();
195 db().commitTransaction();
196 } finally {
197 db().endTransaction();
198 }
199 }
200 }