org/sonews/daemon/command/PostCommand.java
author cli
Wed Aug 12 13:03:23 2009 +0200 (2009-08-12)
changeset 7 0b76e099eb96
parent 1 6fceb66e1ad7
child 12 bb6990c0dd1a
permissions -rw-r--r--
PullFeeder sends an addition "MODE READER" to peers.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.daemon.command;
    20 
    21 import java.io.IOException;
    22 import java.io.ByteArrayInputStream;
    23 import java.io.ByteArrayOutputStream;
    24 import java.nio.charset.Charset;
    25 import java.nio.charset.IllegalCharsetNameException;
    26 import java.nio.charset.UnsupportedCharsetException;
    27 import java.sql.SQLException;
    28 import java.util.Arrays;
    29 import java.util.Locale;
    30 import javax.mail.MessagingException;
    31 import javax.mail.internet.AddressException;
    32 import javax.mail.internet.InternetHeaders;
    33 import org.sonews.config.Config;
    34 import org.sonews.util.Log;
    35 import org.sonews.mlgw.Dispatcher;
    36 import org.sonews.storage.Article;
    37 import org.sonews.storage.Group;
    38 import org.sonews.daemon.NNTPConnection;
    39 import org.sonews.storage.Headers;
    40 import org.sonews.storage.StorageBackendException;
    41 import org.sonews.storage.StorageManager;
    42 import org.sonews.feed.FeedManager;
    43 import org.sonews.util.Stats;
    44 
    45 /**
    46  * Implementation of the POST command. This command requires multiple lines
    47  * from the client, so the handling of asynchronous reading is a little tricky
    48  * to handle.
    49  * @author Christian Lins
    50  * @since sonews/0.5.0
    51  */
    52 public class PostCommand implements Command
    53 {
    54   
    55   private final Article article   = new Article();
    56   private int           lineCount = 0;
    57   private long          bodySize  = 0;
    58   private InternetHeaders headers = null;
    59   private long          maxBodySize  = 
    60     Config.inst().get(Config.ARTICLE_MAXSIZE, 128) * 1024L; // Size in bytes
    61   private PostState     state     = PostState.WaitForLineOne;
    62   private final ByteArrayOutputStream bufBody   = new ByteArrayOutputStream();
    63   private final StringBuilder         strHead   = new StringBuilder();
    64 
    65   @Override
    66   public String[] getSupportedCommandStrings()
    67   {
    68     return new String[]{"POST"};
    69   }
    70 
    71   @Override
    72   public boolean hasFinished()
    73   {
    74     return this.state == PostState.Finished;
    75   }
    76 
    77   @Override
    78   public boolean isStateful()
    79   {
    80     return true;
    81   }
    82 
    83   /**
    84    * Process the given line String. line.trim() was called by NNTPConnection.
    85    * @param line
    86    * @throws java.io.IOException
    87    * @throws java.sql.SQLException
    88    */
    89   @Override // TODO: Refactor this method to reduce complexity!
    90   public void processLine(NNTPConnection conn, String line, byte[] raw)
    91     throws IOException, StorageBackendException
    92   {
    93     switch(state)
    94     {
    95       case WaitForLineOne:
    96       {
    97         if(line.equalsIgnoreCase("POST"))
    98         {
    99           conn.println("340 send article to be posted. End with <CR-LF>.<CR-LF>");
   100           state = PostState.ReadingHeaders;
   101         }
   102         else
   103         {
   104           conn.println("500 invalid command usage");
   105         }
   106         break;
   107       }
   108       case ReadingHeaders:
   109       {
   110         strHead.append(line);
   111         strHead.append(NNTPConnection.NEWLINE);
   112         
   113         if("".equals(line) || ".".equals(line))
   114         {
   115           // we finally met the blank line
   116           // separating headers from body
   117           
   118           try
   119           {
   120             // Parse the header using the InternetHeader class from JavaMail API
   121             headers = new InternetHeaders(
   122               new ByteArrayInputStream(strHead.toString().trim()
   123                 .getBytes(conn.getCurrentCharset())));
   124 
   125             // add the header entries for the article
   126             article.setHeaders(headers);
   127           }
   128           catch (MessagingException e)
   129           {
   130             e.printStackTrace();
   131             conn.println("500 posting failed - invalid header");
   132             state = PostState.Finished;
   133             break;
   134           }
   135 
   136           // Change charset for reading body; 
   137           // for multipart messages UTF-8 is returned
   138           //conn.setCurrentCharset(article.getBodyCharset());
   139           
   140           state = PostState.ReadingBody;
   141           
   142           if(".".equals(line))
   143           {
   144             // Post an article without body
   145             postArticle(conn, article);
   146             state = PostState.Finished;
   147           }
   148         }
   149         break;
   150       }
   151       case ReadingBody:
   152       {
   153         if(".".equals(line))
   154         {    
   155           // Set some headers needed for Over command
   156           headers.setHeader(Headers.LINES, Integer.toString(lineCount));
   157           headers.setHeader(Headers.BYTES, Long.toString(bodySize));
   158 
   159           byte[] body = bufBody.toByteArray();
   160           if(body.length >= 2)
   161           {
   162             // Remove trailing CRLF
   163             body = Arrays.copyOf(body, body.length - 2);
   164           }
   165           article.setBody(body); // set the article body
   166           
   167           postArticle(conn, article);
   168           state = PostState.Finished;
   169         }
   170         else
   171         {
   172           bodySize += line.length() + 1;
   173           lineCount++;
   174           
   175           // Add line to body buffer
   176           bufBody.write(raw, 0, raw.length);
   177           bufBody.write(NNTPConnection.NEWLINE.getBytes());
   178           
   179           if(bodySize > maxBodySize)
   180           {
   181             conn.println("500 article is too long");
   182             state = PostState.Finished;
   183             break;
   184           }
   185           
   186           // Check if this message is a MIME-multipart message and needs a
   187           // charset change
   188           /*try
   189           {
   190             line = line.toLowerCase(Locale.ENGLISH);
   191             if(line.startsWith(Headers.CONTENT_TYPE))
   192             {
   193               int idxStart = line.indexOf("charset=") + "charset=".length();
   194               int idxEnd   = line.indexOf(";", idxStart);
   195               if(idxEnd < 0)
   196               {
   197                 idxEnd = line.length();
   198               }
   199 
   200               if(idxStart > 0)
   201               {
   202                 String charsetName = line.substring(idxStart, idxEnd);
   203                 if(charsetName.length() > 0 && charsetName.charAt(0) == '"')
   204                 {
   205                   charsetName = charsetName.substring(1, charsetName.length() - 1);
   206                 }
   207 
   208                 try
   209                 {
   210                   conn.setCurrentCharset(Charset.forName(charsetName));
   211                 }
   212                 catch(IllegalCharsetNameException ex)
   213                 {
   214                   Log.msg("PostCommand: " + ex, false);
   215                 }
   216                 catch(UnsupportedCharsetException ex)
   217                 {
   218                   Log.msg("PostCommand: " + ex, false);
   219                 }
   220               } // if(idxStart > 0)
   221             }
   222           }
   223           catch(Exception ex)
   224           {
   225             ex.printStackTrace();
   226           }*/
   227         }
   228         break;
   229       }
   230       default:
   231       {
   232         // Should never happen
   233         Log.msg("PostCommand::processLine(): already finished...", false);
   234       }
   235     }
   236   }
   237   
   238   /**
   239    * Article is a control message and needs special handling.
   240    * @param article
   241    */
   242   private void controlMessage(NNTPConnection conn, Article article)
   243     throws IOException
   244   {
   245     String[] ctrl = article.getHeader(Headers.CONTROL)[0].split(" ");
   246     if(ctrl.length == 2) // "cancel <mid>"
   247     {
   248       try
   249       {
   250         StorageManager.current().delete(ctrl[1]);
   251         
   252         // Move cancel message to "control" group
   253         article.setHeader(Headers.NEWSGROUPS, "control");
   254         StorageManager.current().addArticle(article);
   255         conn.println("240 article cancelled");
   256       }
   257       catch(StorageBackendException ex)
   258       {
   259         Log.msg(ex, false);
   260         conn.println("500 internal server error");
   261       }
   262     }
   263     else
   264     {
   265       conn.println("441 unknown control header");
   266     }
   267   }
   268   
   269   private void supersedeMessage(NNTPConnection conn, Article article)
   270     throws IOException
   271   {
   272     try
   273     {
   274       String oldMsg = article.getHeader(Headers.SUPERSEDES)[0];
   275       StorageManager.current().delete(oldMsg);
   276       StorageManager.current().addArticle(article);
   277       conn.println("240 article replaced");
   278     }
   279     catch(StorageBackendException ex)
   280     {
   281       Log.msg(ex, false);
   282       conn.println("500 internal server error");
   283     }
   284   }
   285   
   286   private void postArticle(NNTPConnection conn, Article article)
   287     throws IOException
   288   {
   289     if(article.getHeader(Headers.CONTROL)[0].length() > 0)
   290     {
   291       controlMessage(conn, article);
   292     }
   293     else if(article.getHeader(Headers.SUPERSEDES)[0].length() > 0)
   294     {
   295       supersedeMessage(conn, article);
   296     }
   297     else // Post the article regularily
   298     {
   299       // Try to create the article in the database or post it to
   300       // appropriate mailing list
   301       try
   302       {
   303         boolean success = false;
   304         String[] groupnames = article.getHeader(Headers.NEWSGROUPS)[0].split(",");
   305         for(String groupname : groupnames)
   306         {
   307           Group group = StorageManager.current().getGroup(groupname);
   308           if(group != null && !group.isDeleted())
   309           {
   310             if(group.isMailingList() && !conn.isLocalConnection())
   311             {
   312               // Send to mailing list; the Dispatcher writes 
   313               // statistics to database
   314               Dispatcher.toList(article);
   315               success = true;
   316             }
   317             else
   318             {
   319               // Store in database
   320               if(!StorageManager.current().isArticleExisting(article.getMessageID()))
   321               {
   322                 StorageManager.current().addArticle(article);
   323 
   324                 // Log this posting to statistics
   325                 Stats.getInstance().mailPosted(
   326                   article.getHeader(Headers.NEWSGROUPS)[0]);
   327               }
   328               success = true;
   329             }
   330           }
   331         } // end for
   332 
   333         if(success)
   334         {
   335           conn.println("240 article posted ok");
   336           FeedManager.queueForPush(article);
   337         }
   338         else
   339         {
   340           conn.println("441 newsgroup not found");
   341         }
   342       }
   343       catch(AddressException ex)
   344       {
   345         Log.msg(ex.getMessage(), true);
   346         conn.println("441 invalid sender address");
   347       }
   348       catch(MessagingException ex)
   349       {
   350         // A MessageException is thrown when the sender email address is
   351         // invalid or something is wrong with the SMTP server.
   352         System.err.println(ex.getLocalizedMessage());
   353         conn.println("441 " + ex.getClass().getCanonicalName() + ": " + ex.getLocalizedMessage());
   354       }
   355       catch(StorageBackendException ex)
   356       {
   357         ex.printStackTrace();
   358         conn.println("500 internal server error");
   359       }
   360     }
   361   }
   362 
   363 }