added do=mail
authorjohn <john>
Sun, 12 Jan 2003 22:25:18 +0000 (22:25 +0000)
committerjohn <john>
Sun, 12 Jan 2003 22:25:18 +0000 (22:25 +0000)
source/mircoders/servlet/ServletModuleOpenIndy.java

index 4ed48f6..56a4fdf 100755 (executable)
@@ -44,6 +44,8 @@ import freemarker.template.*;
 import com.oreilly.servlet.multipart.*;\r
 import com.oreilly.servlet.*;\r
 \r
+import org.apache.commons.net.smtp.*;\r
+\r
 import org.xml.sax.InputSource;\r
 import org.xml.sax.XMLReader;\r
 \r
@@ -67,6 +69,7 @@ import mir.entity.*;
 import mir.storage.*;\r
 import mir.media.*;\r
 import mir.log.*;\r
+import mir.util.StringRoutines;\r
 \r
 import mircoders.entity.*;\r
 import mircoders.storage.*;\r
@@ -84,7 +87,7 @@ import mircoders.search.*;
  *    open-postings to the newswire\r
  *\r
  * @author mir-coders group\r
- * @version $Id: ServletModuleOpenIndy.java,v 1.52 2002/12/23 03:43:46 mh Exp $\r
+ * @version $Id: ServletModuleOpenIndy.java,v 1.53 2003/01/12 22:25:18 john Exp $\r
  *\r
  */\r
 \r
@@ -96,6 +99,7 @@ public class ServletModuleOpenIndy extends ServletModule
   private String        postingFormTemplate, postingFormDoneTemplate,\r
     postingFormDupeTemplate;\r
   private String        searchResultsTemplate;\r
+  private String        prepareMailTemplate,sentMailTemplate;\r
   private ModuleContent contentModule;\r
   private ModuleComment commentModule;\r
   private ModuleImages  imageModule;\r
@@ -117,6 +121,8 @@ public class ServletModuleOpenIndy extends ServletModule
       postingFormDoneTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDoneTemplate");\r
       postingFormDupeTemplate = MirConfig.getProp("ServletModule.OpenIndy.PostingDupeTemplate");\r
       searchResultsTemplate = MirConfig.getProp("ServletModule.OpenIndy.SearchResultsTemplate");\r
+      prepareMailTemplate = MirConfig.getProp("ServletModule.OpenIndy.PrepareMailTemplate");\r
+      sentMailTemplate = MirConfig.getProp("ServletModule.OpenIndy.SentMailTemplate");\r
       directOp = MirConfig.getProp("DirectOpenposting").toLowerCase();\r
       passwdProtection = MirConfig.getProp("PasswdProtection").toLowerCase();\r
       mainModule = new ModuleComment(DatabaseComment.getInstance());\r
@@ -435,6 +441,134 @@ public class ServletModuleOpenIndy extends ServletModule
     deliver(req, res, mergeData, postingFormDoneTemplate);\r
   }\r
 \r
