view src/nabble/view/web/mailing_list/SubscribeToMailingList.jtp @ 19:18cf4872fd7f

remove anonymous posting
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 29 May 2020 22:58:25 -0600
parents 7ecd1a4ef557
children
line wrap: on
line source

<%
package nabble.view.web.mailing_list;

import fschmidt.util.mail.Mail;
import fschmidt.util.mail.MailAddress;
import fschmidt.util.mail.MailException;
import fschmidt.util.mail.MailHome;
import fschmidt.util.mail.PlainTextContent;
import fschmidt.util.mail.TextContent;
import fschmidt.util.servlet.AuthorizingServlet;
import nabble.model.ListServer;
import nabble.model.MailingList;
import nabble.model.ModelHome;
import nabble.model.Node;
import nabble.model.User;
import nabble.view.lib.Jtp;
import nabble.view.lib.Shared;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


public final class SubscribeToMailingList extends HttpServlet implements AuthorizingServlet {

	private static final Logger logger = LoggerFactory.getLogger(SubscribeToMailingList.class);

	public String getAuthorizationKey(HttpServletRequest request) throws ServletException {
		return Jtp.getReadAuthorizationKey( Jtp.getSiteNotNull(request).getNode(Jtp.getLong(request,"node")) );
	}

	public boolean authorize(String key,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		return Jtp.authorizeForRead(key,request,response); 
	}

	protected void service(HttpServletRequest request,HttpServletResponse response)
		throws ServletException, IOException
	{
		User user = Jtp.getUser(request);

		if (user == null) {
			Jtp.login("You must login to subscribe to mailing list.", request, response);
			return;
		}

		Node node = Jtp.getSiteNotNull(request).getNode(Jtp.getLong(request, "node"));
		boolean allowed = Jtp.canBeEditedBy(node,user);
		if (!allowed) {
			Jtp.login("Only administrators can proceed in this area.", request, response);
			return;
		}

		PrintWriter out = response.getWriter();
		boolean isSendEmail = "SendEmail".equals(request.getParameter("action"));

		MailingList mailingList = node.getMailingList();
		String subscriptionAddress = mailingList.getSubscriberAddress().getAddrSpec();
		char c = 'A';
		%>
		<html>
			<head>
				<META NAME="robots" CONTENT="noindex,nofollow">
				<% Shared.title(request,response,"Subscription Instructions"); %>
				<style type="text/css">
					div.field-title {
						margin-top: 0;
					}
					td.number {
						width: 3em;
						padding-bottom: .2em;
					}
					span.number {
						font-size: 200%;
						padding: 0 .3em .03em;
						border-width:1px;
						border-style:solid;
					}
				</style>
			</head>
			<body>
				<% Shared.minHeader(request, response, node);%>
				<% Shared.editHeader(node.getSubjectHtml(), "Subscription Instructions", out); %>
				<%
				if (isSendEmail && "POST".equals(request.getMethod())) {
					String errorMsg = sendRequest(node, request, out);
					Shared.errorMessage(request, response, errorMsg, "Failed to send the request." );
				}
				%>
				<div class="field-box light-border-color">
					<div class="second-font field-title">General Instructions</div>
					<div style="margin-left:1.5em">
						Your mailing list archive will <b>ONLY</b> work properly if you have the following email address subscribed to the mailing list:
						<div class="info-message" style="margin:.2em;padding:.2em;font-weight:bold"><%=subscriptionAddress%></div>
						This can be done in several ways. Below you can find the most common ones.
					</div>
				</div>

				<div class="second-font field-title" style="margin-top:1em">Subscription Options</div>
				<div class="weak-color">Choose the best option for this mailing list.</div>

				<div class="field-box light-border-color">
					<table style="margin-left:1.3em">
						<tr valign="top">
							<td class="number"><span class="number shaded-bg-color medium-border-color"><%=c++%></span></td>
							<td>
								<div class="second-font field-title">Add to subscriber's list</div>
								Mailing list administrators usually can add email addresses to the subscribers' list directly.
								If you can do this, add <b><%=subscriptionAddress%></b> to that list.
							</td>
						</tr>
					</table>
				</div>

				<% if (mailingList.getListServer() == ListServer.google) { %>
				<div class="field-box light-border-color">
					<table style="margin-left:1.3em">
						<tr valign="top">
							<td class="number"><span class="number shaded-bg-color medium-border-color"><%=c++%></span></td>
							<td>
								<div class="second-font field-title">Use the "Invite" feature of GoogleGroups </div>
								You can go to the <a href="http://groups.google.com/" rel="nofollow">Google Groups website</a> and
								invite the following address to be a member:
								<b><%=subscriptionAddress%></b>.<br>
								The confirmation email will be forwarded to you so that you can complete this step.
							</td>
						</tr>
					</table>
				</div>
				<div class="field-box light-border-color">
					<table style="margin-left:1.3em">
						<tr valign="top">
							<td class="number"><span class="number shaded-bg-color medium-border-color"><%=c++%></span></td>
							<td>
								<div class="second-font field-title">Create an account in GoogleGroups for the subscription email</div>
								You can go to the <a href="http://groups.google.com/" rel="nofollow">Google Groups website</a> and
								create an account for this email:
								<b><%=subscriptionAddress%></b>.<br>
								(Google will probably send a confirmation number to your cell phone so that you can finish the registration)<br>
								When you have finished the account set up, you can go to the mailing list page and click on "Join this group".
							</td>
						</tr>
					</table>
				</div>
				<% } else { %>
					<div class="field-box light-border-color">
						<table style="margin-left:1.3em">
							<tr valign="top">
								<td class="number"><span class="number shaded-bg-color medium-border-color"><%=c++%></span></td>
								<td>
									<div class="second-font field-title">Go to mailing list website</div>
									You can go to the mailing list homepage (<a href="<%=mailingList.getUrl()%>"><%=mailingList.getUrl()%></a>)
									and subscribe <b><%=subscriptionAddress%></b> to the list.
									The confirmation email will be forwarded to your email address as soon as it is received by Nabble.com.
									You will have to follow the instructions in that email to confirm this request.
								</td>
							</tr>
						</table>
					</div>
				<% } %>
				<% emailForm(node, true, "Subscribe by email", "Send Subscription Request", c, request, out); %>

				<% Shared.footer(request,response); %>
				<% Shared.analytics(request,response); %>
			</body>
		</html>
		<%
	}

