//
//   ************************ Security Warning! ************************
//   This is a serious warning, and you need to pay attention to it.
//
//   To work, this program needs you to put your ftp password into the
//   code as a String.  This is only appropriate on an Intranet or when 
//   the applet is on a page to which access is otherwise controlled.
//
//   A malicious person could access the applet class file and easily
//   retrieve YOUR password.  Then they would have access to your ftp
//   account.  They could add, delete, or change files.  They could even
//   change the password.  That would be very bad for you.
//
//   Giving away your password is frequently not allowed by the terms of
//   your account provider.   Do NOT use this program unless you are 
//   authorized to pass on your ftp password, and you are able to firewall
//   the ftp account or otherwise protect against intrusion.   One thing
//   you could do is have a very small ftp area, and only allow existing
//   files to be appended (for example).
//
//   Creating an applet with this program is like publishing your password
//   to anyone who can access the applet class file.
//
//   If you still want to proceed, it is at your own risk.
// **************************** Security Warning! ************************


// Instructions:
//   1.   This file contains the applet I/O class Linlyn, and
//        a sample applet, t, that uses it.
//
//   2.   modify the literal strings in t.java
//        to use your server, username, passwd etc.
//
//   3.   compile by typing
//            javac t.java
//
//   4.   try running by:
//            appletviewer t.java    
//        it will upload and retrieve a file for an applet
//        (The html to run it is in this file, so t.java not t.html is OK).
//
//   5.   modify t.java to be your own applet
//   
//        While getting started, you might want to uncomment the
//	  "debug" lines near the head of Linlyn.java; this will
//	  produce output to help in your initial configuration.
//
//  if this works for you, send a note to pvdl@best.com,  May 1998.

/*

<html>
<body>
This runs the Linlyn applet
<p>
<applet code=t.class width=400 height=200>   </applet>
<hr>
</body>
</html>

 */

// a test applet to show the use of the Linlyn class
// you don't have to embed the passwd in the code - you could
// prompt for it.
//
import java.awt.*;   // needed by the applet
import java.applet.*;

import java.io.*;   // needed by Linlyn
import java.net.*;
import java.util.*;

public class t extends Applet {

    public void init() {
        try {     
            // upload to file
            // You must replace the strings with actual values to use.
            //  e.g.       Linlyn("microsoft.com","billg",    "allmy$$$");
            Linlyn u = new Linlyn("YOUR_SERVER", "USERNAME", "PASSWD");
            u.upload("pub", "score.txt", 
				"'Just Java 1.1' is a great Java book! \n" +
                                "available quick and cheap from \n" +
                   " http://www.amazon.com/exec/obidos/ISBN%3D0137841744 " ); 

            // download from file
            // You must replace the strings with actual values to use.
            Linlyn d = new Linlyn("YOUR_SERVER", "USERNAME", "PASSWD");
            String s = d.download("pub", "score.txt");

            // display file contents
            TextArea ta = new TextArea( s, 3, 40);
            add(ta);

        } catch(java.io.IOException ioe) {ioe.printStackTrace();}
    }
}


//////////////////////////////////////////
/// split here for Linlyn.java //
//  At last!  Java code to read/write files on the server from an applet!
//  This is the famous Linlyn code.
//
//  Use:  
//    compile this file, and have your applet call it as below.
//
//    to upload a file:  
//          Linlyn ftp = new Linlyn( <servername>, <user>, <password> );
//          ftp.upload( <directory>, <filename>, <contents of file> );
//
//    to download a file:  
//          Linlyn ftp = new Linlyn( <servername>, <user>, <password> );
//          String contents = ftp.download( <directory>, <filename> );
//
//          the default is ASCII transfer, an overloaded method does bin.
//
//    All parameters and return values are Strings. E.g.
//          Linlyn ftp = new Linlyn( "rtfm.mit.edu", "anonymous", "linden@" );
//          String contents = ftp.download( 
//                        "/pub/usenet-by-group/comp.lang.java.programmer"
//                        "Java_Programmers_FAQ" );
//
//          [the actual values above are not generally valid, substitute
//           your own server for your first attempt, see note 1.]
//
//    Notes:
//      1.  Usual applet security rules apply: you can only get a file
//          from the server that served the applet.
//      2.  The applet server must also be an FTP server.  This is NOT true
//          for some ISPs, such as best.com.  They have separate FTP and
//          http machines.  This code may work on such a setup if you put
//          the classfiles into the ftp area, and in the HTML file say:
//            <applet  codebase="ftp:///home/linden/ftp"  code="t.class" 
//      3.  This code does not break Java security.
//          It uses FTP to transfer files.  If the author of the applet
//          has FTP disabled you are out of luck.
//          It breaks regular system security however, as it publishes
//          (effectively) your ftp password.  Only use on an Intranet and
//          with authorization.
//      4.  Compiling this causes some deprecation warnings.   We wanted to 
//          stick with code that would work in JDK 1.0 browsers.
//      5.  Each upload or download creates, uses, and terminates a new
//          ftp session.  This is intended for low volume transfer, such
//          as the ever popular high-score files.
//      6.  Look at the source for the methods for binary transfers.
//
//    Version 1.0   May 6 1998.
//    Version 1.1   May 20 1998. -- added a debugging flag
//    Version 1.1a  May 26 1998. -- fixed the ASCII/BIN flag inversion
//    Version 1.1b  May 29 1998. -- added the security warning.
//    Version 2.0   Jul 1, 1998. -- Updated to parse multi-string responses 
//                                  a la RFC 959
//    Version 2.1   Aug 5, 1998. -- Updated to work with VMS ftp servers
//                                  VMS does not send either a ")" OR a ")."
//                                  terminating the IP number, port sequence 
//                                  in response to PASV.
//    Version 2.1a  Aug 6, 1998  -- more than one line as a "hello" message.
//                                  (tvalesky@patriot.net)
//    Version 2.2   Sep 22 1998  -- added a flush() in ftpSendCmd.
//    Version 2.2a  Dec 09 1998  -- added comments on compiling w/o deprecation
//
//    Authors:
//          Robert Lynch
//          Peter van der Linden  (Author of "Just Java" book).
//   
//    Support:
//          Unsupported: That's why we give you the source.
//          Help may be available on time & materials basis only.
//			You can get copious debug information by changing to
//			DEBUG=true     below and recompiling.
//
//    Copyright 1998 Robert Lynch, Peter van der Linden
//    This work is distributed under the GNU GPL, version 2.
//
//    Those using the code do so at their own risk and the authors 
//    are not responsible for any costs, loss, or damage which may
//    thereby be incurred. 

// import java.io.*;
// import java.net.*;
// import java.util.*;

class Linlyn {

    // FOR INITIAL DEBUGGING: set the variable to "true"
    private boolean DEBUG = false;

    // constructor needs servername, username and passwd
    public Linlyn(String server, String user, String pass) {
        try { 
            ftpConnect(server);
            ftpLogin(user, pass);
        } catch(IOException ioe) {ioe.printStackTrace();}
    }

    public String download(String dir, String file)
        throws IOException { return download(dir, file, true); }

    public String download(String dir, String file, boolean asc)
        throws IOException {
        ftpSetDir(dir);
		ftpSetTransferType(asc);
     