1 ////////////////////////////////////////////////////////////
\r
3 // SFML - Simple and Fast Multimedia Library
\r
4 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
\r
6 // This software is provided 'as-is', without any express or implied warranty.
\r
7 // In no event will the authors be held liable for any damages arising from the use of this software.
\r
9 // Permission is granted to anyone to use this software for any purpose,
\r
10 // including commercial applications, and to alter it and redistribute it freely,
\r
11 // subject to the following restrictions:
\r
13 // 1. The origin of this software must not be misrepresented;
\r
14 // you must not claim that you wrote the original software.
\r
15 // If you use this software in a product, an acknowledgment
\r
16 // in the product documentation would be appreciated but is not required.
\r
18 // 2. Altered source versions must be plainly marked as such,
\r
19 // and must not be misrepresented as being the original software.
\r
21 // 3. This notice may not be removed or altered from any source distribution.
\r
23 ////////////////////////////////////////////////////////////
\r
25 #ifndef SFML_FTP_HPP
\r
26 #define SFML_FTP_HPP
\r
28 ////////////////////////////////////////////////////////////
\r
30 ////////////////////////////////////////////////////////////
\r
31 #include <SFML/System/NonCopyable.hpp>
\r
32 #include <SFML/Network/SocketTCP.hpp>
\r
41 ////////////////////////////////////////////////////////////
\r
42 /// This class provides methods for manipulating the FTP
\r
43 /// protocol (described in RFC 959).
\r
44 /// It provides easy access and transfers to remote
\r
45 /// directories and files on a FTP server
\r
46 ////////////////////////////////////////////////////////////
\r
47 class SFML_API Ftp : NonCopyable
\r
51 ////////////////////////////////////////////////////////////
\r
52 /// Enumeration of transfer modes
\r
53 ////////////////////////////////////////////////////////////
\r
56 Binary, ///< Binary mode (file is transfered as a sequence of bytes)
\r
57 Ascii, ///< Text mode using ASCII encoding
\r
58 Ebcdic ///< Text mode using EBCDIC encoding
\r
61 ////////////////////////////////////////////////////////////
\r
62 /// This class wraps a FTP response, which is basically :
\r
65 ////////////////////////////////////////////////////////////
\r
66 class SFML_API Response
\r
70 ////////////////////////////////////////////////////////////
\r
71 /// Enumerate all the valid status codes returned in
\r
73 ////////////////////////////////////////////////////////////
\r
76 // 1xx: the requested action is being initiated,
\r
77 // expect another reply before proceeding with a new command
\r
78 RestartMarkerReply = 110, ///< Restart marker reply
\r
79 ServiceReadySoon = 120, ///< Service ready in N minutes
\r
80 DataConnectionAlreadyOpened = 125, ///< Data connection already opened, transfer starting
\r
81 OpeningDataConnection = 150, ///< File status ok, about to open data connection
\r
83 // 2xx: the requested action has been successfully completed
\r
84 Ok = 200, ///< Command ok
\r
85 PointlessCommand = 202, ///< Command not implemented
\r
86 SystemStatus = 211, ///< System status, or system help reply
\r
87 DirectoryStatus = 212, ///< Directory status
\r
88 FileStatus = 213, ///< File status
\r
89 HelpMessage = 214, ///< Help message
\r
90 SystemType = 215, ///< NAME system type, where NAME is an official system name from the list in the Assigned Numbers document
\r
91 ServiceReady = 220, ///< Service ready for new user
\r
92 ClosingConnection = 221, ///< Service closing control connection
\r
93 DataConnectionOpened = 225, ///< Data connection open, no transfer in progress
\r
94 ClosingDataConnection = 226, ///< Closing data connection, requested file action successful
\r
95 EnteringPassiveMode = 227, ///< Entering passive mode
\r
96 LoggedIn = 230, ///< User logged in, proceed. Logged out if appropriate
\r
97 FileActionOk = 250, ///< Requested file action ok
\r
98 DirectoryOk = 257, ///< PATHNAME created
\r
100 // 3xx: the command has been accepted, but the requested action
\r
101 // is dormant, pending receipt of further information
\r
102 NeedPassword = 331, ///< User name ok, need password
\r
103 NeedAccountToLogIn = 332, ///< Need account for login
\r
104 NeedInformation = 350, ///< Requested file action pending further information
\r
106 // 4xx: the command was not accepted and the requested action did not take place,
\r
107 // but the error condition is temporary and the action may be requested again
\r
108 ServiceUnavailable = 421, ///< Service not available, closing control connection
\r
109 DataConnectionUnavailable = 425, ///< Can't open data connection
\r
110 TransferAborted = 426, ///< Connection closed, transfer aborted
\r
111 FileActionAborted = 450, ///< Requested file action not taken
\r
112 LocalError = 451, ///< Requested action aborted, local error in processing
\r
113 InsufficientStorageSpace = 452, ///< Requested action not taken; insufficient storage space in system, file unavailable
\r
115 // 5xx: the command was not accepted and
\r
116 // the requested action did not take place
\r
117 CommandUnknown = 500, ///< Syntax error, command unrecognized
\r
118 ParametersUnknown = 501, ///< Syntax error in parameters or arguments
\r
119 CommandNotImplemented = 502, ///< Command not implemented
\r
120 BadCommandSequence = 503, ///< Bad sequence of commands
\r
121 ParameterNotImplemented = 504, ///< Command not implemented for that parameter
\r
122 NotLoggedIn = 530, ///< Not logged in
\r
123 NeedAccountToStore = 532, ///< Need account for storing files
\r
124 FileUnavailable = 550, ///< Requested action not taken, file unavailable
\r
125 PageTypeUnknown = 551, ///< Requested action aborted, page type unknown
\r
126 NotEnoughMemory = 552, ///< Requested file action aborted, exceeded storage allocation
\r
127 FilenameNotAllowed = 553, ///< Requested action not taken, file name not allowed
\r
129 // 10xx: SFML custom codes
\r
130 InvalidResponse = 1000, ///< Response is not a valid FTP one
\r
131 ConnectionFailed = 1001, ///< Connection with server failed
\r
132 ConnectionClosed = 1002, ///< Connection with server closed
\r
133 InvalidFile = 1003 ///< Invalid file to upload / download
\r
136 ////////////////////////////////////////////////////////////
\r
137 /// Default constructor
\r
139 /// \param Code : Response status code (InvalidResponse by default)
\r
140 /// \param Message : Response message (empty by default)
\r
142 ////////////////////////////////////////////////////////////
\r
143 Response(Status Code = InvalidResponse, const std::string& Message = "");
\r
145 ////////////////////////////////////////////////////////////
\r
146 /// Convenience function to check if the response status code
\r
147 /// means a success
\r
149 /// \return True if status is success (code < 400)
\r
151 ////////////////////////////////////////////////////////////
\r
154 ////////////////////////////////////////////////////////////
\r
155 /// Get the response status code
\r
157 /// \return Status code
\r
159 ////////////////////////////////////////////////////////////
\r
160 Status GetStatus() const;
\r
162 ////////////////////////////////////////////////////////////
\r
163 /// Get the full message contained in the response
\r
165 /// \return The response message
\r
167 ////////////////////////////////////////////////////////////
\r
168 const std::string& GetMessage() const;
\r
172 ////////////////////////////////////////////////////////////
\r
174 ////////////////////////////////////////////////////////////
\r
175 Status myStatus; ///< Status code returned from the server
\r
176 std::string myMessage; ///< Last message received from the server
\r
179 ////////////////////////////////////////////////////////////
\r
180 /// Specialization of FTP response returning a directory
\r
181 ////////////////////////////////////////////////////////////
\r
182 class SFML_API DirectoryResponse : public Response
\r
186 ////////////////////////////////////////////////////////////
\r
187 /// Default constructor
\r
189 /// \param Resp : Source response
\r
191 ////////////////////////////////////////////////////////////
\r
192 DirectoryResponse(Response Resp);
\r
194 ////////////////////////////////////////////////////////////
\r
195 /// Get the directory returned in the response
\r
197 /// \return Directory name
\r
199 ////////////////////////////////////////////////////////////
\r
200 const std::string& GetDirectory() const;
\r
204 ////////////////////////////////////////////////////////////
\r
206 ////////////////////////////////////////////////////////////
\r
207 std::string myDirectory; ///< Directory extracted from the response message
\r
211 ////////////////////////////////////////////////////////////
\r
212 /// Specialization of FTP response returning a filename lisiting
\r
213 ////////////////////////////////////////////////////////////
\r
214 class SFML_API ListingResponse : public Response
\r
218 ////////////////////////////////////////////////////////////
\r
219 /// Default constructor
\r
221 /// \param Resp : Source response
\r
222 /// \param Data : Data containing the raw listing
\r
224 ////////////////////////////////////////////////////////////
\r
225 ListingResponse(Response Resp, const std::vector<char>& Data);
\r
227 ////////////////////////////////////////////////////////////
\r
228 /// Get the number of filenames in the listing
\r
230 /// \return Total number of filenames
\r
232 ////////////////////////////////////////////////////////////
\r
233 std::size_t GetCount() const;
\r
235 ////////////////////////////////////////////////////////////
\r
236 /// Get the Index-th filename in the directory
\r
238 /// \param Index : Index of the filename to get
\r
240 /// \return Index-th filename
\r
242 ////////////////////////////////////////////////////////////
\r
243 const std::string& GetFilename(std::size_t Index) const;
\r
247 ////////////////////////////////////////////////////////////
\r
249 ////////////////////////////////////////////////////////////
\r
250 std::vector<std::string> myFilenames; ///< Filenames extracted from the data
\r
254 ////////////////////////////////////////////////////////////
\r
255 /// Destructor -- close the connection with the server
\r
257 ////////////////////////////////////////////////////////////
\r
260 ////////////////////////////////////////////////////////////
\r
261 /// Connect to the specified FTP server
\r
263 /// \param Server : FTP server to connect to
\r
264 /// \param Port : Port used for connection (21 by default, standard FTP port)
\r
265 /// \param Timeout : Maximum time to wait, in seconds (0 by default, means no timeout)
\r
267 /// \return Server response to the request
\r
269 ////////////////////////////////////////////////////////////
\r
270 Response Connect(const IPAddress& Server, unsigned short Port = 21, float Timeout = 0.f);
\r
272 ////////////////////////////////////////////////////////////
\r
273 /// Log in using anonymous account
\r
275 /// \return Server response to the request
\r
277 ////////////////////////////////////////////////////////////
\r
280 ////////////////////////////////////////////////////////////
\r
281 /// Log in using a username and a password
\r
283 /// \param UserName : User name
\r
284 /// \param Password : Password
\r
286 /// \return Server response to the request
\r
288 ////////////////////////////////////////////////////////////
\r
289 Response Login(const std::string& UserName, const std::string& Password);
\r
291 ////////////////////////////////////////////////////////////
\r
292 /// Close the connection with FTP server
\r
294 /// \return Server response to the request
\r
296 ////////////////////////////////////////////////////////////
\r
297 Response Disconnect();
\r
299 ////////////////////////////////////////////////////////////
\r
300 /// Send a null command just to prevent from being disconnected
\r
302 /// \return Server response to the request
\r
304 ////////////////////////////////////////////////////////////
\r
305 Response KeepAlive();
\r
307 ////////////////////////////////////////////////////////////
\r
308 /// Get the current working directory
\r
310 /// \return Server response to the request
\r
312 ////////////////////////////////////////////////////////////
\r
313 DirectoryResponse GetWorkingDirectory();
\r
315 ////////////////////////////////////////////////////////////
\r
316 /// Get the contents of the given directory
\r
317 /// (subdirectories and files)
\r
319 /// \param Directory : Directory to list ("" by default, the current one)
\r
321 /// \return Server response to the request
\r
323 ////////////////////////////////////////////////////////////
\r
324 ListingResponse GetDirectoryListing(const std::string& Directory = "");
\r
326 ////////////////////////////////////////////////////////////
\r
327 /// Change the current working directory
\r
329 /// \param Directory : New directory, relative to the current one
\r
331 /// \return Server response to the request
\r
333 ////////////////////////////////////////////////////////////
\r
334 Response ChangeDirectory(const std::string& Directory);
\r
336 ////////////////////////////////////////////////////////////
\r
337 /// Go to the parent directory of the current one
\r
339 /// \return Server response to the request
\r
341 ////////////////////////////////////////////////////////////
\r
342 Response ParentDirectory();
\r
344 ////////////////////////////////////////////////////////////
\r
345 /// Create a new directory
\r
347 /// \param Name : Name of the directory to create
\r
349 /// \return Server response to the request
\r
351 ////////////////////////////////////////////////////////////
\r
352 Response MakeDirectory(const std::string& Name);
\r
354 ////////////////////////////////////////////////////////////
\r
355 /// Remove an existing directory
\r
357 /// \param Name : Name of the directory to remove
\r
359 /// \return Server response to the request
\r
361 ////////////////////////////////////////////////////////////
\r
362 Response DeleteDirectory(const std::string& Name);
\r
364 ////////////////////////////////////////////////////////////
\r
367 /// \param File : File to rename
\r
368 /// \param NewName : New name
\r
370 /// \return Server response to the request
\r
372 ////////////////////////////////////////////////////////////
\r
373 Response RenameFile(const std::string& File, const std::string& NewName);
\r
375 ////////////////////////////////////////////////////////////
\r
376 /// Remove an existing file
\r
378 /// \param Name : File to remove
\r
380 /// \return Server response to the request
\r
382 ////////////////////////////////////////////////////////////
\r
383 Response DeleteFile(const std::string& Name);
\r
385 ////////////////////////////////////////////////////////////
\r
386 /// Download a file from the server
\r
388 /// \param DistantFile : Path of the distant file to download
\r
389 /// \param DestPath : Where to put to file on the local computer
\r
390 /// \param Mode : Transfer mode (binary by default)
\r
392 /// \return Server response to the request
\r
394 ////////////////////////////////////////////////////////////
\r
395 Response Download(const std::string& DistantFile, const std::string& DestPath, TransferMode Mode = Binary);
\r
397 ////////////////////////////////////////////////////////////
\r
398 /// Upload a file to the server
\r
400 /// \param LocalFile : Path of the local file to upload
\r
401 /// \param DestPath : Where to put to file on the server
\r
402 /// \param Mode : Transfer mode (binary by default)
\r
404 /// \return Server response to the request
\r
406 ////////////////////////////////////////////////////////////
\r
407 Response Upload(const std::string& LocalFile, const std::string& DestPath, TransferMode Mode = Binary);
\r
411 ////////////////////////////////////////////////////////////
\r
412 /// Send a command to the FTP server
\r
414 /// \param Command : Command to send
\r
415 /// \param Parameter : Command parameter ("" by default)
\r
417 /// \return Server response to the request
\r
419 ////////////////////////////////////////////////////////////
\r
420 Response SendCommand(const std::string& Command, const std::string& Parameter = "");
\r
422 ////////////////////////////////////////////////////////////
\r
423 /// Receive a response from the server
\r
424 /// (usually after a command has been sent)
\r
426 /// \return Server response to the request
\r
428 ////////////////////////////////////////////////////////////
\r
429 Response GetResponse();
\r
431 ////////////////////////////////////////////////////////////
\r
432 /// Utility class for exchanging datas with the server
\r
433 /// on the data channel
\r
434 ////////////////////////////////////////////////////////////
\r
437 friend class DataChannel;
\r
439 ////////////////////////////////////////////////////////////
\r
441 ////////////////////////////////////////////////////////////
\r
442 SocketTCP myCommandSocket; ///< Socket holding the control connection with the server
\r
448 #endif // SFML_FTP_HPP
\r