src/org/sonews/daemon/command/ListCommand.java
author cli
Mon, 30 Aug 2010 00:20:06 +0200
changeset 39 73b21e9f3958
parent 37 74139325d305
child 48 b78e77619152
permissions -rw-r--r--
Some work on XDAEMON command.
     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.util.List;
    23 import java.util.regex.Matcher;
    24 import java.util.regex.Pattern;
    25 import java.util.regex.PatternSyntaxException;
    26 import org.sonews.daemon.NNTPConnection;
    27 import org.sonews.storage.Channel;
    28 import org.sonews.storage.StorageBackendException;
    29 import org.sonews.util.Log;
    30 
    31 /**
    32  * Class handling the LIST command.
    33  * @author Christian Lins
    34  * @author Dennis Schwerdel
    35  * @since n3tpd/0.1
    36  */
    37 public class ListCommand implements Command
    38 {
    39 
    40 	@Override
    41 	public String[] getSupportedCommandStrings()
    42 	{
    43 		return new String[] {"LIST"};
    44 	}
    45 
    46 	@Override
    47 	public boolean hasFinished()
    48 	{
    49 		return true;
    50 	}
    51 
    52 	@Override
    53 	public String impliedCapability()
    54 	{
    55 		return null;
    56 	}
    57 
    58 	@Override
    59 	public boolean isStateful()
    60 	{
    61 		return false;
    62 	}
    63 
    64 	@Override
    65 	public void processLine(NNTPConnection conn, final String line, byte[] raw)
    66 		throws IOException, StorageBackendException
    67 	{
    68 		final String[] command = line.split(" ");
    69 
    70 		if (command.length >= 2) {
    71 			if (command[1].equalsIgnoreCase("OVERVIEW.FMT")) {
    72 				conn.println("215 information follows");
    73 				conn.println("Subject:\nFrom:\nDate:\nMessage-ID:\nReferences:\nBytes:\nLines:\nXref");
    74 				conn.println(".");
    75 			} else if (command[1].equalsIgnoreCase("NEWSGROUPS")) {
    76 				conn.println("215 information follows");
    77 				final List<Channel> list = Channel.getAll();
    78 				for (Channel g : list) {
    79 					conn.println(g.getName() + "\t" + "-");
    80 				}
    81 				conn.println(".");
    82 			} else if (command[1].equalsIgnoreCase("SUBSCRIPTIONS")) {
    83 				conn.println("215 information follows");
    84 				conn.println(".");
    85 			} else if (command[1].equalsIgnoreCase("EXTENSIONS")) {
    86 				conn.println("202 Supported NNTP extensions.");
    87 				conn.println("LISTGROUP");
    88 				conn.println("XDAEMON");
    89 				conn.println("XPAT");
    90 				conn.println(".");
    91 			} else if (command[1].equalsIgnoreCase("ACTIVE")) {
    92 				String pattern = command.length == 2
    93 					? null : command[2].replace("*", "\\w*");
    94 				printGroupInfo(conn, pattern);
    95 			} else {
    96 				conn.println("500 unknown argument to LIST command");
    97 			}
    98 		} else {
    99 			printGroupInfo(conn, null);
   100 		}
   101 	}
   102 
   103 	private void printGroupInfo(NNTPConnection conn, String pattern)
   104 		throws IOException, StorageBackendException
   105 	{
   106 		final List<Channel> groups = Channel.getAll();
   107 		if (groups != null) {
   108 			conn.println("215 list of newsgroups follows");
   109 			for (Channel g : groups) {
   110 				try {
   111 					Matcher matcher = pattern == null
   112 						? null : Pattern.compile(pattern).matcher(g.getName());
   113 					if (!g.isDeleted()
   114 						&& (matcher == null || matcher.find())) {
   115 						String writeable = g.isWriteable() ? " y" : " n";
   116 						// Indeed first the higher article number then the lower
   117 						conn.println(g.getName() + " " + g.getLastArticleNumber() + " "
   118 							+ g.getFirstArticleNumber() + writeable);
   119 					}
   120 				} catch (PatternSyntaxException ex) {
   121 					Log.get().info(ex.toString());
   122 				}
   123 			}
   124 			conn.println(".");
   125 		} else {
   126 			conn.println("500 server backend malfunction");
   127 		}
   128 	}
   129 }