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