001/*
002 * Version 0.70 01/04/2002
003 *
004 * Visit my url for update: http://www.geocities.com/beapetrovicova/
005 * 
006 * jFtp was developed by Bea Petrovicova <beapetrovicova@yahoo.com>.
007 * The design and implementation of jFtp are available for royalty-free 
008 * adoption and use. This software is provided 'as is' without any 
009 * guarantees. Copyright is retained by Bea Petrovicova. Redistribution 
010 * of any part of jFtp or any derivative works must include this notice.
011 * 
012 */  
013
014package cz.dhl.ftp;
015
016import java.util.Hashtable;
017
018final class FtpInterpret
019{  /* do not allow execution *
020   private static final String PASV = "PASV", REST = "REST", MODE = "MODE", STRU = "STRU";
021   /* do not allow manual execution *
022   private static final String PORT = "PORT", TYPE = "TYPE", RETR = "RETR", STOR = "STOR";
023   private static final String STOU = "STOU", APPE = "APPE", LIST = "LIST", NLST = "NLST";
024   private static final String USER = "USER", PASS = "PASS", QUIT = "QUIT";  
025   //private static final String notManual[] = {PORT,TYPE,RETR,STOR,STOU,APPE,LIST,NLST,USER,PASS,QUIT}; */
026   
027   /* do no require dir context update *
028   private static final String MKD = "MKD";
029   /* do no require dir context update *
030   private static final String RNFR = "RNFR", PWD = "PWD";
031   /* do no require dir context update on error. *
032   private static final String DELE = "DELE", RNTO = "RNTO", CDUP = "CDUP";
033   private static final String CWD = "CWD", RMD =  "RMD"; */
034   private static final String notAllow[];
035   private static final String notManual[];
036   
037   private static final Hashtable library = new Hashtable();
038   
039   static
040   {  /***************************************************************************
041       * RESULT                              * GROUP                             *
042       *                                     * .0.  Syntax                       *
043       * 1..  Positive Preliminary           * .1.  Information                  *
044       * 2..  Positive Completion            * .2.  Connections                  *
045       * 3..  Positive Intermediate          * .3.  Authentication & Accounting  *
046       * 4..  Negative Transient Completion  *                                   *
047       * 5..  Negative Permanent Completion  * .5.  File System                  * 
048       ***************************************************************************/
049
050      String loginok[] = {"220"};
051      putReplies("login-done", loginok);
052      
053      /* 20.   Positive Completion Syntax reply 
054         23.   Positive Completion Authentication & Accounting reply 
055         33.   Positive Intermediate Authentication & Accounting reply */
056      String userok[] = {"230", "331"};
057      putReplies("USER", userok);
058      String skipok[] = {"230"};
059      putReplies("USER-done", skipok);
060      String passok[] = {"230", "202"};
061      putReplies("PASS",passok);
062      
063      /* 12.   Positive Preliminary Connections reply 
064         15.   Positive Preliminary File System reply */
065      String listok[] = {"125", "150"};
066      putReplies("RETR", listok);
067      putReplies("STOR", listok);
068      putReplies("STOU", listok);
069      putReplies("APPE", listok);
070      putReplies("LIST", listok);
071      putReplies("NLST", listok);   
072      /* 22.   Positive Completion Connections reply 
073         25.   Positive Completion File System reply */
074      String doneok[] = {"226", "250"};
075      putReplies("data-done",doneok);
076      String aborok[] = {"225"};
077      putReplies("ABOR",aborok);
078
079      /* 20.   Positive Completion Syntax reply */
080      String typeok[] = {"200"};
081      putReplies("TYPE",typeok);
082      putReplies("PORT",typeok);
083         
084      /* 25.   Positive Completion File system reply 
085         35.   Positive Intermediate File system reply */
086      String cwdok[] = {"250"};
087      putReplies("CWD",cwdok);
088      putReplies("CDUP",cwdok);
089      putReplies("RMD",cwdok);
090      putReplies("DELE",cwdok);
091      String rnfrok[] = {"350"};
092      putReplies("RNFR",rnfrok);
093      putReplies("RNTO",cwdok);
094      String mkdok[] = {"257"};
095      putReplies("MKD",mkdok);
096      putReplies("PWD",mkdok);
097   
098      String systok[] = {"215"};
099      putReplies("SYST",systok);
100   
101      /* 22.   Positive Completion Connections reply */
102      String quitok[] = {"221"};
103      putReplies("QUIT",quitok);
104      String pasvok[] = {"227"};
105      putReplies("PASV",pasvok);
106      
107      String nA[] = {"REST", "MODE", "STRU"};
108      notAllow = nA;
109      String nM[] = {"PORT", "TYPE", "RETR", "STOR",
110                     "STOU", "APPE", "LIST", "NLST",
111                     "USER", "PASS", "QUIT", "PASV"};  
112      notManual = nM;
113   }
114
115   static boolean startsWith(String string,String prefixes[])
116   {  boolean done=false;
117      for(int i=0;i<prefixes.length;i++)
118         if(string.indexOf(prefixes[i])==0)
119            { done=true; break; }
120      return done;
121   }   
122   
123   protected FtpInterpret() {}
124   
125   static String getCommand(String commandline)
126   {  return commandline.substring(0,((commandline.indexOf(" ")>=0&&
127      commandline.indexOf(" ")<=4)?commandline.indexOf(" "):commandline.length())); }
128
129   static void putReplies(String command, String replies[])
130      { library.put(command,replies); }
131   
132   static String[] getReplies(String commandline)
133   {  String replies[];
134      String command = getCommand(commandline);
135      replies = (String[])library.get(command);
136      if(replies==null) 
137         replies = new String[0];
138      return replies;
139   }
140   
141   static boolean allowExecution(String commandline)
142   {  return !startsWith(commandline,notAllow); }
143   
144   static boolean allowManualExecution(String commandline)
145   {  return !startsWith(commandline,notManual); }
146
147/* static boolean updateDir(String command)
148   {  if(command.compareTo(STOR)==0 || command.compareTo(STOU)==0 || 
149         command.compareTo(APPE)==0 || command.compareTo(MKD)==0  || 
150         !updateList(command))
151         return false;
152      else return true; }
153   
154   static boolean updateList(String command)
155   {  if(command.compareTo(TYPE)==0 || command.compareTo(PORT)==0 || 
156         command.compareTo(RETR)==0 || command.compareTo(LIST)==0 ||
157         command.compareTo(NLST)==0 || command.compareTo(RNFR)==0 ||
158         command.compareTo(PWD)==0)
159         return false;
160      else return true; }
161   
162   static boolean updateListOnError(String command)
163   {  if(command.compareTo(DELE)==0 || command.compareTo(RNTO)==0 || 
164         command.compareTo(RMD)==0  || command.compareTo(MKD)==0  ||
165         command.compareTo(CWD)==0  || command.compareTo(CDUP)==0)
166         return false;
167      else return true; } */
168}