comparison src/nabble/view/web/mailing_list/SubscribeToMailingList.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children 18cf4872fd7f
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1
2 package nabble.view.web.mailing_list;
3
4 import fschmidt.util.mail.Mail;
5 import fschmidt.util.mail.MailAddress;
6 import fschmidt.util.mail.MailException;
7 import fschmidt.util.mail.MailHome;
8 import fschmidt.util.mail.PlainTextContent;
9 import fschmidt.util.mail.TextContent;
10 import fschmidt.util.servlet.AuthorizingServlet;
11 import nabble.model.ListServer;
12 import nabble.model.MailingList;
13 import nabble.model.ModelHome;
14 import nabble.model.Node;
15 import nabble.model.User;
16 import nabble.view.lib.Jtp;
17 import nabble.view.lib.Shared;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServlet;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25 import java.io.IOException;
26 import java.io.PrintWriter;
27
28
29 public final class SubscribeToMailingList extends HttpServlet implements AuthorizingServlet {
30
31 private static final Logger logger = LoggerFactory.getLogger(SubscribeToMailingList.class);
32
33 public String getAuthorizationKey(HttpServletRequest request) throws ServletException {
34 return Jtp.getReadAuthorizationKey( Jtp.getSiteNotNull(request).getNode(Jtp.getLong(request,"node")) );
35 }
36
37 public boolean authorize(String key,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
38 return Jtp.authorizeForRead(key,request,response);
39 }
40
41 protected void service(HttpServletRequest request,HttpServletResponse response)
42 throws ServletException, IOException
43 {
44 User user = Jtp.getUser(request,response);
45
46 if (user == null) {
47 Jtp.login("You must login to subscribe to mailing list.", request, response);
48 return;
49 }
50
51 Node node = Jtp.getSiteNotNull(request).getNode(Jtp.getLong(request, "node"));
52 boolean allowed = Jtp.canBeEditedBy(node,user);
53 if (!allowed) {
54 Jtp.login("Only administrators can proceed in this area.", request, response);
55 return;
56 }
57
58 PrintWriter out = response.getWriter();
59 boolean isSendEmail = "SendEmail".equals(request.getParameter("action"));
60
61 MailingList mailingList = node.getMailingList();
62 String subscriptionAddress = mailingList.getSubscriberAddress().getAddrSpec();
63 char c = 'A';
64
65 out.print( "\n<html>\n <head>\n <META NAME=\"robots\" CONTENT=\"noindex,nofollow\">\n " );
66 Shared.title(request,response,"Subscription Instructions");
67 out.print( "\n <style type=\"text/css\">\n div.field-title {\n margin-top: 0;\n }\n td.number {\n width: 3em;\n padding-bottom: .2em;\n }\n span.number {\n font-size: 200%;\n padding: 0 .3em .03em;\n border-width:1px;\n border-style:solid;\n }\n </style>\n </head>\n <body>\n " );
68 Shared.minHeader(request, response, node);
69 out.print( "\n " );
70 Shared.editHeader(node.getSubjectHtml(), "Subscription Instructions", out);
71 out.print( "\n " );
72
73 if (isSendEmail && "POST".equals(request.getMethod())) {
74 String errorMsg = sendRequest(node, request, out);
75 Shared.errorMessage(request, response, errorMsg, "Failed to send the request." );
76 }
77
78 out.print( "\n<div class=\"field-box light-border-color\">\n <div class=\"second-font field-title\">General Instructions</div>\n <div style=\"margin-left:1.5em\">\n Your mailing list archive will <b>ONLY</b> work properly if you have the following email address subscribed to the mailing list:\n <div class=\"info-message\" style=\"margin:.2em;padding:.2em;font-weight:bold\">" );
79 out.print( (subscriptionAddress) );
80 out.print( "</div>\n This can be done in several ways. Below you can find the most common ones.\n </div>\n</div>\n\n<div class=\"second-font field-title\" style=\"margin-top:1em\">Subscription Options</div>\n<div class=\"weak-color\">Choose the best option for this mailing list.</div>\n\n<div class=\"field-box light-border-color\">\n <table style=\"margin-left:1.3em\">\n <tr valign=\"top\">\n <td class=\"number\"><span class=\"number shaded-bg-color medium-border-color\">" );
81 out.print( (c++) );
82 out.print( "</span></td>\n <td>\n <div class=\"second-font field-title\">Add to subscriber's list</div>\n Mailing list administrators usually can add email addresses to the subscribers' list directly.\n If you can do this, add <b>" );
83 out.print( (subscriptionAddress) );
84 out.print( "</b> to that list.\n </td>\n </tr>\n </table>\n</div>\n\n" );
85 if (mailingList.getListServer() == ListServer.google) {
86 out.print( "\n<div class=\"field-box light-border-color\">\n <table style=\"margin-left:1.3em\">\n <tr valign=\"top\">\n <td class=\"number\"><span class=\"number shaded-bg-color medium-border-color\">" );
87 out.print( (c++) );
88 out.print( "</span></td>\n <td>\n <div class=\"second-font field-title\">Use the \"Invite\" feature of GoogleGroups </div>\n You can go to the <a href=\"http://groups.google.com/\" rel=\"nofollow\">Google Groups website</a> and\n invite the following address to be a member:\n <b>" );
89 out.print( (subscriptionAddress) );
90 out.print( "</b>.<br>\n The confirmation email will be forwarded to you so that you can complete this step.\n </td>\n </tr>\n </table>\n</div>\n<div class=\"field-box light-border-color\">\n <table style=\"margin-left:1.3em\">\n <tr valign=\"top\">\n <td class=\"number\"><span class=\"number shaded-bg-color medium-border-color\">" );
91 out.print( (c++) );
92 out.print( "</span></td>\n <td>\n <div class=\"second-font field-title\">Create an account in GoogleGroups for the subscription email</div>\n You can go to the <a href=\"http://groups.google.com/\" rel=\"nofollow\">Google Groups website</a> and\n create an account for this email:\n <b>" );
93 out.print( (subscriptionAddress) );
94 out.print( "</b>.<br>\n (Google will probably send a confirmation number to your cell phone so that you can finish the registration)<br>\n When you have finished the account set up, you can go to the mailing list page and click on \"Join this group\".\n </td>\n </tr>\n </table>\n</div>\n" );
95 } else {
96 out.print( "\n <div class=\"field-box light-border-color\">\n <table style=\"margin-left:1.3em\">\n <tr valign=\"top\">\n <td class=\"number\"><span class=\"number shaded-bg-color medium-border-color\">" );
97 out.print( (c++) );
98 out.print( "</span></td>\n <td>\n <div class=\"second-font field-title\">Go to mailing list website</div>\n You can go to the mailing list homepage (<a href=\"" );
99 out.print( (mailingList.getUrl()) );
100 out.print( "\">" );
101 out.print( (mailingList.getUrl()) );
102 out.print( "</a>)\n and subscribe <b>" );
103 out.print( (subscriptionAddress) );
104 out.print( "</b> to the list.\n The confirmation email will be forwarded to your email address as soon as it is received by Nabble.com.\n You will have to follow the instructions in that email to confirm this request.\n </td>\n </tr>\n </table>\n </div>\n" );
105 }
106 out.print( "\n" );
107 emailForm(node, true, "Subscribe by email", "Send Subscription Request", c, request, out);
108 out.print( "\n\n" );
109 Shared.footer(request,response);
110 out.print( "\n" );
111 Shared.analytics(request,response);
112 out.print( "\n</body>\n</html>\n" );
113
114 }
115
116 static String sendRequest(Node node, HttpServletRequest request, PrintWriter out)
117 throws ServletException, IOException
118 {
119 String emailFrom = request.getParameter("emailFrom");
120 String emailToUser = request.getParameter("emailToUser");
121 String emailToDomain = request.getParameter("emailToDomain");
122 String emailSubject = request.getParameter("emailSubject");
123 String emailBody = request.getParameter("emailBody");
124
125 if (emailFrom == null || emailToUser == null || emailToDomain == null) {
126 return "Invalid message parameters";
127 } else {
128 MailAddress toAddress = new MailAddress(emailToUser + emailToDomain);
129 try {
130 Mail mail = MailHome.newMail();
131 mail.setFrom(new MailAddress(emailFrom, "Nabble Forums"));
132 mail.setTo(toAddress);
133 mail.setSubject(emailSubject);
134 mail.setContent(new PlainTextContent(emailBody));
135 ModelHome.send(mail);
136 } catch(MailException e) {
137 logger.warn("",e);
138 return e.getMessage();
139 }
140
141 out.print( "\n<div class=\"info-message\" style=\"padding:1em\">\n <b>Email Sent</b><br/>\n The request has been successfully sent. The confirmation email will be forwarded to your email\n address as soon as it is received by Nabble. You will have to follow the instructions in\n that email to finish the process.<br/>\n <a href=\"" );
142 out.print( (Jtp.path(node)) );
143 out.print( "\">Return to forum</a>\n</div>\n" );
144
145 }
146 return null;
147 }
148
149 static void emailForm(Node node, boolean isSubscription, String title, String button, char c, HttpServletRequest request, PrintWriter out) {
150 MailingList mailingList = node.getMailingList();
151 if (!mailingList.getListServer().canSubscribe())
152 return;
153
154 out.print( "\n<div class=\"field-box light-border-color\">\n <table style=\"margin-left:1.3em\">\n <tr valign=\"top\">\n <td class=\"number\"><span class=\"number shaded-bg-color medium-border-color\">" );
155 out.print( (c) );
156 out.print( "</span></td>\n <td>\n <div class=\"second-font field-title\">" );
157 out.print( (title) );
158 out.print( "</div>\n You can send the request by email. Default content of the email is\n provided according to the mailing list server type (<b>" );
159 out.print( (mailingList.getListServer().getViewName()) );
160 out.print( "</b>)\n that you have specified during the mailing list setup. You can modify it if necessary.\n </td>\n </tr>\n </table>\n</div>\n" );
161
162 String emailFrom = request.getParameter("emailFrom");
163 String emailToUser = request.getParameter("emailToUser");
164 String emailToDomain = request.getParameter("emailToDomain");
165 String emailSubject = request.getParameter("emailSubject");
166 String emailBody = request.getParameter("emailBody");
167 Mail mail = isSubscription? mailingList.subscribeMail(): mailingList.unsubscribeMail();
168
169 if (emailFrom == null) {
170 emailFrom = mail.getFrom().getAddrSpec();
171 }
172
173 if (emailToUser == null || emailToDomain == null) {
174 String emailTo = mail.getTo()[0].getAddrSpec();
175 int index = emailTo.indexOf("@");
176 if(index < 0) {
177 throw new RuntimeException("Invalid emailTo: " + emailTo);
178 }
179 emailToUser = emailTo.substring(0, index);
180 emailToDomain = emailTo.substring(index);
181 }
182
183 if (emailSubject == null) {
184 emailSubject = Jtp.hideNull(mail.getSubject());
185 }
186
187 if (emailBody == null) {
188 emailBody = ((TextContent) mail.getContent()).getText();
189 }
190
191 out.print( "\n<form method=\"post\" action=\"/mailing_list/" );
192 out.print( (isSubscription? "SubscribeToMailingList.jtp":"UnsubscribeFromMailingList.jtp") );
193 out.print( "\" accept-charset=\"UTF-8\">\n <input type=\"hidden\" name=\"node\" value=\"" );
194 out.print( (node.getId()) );
195 out.print( "\">\n <input type=\"hidden\" name=\"action\" value=\"SendEmail\">\n <input type=\"hidden\" name=\"emailToDomain\" value=\"" );
196 out.print( (emailToDomain) );
197 out.print( "\">\n <input type=\"hidden\" name=\"emailToUser\" value=\"" );
198 out.print( (emailToUser) );
199 out.print( "\">\n <input type=\"hidden\" name=\"emailSubject\" value=\"" );
200 out.print( (emailSubject) );
201 out.print( "\">\n <input type=\"hidden\" name=\"emailBody\" value=\"" );
202 out.print( (emailBody) );
203 out.print( "\">\n <input type=\"hidden\" name=\"emailFrom\" value=\"" );
204 out.print( (emailFrom) );
205 out.print( "\"/>\n <table style=\"margin-left:2em\">\n <tr>\n <td align=right>From:&nbsp;</td>\n <td><input type=\"text\" name=\"emailFrom2\" value=\"" );
206 out.print( (emailFrom) );
207 out.print( "\" size=\"50\" disabled/></td>\n </tr>\n <tr>\n <td align=right>To:&nbsp;</td>\n <td valign=\"middle\"><input type=\"text\" name=\"emailToUser2\" value=\"" );
208 out.print( (emailToUser) );
209 out.print( "\" size=\"50\" disabled/>" );
210 out.print( (emailToDomain) );
211 out.print( "</td>\n </tr>\n <tr>\n <td align=right>Subject:&nbsp;</td>\n <td><input type=\"text\" name=\"emailSubject2\" value=\"" );
212 out.print( (emailSubject) );
213 out.print( "\" size=\"50\" disabled/></td>\n </tr>\n <tr>\n <td align=right valign=top>Message:&nbsp;</td>\n <td><textarea cols=\"50\" rows=\"5\" name=\"emailBody2\" wrap=\"SOFT\" disabled>" );
214 out.print( (emailBody) );
215 out.print( "</textarea></td>\n </tr>\n <tr>\n <td><br></td>\n <td>\n <input type=\"submit\" value=\"" );
216 out.print( (button) );
217 out.print( "\" />\n </td>\n </tr>\n </table>\n</form>\n" );
218
219 }
220
221 }
222