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