Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / kioslave / fish / fish.h
blob854e56bd90e1d4da6e5102766d1ac62edd0883fd
1 /***************************************************************************
2 fish.h - a FISH kioslave
3 -------------------
4 begin : Thu Oct 4 17:09:14 CEST 2001
5 copyright : (C) 2001 by Jörg Walter
6 email : trouble@garni.ch
7 ***************************************************************************/
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation, version 2 of the License *
14 * *
15 ***************************************************************************/
16 #ifndef __fish_h__
17 #define __fish_h__
21 #include <kurl.h>
22 #include <kio/global.h>
23 #include <kio/slavebase.h>
24 #include <k3process.h>
25 #include <kio/authinfo.h>
26 #include <time.h>
28 #define FISH_EXEC_CMD 'X'
30 class fishProtocol : public KIO::SlaveBase
32 public:
33 fishProtocol(const QByteArray &pool_socket, const QByteArray &app_socket);
34 virtual ~fishProtocol();
36 /**
37 Connects to a server and logs us in via SSH. Then starts FISH protocol.
38 @ref isConnected is set to true if logging on was successful.
39 It is set to false if the connection becomes closed.
42 void openConnection();
44 /**
45 Clean up connection
47 void shutdownConnection(bool forced=false);
48 /** sets connection information for subsequent commands */
49 void setHost(const QString & host, quint16 port, const QString & user, const QString & pass);
50 /** Forced close of the connection */
51 void closeConnection();
52 /** get a file */
53 void get(const KUrl& url);
54 /** put a file */
55 void put(const KUrl& url, int permissions, KIO::JobFlags flags );
56 /** aborts command sequence and calls error() */
57 void error(int type, const QString &detail);
58 /** executes next command in sequence or calls finished() if all is done */
59 void finished();
60 /** stat a file */
61 void stat(const KUrl& url);
62 /** find mimetype for a file */
63 void mimetype(const KUrl& url);
64 /** list a directory */
65 void listDir(const KUrl& url);
66 /** create a directory */
67 void mkdir(const KUrl&url, int permissions);
68 /** rename a file */
69 void rename(const KUrl& src, const KUrl& dest, KIO::JobFlags flags);
70 /** create a symlink */
71 void symlink(const QString& target, const KUrl& dest, KIO::JobFlags flags);
72 /** change file permissions */
73 void chmod(const KUrl& url, int permissions);
74 /** copies a file */
75 void copy(const KUrl &src, const KUrl &dest, int permissions, KIO::JobFlags flags);
76 /** report status */
77 void slave_status();
78 /** removes a file or directory */
79 void del(const KUrl &u, bool isfile);
80 /** special like background execute */
81 void special( const QByteArray &data );
83 private: // Private attributes
84 /** the SSH process used to communicate with the remote end */
85 pid_t childPid;
86 /** fd for reading and writing to the process */
87 int childFd;
88 /** buffer for data to be written */
89 const char *outBuf;
90 /** current write position in buffer */
91 KIO::fileoffset_t outBufPos;
92 /** length of buffer */
93 KIO::fileoffset_t outBufLen;
94 /** use su if true else use ssh */
95 bool local;
96 /** // FIXME: just a workaround for konq deficiencies */
97 bool isStat;
98 /** // FIXME: just a workaround for konq deficiencies */
99 QString redirectUser, redirectPass;
101 protected: // Protected attributes
102 /** for LIST/STAT */
103 KIO::UDSEntry udsEntry;
104 /** for LIST/STAT */
105 KIO::UDSEntry udsStatEntry;
106 /** for LIST/STAT */
107 long long udsType;
108 /** for LIST/STAT */
109 QString udsMime;
110 /** for LIST/STAT */
111 QString thisFn;
112 /** for STAT */
113 QString wantedFn;
114 QString statPath;
115 /** url of current request */
116 KUrl url;
117 /** true if connection is logged in successfully */
118 bool isLoggedIn;
119 /** host name of current connection */
120 QString connectionHost;
121 /** user name of current connection */
122 QString connectionUser;
123 /** port of current connection */
124 int connectionPort;
125 /** password of current connection */
126 QString connectionPassword;
127 /** AuthInfo object used for logging in */
128 KIO::AuthInfo connectionAuth;
129 /** number of lines received, == 0 -> everything went ok */
130 int errorCount;
131 /** queue for lines to be sent */
132 QStringList qlist;
133 /** queue for commands to be sent */
134 QStringList commandList;
135 /** queue for commands to be sent */
136 QList<int> commandCodes;
137 /** bytes still to be read in raw mode */
138 KIO::fileoffset_t rawRead;
139 /** bytes still to be written in raw mode */
140 KIO::fileoffset_t rawWrite;
141 /** data bytes to read in next read command */
142 KIO::fileoffset_t recvLen;
143 /** data bytes to write in next write command */
144 KIO::fileoffset_t sendLen;
145 /** true if the last write operation was finished */
146 bool writeReady;
147 /** true if a command stack is currently executing */
148 bool isRunning;
149 /** reason of LIST command */
150 enum { CHECK, LIST } listReason;
151 /** true if FISH server understands APPEND command */
152 bool hasAppend;
153 /** permission of created file */
154 int putPerm;
155 /** true if file may be overwritten */
156 bool checkOverwrite;
157 /** current position of write */
158 KIO::fileoffset_t putPos;
159 /** true if file already existed */
160 bool checkExist;
161 /** true if this is the first login attempt (== use cached password) */
162 bool firstLogin;
163 /** write buffer */
164 QByteArray rawData;
165 /** buffer for storing bytes used for MimeMagic */
166 QByteArray mimeBuffer;
167 /** whther the mimetype has been sent already */
168 bool mimeTypeSent;
169 /** number of bytes read so far */
170 KIO::fileoffset_t dataRead;
171 /** details about each fishCommand */
172 static const struct fish_info {
173 const char *command;
174 int params;
175 const char *alt;
176 int lines;
177 } fishInfo[];
178 /** last FISH command sent to server */
179 enum fish_command_type { FISH_FISH, FISH_VER, FISH_PWD, FISH_LIST, FISH_STAT,
180 FISH_RETR, FISH_STOR,
181 FISH_CWD, FISH_CHMOD, FISH_DELE, FISH_MKD, FISH_RMD,
182 FISH_RENAME, FISH_LINK, FISH_SYMLINK, FISH_CHOWN,
183 FISH_CHGRP, FISH_READ, FISH_WRITE, FISH_COPY, FISH_APPEND, FISH_EXEC } fishCommand;
184 int fishCodeLen;
185 protected: // Protected methods
186 /** manages initial communication setup including password queries */
187 int establishConnection(char *buffer, KIO::fileoffset_t buflen);
188 int received(const char *buffer, KIO::fileoffset_t buflen);
189 void sent();
190 /** builds each FISH request and sets the error counter */
191 bool sendCommand(fish_command_type cmd, ...);
192 /** checks response string for result code, converting 000 and 001 appropriately */
193 int handleResponse(const QString &str);
194 /** parses a ls -l time spec */
195 int makeTimeFromLs(const QString &dayStr, const QString &monthStr, const QString &timeyearStr);
196 /** executes a chain of commands */
197 void run();
198 /** creates the subprocess */
199 bool connectionStart();
200 /** writes one chunk of data to stdin of child process */
201 void writeChild(const char *buf, KIO::fileoffset_t len);
202 /** parses response from server and acts accordingly */
203 void manageConnection(const QString &line);
204 /** writes to process */
205 void writeStdin(const QString &line);
206 /** Verify port **/
207 void setHostInternal(const KUrl & u);
212 #endif