chris@1: /*
chris@1: * SONEWS News Server
chris@1: * see AUTHORS for the list of contributors
chris@1: *
chris@1: * This program is free software: you can redistribute it and/or modify
chris@1: * it under the terms of the GNU General Public License as published by
chris@1: * the Free Software Foundation, either version 3 of the License, or
chris@1: * (at your option) any later version.
chris@1: *
chris@1: * This program is distributed in the hope that it will be useful,
chris@1: * but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
chris@1: * GNU General Public License for more details.
chris@1: *
chris@1: * You should have received a copy of the GNU General Public License
chris@1: * along with this program. If not, see .
chris@1: */
chris@1: package org.sonews.daemon.command;
chris@1:
chris@1: import java.io.IOException;
chris@1: import java.util.List;
cli@17: import java.util.regex.Matcher;
cli@17: import java.util.regex.Pattern;
cli@17: import java.util.regex.PatternSyntaxException;
chris@1: import org.sonews.daemon.NNTPConnection;
cli@48: import org.sonews.storage.Group;
chris@3: import org.sonews.storage.StorageBackendException;
cli@17: import org.sonews.util.Log;
chris@1:
chris@1: /**
chris@1: * Class handling the LIST command.
chris@1: * @author Christian Lins
chris@1: * @author Dennis Schwerdel
chris@1: * @since n3tpd/0.1
chris@1: */
cli@49: public class ListCommand implements Command {
chris@1:
cli@37: @Override
cli@49: public String[] getSupportedCommandStrings() {
cli@49: return new String[]{"LIST"};
cli@37: }
chris@1:
cli@37: @Override
cli@49: public boolean hasFinished() {
cli@37: return true;
cli@37: }
chris@3:
cli@37: @Override
cli@49: public String impliedCapability() {
cli@37: return null;
cli@37: }
cli@20:
cli@37: @Override
cli@49: public boolean isStateful() {
cli@37: return false;
cli@37: }
cli@12:
cli@37: @Override
cli@37: public void processLine(NNTPConnection conn, final String line, byte[] raw)
cli@49: throws IOException, StorageBackendException {
cli@37: final String[] command = line.split(" ");
chris@1:
cli@37: if (command.length >= 2) {
cli@37: if (command[1].equalsIgnoreCase("OVERVIEW.FMT")) {
cli@37: conn.println("215 information follows");
cli@37: conn.println("Subject:\nFrom:\nDate:\nMessage-ID:\nReferences:\nBytes:\nLines:\nXref");
cli@37: conn.println(".");
cli@37: } else if (command[1].equalsIgnoreCase("NEWSGROUPS")) {
cli@37: conn.println("215 information follows");
cli@48: final List list = Group.getAll();
cli@48: for (Group g : list) {
cli@37: conn.println(g.getName() + "\t" + "-");
cli@37: }
cli@37: conn.println(".");
cli@37: } else if (command[1].equalsIgnoreCase("SUBSCRIPTIONS")) {
cli@37: conn.println("215 information follows");
cli@37: conn.println(".");
cli@37: } else if (command[1].equalsIgnoreCase("EXTENSIONS")) {
cli@37: conn.println("202 Supported NNTP extensions.");
cli@37: conn.println("LISTGROUP");
cli@37: conn.println("XDAEMON");
cli@37: conn.println("XPAT");
cli@37: conn.println(".");
cli@37: } else if (command[1].equalsIgnoreCase("ACTIVE")) {
cli@37: String pattern = command.length == 2
cli@49: ? null : command[2].replace("*", "\\w*");
cli@37: printGroupInfo(conn, pattern);
cli@37: } else {
cli@37: conn.println("500 unknown argument to LIST command");
cli@37: }
cli@37: } else {
cli@37: printGroupInfo(conn, null);
cli@37: }
cli@37: }
cli@37:
cli@37: private void printGroupInfo(NNTPConnection conn, String pattern)
cli@49: throws IOException, StorageBackendException {
cli@48: final List groups = Group.getAll();
cli@37: if (groups != null) {
cli@37: conn.println("215 list of newsgroups follows");
cli@48: for (Group g : groups) {
cli@37: try {
cli@37: Matcher matcher = pattern == null
cli@49: ? null : Pattern.compile(pattern).matcher(g.getName());
cli@37: if (!g.isDeleted()
cli@49: && (matcher == null || matcher.find())) {
cli@37: String writeable = g.isWriteable() ? " y" : " n";
cli@37: // Indeed first the higher article number then the lower
cli@37: conn.println(g.getName() + " " + g.getLastArticleNumber() + " "
cli@49: + g.getFirstArticleNumber() + writeable);
cli@37: }
cli@37: } catch (PatternSyntaxException ex) {
cli@37: Log.get().info(ex.toString());
cli@37: }
cli@37: }
cli@37: conn.println(".");
cli@37: } else {
cli@37: conn.println("500 server backend malfunction");
cli@37: }
cli@37: }
chris@1: }