org/sonews/daemon/command/OverCommand.java
changeset 1 6fceb66e1ad7
child 3 2fdc9cc89502
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/org/sonews/daemon/command/OverCommand.java	Fri Jun 26 16:48:50 2009 +0200
     1.3 @@ -0,0 +1,281 @@
     1.4 +/*
     1.5 + *   SONEWS News Server
     1.6 + *   see AUTHORS for the list of contributors
     1.7 + *
     1.8 + *   This program is free software: you can redistribute it and/or modify
     1.9 + *   it under the terms of the GNU General Public License as published by
    1.10 + *   the Free Software Foundation, either version 3 of the License, or
    1.11 + *   (at your option) any later version.
    1.12 + *
    1.13 + *   This program is distributed in the hope that it will be useful,
    1.14 + *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 + *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 + *   GNU General Public License for more details.
    1.17 + *
    1.18 + *   You should have received a copy of the GNU General Public License
    1.19 + *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 + */
    1.21 +
    1.22 +package org.sonews.daemon.command;
    1.23 +
    1.24 +import java.io.IOException;
    1.25 +import java.sql.SQLException;
    1.26 +import java.util.List;
    1.27 +import org.sonews.util.Log;
    1.28 +import org.sonews.daemon.NNTPConnection;
    1.29 +import org.sonews.daemon.storage.Article;
    1.30 +import org.sonews.daemon.storage.ArticleHead;
    1.31 +import org.sonews.daemon.storage.Headers;
    1.32 +import org.sonews.util.Pair;
    1.33 +
    1.34 +/**
    1.35 + * Class handling the OVER/XOVER command.
    1.36 + * 
    1.37 + * Description of the XOVER command:
    1.38 + * <pre>
    1.39 + * XOVER [range]
    1.40 + *
    1.41 + * The XOVER command returns information from the overview
    1.42 + * database for the article(s) specified.
    1.43 + *
    1.44 + * The optional range argument may be any of the following:
    1.45 + *              an article number
    1.46 + *              an article number followed by a dash to indicate
    1.47 + *                 all following
    1.48 + *              an article number followed by a dash followed by
    1.49 + *                 another article number
    1.50 + *
    1.51 + * If no argument is specified, then information from the
    1.52 + * current article is displayed. Successful responses start
    1.53 + * with a 224 response followed by the overview information
    1.54 + * for all matched messages. Once the output is complete, a
    1.55 + * period is sent on a line by itself. If no argument is
    1.56 + * specified, the information for the current article is
    1.57 + * returned.  A news group must have been selected earlier,
    1.58 + * else a 412 error response is returned. If no articles are
    1.59 + * in the range specified, a 420 error response is returned
    1.60 + * by the server. A 502 response will be returned if the
    1.61 + * client only has permission to transfer articles.
    1.62 + *
    1.63 + * Each line of output will be formatted with the article number,
    1.64 + * followed by each of the headers in the overview database or the
    1.65 + * article itself (when the data is not available in the overview
    1.66 + * database) for that article separated by a tab character.  The
    1.67 + * sequence of fields must be in this order: subject, author,
    1.68 + * date, message-id, references, byte count, and line count. Other
    1.69 + * optional fields may follow line count. Other optional fields may
    1.70 + * follow line count. These fields are specified by examining the
    1.71 + * response to the LIST OVERVIEW.FMT command. Where no data exists,
    1.72 + * a null field must be provided (i.e. the output will have two tab
    1.73 + * characters adjacent to each other). Servers should not output
    1.74 + * fields for articles that have been removed since the XOVER database
    1.75 + * was created.
    1.76 + *
    1.77 + * The LIST OVERVIEW.FMT command should be implemented if XOVER
    1.78 + * is implemented. A client can use LIST OVERVIEW.FMT to determine
    1.79 + * what optional fields  and in which order all fields will be
    1.80 + * supplied by the XOVER command. 
    1.81 + *
    1.82 + * Note that any tab and end-of-line characters in any header
    1.83 + * data that is returned will be converted to a space character.
    1.84 + *
    1.85 + * Responses:
    1.86 + *
    1.87 + *   224 Overview information follows
    1.88 + *   412 No news group current selected
    1.89 + *   420 No article(s) selected
    1.90 + *   502 no permission
    1.91 + *
    1.92 + * OVER defines additional responses:
    1.93 + *
    1.94 + *  First form (message-id specified)
    1.95 + *    224    Overview information follows (multi-line)
    1.96 + *    430    No article with that message-id
    1.97 + *
    1.98 + *  Second form (range specified)
    1.99 + *    224    Overview information follows (multi-line)
   1.100 + *    412    No newsgroup selected
   1.101 + *    423    No articles in that range
   1.102 + *
   1.103 + *  Third form (current article number used)
   1.104 + *    224    Overview information follows (multi-line)
   1.105 + *    412    No newsgroup selected
   1.106 + *    420    Current article number is invalid
   1.107 + *
   1.108 + * </pre>
   1.109 + * @author Christian Lins
   1.110 + * @since sonews/0.5.0
   1.111 + */
   1.112 +public class OverCommand extends AbstractCommand
   1.113 +{
   1.114 +
   1.115 +  public static final int MAX_LINES_PER_DBREQUEST = 100;
   1.116 +  
   1.117 +  public OverCommand(final NNTPConnection conn)
   1.118 +  {
   1.119 +    super(conn);
   1.120 +  }
   1.121 +
   1.122 +  @Override
   1.123 +  public boolean hasFinished()
   1.124 +  {
   1.125 +    return true;
   1.126 +  }
   1.127 +
   1.128 +  @Override
   1.129 +  public void processLine(final String line)
   1.130 +    throws IOException, SQLException
   1.131 +  {
   1.132 +    if(getCurrentGroup() == null)
   1.133 +    {
   1.134 +      printStatus(412, "No news group current selected");
   1.135 +    }
   1.136 +    else
   1.137 +    {
   1.138 +      String[] command = line.split(" ");
   1.139 +
   1.140 +      // If no parameter was specified, show information about
   1.141 +      // the currently selected article(s)
   1.142 +      if(command.length == 1)
   1.143 +      {
   1.144 +        final Article art = getCurrentArticle();
   1.145 +        if(art == null)
   1.146 +        {
   1.147 +          printStatus(420, "No article(s) selected");
   1.148 +          return;
   1.149 +        }
   1.150 +
   1.151 +        println(buildOverview(art, -1));
   1.152 +      }
   1.153 +      // otherwise print information about the specified range
   1.154 +      else
   1.155 +      {
   1.156 +        int artStart;
   1.157 +        int artEnd   = getCurrentGroup().getLastArticleNumber();
   1.158 +        String[] nums = command[1].split("-");
   1.159 +        if(nums.length >= 1)
   1.160 +        {
   1.161 +          try
   1.162 +          {
   1.163 +            artStart = Integer.parseInt(nums[0]);
   1.164 +          }
   1.165 +          catch(NumberFormatException e) 
   1.166 +          {
   1.167 +            Log.msg(e.getMessage(), true);
   1.168 +            artStart = Integer.parseInt(command[1]);
   1.169 +          }
   1.170 +        }
   1.171 +        else
   1.172 +        {
   1.173 +          artStart = getCurrentGroup().getFirstArticleNumber();
   1.174 +        }
   1.175 +
   1.176 +        if(nums.length >=2)
   1.177 +        {
   1.178 +          try
   1.179 +          {
   1.180 +            artEnd = Integer.parseInt(nums[1]);
   1.181 +          }
   1.182 +          catch(NumberFormatException e) 
   1.183 +          {
   1.184 +            e.printStackTrace();
   1.185 +          }
   1.186 +        }
   1.187 +
   1.188 +        if(artStart > artEnd)
   1.189 +        {
   1.190 +          if(command[0].equalsIgnoreCase("OVER"))
   1.191 +          {
   1.192 +            printStatus(423, "No articles in that range");
   1.193 +          }
   1.194 +          else
   1.195 +          {
   1.196 +            printStatus(224, "(empty) overview information follows:");
   1.197 +            println(".");
   1.198 +          }
   1.199 +        }
   1.200 +        else
   1.201 +        {
   1.202 +          for(int n = artStart; n <= artEnd; n += MAX_LINES_PER_DBREQUEST)
   1.203 +          {
   1.204 +            int nEnd = Math.min(n + MAX_LINES_PER_DBREQUEST - 1, artEnd);
   1.205 +            List<Pair<Long, ArticleHead>> articleHeads = getCurrentGroup()
   1.206 +              .getArticleHeads(n, nEnd);
   1.207 +            if(articleHeads.isEmpty() && n == artStart
   1.208 +              && command[0].equalsIgnoreCase("OVER"))
   1.209 +            {
   1.210 +              // This reply is only valid for OVER, not for XOVER command
   1.211 +              printStatus(423, "No articles in that range");
   1.212 +              return;
   1.213 +            }
   1.214 +            else if(n == artStart)
   1.215 +            {
   1.216 +              // XOVER replies this although there is no data available
   1.217 +              printStatus(224, "Overview information follows");
   1.218 +            }
   1.219 +
   1.220 +            for(Pair<Long, ArticleHead> article : articleHeads)
   1.221 +            {
   1.222 +              String overview = buildOverview(article.getB(), article.getA());
   1.223 +              println(overview);
   1.224 +            }
   1.225 +          } // for
   1.226 +          println(".");
   1.227 +        }
   1.228 +      }
   1.229 +    }
   1.230 +  }
   1.231 +  
   1.232 +  private String buildOverview(ArticleHead art, long nr)
   1.233 +  {
   1.234 +    StringBuilder overview = new StringBuilder();
   1.235 +    overview.append(nr);
   1.236 +    overview.append('\t');
   1.237 +
   1.238 +    String subject = art.getHeader(Headers.SUBJECT)[0];
   1.239 +    if("".equals(subject))
   1.240 +    {
   1.241 +      subject = "<empty>";
   1.242 +    }
   1.243 +    overview.append(escapeString(subject));
   1.244 +    overview.append('\t');
   1.245 +
   1.246 +    overview.append(escapeString(art.getHeader(Headers.FROM)[0]));
   1.247 +    overview.append('\t');
   1.248 +    overview.append(escapeString(art.getHeader(Headers.DATE)[0]));
   1.249 +    overview.append('\t');
   1.250 +    overview.append(escapeString(art.getHeader(Headers.MESSAGE_ID)[0]));
   1.251 +    overview.append('\t');
   1.252 +    overview.append(escapeString(art.getHeader(Headers.REFERENCES)[0]));
   1.253 +    overview.append('\t');
   1.254 +
   1.255 +    String bytes = art.getHeader(Headers.BYTES)[0];
   1.256 +    if("".equals(bytes))
   1.257 +    {
   1.258 +      bytes = "0";
   1.259 +    }
   1.260 +    overview.append(escapeString(bytes));
   1.261 +    overview.append('\t');
   1.262 +
   1.263 +    String lines = art.getHeader(Headers.LINES)[0];
   1.264 +    if("".equals(lines))
   1.265 +    {
   1.266 +      lines = "0";
   1.267 +    }
   1.268 +    overview.append(escapeString(lines));
   1.269 +    overview.append('\t');
   1.270 +    overview.append(escapeString(art.getHeader(Headers.XREF)[0]));
   1.271 +
   1.272 +    // Remove trailing tabs if some data is empty
   1.273 +    return overview.toString().trim();
   1.274 +  }
   1.275 +  
   1.276 +  private String escapeString(String str)
   1.277 +  {
   1.278 +    String nstr = str.replace("\r", "");
   1.279 +    nstr = nstr.replace('\n', ' ');
   1.280 +    nstr = nstr.replace('\t', ' ');
   1.281 +    return nstr.trim();
   1.282 +  }
   1.283 +  
   1.284 +}