The OnCommand event handler is invoked after the client has
sent a command to the server, but before the command has been
processed. To implement
an event handler, the application should create a class derived from
the CFtpServer class, and then override this method.
This event handler is invoked for all commands issued by the
client, including invalid or disabled commands. If the event handler
processes the command, it must call the SendResponse method to
send a success or error response back to the client. If this is not
done, the server will perform the default processing for the command.
Although this event handler will provide the command name, the full
command line can be obtained by
using the GetCommandLine method. Individual command parameters
can be obtained by using the GetCommandParam and
GetCommandParamCount methods.
It is not necessary to use this event handler to disable a command.
The EnableCommand method can be used to enable or disable
specific commands, and the IsCommandEnabled method can be used to
determine if a command is enabled.
If this event handler is used to implement a custom command, it is
recommended that you use the IsClientAuthenticated method to
determine whether or not the client session has been authenticated.
Unless there is a specific need for the custom command to be used
before a client has logged in, the application should not take any
action and send a 530 result code back to the client indicating
authentication is required.
VOID CMyFtpServer::OnCommand(UINT nClientId, LPCTSTR lpszCommand)
{
// Implement a custom command named TIME that will return the local time
if (lstrcmp(lpszCommand, _T("TIME")) == 0)
{
// The command should not have any parameters
if (GetCommandParamCount(nClientId) > 0)
{
SendResponse(nClientId, FTP_REPLY_BADARG);
return;
}
if (IsClientAuthenticated(nClientId))
{
CString strTime = CTime::GetCurrentTime().Format(_T("%Y-%m-%d %H:%M:%S"));
SendResponse(nClientId, FTP_REPLY_CMDOK, strTime);
}
else
{
// The client has not logged in, return an error
SendResponse(nClientId, FTP_REPLY_NOLOGIN);
}
}
}