1 //////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Zhou Xiao-wei, Apr. 2007.
3 // Implemented XylFTPMain.main().
4 // Hacked by Wang Cong, May. 2007.
5 // See AUTHORS and CREDITS to learn more.
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License version 2 as
8 // published by the Free Software Foundation.
9 // See COPYING to learn more.
10 //////////////////////////////////////////////////////////////////////////
17 *@author ZHOU Xiao-Wei
22 public final class XylFTPMain
{
27 private static XylFTPCLI MyFTP
;
30 *Enable verbose output.
32 private static boolean EnableVerbose
= false;
35 *Enable debugging output.
37 private static boolean EnableDebug
= false;
40 *Tests whether it is enable debug.
42 public static boolean GetEnableDebug(){
47 *Tests whether it is enable verbose.
49 public static boolean GetEnableVerbose(){
54 *Sets the value of Enabledebug.
55 *@param Value the new value of Enabledebug.
57 public static void SetEnableDebug(boolean Value
){
62 *Sets the value of Enableverbose.
63 *@param Value the new value of Enableverbose.
65 public static void SetEnableVerbose(boolean Value
){
66 EnableVerbose
= Value
;
70 *Logo used by this program.
72 private final static String LOGO
= "xylftp>";
75 *Parse the port number from echo.
76 *@param From the echo to be parsed
77 *@exception XylFTPException on parsing port error
79 *@return the number of the port
81 private static int ParsePort(String From
) throws XylFTPException
{
86 i
= From
.indexOf("(");
88 throw new XylFTPException("", 1, "Invalid echo!");
89 j
= From
.indexOf(")");
91 throw new XylFTPException("", 1, "Invalid echo!");
92 t
= From
.substring(i
+1, j
);
94 port1
= Integer
.parseInt(nums
[4]);
95 port2
= Integer
.parseInt(nums
[5]);
96 return port1
*256 + port2
;
100 *Parse the IP address from the echo.
101 *@param From the echo to be parsed
102 *@exception XylFTPException on parsing IP error
103 *@see XylFTPException
104 *@return all the numbers of the IP as a string
106 private static String
ParseIP(String From
) throws XylFTPException
{
110 i
= From
.indexOf("(");
112 throw new XylFTPException("PASV", 1, "Invalid echo!");
113 j
= From
.indexOf(")");
115 throw new XylFTPException("PASV", 1, "Invalid echo!");
116 t
= From
.substring(i
+1, j
);
119 throw new XylFTPException("PASV", 1, "Invalid echo!");
120 return (nums
[0]+"."+nums
[1]+"."+nums
[2]+"."+nums
[3]);
127 public static void main(String args
[]) {
129 // 0 for standard read
130 // 1 for read username
131 // 2 for read password
132 // 3 for read help parameter
134 boolean toConnectAtStart
= false;
135 MyFTP
= new XylFTPCLI();
137 for (int i
= 0; i
< args
.length
; i
++) {
138 if (flag
== 0) { // standard read
139 if (args
[i
].charAt(0) == '-' && args
[i
].charAt(1) == '-') { // long argument
140 if (args
[i
].equals("--user")) {
141 if (i
< args
.length
- 1) {
142 flag
= 1; // Read username next time
143 } else { // Nothing after --user indicates an error
147 } else if (args
[i
].equals("--help")) {
148 if (i
< args
.length
- 1
149 && args
[i
+ 1].charAt(0) != '-') { // Need to read help parameter
155 } else if (args
[i
].equals("--password")) {
156 if (i
< args
.length
- 1) {
157 flag
= 2; // Read password next time
158 } else { // Nothing after --password indicates an error
162 } else if (args
[i
].equals("--version")) {
165 } else if (args
[i
].equals("--verbose")) {
166 EnableVerbose
= true;
167 } else if (args
[i
].equals("--debug")) {
169 } else { // illegal long argument
174 } else if (args
[i
].charAt(0) == '-') { // short argument
175 if (args
[i
].length() > 2 || args
[i
].length() <= 1) { // Short argument too long or too short
179 ch
= args
[i
].charAt(1); // argument char
182 if (i
< args
.length
- 1) {
183 flag
= 1; // Read username next time
184 } else { // Nothing after -u indicates an error
190 if (i
< args
.length
- 1
191 && args
[i
+ 1].charAt(0) != '-') { // Need to read help parameter
199 if (i
< args
.length
- 1) {
200 flag
= 2; // Read password next time
201 } else { // Nothing after -p indicates an error
211 EnableVerbose
= true;
216 default: // illegal short argument
222 if (toConnectAtStart
) { // illegal argument
225 } else { // Command line contains host name
226 toConnectAtStart
= true;
227 MyFTP
.SetHost(args
[i
]);
230 } else if (flag
== 1) { // Read Username
231 MyFTP
.SetUserName(args
[i
]);
233 } else if (flag
== 2) { // Read password
234 MyFTP
.SetPassWord(args
[i
]);
236 } else if (flag
== 3) { // Read help parameter
237 MyFTP
.ShowHelp(args
[i
]);
246 String HostIP
= null;
249 if (toConnectAtStart
) {
251 MyFTP
.OpenConnection();
252 String s
= MyFTP
.GetEcho();
254 throw new XylFTPException("xylftp", 0,
255 "Cann't get an echo.");
257 System
.out
.println(s
);
258 while (s
.charAt(3) == '-'){
261 throw new XylFTPException("xylftp",
262 0, "Can't get an echo.");
264 System
.out
.println("<---"+s
);
265 System
.out
.println(s
);
268 cmds
= MyFTP
.MakeCommands("USER "
269 +MyFTP
.GetUserName()+"\r\n"
270 +"PASS "+MyFTP
.GetPassWord()
273 toConnectAtStart
= false;
276 System
.out
.print(LOGO
);
277 cmds
= MyFTP
.GetCommands();
282 for (int j
= 0; j
< cmds
.length
; j
++) {
284 if (cmds
[j
].startsWith("PASS"))
285 System
.out
.println("--->PASS ******");
287 System
.out
.println("--->"+cmds
[j
]);
288 int restore
= MyFTP
.GetStatus();
290 MyFTP
.SendCommand(cmds
[j
]);
291 } catch (IOException e
) {
292 throw new XylFTPException("xylftp", restore
,
295 if (cmds
[j
].startsWith("RETR")
296 || cmds
[j
].startsWith("LIST")
297 || cmds
[j
].startsWith("NLST")){
300 if (MyFTP
.IsPassive())
301 MyFTP
.OpenDataConnection(HostIP
,
304 MyFTP
.OpenDataConnection();
305 } catch (Exception e
) {
306 if (MyFTP
.HasEcho()){
307 String ec
= MyFTP
.GetEcho();
309 System
.out
.println("<---"+ec
);
310 MyFTP
.ProcessEcho(ec
);
312 throw new XylFTPException("xylftp", 2,
316 if (cmds
[j
].startsWith("STOR")){
319 if (MyFTP
.IsPassive())
320 MyFTP
.OpenDataConnection(HostIP
, HostPort
);
322 MyFTP
.OpenDataConnection();
323 } catch (Exception e
) {
324 throw new XylFTPException("", 2, e
.getMessage());
327 if (cmds
[j
].startsWith("PORT")){
328 MyFTP
.ReadyForActive();
331 boolean toGetEchoAgain
;
333 toGetEchoAgain
= false;
335 echo
= MyFTP
.GetEcho();
337 throw new XylFTPException("xylftp",
338 0, "Can't get an echo.");
339 } catch (IOException e
) {
340 throw new XylFTPException("xylftp", 0,
343 if (!MyFTP
.IsValidEcho(echo
))
344 throw new XylFTPException("xylftp", 0,
347 System
.out
.println("<---"+echo
);
348 echoMeaning
= MyFTP
.ProcessEcho(echo
);
349 while (echoMeaning
== 6) {
350 echo
= MyFTP
.GetEcho();
352 throw new XylFTPException("xylftp",
353 0, "Can't get an echo.");
355 System
.out
.println("<---"+echo
);
356 echoMeaning
= MyFTP
.ProcessEcho(echo
);
358 switch (echoMeaning
) {
360 throw new XylFTPException("panic", 0,
363 throw new XylFTPException("xylftp", 0,
365 case 1: // Need data connection
366 if (cmds
[j
].startsWith("RETR")
367 || cmds
[j
].startsWith("LIST")
368 || cmds
[j
].startsWith("NLST")){
370 if (MyFTP
.IsPassive())
371 MyFTP
.GetFilePassive();
373 MyFTP
.GetFileActive();
374 MyFTP
.CloseDataConnection();
375 } catch (Exception e
) {
376 throw new XylFTPException("", e
.getMessage());
380 toGetEchoAgain
= true;
382 if (cmds
[j
].startsWith("STOR")){
384 if (MyFTP
.IsPassive())
385 MyFTP
.SendFilePassive();
387 MyFTP
.SendFileActive();
388 MyFTP
.CloseDataConnection();
392 toGetEchoAgain
= true;
396 if (cmds
[j
].startsWith("PASS")){
401 MyFTP
.SendCommand("TYPE I");
402 String s
= MyFTP
.GetEcho();
404 MyFTP
.IsValidEcho(s
) &&
405 s
.substring(0,3).equals("200")){
409 MyFTP
.ProcessEcho(s
);
410 MyFTP
.SetTransferMode(0);
413 if (cmds
[j
].startsWith("QUIT")){
415 MyFTP
.CloseConnection();
416 if (j
+1 < cmds
.length
&&
417 cmds
[j
+1].startsWith("QUIT"))
420 if (cmds
[j
].startsWith("NLST") //For 450
421 || cmds
[j
].startsWith("LIST")){
426 if (cmds
[j
].startsWith("TYPE")){
427 String
[]cmd
= cmds
[j
].split("[\t ]+");
428 if(cmd
[1].equals("A"))
429 MyFTP
.SetTransferMode(1);
430 if(cmd
[1].equals("I"))
431 MyFTP
.SetTransferMode(0);
434 if (cmds
[j
].startsWith("PASV")){
436 HostIP
= ParseIP(echo
);
437 HostPort
= ParsePort(echo
);
439 System
.out
.println("Connect to "+HostIP
+": "+HostPort
);
441 if (cmds
[j
].startsWith("PORT")){
449 if (cmds
[j
].startsWith("RETR")
450 || cmds
[j
].startsWith("STOR")) {
455 } while (toGetEchoAgain
); // do
457 } catch (XylFTPException e
) {
458 System
.out
.println(e
.GetMessage());
459 if (e
.GetStatus()!=-1) {
460 if (MyFTP
.GetStatus()!=0 && e
.GetStatus()==0)
462 System
.out
.println("Connection closed!");
463 MyFTP
.CloseConnection();
464 } catch (Exception ee
) {
465 System
.out
.println(ee
.getMessage());
467 MyFTP
.SetStatus(e
.GetStatus());
469 } catch (Exception e
) {
470 System
.out
.println(e
.getMessage());