home *** CD-ROM | disk | FTP | other *** search
- package sun.net.nntp;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintStream;
- import java.util.StringTokenizer;
- import java.util.Vector;
- import sun.net.TelnetInputStream;
- import sun.net.TransferProtocolClient;
-
- public class NntpClient extends TransferProtocolClient {
- public static final int NNTP_PORT = 119;
- String serverName;
- int serverPort;
-
- public NntpClient() {
- }
-
- public NntpClient(String var1) throws IOException {
- this.openServer(var1, 119);
- }
-
- public void openServer(String var1, int var2) throws IOException {
- this.serverName = var1;
- this.serverPort = var2;
- super.openServer(var1, var2);
- if (((TransferProtocolClient)this).readServerResponse() >= 300) {
- throw new NntpProtocolException("Welcome message");
- }
- }
-
- public int askServer(String var1) throws IOException {
- int var2 = 503;
- int var3 = 3;
-
- while(true) {
- --var3;
- if (var3 < 0) {
- return var2;
- }
-
- try {
- super.serverOutput.print(var1);
- var2 = ((TransferProtocolClient)this).readServerResponse();
- if (var2 < 500) {
- return var2;
- }
- } catch (Exception var5) {
- }
-
- try {
- super.serverOutput.close();
- } catch (Exception var4) {
- }
-
- this.openServer(this.serverName, this.serverPort);
- }
- }
-
- InputStream makeStreamRequest(String var1, int var2) throws IOException {
- int var3 = this.askServer(var1 + "\r\n");
- if (var3 != var2) {
- String var4 = null;
-
- try {
- for(int var5 = 0; var5 < 99; ++var5) {
- String var6 = (String)super.serverResponse.elementAt(var5);
- if (var4 == null) {
- var4 = var6;
- } else {
- var4 = var4 + "\n" + var6;
- }
- }
- } catch (Exception var7) {
- }
-
- if (var4 == null) {
- var4 = "Command " + var1 + " yielded " + var3 + "; expecting " + var2;
- }
-
- throw new NntpProtocolException(var4);
- } else {
- switch (var3 / 100) {
- case 1:
- case 2:
- default:
- return new NntpInputStream(new TelnetInputStream(super.serverInput, false));
- case 3:
- throw new NntpProtocolException("More input to command expected");
- case 4:
- throw new NntpProtocolException("Server error - cmd OK");
- case 5:
- throw new NntpProtocolException("Error in command: " + var1);
- }
- }
- }
-
- String[] tokenize(String var1) {
- Vector var2 = new Vector();
- StringTokenizer var3 = new StringTokenizer(var1);
-
- while(var3.hasMoreTokens()) {
- var2.addElement(var3.nextToken());
- }
-
- String[] var4 = new String[var2.size()];
-
- for(int var5 = 0; var5 < var4.length; ++var5) {
- var4[var5] = (String)var2.elementAt(var5);
- }
-
- return var4;
- }
-
- public NewsgroupInfo getGroup(String var1) throws IOException {
- switch (this.askServer("group " + var1 + "\r\n")) {
- case 211:
- String[] var2 = this.tokenize(((TransferProtocolClient)this).getResponseString());
- int var3 = Integer.parseInt(var2[2]);
- int var4 = Integer.parseInt(var2[3]);
- return new NewsgroupInfo(var1, var3, var4);
- case 411:
- throw new UnknownNewsgroupException(var1);
- default:
- throw new NntpProtocolException("unexpected reply: " + ((TransferProtocolClient)this).getResponseString());
- }
- }
-
- public void setGroup(String var1) throws IOException {
- if (this.askServer("group " + var1 + "\r\n") != 211) {
- throw new UnknownNewsgroupException(var1);
- }
- }
-
- public InputStream getArticle(int var1) throws IOException {
- return this.makeStreamRequest("article " + var1, 220);
- }
-
- public InputStream getArticle(String var1) throws IOException {
- if (var1.charAt(0) != '<') {
- var1 = "<" + var1 + ">";
- }
-
- return this.makeStreamRequest("article " + var1, 220);
- }
-
- public InputStream getHeader(int var1) throws IOException {
- return this.makeStreamRequest("head " + var1, 221);
- }
-
- public InputStream getHeader(String var1) throws IOException {
- if (var1.charAt(0) != '<') {
- var1 = "<" + var1 + ">";
- }
-
- return this.makeStreamRequest("head " + var1, 221);
- }
-
- public PrintStream startPost() throws IOException {
- return this.askServer("post\r\n") == 340 ? super.serverOutput : null;
- }
-
- public boolean finishPost() throws IOException {
- return this.askServer(".\r\n") == 240;
- }
- }
-