lftp.pp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. { lFTP CopyRight (C) 2005-2006 Ales Katona
  2. This library is Free software; you can rediStribute it and/or modify it
  3. under the terms of the GNU Library General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or (at your
  5. option) any later version.
  6. This program is diStributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
  8. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  9. for more details.
  10. You should have received a Copy of the GNU Library General Public License
  11. along with This library; if not, Write to the Free Software Foundation,
  12. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  13. This license has been modified. See File LICENSE for more inFormation.
  14. Should you find these sources withOut a LICENSE File, please contact
  15. me at [email protected]
  16. }
  17. unit lFTP;
  18. {$mode objfpc}{$H+}
  19. {$inline on}
  20. {$macro on}
  21. //{$define debug}
  22. interface
  23. uses
  24. Classes, lNet, lTelnet;
  25. type
  26. TLFTP = class;
  27. TLFTPClient = class;
  28. TLFTPStatus = (fsNone, fsCon, fsUser, fsPass, fsPasv, fsPort, fsList, fsRetr,
  29. fsStor, fsType, fsCWD, fsMKD, fsRMD, fsDEL, fsRNFR, fsRNTO,
  30. fsSYS, fsFeat, fsPWD, fsHelp, fsLast);
  31. TLFTPStatusSet = set of TLFTPStatus;
  32. TLFTPStatusRec = record
  33. Status: TLFTPStatus;
  34. Args: array[1..2] of string;
  35. end;
  36. TLFTPTransferMethod = (ftActive, ftPassive);
  37. TLFTPClientStatusEvent = procedure (aSocket: TLSocket;
  38. const aStatus: TLFTPStatus) of object;
  39. { TLFTPStatusStack }
  40. { TLFTPStatusFront }
  41. {$DEFINE __front_type__ := TLFTPStatusRec}
  42. {$i lcontainersh.inc}
  43. TLFTPStatusFront = TLFront;
  44. TLFTP = class(TLComponent, ILDirect)
  45. protected
  46. FControl: TLTelnetClient;
  47. FData: TLTcp;//TLTcpList;
  48. FSending: Boolean;
  49. FTransferMethod: TLFTPTransferMethod;
  50. function GetConnected: Boolean; virtual;
  51. function GetTimeout: DWord;
  52. procedure SetTimeout(const Value: DWord);
  53. function GetSocketClass: TLSocketClass;
  54. procedure SetSocketClass(Value: TLSocketClass);
  55. public
  56. constructor Create(aOwner: TComponent); override;
  57. destructor Destroy; override;
  58. function Get(var aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; virtual; abstract;
  59. function GetMessage(out msg: string; aSocket: TLSocket = nil): Integer; virtual; abstract;
  60. function Send(const aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; virtual; abstract;
  61. function SendMessage(const msg: string; aSocket: TLSocket = nil): Integer; virtual; abstract;
  62. public
  63. property Connected: Boolean read GetConnected;
  64. property Timeout: DWord read GetTimeout write SetTimeout;
  65. property SocketClass: TLSocketClass read GetSocketClass write SetSocketClass;
  66. property ControlConnection: TLTelnetClient read FControl;
  67. property DataConnection: TLTCP read FData;
  68. property TransferMethod: TLFTPTransferMethod read FTransferMethod write FTransferMethod;
  69. end;
  70. { TLFTPTelnetClient }
  71. TLFTPTelnetClient = class(TLTelnetClient)
  72. protected
  73. procedure React(const Operation, Command: Char); override;
  74. end;
  75. { TLFTPClient }
  76. TLFTPClient = class(TLFTP, ILClient)
  77. protected
  78. FStatus: TLFTPStatusFront;
  79. FCommandFront: TLFTPStatusFront;
  80. FStoreFile: TFileStream;
  81. FExpectedBinary: Boolean;
  82. FPipeLine: Boolean;
  83. FPassword: string;
  84. FStatusFlags: array[TLFTPStatus] of Boolean;
  85. FOnError: TLSocketErrorEvent;
  86. FOnReceive: TLSocketEvent;
  87. FOnSent: TLSocketProgressEvent;
  88. FOnControl: TLSocketEvent;
  89. FOnConnect: TLSocketEvent;
  90. FOnSuccess: TLFTPClientStatusEvent;
  91. FOnFailure: TLFTPClientStatusEvent;
  92. FChunkSize: Word;
  93. FLastPort: Word;
  94. FStartPort: Word;
  95. FStatusSet: TLFTPStatusSet;
  96. FSL: TStringList; // for evaluation, I want to prevent constant create/free
  97. procedure OnRe(aSocket: TLSocket);
  98. procedure OnDs(aSocket: TLSocket);
  99. procedure OnSe(aSocket: TLSocket);
  100. procedure OnEr(const msg: string; aSocket: TLSocket);
  101. procedure OnControlEr(const msg: string; aSocket: TLSocket);
  102. procedure OnControlRe(aSocket: TLSocket);
  103. procedure OnControlCo(aSocket: TLSocket);
  104. procedure OnControlDs(aSocket: TLSocket);
  105. function GetTransfer: Boolean;
  106. function GetEcho: Boolean;
  107. procedure SetEcho(const Value: Boolean);
  108. function GetConnected: Boolean; override;
  109. function GetBinary: Boolean;
  110. procedure SetBinary(const Value: Boolean);
  111. function CanContinue(const aStatus: TLFTPStatus; const Arg1, Arg2: string): Boolean;
  112. function CleanInput(var s: string): Integer;
  113. procedure SetStartPor(const Value: Word);
  114. procedure EvaluateAnswer(const Ans: string);
  115. procedure PasvPort;
  116. function User(const aUserName: string): Boolean;
  117. function Password(const aPassword: string): Boolean;
  118. procedure SendChunk(const Event: Boolean);
  119. procedure ExecuteFrontCommand;
  120. public
  121. constructor Create(aOwner: TComponent); override;
  122. destructor Destroy; override;
  123. function Get(var aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; override;
  124. function GetMessage(out msg: string; aSocket: TLSocket = nil): Integer; override;
  125. function Send(const aData; const aSize: Integer; aSocket: TLSocket = nil): Integer; override;
  126. function SendMessage(const msg: string; aSocket: TLSocket = nil): Integer; override;
  127. function Connect(const aHost: string; const aPort: Word = 21): Boolean; virtual; overload;
  128. function Connect: Boolean; virtual; overload;
  129. function Authenticate(const aUsername, aPassword: string): Boolean;
  130. function GetData(var aData; const aSize: Integer): Integer;
  131. function GetDataMessage: string;
  132. function Retrieve(const FileName: string): Boolean;
  133. function Put(const FileName: string): Boolean; virtual; // because of LCLsocket
  134. function ChangeDirectory(const DestPath: string): Boolean;
  135. function MakeDirectory(const DirName: string): Boolean;
  136. function RemoveDirectory(const DirName: string): Boolean;
  137. function DeleteFile(const FileName: string): Boolean;
  138. function Rename(const FromName, ToName: string): Boolean;
  139. public
  140. procedure List(const FileName: string = '');
  141. procedure Nlst(const FileName: string = '');
  142. procedure SystemInfo;
  143. procedure FeatureList;
  144. procedure PresentWorkingDirectory;
  145. procedure Help(const Arg: string);
  146. procedure Disconnect; override;
  147. procedure CallAction; override;
  148. public
  149. property StatusSet: TLFTPStatusSet read FStatusSet write FStatusSet;
  150. property ChunkSize: Word read FChunkSize write FChunkSize;
  151. property Binary: Boolean read GetBinary write SetBinary;
  152. property PipeLine: Boolean read FPipeLine write FPipeLine;
  153. property Echo: Boolean read GetEcho write SetEcho;
  154. property StartPort: Word read FStartPort write FStartPort;
  155. property Transfer: Boolean read GetTransfer;
  156. property OnError: TLSocketErrorEvent read FOnError write FOnError;
  157. property OnConnect: TLSocketEvent read FOnConnect write FOnConnect;
  158. property OnSent: TLSocketProgressEvent read FOnSent write FOnSent;
  159. property OnReceive: TLSocketEvent read FOnReceive write FOnReceive;
  160. property OnControl: TLSocketEvent read FOnControl write FOnControl;
  161. property OnSuccess: TLFTPClientStatusEvent read FOnSuccess write FOnSuccess;
  162. property OnFailure: TLFTPClientStatusEvent read FOnFailure write FOnFailure;
  163. end;
  164. function FTPStatusToStr(const aStatus: TLFTPStatus): string;
  165. implementation
  166. uses
  167. SysUtils;
  168. const
  169. FLE = #13#10;
  170. DEFAULT_PORT = 1024;
  171. EMPTY_REC: TLFTPStatusRec = (Status: fsNone; Args: ('', ''));
  172. FTPStatusStr: array[TLFTPStatus] of string = ('None', 'Connect', 'Authenticate', 'Password',
  173. 'Passive', 'Active', 'List', 'Retrieve',
  174. 'Store', 'Type', 'CWD', 'MKDIR',
  175. 'RMDIR', 'Delete', 'RenameFrom',
  176. 'RenameTo', 'System', 'Features',
  177. 'PWD', 'HELP', 'LAST');
  178. procedure Writedbg(const ar: array of const);
  179. {$ifdef debug}
  180. var
  181. i: Integer;
  182. begin
  183. if High(ar) >= 0 then
  184. for i := 0 to High(ar) do
  185. case ar[i].vtype of
  186. vtInteger: Write(ar[i].vinteger);
  187. vtString: Write(ar[i].vstring^);
  188. vtAnsiString: Write(AnsiString(ar[i].vpointer));
  189. vtBoolean: Write(ar[i].vboolean);
  190. vtChar: Write(ar[i].vchar);
  191. vtExtended: Write(Extended(ar[i].vpointer^));
  192. end;
  193. Writeln;
  194. end;
  195. {$else}
  196. begin
  197. end;
  198. {$endif}
  199. function MakeStatusRec(const aStatus: TLFTPStatus; const Arg1, Arg2: string): TLFTPStatusRec;
  200. begin
  201. Result.Status := aStatus;
  202. Result.Args[1] := Arg1;
  203. Result.Args[2] := Arg2;
  204. end;
  205. function FTPStatusToStr(const aStatus: TLFTPStatus): string;
  206. begin
  207. Result := FTPStatusStr[aStatus];
  208. end;
  209. {$i lcontainers.inc}
  210. { TLFTP }
  211. function TLFTP.GetConnected: Boolean;
  212. begin
  213. Result := FControl.Connected;
  214. end;
  215. function TLFTP.GetTimeout: DWord;
  216. begin
  217. Result := FControl.Timeout;
  218. end;
  219. procedure TLFTP.SetTimeout(const Value: DWord);
  220. begin
  221. FControl.Timeout := Value;
  222. FData.Timeout := Value;
  223. end;
  224. function TLFTP.GetSocketClass: TLSocketClass;
  225. begin
  226. Result := FControl.SocketClass;
  227. end;
  228. procedure TLFTP.SetSocketClass(Value: TLSocketClass);
  229. begin
  230. FControl.SocketClass := Value;
  231. FData.SocketClass := Value;
  232. end;
  233. constructor TLFTP.Create(aOwner: TComponent);
  234. begin
  235. inherited Create(aOwner);
  236. FHost := '';
  237. FPort := 21;
  238. FControl := TLFTPTelnetClient.Create(nil);
  239. FData := TLTcp.Create(nil);
  240. FTransferMethod := ftPassive; // let's be modern
  241. end;
  242. destructor TLFTP.Destroy;
  243. begin
  244. FControl.Free;
  245. FData.Free;
  246. inherited Destroy;
  247. end;
  248. { TLFTPTelnetClient }
  249. procedure TLFTPTelnetClient.React(const Operation, Command: Char);
  250. begin
  251. // don't do a FUCK since they broke Telnet in FTP as per-usual
  252. end;
  253. { TLFTPClient }
  254. constructor TLFTPClient.Create(aOwner: TComponent);
  255. const
  256. DEFAULT_CHUNK = 8192;
  257. var
  258. s: TLFTPStatus;
  259. begin
  260. inherited Create(aOwner);
  261. FControl.OnReceive := @OnControlRe;
  262. FControl.OnConnect := @OnControlCo;
  263. FControl.OnError := @OnControlEr;
  264. FControl.OnDisconnect := @OnControlDs;
  265. FData.OnReceive := @OnRe;
  266. FData.OnDisconnect := @OnDs;
  267. FData.OnCanSend := @OnSe;
  268. FData.OnError := @OnEr;
  269. FStatusSet := []; // empty Event set
  270. FPassWord := '';
  271. FChunkSize := DEFAULT_CHUNK;
  272. FStartPort := DEFAULT_PORT;
  273. FSL := TStringList.Create;
  274. FLastPort := FStartPort;
  275. for s := fsNone to fsDEL do
  276. FStatusFlags[s] := False;
  277. FStatus := TLFTPStatusFront.Create(EMPTY_REC);
  278. FCommandFront := TLFTPStatusFront.Create(EMPTY_REC);
  279. FStoreFile := nil;
  280. end;
  281. destructor TLFTPClient.Destroy;
  282. begin
  283. Disconnect;
  284. FSL.Free;
  285. FStatus.Free;
  286. FCommandFront.Free;
  287. if Assigned(FStoreFile) then
  288. FreeAndNil(FStoreFile);
  289. inherited Destroy;
  290. end;
  291. procedure TLFTPClient.OnRe(aSocket: TLSocket);
  292. begin
  293. if Assigned(FOnReceive) then
  294. FOnReceive(aSocket);
  295. end;
  296. procedure TLFTPClient.OnDs(aSocket: TLSocket);
  297. begin
  298. FSending := False;
  299. Writedbg(['Disconnected']);
  300. end;
  301. procedure TLFTPClient.OnSe(aSocket: TLSocket);
  302. begin
  303. if Connected and FSending then
  304. SendChunk(True);
  305. end;
  306. procedure TLFTPClient.OnEr(const msg: string; aSocket: TLSocket);
  307. begin
  308. FSending := False;
  309. if Assigned(FOnError) then
  310. FOnError(msg, aSocket);
  311. end;
  312. procedure TLFTPClient.OnControlEr(const msg: string; aSocket: TLSocket);
  313. begin
  314. FSending := False;
  315. if Assigned(FOnError) then
  316. FOnError(msg, aSocket);
  317. end;
  318. procedure TLFTPClient.OnControlRe(aSocket: TLSocket);
  319. begin
  320. if Assigned(FOnControl) then
  321. FOnControl(aSocket);
  322. end;
  323. procedure TLFTPClient.OnControlCo(aSocket: TLSocket);
  324. begin
  325. if Assigned(FOnConnect) then
  326. FOnConnect(aSocket);
  327. end;
  328. procedure TLFTPClient.OnControlDs(aSocket: TLSocket);
  329. begin
  330. if Assigned(FOnError) then
  331. FOnError('Connection lost', aSocket);
  332. end;
  333. function TLFTPClient.GetTransfer: Boolean;
  334. begin
  335. Result := FData.Connected;
  336. end;
  337. function TLFTPClient.GetEcho: Boolean;
  338. begin
  339. Result := FControl.OptionIsSet(TS_ECHO);
  340. end;
  341. function TLFTPClient.GetConnected: Boolean;
  342. begin
  343. Result := FStatusFlags[fsCon] and inherited;
  344. end;
  345. function TLFTPClient.GetBinary: Boolean;
  346. begin
  347. Result := FStatusFlags[fsType];
  348. end;
  349. function TLFTPClient.CanContinue(const aStatus: TLFTPStatus; const Arg1,
  350. Arg2: string): Boolean;
  351. begin
  352. Result := FPipeLine or FStatus.Empty;
  353. if not Result then
  354. FCommandFront.Insert(MakeStatusRec(aStatus, Arg1, Arg2));
  355. end;
  356. function TLFTPClient.CleanInput(var s: string): Integer;
  357. var
  358. i: Integer;
  359. begin
  360. FSL.Text := s;
  361. if FSL.Count > 0 then
  362. for i := 0 to FSL.Count-1 do
  363. if Length(FSL[i]) > 0 then EvaluateAnswer(FSL[i]);
  364. s := StringReplace(s, FLE, LineEnding, [rfReplaceAll]);
  365. i := Pos('PASS', s);
  366. if i > 0 then
  367. s := Copy(s, 1, i-1) + 'PASS';
  368. Result := Length(s);
  369. end;
  370. procedure TLFTPClient.SetStartPor(const Value: Word);
  371. begin
  372. FStartPort := Value;
  373. if Value > FLastPort then
  374. FLastPort := Value;
  375. end;
  376. procedure TLFTPClient.SetEcho(const Value: Boolean);
  377. begin
  378. if Value then
  379. FControl.SetOption(TS_ECHO)
  380. else
  381. FControl.UnSetOption(TS_ECHO);
  382. end;
  383. procedure TLFTPClient.SetBinary(const Value: Boolean);
  384. const
  385. TypeBool: array[Boolean] of string = ('A', 'I');
  386. begin
  387. if CanContinue(fsType, BoolToStr(Value), '') then begin
  388. FExpectedBinary := Value;
  389. FControl.SendMessage('TYPE ' + TypeBool[Value] + FLE);
  390. FStatus.Insert(MakeStatusRec(fsType, '', ''));
  391. end;
  392. end;
  393. procedure TLFTPClient.EvaluateAnswer(const Ans: string);
  394. function GetNum: Integer;
  395. begin
  396. try
  397. Result := StrToInt(Copy(Ans, 1, 3));
  398. except
  399. Result := -1;
  400. end;
  401. end;
  402. procedure ParsePortIP(s: string);
  403. var
  404. i, l: Integer;
  405. aIP: string;
  406. aPort: Word;
  407. sl: TStringList;
  408. begin
  409. if Length(s) >= 15 then begin
  410. sl := TStringList.Create;
  411. for i := Length(s) downto 5 do
  412. if s[i] = ',' then Break;
  413. while (i <= Length(s)) and (s[i] in ['0'..'9', ',']) do Inc(i);
  414. if not (s[i] in ['0'..'9', ',']) then Dec(i);
  415. l := 0;
  416. while s[i] in ['0'..'9', ','] do begin
  417. Inc(l);
  418. Dec(i);
  419. end;
  420. Inc(i);
  421. s := Copy(s, i, l);
  422. sl.CommaText := s;
  423. aIP := sl[0] + '.' + sl[1] + '.' + sl[2] + '.' + sl[3];
  424. try
  425. aPort := (StrToInt(sl[4]) * 256) + StrToInt(sl[5]);
  426. except
  427. aPort := 0;
  428. end;
  429. Writedbg(['Server PASV addr/port - ', aIP, ' : ', aPort]);
  430. if (aPort > 0) and FData.Connect(aIP, aPort) then
  431. Writedbg(['Connected after PASV']);
  432. sl.Free;
  433. FStatus.Remove;
  434. end;
  435. end;
  436. procedure SendFile;
  437. begin
  438. FStoreFile.Position := 0;
  439. FSending := True;
  440. SendChunk(False);
  441. end;
  442. function ValidResponse(const Answer: string): Boolean; inline;
  443. begin
  444. Result := (Length(Ans) >= 3) and
  445. (Ans[1] in ['1'..'5']) and
  446. (Ans[2] in ['0'..'9']) and
  447. (Ans[3] in ['0'..'9']);
  448. if Result then
  449. Result := (Length(Ans) = 3) or ((Length(Ans) > 3) and (Ans[4] = ' '));
  450. end;
  451. procedure Eventize(const aStatus: TLFTPStatus; const Res: Boolean);
  452. begin
  453. if Res then begin
  454. if Assigned(FOnSuccess) and (aStatus in FStatusSet) then
  455. FOnSuccess(FData.Iterator, aStatus);
  456. end else begin
  457. if Assigned(FOnFailure) and (aStatus in FStatusSet) then
  458. FOnFailure(FData.Iterator, aStatus);
  459. end;
  460. end;
  461. var
  462. x: Integer;
  463. begin
  464. x := GetNum;
  465. Writedbg(['WOULD EVAL: ', FTPStatusStr[FStatus.First.Status], ' with value: ',
  466. x, ' from "', Ans, '"']);
  467. if ValidResponse(Ans) then
  468. if not FStatus.Empty then begin
  469. Writedbg(['EVAL: ', FTPStatusStr[FStatus.First.Status], ' with value: ', x]);
  470. case FStatus.First.Status of
  471. fsCon : case x of
  472. 220:
  473. begin
  474. FStatusFlags[FStatus.First.Status] := True;
  475. Eventize(FStatus.First.Status, True);
  476. FStatus.Remove;
  477. end;
  478. else
  479. begin
  480. FStatusFlags[FStatus.First.Status] := False;
  481. Eventize(FStatus.First.Status, False);
  482. FStatus.Remove;
  483. end;
  484. end;
  485. fsUser : case x of
  486. 230:
  487. begin
  488. FStatusFlags[FStatus.First.Status] := True;
  489. Eventize(FStatus.First.Status, True);
  490. FStatus.Remove;
  491. end;
  492. 331,
  493. 332:
  494. begin
  495. FStatus.Remove;
  496. Password(FPassword);
  497. end;
  498. else
  499. begin
  500. FStatusFlags[FStatus.First.Status] := False;
  501. Eventize(FStatus.First.Status, False);
  502. FStatus.Remove;
  503. end;
  504. end;
  505. fsPass : case x of
  506. 230:
  507. begin
  508. FStatusFlags[FStatus.First.Status] := True;
  509. Eventize(FStatus.First.Status, True);
  510. FStatus.Remove;
  511. end;
  512. else
  513. begin
  514. FStatusFlags[FStatus.First.Status] := False;
  515. Eventize(FStatus.First.Status, False);
  516. FStatus.Remove;
  517. end;
  518. end;
  519. fsPasv : case x of
  520. 227: ParsePortIP(Ans);
  521. 300..600: FStatus.Remove;
  522. end;
  523. fsPort : case x of
  524. 200:
  525. begin
  526. Eventize(FStatus.First.Status, True);
  527. FStatus.Remove;
  528. end;
  529. else
  530. begin
  531. Eventize(FStatus.First.Status, False);
  532. FStatus.Remove;
  533. end;
  534. end;
  535. fsType : case x of
  536. 200:
  537. begin
  538. FStatusFlags[FStatus.First.Status] := FExpectedBinary;
  539. Writedbg(['Binary mode: ', FExpectedBinary]);
  540. Eventize(FStatus.First.Status, True);
  541. FStatus.Remove;
  542. end;
  543. else
  544. begin
  545. Eventize(FStatus.First.Status, False);
  546. FStatus.Remove;
  547. end;
  548. end;
  549. fsRetr : case x of
  550. 125, 150: begin { Do nothing } end;
  551. 226:
  552. begin
  553. Eventize(FStatus.First.Status, True);
  554. FStatus.Remove;
  555. end;
  556. else
  557. begin
  558. FData.Disconnect;
  559. Writedbg(['Disconnecting data connection']);
  560. Eventize(FStatus.First.Status, False);
  561. FStatus.Remove; // error after connection established
  562. end;
  563. end;
  564. fsStor : case x of
  565. 125, 150: SendFile;
  566. 226:
  567. begin
  568. Eventize(FStatus.First.Status, True);
  569. FStatus.Remove;
  570. end;
  571. else
  572. begin
  573. Eventize(FStatus.First.Status, True);
  574. FStatus.Remove;
  575. end;
  576. end;
  577. fsCWD : case x of
  578. 200, 250:
  579. begin
  580. FStatusFlags[FStatus.First.Status] := True;
  581. Eventize(FStatus.First.Status, True);
  582. FStatus.Remove;
  583. end;
  584. else
  585. begin
  586. FStatusFlags[FStatus.First.Status] := False;
  587. Eventize(FStatus.First.Status, False);
  588. FStatus.Remove;
  589. end;
  590. end;
  591. fsList : case x of
  592. 125, 150: begin { do nothing } end;
  593. 226:
  594. begin
  595. Eventize(FStatus.First.Status, True);
  596. FStatus.Remove;
  597. end;
  598. else
  599. begin
  600. Eventize(FStatus.First.Status, False);
  601. FStatus.Remove;
  602. end;
  603. end;
  604. fsMKD : case x of
  605. 250, 257:
  606. begin
  607. FStatusFlags[FStatus.First.Status] := True;
  608. Eventize(FStatus.First.Status, True);
  609. FStatus.Remove;
  610. end;
  611. else
  612. begin
  613. FStatusFlags[FStatus.First.Status] := False;
  614. Eventize(FStatus.First.Status, False);
  615. FStatus.Remove;
  616. end;
  617. end;
  618. fsRMD,
  619. fsDEL : case x of
  620. 250:
  621. begin
  622. FStatusFlags[FStatus.First.Status] := True;
  623. Eventize(FStatus.First.Status, True);
  624. FStatus.Remove;
  625. end;
  626. else
  627. begin
  628. FStatusFlags[FStatus.First.Status] := False;
  629. Eventize(FStatus.First.Status, False);
  630. FStatus.Remove;
  631. end;
  632. end;
  633. fsRNFR : case x of
  634. 350:
  635. begin
  636. FStatusFlags[FStatus.First.Status] := True;
  637. Eventize(FStatus.First.Status, True);
  638. FStatus.Remove;
  639. end;
  640. else
  641. begin
  642. Eventize(FStatus.First.Status, False);
  643. FStatus.Remove;
  644. end;
  645. end;
  646. fsRNTO : case x of
  647. 250:
  648. begin
  649. FStatusFlags[FStatus.First.Status] := True;
  650. Eventize(FStatus.First.Status, True);
  651. FStatus.Remove;
  652. end;
  653. else
  654. begin
  655. Eventize(FStatus.First.Status, False);
  656. FStatus.Remove;
  657. end;
  658. end;
  659. end;
  660. end;
  661. if FStatus.Empty and not FCommandFront.Empty then
  662. ExecuteFrontCommand;
  663. end;
  664. procedure TLFTPClient.PasvPort;
  665. function StringPair(const aPort: Word): string;
  666. begin
  667. Result := IntToStr(aPort div 256);
  668. Result := Result + ',' + IntToStr(aPort mod 256);
  669. end;
  670. function StringIP: string;
  671. begin
  672. Result := StringReplace(FControl.Connection.Iterator.LocalAddress, '.', ',',
  673. [rfReplaceAll]) + ',';
  674. end;
  675. begin
  676. if FTransferMethod = ftActive then begin
  677. Writedbg(['Sent PORT']);
  678. FData.Disconnect;
  679. FData.Listen(FLastPort);
  680. FControl.SendMessage('PORT ' + StringIP + StringPair(FLastPort) + FLE);
  681. FStatus.Insert(MakeStatusRec(fsPort, '', ''));
  682. if FLastPort < 65535 then
  683. Inc(FLastPort)
  684. else
  685. FLastPort := FStartPort;
  686. end else begin
  687. Writedbg(['Sent PASV']);
  688. FControl.SendMessage('PASV' + FLE);
  689. FStatus.Insert(MakeStatusRec(fsPasv, '', ''));
  690. end;
  691. end;
  692. function TLFTPClient.User(const aUserName: string): Boolean;
  693. begin
  694. Result := not FPipeLine;
  695. if CanContinue(fsUser, aUserName, '') then begin
  696. FControl.SendMessage('USER ' + aUserName + FLE);
  697. FStatus.Insert(MakeStatusRec(fsUser, '', ''));
  698. Result := True;
  699. end;
  700. end;
  701. function TLFTPClient.Password(const aPassword: string): Boolean;
  702. begin
  703. Result := not FPipeLine;
  704. if CanContinue(fsPass, aPassword, '') then begin
  705. FControl.SendMessage('PASS ' + aPassword + FLE);
  706. FStatus.Insert(MakeStatusRec(fsPass, '', ''));
  707. Result := True;
  708. end;
  709. end;
  710. procedure TLFTPClient.SendChunk(const Event: Boolean);
  711. var
  712. Buf: array[0..65535] of Byte;
  713. n: Integer;
  714. Sent: Integer;
  715. begin
  716. repeat
  717. n := FStoreFile.Read(Buf, FChunkSize);
  718. if n > 0 then begin
  719. Sent := FData.Send(Buf, n);
  720. if Event and Assigned(FOnSent) and (Sent > 0) then
  721. FOnSent(FData.Iterator, Sent);
  722. if Sent < n then
  723. FStoreFile.Position := FStoreFile.Position - (n - Sent); // so it's tried next time
  724. end else begin
  725. if Assigned(FOnSent) then
  726. FOnSent(FData.Iterator, 0);
  727. FreeAndNil(FStoreFile);
  728. FSending := False;
  729. {$hint this one calls freeinstance which doesn't pass}
  730. FData.Disconnect;
  731. end;
  732. until (n = 0) or (Sent = 0);
  733. end;
  734. procedure TLFTPClient.ExecuteFrontCommand;
  735. begin
  736. with FCommandFront.First do
  737. case Status of
  738. fsNone : Exit;
  739. fsUser : User(Args[1]);
  740. fsPass : Password(Args[1]);
  741. fsList : List(Args[1]);
  742. fsRetr : Retrieve(Args[1]);
  743. fsStor : Put(Args[1]);
  744. fsCWD : ChangeDirectory(Args[1]);
  745. fsMKD : MakeDirectory(Args[1]);
  746. fsRMD : RemoveDirectory(Args[1]);
  747. fsDEL : DeleteFile(Args[1]);
  748. fsRNFR : Rename(Args[1], Args[2]);
  749. fsSYS : SystemInfo;
  750. fsPWD : PresentWorkingDirectory;
  751. fsHelp : Help(Args[1]);
  752. fsType : SetBinary(StrToBool(Args[1]));
  753. fsFeat : FeatureList;
  754. end;
  755. FCommandFront.Remove;
  756. end;
  757. function TLFTPClient.Get(var aData; const aSize: Integer; aSocket: TLSocket): Integer;
  758. var
  759. s: string;
  760. begin
  761. Result := FControl.Get(aData, aSize, aSocket);
  762. if Result > 0 then begin
  763. SetLength(s, Result);
  764. Move(aData, PChar(s)^, Result);
  765. CleanInput(s);
  766. end;
  767. end;
  768. function TLFTPClient.GetMessage(out msg: string; aSocket: TLSocket): Integer;
  769. begin
  770. Result := FControl.GetMessage(msg, aSocket);
  771. if Result > 0 then
  772. Result := CleanInput(msg);
  773. end;
  774. function TLFTPClient.Send(const aData; const aSize: Integer; aSocket: TLSocket
  775. ): Integer;
  776. begin
  777. Result := FControl.Send(aData, aSize);
  778. end;
  779. function TLFTPClient.SendMessage(const msg: string; aSocket: TLSocket
  780. ): Integer;
  781. begin
  782. Result := FControl.SendMessage(msg);
  783. end;
  784. function TLFTPClient.GetData(var aData; const aSize: Integer): Integer;
  785. begin
  786. Result := FData.Iterator.Get(aData, aSize);
  787. end;
  788. function TLFTPClient.GetDataMessage: string;
  789. begin
  790. Result := '';
  791. if Assigned(FData.Iterator) then
  792. FData.Iterator.GetMessage(Result);
  793. end;
  794. function TLFTPClient.Connect(const aHost: string; const aPort: Word): Boolean;
  795. begin
  796. Result := False;
  797. Disconnect;
  798. if FControl.Connect(aHost, aPort) then begin
  799. FHost := aHost;
  800. FPort := aPort;
  801. FStatus.Insert(MakeStatusRec(fsCon, '', ''));
  802. Result := True;
  803. end;
  804. if FData.Eventer <> FControl.Connection.Eventer then
  805. FData.Eventer := FControl.Connection.Eventer;
  806. end;
  807. function TLFTPClient.Connect: Boolean;
  808. begin
  809. Result := Connect(FHost, FPort);
  810. end;
  811. function TLFTPClient.Authenticate(const aUsername, aPassword: string): Boolean;
  812. begin
  813. FPassword := aPassWord;
  814. Result := User(aUserName);
  815. end;
  816. function TLFTPClient.Retrieve(const FileName: string): Boolean;
  817. begin
  818. Result := not FPipeLine;
  819. if CanContinue(fsRetr, FileName, '') then begin
  820. PasvPort;
  821. FControl.SendMessage('RETR ' + FileName + FLE);
  822. FStatus.Insert(MakeStatusRec(fsRetr, '', ''));
  823. Result := True;
  824. end;
  825. end;
  826. function TLFTPClient.Put(const FileName: string): Boolean;
  827. begin
  828. Result := not FPipeLine;
  829. if FileExists(FileName) and CanContinue(fsStor, FileName, '') then begin
  830. FStoreFile := TFileStream.Create(FileName, fmOpenRead);
  831. PasvPort;
  832. FControl.SendMessage('STOR ' + ExtractFileName(FileName) + FLE);
  833. FStatus.Insert(MakeStatusRec(fsStor, '', ''));
  834. Result := True;
  835. end;
  836. end;
  837. function TLFTPClient.ChangeDirectory(const DestPath: string): Boolean;
  838. begin
  839. Result := not FPipeLine;
  840. if CanContinue(fsCWD, DestPath, '') then begin
  841. FControl.SendMessage('CWD ' + DestPath + FLE);
  842. FStatus.Insert(MakeStatusRec(fsCWD, '', ''));
  843. FStatusFlags[fsCWD] := False;
  844. Result := True;
  845. end;
  846. end;
  847. function TLFTPClient.MakeDirectory(const DirName: string): Boolean;
  848. begin
  849. Result := not FPipeLine;
  850. if CanContinue(fsMKD, DirName, '') then begin
  851. FControl.SendMessage('MKD ' + DirName + FLE);
  852. FStatus.Insert(MakeStatusRec(fsMKD, '', ''));
  853. FStatusFlags[fsMKD] := False;
  854. Result := True;
  855. end;
  856. end;
  857. function TLFTPClient.RemoveDirectory(const DirName: string): Boolean;
  858. begin
  859. Result := not FPipeLine;
  860. if CanContinue(fsRMD, DirName, '') then begin
  861. FControl.SendMessage('RMD ' + DirName + FLE);
  862. FStatus.Insert(MakeStatusRec(fsRMD, '', ''));
  863. FStatusFlags[fsRMD] := False;
  864. Result := True;
  865. end;
  866. end;
  867. function TLFTPClient.DeleteFile(const FileName: string): Boolean;
  868. begin
  869. Result := not FPipeLine;
  870. if CanContinue(fsDEL, FileName, '') then begin
  871. FControl.SendMessage('DELE ' + FileName + FLE);
  872. FStatus.Insert(MakeStatusRec(fsDEL, '', ''));
  873. FStatusFlags[fsDEL] := False;
  874. Result := True;
  875. end;
  876. end;
  877. function TLFTPClient.Rename(const FromName, ToName: string): Boolean;
  878. begin
  879. Result := not FPipeLine;
  880. if CanContinue(fsRNFR, FromName, ToName) then begin
  881. FControl.SendMessage('RNFR ' + FromName + FLE);
  882. FStatus.Insert(MakeStatusRec(fsRNFR, '', ''));
  883. FStatusFlags[fsRNFR] := False;
  884. FControl.SendMessage('RNTO ' + ToName + FLE);
  885. FStatus.Insert(MakeStatusRec(fsRNTO, '', ''));
  886. FStatusFlags[fsRNTO] := False;
  887. Result := True;
  888. end;
  889. end;
  890. procedure TLFTPClient.List(const FileName: string = '');
  891. begin
  892. if CanContinue(fsList, FileName, '') then begin
  893. PasvPort;
  894. FStatus.Insert(MakeStatusRec(fsList, '', ''));
  895. if Length(FileName) > 0 then
  896. FControl.SendMessage('LIST ' + FileName + FLE)
  897. else
  898. FControl.SendMessage('LIST' + FLE);
  899. end;
  900. end;
  901. procedure TLFTPClient.Nlst(const FileName: string);
  902. begin
  903. if CanContinue(fsList, FileName, '') then begin
  904. PasvPort;
  905. FStatus.Insert(MakeStatusRec(fsList, '', ''));
  906. if Length(FileName) > 0 then
  907. FControl.SendMessage('NLST ' + FileName + FLE)
  908. else
  909. FControl.SendMessage('NLST' + FLE);
  910. end;
  911. end;
  912. procedure TLFTPClient.SystemInfo;
  913. begin
  914. if CanContinue(fsSYS, '', '') then
  915. FControl.SendMessage('SYST' + FLE);
  916. end;
  917. procedure TLFTPClient.FeatureList;
  918. begin
  919. if CanContinue(fsFeat, '', '') then
  920. FControl.SendMessage('FEAT' + FLE);
  921. end;
  922. procedure TLFTPClient.PresentWorkingDirectory;
  923. begin
  924. if CanContinue(fsPWD, '', '') then
  925. FControl.SendMessage('PWD' + FLE);
  926. end;
  927. procedure TLFTPClient.Help(const Arg: string);
  928. begin
  929. if CanContinue(fsHelp, Arg, '') then
  930. FControl.SendMessage('HELP ' + Arg + FLE);
  931. end;
  932. procedure TLFTPClient.Disconnect;
  933. var
  934. s: TLFTPStatus;
  935. begin
  936. FControl.Disconnect;
  937. FStatus.Clear;
  938. FData.Disconnect;
  939. FLastPort := FStartPort;
  940. for s := fsNone to fsLast do
  941. FStatusFlags[s] := False;
  942. FCommandFront.Clear;
  943. end;
  944. procedure TLFTPClient.CallAction;
  945. begin
  946. TLFTPTelnetClient(FControl).CallAction;
  947. end;
  948. initialization
  949. Randomize;
  950. end.