1.1 restoration
[mir.git] / source / mircoders / localizer / basic / MirBasicEmailArticleHandler.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package mircoders.localizer.basic;
31
32 import java.io.IOException;
33 import java.io.PrintWriter;
34 import java.io.StringWriter;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Locale;
38 import java.util.Map;
39 import java.util.Vector;
40 import java.util.Collections;
41
42 import mir.config.MirPropertiesConfiguration;
43 import mir.generator.Generator;
44 import mir.generator.GeneratorHelper;
45 import mir.log.LoggerWrapper;
46 import mir.session.Request;
47 import mir.session.Response;
48 import mir.session.Session;
49 import mir.session.SessionExc;
50 import mir.session.SessionFailure;
51 import mir.session.SessionHandler;
52 import mir.session.ValidationHelper;
53 import mir.session.HTTPAdapters.HTTPRequestAdapter;
54 import mir.util.StringRoutines;
55 import mircoders.entity.EntityContent;
56 import mircoders.global.CacheKey;
57 import mircoders.global.MirGlobal;
58 import mircoders.module.ModuleContent;
59 import mircoders.storage.DatabaseContent;
60
61 import org.apache.commons.net.smtp.SMTPClient;
62 import org.apache.commons.net.smtp.SMTPReply;
63
64
65 /**
66  *
67  * <p>Title: Tenative session handler for emailing postings </p>
68  * <p>Description: </p>
69  * <p>Copyright: Copyright (c) 2003</p>
70  * <p>Company: </p>
71  * @author john
72  * @version 1.0
73  */
74
75
76 public class MirBasicEmailArticleHandler implements SessionHandler {
77   protected LoggerWrapper logger;
78   protected MirPropertiesConfiguration configuration;
79
80   public MirBasicEmailArticleHandler() {
81     logger = new LoggerWrapper("Localizer.EmailArticle");
82     try {
83       configuration = MirPropertiesConfiguration.instance();
84     }
85     catch (Throwable t) {
86       logger.fatal("Cannot load configuration: " + t.toString());
87
88       throw new RuntimeException("Cannot load configuration: " + t.toString());
89     }
90   }
91
92   public void processRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
93     if (aSession.getAttribute("initialRequest") == null) {
94       aSession.setAttribute("initialRequest", "no");
95       initialRequest(aRequest, aSession, aResponse);
96     }
97     else {
98       subsequentRequest(aRequest, aSession, aResponse);
99     }
100   }
101
102   protected void initialRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
103     initializeSession(aRequest, aSession);
104     makeInitialResponse(aRequest, aSession, aResponse);
105   }
106
107   protected void initializeSession(Request aRequest, Session aSession) throws SessionExc, SessionFailure {
108     /* do nothing for now */
109
110   }
111
112   protected void makeInitialResponse(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
113     /* if you do not supply an aid to this handler, it should return an error page */
114     /* if you supply a non-functioning/non-published  aid to this handler, it should return an error page, but at a
115        later stage, because we don't check the db until we are potentially populating the cache*/
116     /* otherwise you get to address an article and add some comments */
117     String articleID = aRequest.getParameter("mail_aid");
118     if (articleID == null){
119       throw new SessionExc("makeInitialResponse: article id not set!");
120     }
121     else {
122       aSession.setAttribute("email.aid",articleID);
123       aResponse.setResponseValue("errors", null);
124
125       String mail_language = configuration.getString("Mir.Login.DefaultLanguage", "en");
126       aResponse.setResponseValue("mail_language",mail_language);
127       aResponse.setResponseValue("mail_to","");
128       aResponse.setResponseValue("mail_from","");
129       aResponse.setResponseValue("mail_from_name","");
130       aResponse.setResponseValue("mail_comment","");
131
132       aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.email.PrepareTemplate"));
133
134
135     }
136   }
137
138   protected boolean shouldSendMail(Request aRequest, Session aSession, Response aResponse,List aValidationErrors) throws SessionExc, SessionFailure{
139     if (validate(aRequest,aSession,aResponse,aValidationErrors)){
140       String to=aRequest.getParameter("mail_to");
141       if (to.indexOf('@') == -1
142           || to.indexOf('\n') != -1
143           || to.indexOf('\r') != -1
144           || to.indexOf(',') != -1) {
145         throw new SessionExc("Invalid to address"); // we might want to see this in a log, so it is not a validation error
146       }
147       else return true; // go for it
148     }
149     else{
150       return false; //validation failed, but not in a potentially abusive way
151     }
152
153   }
154
155
156
157
158
159   protected boolean validate(Request aRequest, Session aSession, Response aResponse,List aValidationErrors) throws SessionExc, SessionFailure{
160
161     if (ValidationHelper.testFieldEntered(aRequest, "mail_to", "validationerror.missing", aValidationErrors))
162       aResponse.setResponseValue("mail_to",aRequest.getParameter("mail_to"));
163     if (ValidationHelper.testFieldEntered(aRequest, "mail_from", "validationerror.missing", aValidationErrors))
164       aResponse.setResponseValue("mail_from",aRequest.getParameter("mail_from"));
165     if (ValidationHelper.testFieldEntered(aRequest, "mail_from_name", "validationerror.missing", aValidationErrors))
166       aResponse.setResponseValue("mail_from_name",aRequest.getParameter("mail_from_name"));
167     if (ValidationHelper.testFieldEntered(aRequest, "mail_language", "validationerror.missing", aValidationErrors))
168       aResponse.setResponseValue("mail_language",aRequest.getParameter("mail_language"));
169
170     return (aValidationErrors==null || aValidationErrors.size() == 0);
171   }
172
173   protected String getEmailText(String aid,String language) throws SessionExc{
174     String theText;
175     CacheKey theCacheKey=new CacheKey("email",aid+language);
176
177     if (MirGlobal.mruCache().hasObject(theCacheKey)){
178       logger.info("fetching email text for article "+aid+" from cache");
179       theText = (String) MirGlobal.mruCache().getObject(theCacheKey);
180     }
181     else {
182       try {
183         ModuleContent contentModule = new ModuleContent();
184         EntityContent contentEnt = (EntityContent) contentModule.getById(aid);
185
186         Map articleData = new HashMap();
187         articleData.put("article", MirGlobal.localizer().dataModel().adapterModel().makeEntityAdapter("content", contentEnt));
188         articleData.put("languagecode", language);
189         Map responseData = GeneratorHelper.makeBasicGenerationData(new Locale[] {new Locale(language,""),new Locale(configuration.getString("Mir.Admin.FallbackLanguage", "en"), "")},"bundles.open","bundles.open");
190         responseData.put("data",articleData);
191
192         String emailAnArticleTemplate = configuration.getString("Localizer.OpenSession.email.MailTemplate");
193
194         Generator generator = MirGlobal.localizer().generators().makeOpenPostingGeneratorLibrary().makeGenerator(emailAnArticleTemplate);
195
196         StringWriter theEmailStringWriter = new StringWriter();
197         PrintWriter theEmailPrintWriter = new PrintWriter(theEmailStringWriter);
198         generator.generate(theEmailPrintWriter, responseData, logger);
199
200         theEmailStringWriter.close();
201
202         theText = theEmailStringWriter.toString();
203         MirGlobal.mruCache().storeObject(theCacheKey, theText);
204       }
205       catch (Throwable e) {
206         throw new SessionExc("Couldn't get content for article " + aid + language + ": " + e.getMessage());
207       }
208     }
209
210     return theText;
211   }
212
213   protected String getExtraEmailHeaders(Request aRequest,String to,String from) throws SessionExc {
214
215     String headers = "To: " + to + "\nReply-To: "+ from+"\n";;
216     if (configuration.getString("Localizer.OpenSession.email.includeSenderIP","no").equals("yes"))
217       headers= headers+"X-Originating-IP: "+ ((HTTPRequestAdapter)aRequest).getRequest().getRemoteAddr() + "\n";
218
219     return headers;
220   }
221
222   protected String interpolateComment(String emailText,String comment,String from_name,String language) throws SessionExc{
223     if (comment != null) {
224       String commentTextToInsert =
225           MirGlobal.getBundleFactory().getBundle("etc/bundles/open", new String[] { language }).
226             getValue("email.comment.intro" , Collections.singletonList(from_name)) + "\n";
227
228       try {
229         emailText = StringRoutines.performRegularExpressionReplacement(emailText, "!COMMENT!", commentTextToInsert);
230       }
231       catch (Throwable e) {
232         throw new SessionExc("Problem doing regular expression replacement :" + e.toString());
233       }
234     }
235     else {
236       try {
237         emailText = StringRoutines.performRegularExpressionReplacement(emailText, "!COMMENT!", "");
238       }
239       catch (Throwable e) {
240         throw new SessionExc("Problem doing regular expression replacement " + e.toString());
241       }
242     }
243     return emailText;
244   }
245
246   protected boolean doTheSMTP(String aMessage,String aTo,String aFrom) throws SessionExc{
247    SMTPClient client=new SMTPClient();
248    try {
249      int reply;
250      client.connect(configuration.getString("ServletModule.OpenIndy.SMTPServer"));
251      reply = client.getReplyCode();
252
253      if (!SMTPReply.isPositiveCompletion(reply)) {
254        client.disconnect();
255         throw new SessionExc("SMTP server refused connection.");
256      }
257      boolean trueIfItWorked = client.sendSimpleMessage(configuration.getString("ServletModule.OpenIndy.EmailIsFrom"), aTo, aMessage);
258      client.disconnect();
259      // mission accomplished??
260      if (! trueIfItWorked)
261        throw new SessionExc(client.getReplyString());
262      else
263        return trueIfItWorked;
264    }
265    catch(IOException e) {
266       if(client.isConnected()) {
267         try {
268           client.disconnect();
269         } catch(IOException f) {
270           // do nothing
271         }
272       }
273       throw new SessionExc(e.getMessage());
274    }
275   }
276
277   protected boolean sendMail(Request aRequest,Session aSession,Response aResponse) throws SessionExc,SessionFailure {
278     String to=aRequest.getParameter("mail_to");
279     String from=aRequest.getParameter("mail_from");
280     String from_name=aRequest.getParameter("mail_from_name");
281     String language=aRequest.getParameter("mail_language");
282     String comment=aRequest.getParameter("mail_comment");
283
284     String theEmailText=getEmailText((String) aSession.getAttribute("email.aid"),language);
285     String headers=getExtraEmailHeaders(aRequest,to,from);
286     theEmailText=interpolateComment(theEmailText,comment,from_name,language);
287     String message=headers+theEmailText;  // the space between headers and content is in the template
288
289     return doTheSMTP(message,to,from);
290
291   }
292
293
294   protected void subsequentRequest(Request aRequest, Session aSession, Response aResponse) throws SessionExc, SessionFailure {
295     List validationErrors = new Vector();
296     if (shouldSendMail(aRequest,aSession,aResponse,validationErrors)){
297
298       sendMail(aRequest,aSession,aResponse);
299       aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.email.DoneTemplate"));
300     }
301     else {
302       aResponse.setResponseValue("mail_comment",aRequest.getParameter("mail_comment"));  //everything else is required
303       aResponse.setResponseValue("errors",validationErrors);
304       aResponse.setResponseGenerator(configuration.getString("Localizer.OpenSession.email.PrepareTemplate"));
305     }
306
307   }
308
309       /*
310       String mail_language = aRequest.getParameter("mail_language");
311       if (mail_language == null)
312         mail_language = configuration.getString("Mir.Login.DefaultLanguage", "en");
313
314       aResponse.setResponseValue("mail_language",mail_language);
315       */
316
317
318
319
320 }