// Common, groups Communication and Files
// Copyright Alexander Liss
#include "ftp.h"
FtpSession::FtpSession():
status(0),internet(0),session(0),intercept(0)
{
}
FtpSession::~FtpSession()
{
stop();
}
BOOL FtpSession::set_login(
HINTERNET Internet,
LPCTSTR Server,
LPCTSTR User,
LPCTSTR Password)
{
internet=Internet;
server.set(Server);
user.set(User);
password.set(Password);
return TRUE;
}
BOOL FtpSession::start()
{
if(status || !internet) return FALSE;
session=::InternetConnect(internet,server.data,INTERNET_DEFAULT_FTP_PORT,user.data,password.data,INTERNET_SERVICE_FTP ,0,(DWORD)this);
if(!session) return FALSE;
return TRUE;
}
void FtpSession::stop()
{
if(!session) return;
::InternetCloseHandle(session);
session=0;
}
BOOL FtpSession::create_directory(const char *name)
{
if(status || !session) return FALSE;
return ::FtpCreateDirectory(session,name);
}
BOOL FtpSession::set_current_directory(const char *name)
{
if(status || !session) return FALSE;
return ::FtpSetCurrentDirectory(session,name);
}
BOOL FtpSession::get_current_directory(StrBuffer& name)
{
if(status || !session) return FALSE;
char buf[MAX_PATH];
DWORD z=MAX_PATH;
BOOL success = ::FtpGetCurrentDirectory(session,buf,&z);
if(!success) return FALSE;
name.set(buf);
return TRUE;
}
BOOL FtpSession::put(LPCTSTR lpszLocalFile,
LPCTSTR lpszNewRemoteFile,
DWORD dwFlags)
{
if(status || !session) return FALSE;
return ::FtpPutFile(session,lpszLocalFile,lpszNewRemoteFile,dwFlags,(DWORD)this);
}
BOOL FtpSession::remove(LPCTSTR lpsRemoteFile)
{
if(status || !session) return FALSE;
return ::FtpDeleteFile(session,lpsRemoteFile);
}
int FtpSession::set(FtpIntercept& z)
{
intercept=&z;
if(INTERNET_INVALID_STATUS_CALLBACK == ::InternetSetStatusCallback(session, FtpSession::callback) )
return 1;
return 0;
}
int FtpSession::stop_intercept()
{
::InternetSetStatusCallback(session, 0);
return 0;
}
VOID CALLBACK FtpSession::callback(
HINTERNET h,
DWORD context,
DWORD status,
LPVOID status_info,
DWORD status_info_length
)
{
if(!context) return;
FtpSession *d=(FtpSession *)context;
int g=d->intercept->respond(status,status_info,status_info_length);
if(g)
d->stop();
}
// FUNCTIONS
HINTERNET internet_open(
LPCTSTR lpszAgent,
DWORD dwAccessType,
LPCTSTR lpszProxyName,
LPCTSTR lpszProxyBypass,
DWORD dwFlags)
{
return ::InternetOpen(lpszAgent,dwAccessType,lpszProxyName,lpszProxyBypass,dwFlags);
}
void internet_close_handle(HINTERNET handle)
{
::InternetCloseHandle(handle);
}