diff -r 6ae5e4f8329b -r c404a87db5b7 src/org/sonews/daemon/command/NextPrevCommand.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/sonews/daemon/command/NextPrevCommand.java Sun Aug 29 17:43:58 2010 +0200 @@ -0,0 +1,116 @@ +/* + * SONEWS News Server + * see AUTHORS for the list of contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.sonews.daemon.command; + +import java.io.IOException; +import org.sonews.daemon.NNTPConnection; +import org.sonews.storage.Article; +import org.sonews.storage.Channel; +import org.sonews.storage.StorageBackendException; + +/** + * Class handling the NEXT and LAST command. + * @author Christian Lins + * @author Dennis Schwerdel + * @since n3tpd/0.1 + */ +public class NextPrevCommand implements Command +{ + + @Override + public String[] getSupportedCommandStrings() + { + return new String[]{"NEXT", "PREV"}; + } + + @Override + public boolean hasFinished() + { + return true; + } + + @Override + public String impliedCapability() + { + return null; + } + + @Override + public boolean isStateful() + { + return false; + } + + @Override + public void processLine(NNTPConnection conn, final String line, byte[] raw) + throws IOException, StorageBackendException + { + final Article currA = conn.getCurrentArticle(); + final Channel currG = conn.getCurrentChannel(); + + if (currA == null) + { + conn.println("420 no current article has been selected"); + return; + } + + if (currG == null) + { + conn.println("412 no newsgroup selected"); + return; + } + + final String[] command = line.split(" "); + + if(command[0].equalsIgnoreCase("NEXT")) + { + selectNewArticle(conn, currA, currG, 1); + } + else if(command[0].equalsIgnoreCase("PREV")) + { + selectNewArticle(conn, currA, currG, -1); + } + else + { + conn.println("500 internal server error"); + } + } + + private void selectNewArticle(NNTPConnection conn, Article article, Channel grp, + final int delta) + throws IOException, StorageBackendException + { + assert article != null; + + article = grp.getArticle(grp.getIndexOf(article) + delta); + + if(article == null) + { + conn.println("421 no next article in this group"); + } + else + { + conn.setCurrentArticle(article); + conn.println("223 " + conn.getCurrentChannel().getIndexOf(article) + + " " + article.getMessageID() + + " article retrieved - request text separately"); + } + } + +}