source: filezilla/trunk/fuentes/src/include/commands.h @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 3 years ago

First release to xenial

File size: 8.0 KB
Line 
1#ifndef __COMMANDS_H__
2#define __COMMANDS_H__
3
4// See below for actual commands and their parameters
5
6// Command IDs
7// -----------
8enum class Command
9{
10        none = 0,
11        connect,
12        disconnect,
13        list,
14        transfer,
15        del,
16        removedir,
17        mkdir,
18        rename,
19        chmod,
20        raw,
21
22        // Only used internally
23        cwd,
24        rawtransfer
25};
26
27// Reply codes
28// -----------
29#define FZ_REPLY_OK                             (0x0000)
30#define FZ_REPLY_WOULDBLOCK             (0x0001)
31#define FZ_REPLY_ERROR                  (0x0002)
32#define FZ_REPLY_CRITICALERROR  (0x0004 | FZ_REPLY_ERROR) // If there is no point to retry an operation, this
33                                                                                                                  // code is returned.
34#define FZ_REPLY_CANCELED               (0x0008 | FZ_REPLY_ERROR)
35#define FZ_REPLY_SYNTAXERROR    (0x0010 | FZ_REPLY_ERROR)
36#define FZ_REPLY_NOTCONNECTED   (0x0020 | FZ_REPLY_ERROR)
37#define FZ_REPLY_DISCONNECTED   (0x0040)
38#define FZ_REPLY_INTERNALERROR  (0x0080 | FZ_REPLY_ERROR) // If you get this reply, the error description will be
39                                                                                                                  // given by the last Debug_Warning log message. This
40                                                                                                                  // should not happen unless there is a bug in FileZilla 3.
41#define FZ_REPLY_BUSY                   (0x0100 | FZ_REPLY_ERROR)
42#define FZ_REPLY_ALREADYCONNECTED       (0x0200 | FZ_REPLY_ERROR) // Will be returned by connect if already connected
43#define FZ_REPLY_PASSWORDFAILED 0x0400 // Will be returned if PASS fails with 5yz reply code.
44#define FZ_REPLY_TIMEOUT                (0x0800 | FZ_REPLY_ERROR)
45#define FZ_REPLY_NOTSUPPORTED   (0x1000 | FZ_REPLY_ERROR) // Will be returned if command not supported by that protocol
46#define FZ_REPLY_WRITEFAILED    (0x2000 | FZ_REPLY_ERROR) // Happens if local file could not be written during transfer
47#define FZ_REPLY_LINKNOTDIR             (0x4000)
48
49// --------------- //
50// Actual commands //
51// --------------- //
52
53class CCommand
54{
55public:
56        CCommand() = default;
57        virtual ~CCommand() {}; // TODO: One GCC >= 4.8 is in Debian Stable (Jessie by then), make default and add testcase to configure.
58
59        virtual Command GetId() const = 0;
60        virtual CCommand *Clone() const = 0;
61
62        virtual bool valid() const { return true; }
63
64protected:
65        CCommand(CCommand const&) = default;
66        CCommand& operator=(CCommand const&) = default;
67};
68
69template<typename Derived, Command id>
70class CCommandHelper : public CCommand
71{
72public:
73        virtual Command GetId() const final { return id; }
74
75        virtual CCommand* Clone() const final {
76                return new Derived(static_cast<Derived const&>(*this));
77        }
78
79protected:
80        CCommandHelper<Derived, id>() = default;
81        CCommandHelper<Derived, id>(CCommandHelper<Derived, id> const&) = default;
82        CCommandHelper<Derived, id>& operator=(CCommandHelper<Derived, id> const&) = default;
83};
84
85template<Command id>
86class CBasicCommand final : public CCommandHelper<CBasicCommand<id>, id>
87{
88};
89
90class CConnectCommand final : public CCommandHelper<CConnectCommand, Command::connect>
91{
92public:
93        explicit CConnectCommand(CServer const& server, bool retry_conncting = true);
94
95        CServer const& GetServer() const;
96        bool RetryConnecting() const { return m_retry_connecting; }
97protected:
98        CServer const m_Server;
99        bool const m_retry_connecting;
100};
101
102typedef CBasicCommand<Command::disconnect> CDisconnectCommand;
103
104#define LIST_FLAG_REFRESH 1
105#define LIST_FLAG_AVOID 2
106#define LIST_FLAG_FALLBACK_CURRENT 4
107#define LIST_FLAG_LINK 8
108class CListCommand final : public CCommandHelper<CListCommand, Command::list>
109{
110        // Without a given directory, the current directory will be listed.
111        // Directories can either be given as absolute path or as
112        // pair of an absolute path and the very last path segments.
113
114        // Set LIST_FLAG_REFRESH to get a directory listing even if a cache
115        // lookup can be made after finding out true remote directory.
116        //
117        // Set LIST_FLAG_AVOID to get a directory listing only if cache lookup
118        // fails or contains unsure entries, otherwise don't send listing.
119        //
120        // If LIST_FLAG_FALLBACK_CURRENT is set and CWD fails, list whatever
121        // directory we are currently in. Useful for initial reconnect to the
122        // server when we don't know if remote directory still exists
123        //
124        // LIST_FLAG_LINK is used for symlink discovery. There's unfortunately
125        // no sane way to distinguish between symlinks to files and symlinks to
126        // directories.
127public:
128        explicit CListCommand(int flags = 0);
129        explicit CListCommand(CServerPath path, wxString subDir = wxString(), int flags = 0);
130
131        CServerPath GetPath() const;
132        wxString GetSubDir() const;
133
134        int GetFlags() const { return m_flags; }
135
136        bool valid() const;
137
138protected:
139        CServerPath const m_path;
140        wxString const m_subDir;
141        int const m_flags;
142};
143
144class CFileTransferCommand final : public CCommandHelper<CFileTransferCommand, Command::transfer>
145{
146public:
147        class t_transferSettings final
148        {
149        public:
150                t_transferSettings()
151                        : binary(true)
152                {}
153
154                bool binary;
155        };
156
157        // For uploads, set download to false.
158        // For downloads, localFile can be left empty if supported by protocol.
159        // Check for nId_data notification.
160        // FIXME: localFile empty iff protocol is HTTP.
161        CFileTransferCommand(wxString const& localFile, CServerPath const& remotePath, wxString const& remoteFile, bool download, t_transferSettings const& m_transferSettings);
162
163        wxString GetLocalFile() const;
164        CServerPath GetRemotePath() const;
165        wxString GetRemoteFile() const;
166        bool Download() const;
167        const t_transferSettings& GetTransferSettings() const { return m_transferSettings; }
168
169protected:
170        wxString const m_localFile;
171        CServerPath const m_remotePath;
172        wxString const m_remoteFile;
173        bool const m_download;
174        t_transferSettings const m_transferSettings;
175};
176
177class CRawCommand final : public CCommandHelper<CRawCommand, Command::raw>
178{
179public:
180        explicit CRawCommand(const wxString &command);
181
182        wxString GetCommand() const;
183
184        bool valid() const { return !m_command.empty(); }
185
186protected:
187        wxString m_command;
188};
189
190class CDeleteCommand final : public CCommandHelper<CDeleteCommand, Command::del>
191{
192public:
193        CDeleteCommand(CServerPath const& path, std::deque<wxString> && files);
194
195        CServerPath GetPath() const { return m_path; }
196        const std::deque<wxString>& GetFiles() const { return m_files; }
197        std::deque<wxString>&& ExtractFiles() { return std::move(m_files); }
198
199        bool valid() const { return !GetPath().empty() && !GetFiles().empty(); }
200protected:
201        CServerPath const m_path;
202        std::deque<wxString> m_files;
203};
204
205class CRemoveDirCommand final : public CCommandHelper<CRemoveDirCommand, Command::removedir>
206{
207public:
208        // Directories can either be given as absolute path or as
209        // pair of an absolute path and the very last path segments.
210        CRemoveDirCommand(CServerPath const& path, wxString const& subdDir);
211
212        CServerPath GetPath() const { return m_path; }
213        wxString GetSubDir() const { return m_subDir; }
214
215        bool valid() const;
216
217protected:
218        CServerPath const m_path;
219        wxString const m_subDir;
220};
221
222class CMkdirCommand final : public CCommandHelper<CMkdirCommand, Command::mkdir>
223{
224public:
225        explicit CMkdirCommand(CServerPath const& path);
226
227        CServerPath GetPath() const { return m_path; }
228
229        bool valid() const;
230
231protected:
232        CServerPath const m_path;
233};
234
235class CRenameCommand final : public CCommandHelper<CRenameCommand, Command::rename>
236{
237public:
238        CRenameCommand(CServerPath const& fromPath, wxString const& fromFile,
239                                   CServerPath const& toPath, wxString const& toFile);
240
241        CServerPath GetFromPath() const { return m_fromPath; }
242        CServerPath GetToPath() const { return m_toPath; }
243        wxString GetFromFile() const { return m_fromFile; }
244        wxString GetToFile() const { return m_toFile; }
245
246        bool valid() const;
247
248protected:
249        CServerPath const m_fromPath;
250        CServerPath const m_toPath;
251        wxString const m_fromFile;
252        wxString const m_toFile;
253};
254
255class CChmodCommand final : public CCommandHelper<CChmodCommand, Command::chmod>
256{
257public:
258        // The permission string should be given in a format understandable by the server.
259        // Most likely it's the defaut octal representation used by the unix chmod command,
260        // i.e. chmod 755 foo.bar
261        CChmodCommand(const CServerPath& path, const wxString& file, const wxString& permission);
262
263        CServerPath GetPath() const { return m_path; }
264        wxString GetFile() const { return m_file; }
265        wxString GetPermission() const { return m_permission; }
266
267        bool valid() const;
268
269protected:
270        CServerPath const m_path;
271        wxString const m_file;
272        wxString const m_permission;
273};
274
275#endif
Note: See TracBrowser for help on using the repository browser.