+    /*\r
+   * Method for preparing and sending a content as an email message\r
+   */\r
+  \r
+  public void mail(HttpServletRequest req, HttpServletResponse res)\r
+    throws ServletModuleException, ServletModuleUserException {\r
+    String aid = req.getParameter("mail_aid");\r
+    if (aid == null){\r
+      throw new ServletModuleUserException("An article id must be specified in requests to email an article.  Something therefore went badly wrong....");\r
+    }\r
+    \r
+    String to = req.getParameter("mail_to");\r
+    String from = req.getParameter("mail_from");\r
+    String from_name = req.getParameter("mail_from_name");\r
+    String comment = req.getParameter("mail_comment");\r
+    String mail_language = req.getParameter("mail_language");\r
+    \r
+    SimpleHash mergeData = new SimpleHash();\r
+    \r
+    if (to == null || from == null || from_name == null|| to.equals("") || from.equals("") || from_name.equals("") || mail_language == null || mail_language.equals("")){\r
+\r
+      for (Enumeration theParams = req.getParameterNames(); theParams.hasMoreElements() ;) {\r
+       String pName=(String)theParams.nextElement();\r
+       if (pName.startsWith("mail_")){\r
+         mergeData.put(pName,new SimpleScalar(req.getParameter(pName)));\r
+       }\r
+      }\r
+      deliver(req,res,mergeData,prepareMailTemplate); \r
+    }\r
+    else {\r
+      //run checks on to and from and mail_language to make sure no monkey business occurring\r
+      if (mail_language.indexOf('.') != -1 || mail_language.indexOf('/') != -1 ){\r
+       throw new ServletModuleUserException("Sorry, you've entered an illegal character into the language field.  Go back and try again, asshole.");\r
+      }\r
+      if (to.indexOf('\n') != -1 \r
+         || to.indexOf('\r') != -1 \r
+         || to.indexOf(',') != -1 \r
+         || from.indexOf('\n') != -1 \r
+         || from.indexOf('\r') != -1 \r
+         || from.indexOf(',') != -1 ){\r
+       throw new ServletModuleUserException("Sorry, you've entered an illegal character into the from or to field.  Go back and try again.");\r
+      }\r
+      EntityContent contentEnt;\r
+      try{\r
+       contentEnt = (EntityContent)contentModule.getById(aid);\r
+      }\r
+      catch (ModuleException e){ \r
+       throw new ServletModuleUserException("Couldn't get content for article "+aid);\r
+      }\r
+      String producerStorageRoot=MirConfig.getProp("Producer.StorageRoot");\r
+      String producerDocRoot=MirConfig.getProp("Producer.DocRoot");\r
+      String publishPath = contentEnt.getValue("publish_path");\r
+      String txtFilePath = producerStorageRoot + producerDocRoot + "/" + mail_language + \r
+        publishPath + "/" + aid + ".txt";\r
+      \r
+\r
+      File inputFile = new File(txtFilePath);\r
+      String content;\r
+      \r
+      try{\r
+       FileReader in = new FileReader(inputFile);\r
+       StringWriter out = new StringWriter();\r
+       int c;\r
+       while ((c = in.read()) != -1)\r
+         out.write(c);\r
+       in.close();\r
+       content= out.toString();\r
+      }\r
+      catch (FileNotFoundException e){\r
+       throw new ServletModuleUserException("No text file found in " + txtFilePath);\r
+      }\r
+      catch (IOException e){\r
+       throw new ServletModuleUserException("Problem reading file in " + txtFilePath);\r
+      }\r
+      // add some headers\r
+      content = "To: " + to + "\nReply-To: "+ from + "\n" + content;\r
+      // put in the comment where it should go\r
+      if (comment != null) { \r
+       String commentTextToInsert = "\n\nAttached comment from " + from_name + ":\n" + comment;\r
+       try {\r
+         content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!",commentTextToInsert);\r
+       }\r
+       catch (Exception e){\r
+         throw new ServletModuleUserException("Problem doing regular expression replacement " + e.toString());\r
+       }\r
+      }\r
+      else{\r
+       try {\r
+         content=StringRoutines.performRegularExpressionReplacement(content,"!COMMENT!","");\r
+       }\r
+       catch (Exception e){\r
+         throw new ServletModuleUserException("Problem doing regular expression replacement " + e.toString());\r
+       }\r
+      }\r
+      \r
+      SMTPClient client=new SMTPClient();\r
+      try {\r
+       int reply;\r
+       client.connect("localhost");\r
+       System.out.print(client.getReplyString());\r
+       \r
+       reply = client.getReplyCode();\r
+       \r
+       if(!SMTPReply.isPositiveCompletion(reply)) {\r
+         client.disconnect();\r
+         throw new ServletModuleUserException("SMTP server refused connection.");\r
+       }\r
+       \r
+       client.sendSimpleMessage(MirConfig.getProp("ServletModule.OpenIndy.EmailIsFrom"),to,content);\r
+       \r
+       client.disconnect();\r
+       //mission accomplished\r
+       deliver(req,res,mergeData,sentMailTemplate); \r
+\r
+      } catch(IOException e) {\r
+       if(client.isConnected()) {\r
+         try {\r
+           client.disconnect();\r
+         } catch(IOException f) {\r
+           // do nothing\r
+         }\r
+       }\r
+       throw new ServletModuleUserException(e.toString());\r
+      }\r
+    }\r
+  }\r
+\r
+\r
   /*\r
    * Method for querying a lucene index\r
    */\r