org/sonews/daemon/command/OverCommand.java
changeset 35 ed84c8bdd87b
parent 34 9f0b95aafaa3
child 36 c404a87db5b7
     1.1 --- a/org/sonews/daemon/command/OverCommand.java	Sun Aug 29 17:04:25 2010 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,294 +0,0 @@
     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.util.List;
    1.26 -import org.sonews.util.Log;
    1.27 -import org.sonews.daemon.NNTPConnection;
    1.28 -import org.sonews.storage.Article;
    1.29 -import org.sonews.storage.ArticleHead;
    1.30 -import org.sonews.storage.Headers;
    1.31 -import org.sonews.storage.StorageBackendException;
    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 implements Command
   1.113 -{
   1.114 -
   1.115 -  public static final int MAX_LINES_PER_DBREQUEST = 200;
   1.116 -
   1.117 -  @Override
   1.118 -  public String[] getSupportedCommandStrings()
   1.119 -  {
   1.120 -    return new String[]{"OVER", "XOVER"};
   1.121 -  }
   1.122 -
   1.123 -  @Override
   1.124 -  public boolean hasFinished()
   1.125 -  {
   1.126 -    return true;
   1.127 -  }
   1.128 -
   1.129 -  @Override
   1.130 -  public String impliedCapability()
   1.131 -  {
   1.132 -    return null;
   1.133 -  }
   1.134 -
   1.135 -  @Override
   1.136 -  public boolean isStateful()
   1.137 -  {
   1.138 -    return false;
   1.139 -  }
   1.140 -
   1.141 -  @Override
   1.142 -  public void processLine(NNTPConnection conn, final String line, byte[] raw)
   1.143 -    throws IOException, StorageBackendException
   1.144 -  {
   1.145 -    if(conn.getCurrentChannel() == null)
   1.146 -    {
   1.147 -      conn.println("412 no newsgroup selected");
   1.148 -    }
   1.149 -    else
   1.150 -    {
   1.151 -      String[] command = line.split(" ");
   1.152 -
   1.153 -      // If no parameter was specified, show information about
   1.154 -      // the currently selected article(s)
   1.155 -      if(command.length == 1)
   1.156 -      {
   1.157 -        final Article art = conn.getCurrentArticle();
   1.158 -        if(art == null)
   1.159 -        {
   1.160 -          conn.println("420 no article(s) selected");
   1.161 -          return;
   1.162 -        }
   1.163 -
   1.164 -        conn.println(buildOverview(art, -1));
   1.165 -      }
   1.166 -      // otherwise print information about the specified range
   1.167 -      else
   1.168 -      {
   1.169 -        long artStart;
   1.170 -        long artEnd   = conn.getCurrentChannel().getLastArticleNumber();
   1.171 -        String[] nums = command[1].split("-");
   1.172 -        if(nums.length >= 1)
   1.173 -        {
   1.174 -          try
   1.175 -          {
   1.176 -            artStart = Integer.parseInt(nums[0]);
   1.177 -          }
   1.178 -          catch(NumberFormatException e) 
   1.179 -          {
   1.180 -            Log.get().info(e.getMessage());
   1.181 -            artStart = Integer.parseInt(command[1]);
   1.182 -          }
   1.183 -        }
   1.184 -        else
   1.185 -        {
   1.186 -          artStart = conn.getCurrentChannel().getFirstArticleNumber();
   1.187 -        }
   1.188 -
   1.189 -        if(nums.length >=2)
   1.190 -        {
   1.191 -          try
   1.192 -          {
   1.193 -            artEnd = Integer.parseInt(nums[1]);
   1.194 -          }
   1.195 -          catch(NumberFormatException e) 
   1.196 -          {
   1.197 -            e.printStackTrace();
   1.198 -          }
   1.199 -        }
   1.200 -
   1.201 -        if(artStart > artEnd)
   1.202 -        {
   1.203 -          if(command[0].equalsIgnoreCase("OVER"))
   1.204 -          {
   1.205 -            conn.println("423 no articles in that range");
   1.206 -          }
   1.207 -          else
   1.208 -          {
   1.209 -            conn.println("224 (empty) overview information follows:");
   1.210 -            conn.println(".");
   1.211 -          }
   1.212 -        }
   1.213 -        else
   1.214 -        {
   1.215 -          for(long n = artStart; n <= artEnd; n += MAX_LINES_PER_DBREQUEST)
   1.216 -          {
   1.217 -            long nEnd = Math.min(n + MAX_LINES_PER_DBREQUEST - 1, artEnd);
   1.218 -            List<Pair<Long, ArticleHead>> articleHeads = conn.getCurrentChannel()
   1.219 -              .getArticleHeads(n, nEnd);
   1.220 -            if(articleHeads.isEmpty() && n == artStart
   1.221 -              && command[0].equalsIgnoreCase("OVER"))
   1.222 -            {
   1.223 -              // This reply is only valid for OVER, not for XOVER command
   1.224 -              conn.println("423 no articles in that range");
   1.225 -              return;
   1.226 -            }
   1.227 -            else if(n == artStart)
   1.228 -            {
   1.229 -              // XOVER replies this although there is no data available
   1.230 -              conn.println("224 overview information follows");
   1.231 -            }
   1.232 -
   1.233 -            for(Pair<Long, ArticleHead> article : articleHeads)
   1.234 -            {
   1.235 -              String overview = buildOverview(article.getB(), article.getA());
   1.236 -              conn.println(overview);
   1.237 -            }
   1.238 -          } // for
   1.239 -          conn.println(".");
   1.240 -        }
   1.241 -      }
   1.242 -    }
   1.243 -  }
   1.244 -  
   1.245 -  private String buildOverview(ArticleHead art, long nr)
   1.246 -  {
   1.247 -    StringBuilder overview = new StringBuilder();
   1.248 -    overview.append(nr);
   1.249 -    overview.append('\t');
   1.250 -
   1.251 -    String subject = art.getHeader(Headers.SUBJECT)[0];
   1.252 -    if("".equals(subject))
   1.253 -    {
   1.254 -      subject = "<empty>";
   1.255 -    }
   1.256 -    overview.append(escapeString(subject));
   1.257 -    overview.append('\t');
   1.258 -
   1.259 -    overview.append(escapeString(art.getHeader(Headers.FROM)[0]));
   1.260 -    overview.append('\t');
   1.261 -    overview.append(escapeString(art.getHeader(Headers.DATE)[0]));
   1.262 -    overview.append('\t');
   1.263 -    overview.append(escapeString(art.getHeader(Headers.MESSAGE_ID)[0]));
   1.264 -    overview.append('\t');
   1.265 -    overview.append(escapeString(art.getHeader(Headers.REFERENCES)[0]));
   1.266 -    overview.append('\t');
   1.267 -
   1.268 -    String bytes = art.getHeader(Headers.BYTES)[0];
   1.269 -    if("".equals(bytes))
   1.270 -    {
   1.271 -      bytes = "0";
   1.272 -    }
   1.273 -    overview.append(escapeString(bytes));
   1.274 -    overview.append('\t');
   1.275 -
   1.276 -    String lines = art.getHeader(Headers.LINES)[0];
   1.277 -    if("".equals(lines))
   1.278 -    {
   1.279 -      lines = "0";
   1.280 -    }
   1.281 -    overview.append(escapeString(lines));
   1.282 -    overview.append('\t');
   1.283 -    overview.append(escapeString(art.getHeader(Headers.XREF)[0]));
   1.284 -
   1.285 -    // Remove trailing tabs if some data is empty
   1.286 -    return overview.toString().trim();
   1.287 -  }
   1.288 -  
   1.289 -  private String escapeString(String str)
   1.290 -  {
   1.291 -    String nstr = str.replace("\r", "");
   1.292 -    nstr = nstr.replace('\n', ' ');
   1.293 -    nstr = nstr.replace('\t', ' ');
   1.294 -    return nstr.trim();
   1.295 -  }
   1.296 -  
   1.297 -}