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
chris@1
     1
/*
chris@1
     2
 *   SONEWS News Server
chris@1
     3
 *   see AUTHORS for the list of contributors
chris@1
     4
 *
chris@1
     5
 *   This program is free software: you can redistribute it and/or modify
chris@1
     6
 *   it under the terms of the GNU General Public License as published by
chris@1
     7
 *   the Free Software Foundation, either version 3 of the License, or
chris@1
     8
 *   (at your option) any later version.
chris@1
     9
 *
chris@1
    10
 *   This program is distributed in the hope that it will be useful,
chris@1
    11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@1
    12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@1
    13
 *   GNU General Public License for more details.
chris@1
    14
 *
chris@1
    15
 *   You should have received a copy of the GNU General Public License
chris@1
    16
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@1
    17
 */
chris@1
    18
package org.sonews.daemon.command;
chris@1
    19
chris@1
    20
import java.io.IOException;
chris@1
    21
import java.util.List;
cli@17
    22
import java.util.regex.Matcher;
cli@17
    23
import java.util.regex.Pattern;
cli@17
    24
import java.util.regex.PatternSyntaxException;
chris@1
    25
import org.sonews.daemon.NNTPConnection;
cli@48
    26
import org.sonews.storage.Group;
chris@3
    27
import org.sonews.storage.StorageBackendException;
cli@17
    28
import org.sonews.util.Log;
chris@1
    29
chris@1
    30
/**
chris@1
    31
 * Class handling the LIST command.
chris@1
    32
 * @author Christian Lins
chris@1
    33
 * @author Dennis Schwerdel
chris@1
    34
 * @since n3tpd/0.1
chris@1
    35
 */
cli@49
    36
public class ListCommand implements Command {
chris@1
    37
cli@37
    38
	@Override
cli@49
    39
	public String[] getSupportedCommandStrings() {
cli@49
    40
		return new String[]{"LIST"};
cli@37
    41
	}
chris@1
    42
cli@37
    43
	@Override
cli@49
    44
	public boolean hasFinished() {
cli@37
    45
		return true;
cli@37
    46
	}
chris@3
    47
cli@37
    48
	@Override
cli@49
    49
	public String impliedCapability() {
cli@37
    50
		return null;
cli@37
    51
	}
cli@20
    52
cli@37
    53
	@Override
cli@49
    54
	public boolean isStateful() {
cli@37
    55
		return false;
cli@37
    56
	}
cli@12
    57
cli@37
    58
	@Override
cli@37
    59
	public void processLine(NNTPConnection conn, final String line, byte[] raw)
cli@49
    60
			throws IOException, StorageBackendException {
cli@37
    61
		final String[] command = line.split(" ");
chris@1
    62
cli@37
    63
		if (command.length >= 2) {
cli@37
    64
			if (command[1].equalsIgnoreCase("OVERVIEW.FMT")) {
cli@37
    65
				conn.println("215 information follows");
cli@37
    66
				conn.println("Subject:\nFrom:\nDate:\nMessage-ID:\nReferences:\nBytes:\nLines:\nXref");
cli@37
    67
				conn.println(".");
cli@37
    68
			} else if (command[1].equalsIgnoreCase("NEWSGROUPS")) {
cli@37
    69
				conn.println("215 information follows");
cli@48
    70
				final List<Group> list = Group.getAll();
cli@48
    71
				for (Group g : list) {
cli@37
    72
					conn.println(g.getName() + "\t" + "-");
cli@37
    73
				}
cli@37
    74
				conn.println(".");
cli@37
    75
			} else if (command[1].equalsIgnoreCase("SUBSCRIPTIONS")) {
cli@37
    76
				conn.println("215 information follows");
cli@37
    77
				conn.println(".");
cli@37
    78
			} else if (command[1].equalsIgnoreCase("EXTENSIONS")) {
cli@37
    79
				conn.println("202 Supported NNTP extensions.");
cli@37
    80
				conn.println("LISTGROUP");
cli@37
    81
				conn.println("XDAEMON");
cli@37
    82
				conn.println("XPAT");
cli@37
    83
				conn.println(".");
cli@37
    84
			} else if (command[1].equalsIgnoreCase("ACTIVE")) {
cli@37
    85
				String pattern = command.length == 2
cli@49
    86
						? null : command[2].replace("*", "\\w*");
cli@37
    87
				printGroupInfo(conn, pattern);
cli@37
    88
			} else {
cli@37
    89
				conn.println("500 unknown argument to LIST command");
cli@37
    90
			}
cli@37
    91
		} else {
cli@37
    92
			printGroupInfo(conn, null);
cli@37
    93
		}
cli@37
    94
	}
cli@37
    95
cli@37
    96
	private void printGroupInfo(NNTPConnection conn, String pattern)
cli@49
    97
			throws IOException, StorageBackendException {
cli@48
    98
		final List<Group> groups = Group.getAll();
cli@37
    99
		if (groups != null) {
cli@37
   100
			conn.println("215 list of newsgroups follows");
cli@48
   101
			for (Group g : groups) {
cli@37
   102
				try {
cli@37
   103
					Matcher matcher = pattern == null
cli@49
   104
							? null : Pattern.compile(pattern).matcher(g.getName());
cli@37
   105
					if (!g.isDeleted()
cli@49
   106
							&& (matcher == null || matcher.find())) {
cli@37
   107
						String writeable = g.isWriteable() ? " y" : " n";
cli@37
   108
						// Indeed first the higher article number then the lower
cli@37
   109
						conn.println(g.getName() + " " + g.getLastArticleNumber() + " "
cli@49
   110
								+ g.getFirstArticleNumber() + writeable);
cli@37
   111
					}
cli@37
   112
				} catch (PatternSyntaxException ex) {
cli@37
   113
					Log.get().info(ex.toString());
cli@37
   114
				}
cli@37
   115
			}
cli@37
   116
			conn.println(".");
cli@37
   117
		} else {
cli@37
   118
			conn.println("500 server backend malfunction");
cli@37
   119
		}
cli@37
   120
	}
chris@1
   121
}