[KBear-cvs] kbear/kbear/ftp kbearftp.cpp,1.51,1.52 kbearftp.h,1.13,1.14
Brought to you by:
kbjorn
From: <kb...@us...> - 2003-11-19 00:38:00
|
Update of /cvsroot/kbear/kbear/kbear/ftp In directory sc8-pr-cvs1:/tmp/cvs-serv15902/kbear/ftp Modified Files: kbearftp.cpp kbearftp.h Log Message: Added simple wrapper class with static methods for different simle job classes Index: kbearftp.cpp =================================================================== RCS file: /cvsroot/kbear/kbear/kbear/ftp/kbearftp.cpp,v retrieving revision 1.51 retrieving revision 1.52 diff -C2 -d -r1.51 -r1.52 *** kbearftp.cpp 23 Aug 2003 07:47:51 -0000 1.51 --- kbearftp.cpp 19 Nov 2003 00:37:56 -0000 1.52 *************** *** 244,247 **** --- 244,253 ---- kdDebug(7102) << "KBearFtp::setHost " << _host << endl; + QString user = _user; + QString pass = _pass.isEmpty() ? QString::null : _pass;; + if( _user.isEmpty() ) { + user = FTP_LOGIN; + pass = FTP_PASSWD; + } m_proxyURL = metaData("UseProxy"); kdDebug(7102) << "Proxy URL: " << m_proxyURL.url() << endl; *************** *** 259,267 **** void KBearFtp::openConnection() { - ftpOpenConnection(); - } - - - void KBearFtp::ftpOpenConnection (bool login) { kdDebug(7102) << "openConnection " << m_host << ":" << m_port << " " << m_user << " [password hidden]" << endl; --- 265,268 ---- *************** *** 334,345 **** kdDebug(7102) << "Connected ...." << endl; ! if( login ) { ! if( m_bUseFirewall ) ! m_bLoggedOn = ftpFirewallLogin(); ! else ! m_bLoggedOn = ftpLogin(); ! if( !m_bLoggedOn ) ! return; // error emitted by ftpLogin ! } connected(); --- 335,344 ---- kdDebug(7102) << "Connected ...." << endl; ! if( m_bUseFirewall ) ! m_bLoggedOn = ftpFirewallLogin(); ! else ! m_bLoggedOn = ftpLogin(); ! if( !m_bLoggedOn ) ! return; // error emitted by ftpLogin connected(); *************** *** 671,674 **** --- 670,674 ---- // Try anonymous login if both username/password // information is blank. + /* if (user.isEmpty() && pass.isEmpty()) { *************** *** 676,680 **** pass = FTP_PASSWD; } ! AuthInfo info; info.url.setProtocol( QString::fromLatin1("ftp") ); --- 676,680 ---- pass = FTP_PASSWD; } ! */ AuthInfo info; info.url.setProtocol( QString::fromLatin1("ftp") ); *************** *** 921,925 **** if(maxretries > 0 && !isPassCmd) { closeConnection (); ! ftpOpenConnection ( false ); if(m_bFtpStarted) ftpSendCmd ( cmd, maxretries - 1 ); --- 921,925 ---- if(maxretries > 0 && !isPassCmd) { closeConnection (); ! openConnection (); if(m_bFtpStarted) ftpSendCmd ( cmd, maxretries - 1 ); *************** *** 936,940 **** closeConnection(); // Close the old connection... ! ftpOpenConnection(); // Attempt to re-establish a new connection... if(!m_bLoggedOn) { --- 936,940 ---- closeConnection(); // Close the old connection... ! openConnection(); // Attempt to re-establish a new connection... if(!m_bLoggedOn) { Index: kbearftp.h =================================================================== RCS file: /cvsroot/kbear/kbear/kbear/ftp/kbearftp.h,v retrieving revision 1.13 retrieving revision 1.14 diff -C2 -d -r1.13 -r1.14 *** kbearftp.h 23 Aug 2003 07:47:51 -0000 1.13 --- kbearftp.h 19 Nov 2003 00:37:56 -0000 1.14 *************** *** 1,289 **** ! ! #ifndef __kbearftp_h__ ! #define __kbearftp_h__ ! ! #include <config.h> ! ! #include <stdio.h> ! #include <sys/types.h> ! ! #define FTP_BUFSIZ 1024 ! ! #include <qcstring.h> ! #include <qstring.h> ! #include <qstringlist.h> ! ! #include <kurl.h> ! #include <kio/slavebase.h> ! ! class KExtendedSocket; ! ! struct FtpEntry ! { ! QString name; ! QString owner; ! QString group; ! QString link; ! ! long size; ! mode_t type; ! mode_t access; ! time_t date; ! }; ! ! struct netbuf ! { ! char *cput,*cget; ! int handle; ! int cavail,cleft; ! char buf[FTP_BUFSIZ]; ! }; ! ! class KBearFtp : public KIO::SlaveBase ! { ! public: ! KBearFtp( const QCString &pool, const QCString &app ); ! ~KBearFtp(); ! ! virtual void setHost( const QString& host, int port, const QString& user, const QString& pass ); ! ! /** ! * Connects to a ftp server and logs us in ! * @ref m_bLoggedOn is set to true if logging on was successfull. ! * It is set to false if the connection becomes closed. ! * ! */ ! virtual void openConnection(); ! ! /** ! * Closes the connection ! */ ! virtual void closeConnection(); ! ! virtual void stat( const KURL &url ); ! ! virtual void listDir( const KURL & url ); ! virtual void mkdir( const KURL & url, int permissions ); ! virtual void rename( const KURL & src, const KURL & dst, bool overwrite ); ! virtual void del( const KURL & url, bool isfile ); ! virtual void chmod( const KURL & url, int permissions ); ! ! virtual void get( const KURL& url ); ! virtual void put( const KURL& url, int permissions, bool overwrite, bool resume); ! virtual void special( const QByteArray& ); ! //virtual void mimetype( const KURL& url ); ! ! virtual void slave_status(); ! ! // unsupported, AFAIK ! // virtual void copy( const KURL &src, const KURL &dest, int permissions, bool overwrite ); ! ! private: ! ! // All the methods named ftpXyz do NOT emit errors, they simply return true ! // or false (they are lowlevel methods). The methods not named this way ! // emit error on error (they are highlevel methods). ! ! /** ! * Connect and login to the FTP server. ! * ! * If login is set to false, this function will not attempt ! * to login to the server. ! * ! * @param login if true send login info to the FTP server. ! */ ! void ftpOpenConnection ( bool login = true ); ! ! /** ! * Executes any auto login macro's as specified in a .netrc file. ! */ ! void ftpAutoLoginMacro (); ! ! /** ! * Called by @ref openConnection. It opens the control connection to the ftp server. ! * ! * @return true on success. ! */ ! bool connect( const QString & host, unsigned short int port = 0 ); ! ! /** ! * Called by @ref openConnection. It logs us in. ! * @ref m_initialPath is set to the current working directory ! * if logging on was successfull. ! * ! * @return true on success. ! */ ! bool ftpLogin(); ! bool ftpFirewallLogin(); ! bool matchAutoExtensions( const QString& fileName ); ! QStringList parseFirewallMacro( const QString& macro ); ! /** ! * ftpSendCmd - send a command (@p cmd) and read response ! * ! * @param maxretries number of time it should retry. Since it recursively ! * calls itself if it can't read the answer (this happens especially after ! * timeouts), we need to limit the recursiveness ;-) ! * ! * return true if any response received, false on error ! */ ! bool ftpSendCmd( const QCString& cmd, int maxretries = 1 ); ! ! /** ! * Use the SIZE command to get the file size. ! * @param mode the size depends on the transfer mode, hence this arg. ! * @return true on success ! * Gets the size into m_size. ! */ ! bool ftpSize( const QString & path, char mode ); ! ! /** ! * Runs a command on the ftp server like "list" or "retr". In contrast to ! * @ref ftpSendCmd a data connection is opened. The corresponding socket ! * @ref sData is available for reading/writing on success. ! * The connection must be closed afterwards with @ref ftpCloseCommand. ! * ! * @param mode is 'A' or 'I'. 'A' means ASCII transfer, 'I' means binary transfer. ! * @param errorcode the command-dependent error code to emit on error ! * ! * @return true if the command was accepted by the server. ! */ ! bool ftpOpenCommand( const char *command, const QString & path, char mode, ! int errorcode, unsigned long offset = 0 ); ! ! /** ! * The counterpart to @ref openCommand. ! * Closes data sockets and then reads line sent by server at ! * end of command. ! * @return false on error (line doesn't start with '2') ! */ ! bool ftpCloseCommand(); ! ! void closeSockets(); ! ! //void ftpAbortTransfer(); ! ! /** ! * Used by @ref openCommand ! */ ! bool ftpOpenPASVDataConnection(); ! /** ! * Used by @ref openCommand ! */ ! bool ftpOpenEPSVDataConnection(); ! /** ! * Used by @ref openCommand ! */ ! bool ftpOpenEPRTDataConnection(); ! /** ! * Used by @ref openCommand ! */ ! bool ftpOpenDataConnection(); ! /** ! * ftpAcceptConnect - wait for incoming connection ! * ! * return -2 on error or timeout ! * otherwise returns socket descriptor ! */ ! int ftpAcceptConnect(); ! ! size_t ftpRead( void *buffer, long len ); ! size_t ftpWrite( void *buffer, long len ); ! ! bool ftpChmod( const QString & path, int permissions ); ! ! // used by listDir ! bool ftpOpenDir( const QString & path ); ! bool ftpCloseDir(); ! // return 0L on end ! FtpEntry * ftpReadDir(); ! // used by ftpReadDir ! FtpEntry * ftpParseDir( char* buffer ); ! void createUDSEntry( const QString & filename, FtpEntry * e, KIO::UDSEntry & entry, bool isDir ); ! void shortStatAnswer( const QString& filename, bool isDir ); ! void statAnswerNotFound( const QString & path, const QString & filename ); ! ! bool ftpRename( const QString & src, const QString & dst, bool overwrite ); ! ! /** ! * read a line of text ! * ! * return -1 on error, bytecount otherwise ! */ ! int ftpReadline( char *buf, int max, netbuf *ctl ); ! ! /** ! * read a response from the server, into rspbuf ! * @return first char of response (rspbuf[0]), '\0' if we couldn't read the response ! */ ! char readresp(); ! ! int ftpConvertFromNVTASCII( char* buff, int cnt ); ! void ftpConvertToNVTASCII( QByteArray& buff ); ! ! ! private: // data members ! ! /** ! * Connected to the socket from which we read a directory listing. ! * If it is not zero, then a "list" command is in progress. ! */ ! FILE *dirfile; ! ! /** ! * This is the data connection socket from which we read the data. ! */ ! int sData; ! /** ! * The control stream socket ! */ ! int sControl; ! /** ! * The server socket for a data connection. This is needed since the ! * ftp server must open a connection to us. ! */ ! int sDatal; ! ! QString m_host; ! unsigned short int m_port; ! QString m_user; ! QString m_pass; ! /** ! * Where we end up after connecting ! */ ! QString m_initialPath; ! KURL m_proxyURL; ! ! netbuf *nControl; ! char rspbuf[256]; ! ! bool m_bLoggedOn; ! bool m_bFtpStarted; ! bool m_bPasv; ! bool m_bUseProxy; ! bool m_bPersistent; ! bool m_bUseFirewall; ! unsigned int m_firewallType; ! unsigned int m_transferMode; ! char m_chTransferMode; ! QString m_firewallHost; ! int m_firewallPort; ! QString m_firewallUser; ! QString m_firewallPass; ! QString m_firewallAccount; ! size_t m_size; ! static size_t UnknownSize; ! ! enum ! { ! epsvUnknown = 0x01, ! epsvAllUnknown = 0x02, ! eprtUnknown = 0x04, ! epsvAllSent = 0x10, ! pasvUnknown = 0x20 ! }; ! int m_extControl; ! KExtendedSocket *ksControl; ! ! }; ! ! #endif --- 1 ---- ! #ifndef __kbearftp_h__ #define __kbearftp_h__ #include <config.h> #include <stdio.h> #include <sys/types.h> #define FTP_BUFSIZ 1024 #include <qcstring.h> #include <qstring.h> #include <qstringlist.h> #include <kurl.h> #include <kio/slavebase.h> class KExtendedSocket; struct FtpEntry { QString name; QString owner; QString group; QString link; long size; mode_t type; mode_t access; time_t date; }; struct netbuf { char *cput,*cget; int handle; int cavail,cleft; char buf[FTP_BUFSIZ]; }; class KBearFtp : public KIO::SlaveBase { public: KBearFtp( const QCString &pool, const QCString &app ); ~KBearFtp(); virtual void setHost( const QString& host, int port, const QString& user, const QString& pass ); /** * Connects to a ftp server and logs us in * @ref m_bLoggedOn is set to true if logging on was successfull. * It is set to false if the connection becomes closed. * */ virtual void openConnection(); /** * Closes the connection */ virtual void closeConnection(); virtual void stat( const KURL &url ); virtual void listDir( const KURL & url ); virtual void mkdir( const KURL & url, int permissions ); virtual void rename( const KURL & src, const KURL & dst, bool overwrite ); virtual void del( const KURL & url, bool isfile ); virtual void chmod( const KURL & url, int permissions ); virtual void get( const KURL& url ); virtual void put( const KURL& url, int permissions, bool overwrite, bool resume); virtual void special( const QByteArray& ); //virtual void mimetype( const KURL& url ); virtual void slave_status(); // unsupported, AFAIK // virtual void copy( const KURL &src, const KURL &dest, int permissions, bool overwrite ); private: // All the methods named ftpXyz do NOT emit errors, they simply return true // or false (they are lowlevel methods). The methods not named this way // emit error on error (they are highlevel methods). /** * Connect and login to the FTP server. * * If login is set to false, this function will not attempt * to login to the server. * * @param login if true send login info to the FTP server. */ // void ftpOpenConnection ( bool login = true ); /** * Executes any auto login macro's as specified in a .netrc file. */ void ftpAutoLoginMacro (); /** * Called by @ref openConnection. It opens the control connection to the ftp server. * * @return true on success. */ bool connect( const QString & host, unsigned short int port = 0 ); /** * Called by @ref openConnection. It logs us in. * @ref m_initialPath is set to the current working directory * if logging on was successfull. * * @return true on success. */ bool ftpLogin(); bool ftpFirewallLogin(); bool matchAutoExtensions( const QString& fileName ); QStringList parseFirewallMacro( const QString& macro ); /** * ftpSendCmd - send a command (@p cmd) and read response * * @param maxretries number of time it should retry. Since it recursively * calls itself if it can't read the answer (this happens especially after * timeouts), we need to limit the recursiveness ;-) * * return true if any response received, false on error */ bool ftpSendCmd( const QCString& cmd, int maxretries = 1 ); /** * Use the SIZE command to get the file size. * @param mode the size depends on the transfer mode, hence this arg. * @return true on success * Gets the size into m_size. */ bool ftpSize( const QString & path, char mode ); /** * Runs a command on the ftp server like "list" or "retr". In contrast to * @ref ftpSendCmd a data connection is opened. The corresponding socket * @ref sData is available for reading/writing on success. * The connection must be closed afterwards with @ref ftpCloseCommand. * * @param mode is 'A' or 'I'. 'A' means ASCII transfer, 'I' means binary transfer. * @param errorcode the command-dependent error code to emit on error * * @return true if the command was accepted by the server. */ bool ftpOpenCommand( const char *command, const QString & path, char mode, int errorcode, unsigned long offset = 0 ); /** * The counterpart to @ref openCommand. * Closes data sockets and then reads line sent by server at * end of command. * @return false on error (line doesn't start with '2') */ bool ftpCloseCommand(); void closeSockets(); //void ftpAbortTransfer(); /** * Used by @ref openCommand */ bool ftpOpenPASVDataConnection(); /** * Used by @ref openCommand */ bool ftpOpenEPSVDataConnection(); /** * Used by @ref openCommand */ bool ftpOpenEPRTDataConnection(); /** * Used by @ref openCommand */ bool ftpOpenDataConnection(); /** * ftpAcceptConnect - wait for incoming connection * * return -2 on error or timeout * otherwise returns socket descriptor */ int ftpAcceptConnect(); size_t ftpRead( void *buffer, long len ); size_t ftpWrite( void *buffer, long len ); bool ftpChmod( const QString & path, int permissions ); // used by listDir bool ftpOpenDir( const QString & path ); bool ftpCloseDir(); // return 0L on end FtpEntry * ftpReadDir(); // used by ftpReadDir FtpEntry * ftpParseDir( char* buffer ); void createUDSEntry( const QString & filename, FtpEntry * e, KIO::UDSEntry & entry, bool isDir ); void shortStatAnswer( const QString& filename, bool isDir ); void statAnswerNotFound( const QString & path, const QString & filename ); bool ftpRename( const QString & src, const QString & dst, bool overwrite ); /** * read a line of text * * return -1 on error, bytecount otherwise */ int ftpReadline( char *buf, int max, netbuf *ctl ); /** * read a response from the server, into rspbuf * @return first char of response (rspbuf[0]), '\0' if we couldn't read the response */ char readresp(); int ftpConvertFromNVTASCII( char* buff, int cnt ); void ftpConvertToNVTASCII( QByteArray& buff ); private: // data members /** * Connected to the socket from which we read a directory listing. * If it is not zero, then a "list" command is in progress. */ FILE *dirfile; /** * This is the data connection socket from which we read the data. */ int sData; /** * The control stream socket */ int sControl; /** * The server socket for a data connection. This is needed since the * ftp server must open a connection to us. */ int sDatal; QString m_host; unsigned short int m_port; QString m_user; QString m_pass; /** * Where we end up after connecting */ QString m_initialPath; KURL m_proxyURL; netbuf *nControl; char rspbuf[256]; bool m_bLoggedOn; bool m_bFtpStarted; bool m_bPasv; bool m_bUseProxy; bool m_bPersistent; bool m_bUseFirewall; unsigned int m_firewallType; unsigned int m_transferMode; char m_chTransferMode; QString m_firewallHost; int m_firewallPort; QString m_firewallUser; QString m_firewallPass; QString m_firewallAccount; size_t m_size; static size_t UnknownSize; enum { epsvUnknown = 0x01, epsvAllUnknown = 0x02, eprtUnknown = 0x04, epsvAllSent = 0x10, pasvUnknown = 0x20 }; int m_extControl; KExtendedSocket *ksControl; }; #endif \ No newline at end of file |