src/org/sonews/daemon/command/ListCommand.java
changeset 44 5d7d1adf387f
parent 35 ed84c8bdd87b
child 48 b78e77619152
     1.1 --- a/src/org/sonews/daemon/command/ListCommand.java	Sun Aug 29 17:28:58 2010 +0200
     1.2 +++ b/src/org/sonews/daemon/command/ListCommand.java	Tue Jun 07 11:55:22 2011 +0200
     1.3 @@ -37,117 +37,93 @@
     1.4  public class ListCommand implements Command
     1.5  {
     1.6  
     1.7 -  @Override
     1.8 -  public String[] getSupportedCommandStrings()
     1.9 -  {
    1.10 -    return new String[]{"LIST"};
    1.11 -  }
    1.12 +	@Override
    1.13 +	public String[] getSupportedCommandStrings()
    1.14 +	{
    1.15 +		return new String[] {"LIST"};
    1.16 +	}
    1.17  
    1.18 -  @Override
    1.19 -  public boolean hasFinished()
    1.20 -  {
    1.21 -    return true;
    1.22 -  }
    1.23 +	@Override
    1.24 +	public boolean hasFinished()
    1.25 +	{
    1.26 +		return true;
    1.27 +	}
    1.28  
    1.29 -  @Override
    1.30 -  public String impliedCapability()
    1.31 -  {
    1.32 -    return null;
    1.33 -  }
    1.34 +	@Override
    1.35 +	public String impliedCapability()
    1.36 +	{
    1.37 +		return null;
    1.38 +	}
    1.39  
    1.40 -  @Override
    1.41 -  public boolean isStateful()
    1.42 -  {
    1.43 -    return false;
    1.44 -  }
    1.45 -  
    1.46 -  @Override
    1.47 -  public void processLine(NNTPConnection conn, final String line, byte[] raw)
    1.48 -    throws IOException, StorageBackendException
    1.49 -  {
    1.50 -    final String[] command = line.split(" ");
    1.51 -    
    1.52 -    if(command.length >= 2)
    1.53 -    {
    1.54 -      if(command[1].equalsIgnoreCase("OVERVIEW.FMT"))
    1.55 -      {
    1.56 -        conn.println("215 information follows");
    1.57 -        conn.println("Subject:\nFrom:\nDate:\nMessage-ID:\nReferences:\nBytes:\nLines:\nXref");
    1.58 -        conn.println(".");
    1.59 -      }
    1.60 -      else if(command[1].equalsIgnoreCase("NEWSGROUPS"))
    1.61 -      {
    1.62 -        conn.println("215 information follows");
    1.63 -        final List<Channel> list = Channel.getAll();
    1.64 -        for (Channel g : list)
    1.65 -        {
    1.66 -          conn.println(g.getName() + "\t" + "-");
    1.67 -        }
    1.68 -        conn.println(".");
    1.69 -      }
    1.70 -      else if(command[1].equalsIgnoreCase("SUBSCRIPTIONS"))
    1.71 -      {
    1.72 -        conn.println("215 information follows");
    1.73 -        conn.println(".");
    1.74 -      }
    1.75 -      else if(command[1].equalsIgnoreCase("EXTENSIONS"))
    1.76 -      {
    1.77 -        conn.println("202 Supported NNTP extensions.");
    1.78 -        conn.println("LISTGROUP");
    1.79 -        conn.println("XDAEMON");
    1.80 -        conn.println("XPAT");
    1.81 -        conn.println(".");
    1.82 -      }
    1.83 -      else if(command[1].equalsIgnoreCase("ACTIVE"))
    1.84 -      {
    1.85 -        String  pattern  = command.length == 2
    1.86 -          ? null : command[2].replace("*", "\\w*");
    1.87 -        printGroupInfo(conn, pattern);
    1.88 -      }
    1.89 -      else
    1.90 -      {
    1.91 -        conn.println("500 unknown argument to LIST command");
    1.92 -      }
    1.93 -    }
    1.94 -    else
    1.95 -    {
    1.96 -      printGroupInfo(conn, null);
    1.97 -    }
    1.98 -  }
    1.99 +	@Override
   1.100 +	public boolean isStateful()
   1.101 +	{
   1.102 +		return false;
   1.103 +	}
   1.104  
   1.105 -  private void printGroupInfo(NNTPConnection conn, String pattern)
   1.106 -    throws IOException, StorageBackendException
   1.107 -  {
   1.108 -    final List<Channel> groups = Channel.getAll();
   1.109 -    if(groups != null)
   1.110 -    {
   1.111 -      conn.println("215 list of newsgroups follows");
   1.112 -      for(Channel g : groups)
   1.113 -      {
   1.114 -        try
   1.115 -        {
   1.116 -          Matcher matcher = pattern == null ?
   1.117 -            null : Pattern.compile(pattern).matcher(g.getName());
   1.118 -          if(!g.isDeleted() &&
   1.119 -            (matcher == null || matcher.find()))
   1.120 -          {
   1.121 -            String writeable = g.isWriteable() ? " y" : " n";
   1.122 -            // Indeed first the higher article number then the lower
   1.123 -            conn.println(g.getName() + " " + g.getLastArticleNumber() + " "
   1.124 -              + g.getFirstArticleNumber() + writeable);
   1.125 -          }
   1.126 -        }
   1.127 -        catch(PatternSyntaxException ex)
   1.128 -        {
   1.129 -          Log.get().info(ex.toString());
   1.130 -        }
   1.131 -      }
   1.132 -      conn.println(".");
   1.133 -    }
   1.134 -    else
   1.135 -    {
   1.136 -      conn.println("500 server backend malfunction");
   1.137 -    }
   1.138 -  }
   1.139 +	@Override
   1.140 +	public void processLine(NNTPConnection conn, final String line, byte[] raw)
   1.141 +		throws IOException, StorageBackendException
   1.142 +	{
   1.143 +		final String[] command = line.split(" ");
   1.144  
   1.145 +		if (command.length >= 2) {
   1.146 +			if (command[1].equalsIgnoreCase("OVERVIEW.FMT")) {
   1.147 +				conn.println("215 information follows");
   1.148 +				conn.println("Subject:\nFrom:\nDate:\nMessage-ID:\nReferences:\nBytes:\nLines:\nXref");
   1.149 +				conn.println(".");
   1.150 +			} else if (command[1].equalsIgnoreCase("NEWSGROUPS")) {
   1.151 +				conn.println("215 information follows");
   1.152 +				final List<Channel> list = Channel.getAll();
   1.153 +				for (Channel g : list) {
   1.154 +					conn.println(g.getName() + "\t" + "-");
   1.155 +				}
   1.156 +				conn.println(".");
   1.157 +			} else if (command[1].equalsIgnoreCase("SUBSCRIPTIONS")) {
   1.158 +				conn.println("215 information follows");
   1.159 +				conn.println(".");
   1.160 +			} else if (command[1].equalsIgnoreCase("EXTENSIONS")) {
   1.161 +				conn.println("202 Supported NNTP extensions.");
   1.162 +				conn.println("LISTGROUP");
   1.163 +				conn.println("XDAEMON");
   1.164 +				conn.println("XPAT");
   1.165 +				conn.println(".");
   1.166 +			} else if (command[1].equalsIgnoreCase("ACTIVE")) {
   1.167 +				String pattern = command.length == 2
   1.168 +					? null : command[2].replace("*", "\\w*");
   1.169 +				printGroupInfo(conn, pattern);
   1.170 +			} else {
   1.171 +				conn.println("500 unknown argument to LIST command");
   1.172 +			}
   1.173 +		} else {
   1.174 +			printGroupInfo(conn, null);
   1.175 +		}
   1.176 +	}
   1.177 +
   1.178 +	private void printGroupInfo(NNTPConnection conn, String pattern)
   1.179 +		throws IOException, StorageBackendException
   1.180 +	{
   1.181 +		final List<Channel> groups = Channel.getAll();
   1.182 +		if (groups != null) {
   1.183 +			conn.println("215 list of newsgroups follows");
   1.184 +			for (Channel g : groups) {
   1.185 +				try {
   1.186 +					Matcher matcher = pattern == null
   1.187 +						? null : Pattern.compile(pattern).matcher(g.getName());
   1.188 +					if (!g.isDeleted()
   1.189 +						&& (matcher == null || matcher.find())) {
   1.190 +						String writeable = g.isWriteable() ? " y" : " n";
   1.191 +						// Indeed first the higher article number then the lower
   1.192 +						conn.println(g.getName() + " " + g.getLastArticleNumber() + " "
   1.193 +							+ g.getFirstArticleNumber() + writeable);
   1.194 +					}
   1.195 +				} catch (PatternSyntaxException ex) {
   1.196 +					Log.get().info(ex.toString());
   1.197 +				}
   1.198 +			}
   1.199 +			conn.println(".");
   1.200 +		} else {
   1.201 +			conn.println("500 server backend malfunction");
   1.202 +		}
   1.203 +	}
   1.204  }