	static String sendRequest(Node node, HttpServletRequest request, PrintWriter out)
		throws ServletException, IOException
	{
		String emailFrom = request.getParameter("emailFrom");
		String emailToUser = request.getParameter("emailToUser");
		String emailToDomain = request.getParameter("emailToDomain");
		String emailSubject = request.getParameter("emailSubject");
		String emailBody = request.getParameter("emailBody");

		if (emailFrom == null || emailToUser == null || emailToDomain == null) {
			return "Invalid message parameters";
		} else {
			MailAddress toAddress = new MailAddress(emailToUser + emailToDomain);
			try {
				Mail mail = MailHome.newMail();
				mail.setFrom(new MailAddress(emailFrom, "Nabble Forums"));
				mail.setTo(toAddress);
				mail.setSubject(emailSubject);
				mail.setContent(new PlainTextContent(emailBody));
				ModelHome.send(mail);
			} catch(MailException e) {
				logger.warn("",e);
				return e.getMessage();
			}
			%>
			<div class="info-message" style="padding:1em">
				<b>Email Sent</b><br/>
				The request has been successfully sent. The confirmation email will be forwarded to your email
				address as soon as it is received by Nabble. You will have to follow the instructions in
				that email to finish the process.<br/>
				<a href="<%=Jtp.path(node)%>">Return to forum</a>
			</div>
			<%
		}
		return null;
	}	

	static void emailForm(Node node, boolean isSubscription, String title, String button, char c, HttpServletRequest request, PrintWriter out) {
		MailingList mailingList = node.getMailingList();
		if (!mailingList.getListServer().canSubscribe())
			return;
		%>
		<div class="field-box light-border-color">
			<table style="margin-left:1.3em">
				<tr valign="top">
					<td class="number"><span class="number shaded-bg-color medium-border-color"><%=c%></span></td>
					<td>
						<div class="second-font field-title"><%=title%></div>
						You can send the request by email. Default content of the email is
						provided according to the mailing list server type (<b><%=mailingList.getListServer().getViewName()%></b>)
						that you have specified during the mailing list setup. You can modify it if necessary.
					</td>
				</tr>
			</table>
		</div>
		<%
		String emailFrom = request.getParameter("emailFrom");
		String emailToUser = request.getParameter("emailToUser");
		String emailToDomain = request.getParameter("emailToDomain");
		String emailSubject = request.getParameter("emailSubject");
		String emailBody = request.getParameter("emailBody");
		Mail mail = isSubscription? mailingList.subscribeMail(): mailingList.unsubscribeMail();

		if (emailFrom == null) {
			emailFrom = mail.getFrom().getAddrSpec();
		}

		if (emailToUser == null || emailToDomain == null) {
			String emailTo = mail.getTo()[0].getAddrSpec();
			int index = emailTo.indexOf("@");
			if(index < 0) {
				throw new RuntimeException("Invalid emailTo: " + emailTo);
			}
			emailToUser = emailTo.substring(0, index);
			emailToDomain = emailTo.substring(index);
		}

		if (emailSubject == null) {
			emailSubject = Jtp.hideNull(mail.getSubject());
		}

		if (emailBody == null) {
			emailBody = ((TextContent) mail.getContent()).getText();
		}
		%>
		<form method="post" action="/mailing_list/<%=isSubscription? "SubscribeToMailingList.jtp":"UnsubscribeFromMailingList.jtp"%>" accept-charset="UTF-8">
			<input type="hidden" name="node" value="<%=node.getId()%>">
			<input type="hidden" name="action" value="SendEmail">
			<input type="hidden" name="emailToDomain" value="<%=emailToDomain%>">
			<input type="hidden" name="emailToUser" value="<%=emailToUser%>">
			<input type="hidden" name="emailSubject" value="<%=emailSubject%>">
			<input type="hidden" name="emailBody" value="<%=emailBody%>">
			<input type="hidden" name="emailFrom" value="<%=emailFrom%>"/>
			<table style="margin-left:2em">
				<tr>
					<td align=right>From:&nbsp;</td>
					<td><input type="text" name="emailFrom2" value="<%=emailFrom%>" size="50" disabled/></td>
				</tr>
				<tr>
					<td align=right>To:&nbsp;</td>
					<td valign="middle"><input type="text" name="emailToUser2" value="<%=emailToUser%>" size="50" disabled/><%=emailToDomain%></td>
				</tr>
				<tr>
					<td align=right>Subject:&nbsp;</td>
					<td><input type="text" name="emailSubject2" value="<%=emailSubject%>" size="50" disabled/></td>
				</tr>
				<tr>
					<td align=right valign=top>Message:&nbsp;</td>
					<td><textarea cols="50" rows="5" name="emailBody2" wrap="SOFT" disabled><%=emailBody%></textarea></td>
				</tr>
				<tr>
					<td><br></td>
					<td>
						<input type="submit" value="<%=button%>" />
					</td>
				</tr>
			</table>
		</form>
		<%
	}

}
%>