/** * Connect * * Description: Given a connection object, connect m_ftpClient. */ bool CFtpManager::Connect(CConnection * con, CUT_FTPClient & ftpClient) { bool result = false; if(!ftpClient.IsConnected()) // Not connected { int retcode = 11; retcode = ftpClient.FTPConnect(con->host, con->user, con->password, _T("")); if(retcode == UTE_SUCCESS) { /* int index = 0; size_t size = 0; _TCHAR pBuf[MAX_PATH+1]; *pBuf = 0; while(m_ftpClient.GetMultiLineResponse(pBuf, MAX_PATH, index, &size) == UTE_SUCCESS) { index++; //history.AddLine(pBuf); //TRACE1(" data %s \n", pBuf); } */ result = true; ftpClient.SetFireWallMode(TRUE); } if(retcode == UTE_BAD_HOSTNAME){TRACE0(" UTE_BAD_HOSTNAME \n");} if(retcode == UTE_CONNECT_FAILED){TRACE0(" UTE_CONNECT_FAILED \n");} if(retcode == UTE_NO_RESPONSE){TRACE0(" UTE_NO_RESPONSE \n");} if(retcode == UTE_INVALID_RESPONSE){TRACE0(" UTE_INVALID_RESPONSE \n");} if(retcode == UTE_USER_NA){TRACE0(" UTE_USER_NA \n");} if(retcode == UTE_PASS_NA){TRACE0(" UTE_PASS_NA \n");} if(retcode == UTE_ACCT_NA){TRACE0(" UTE_ACCT_NA \n");} } else { result = true; } return result; }
BOOL CALLBACK DlgProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM /* lParam */) { static CUT_FTPClient ftpClient; static CUH_Control history; static int align = TA_LEFT; static int textColor = RGB(0,0,0); static int backColor = RGB(255,255,255); static HFONT hFont; HWND hwndCheck1; int state; LPSTR pbuf; BOOL bTrans = TRUE; switch(message) { case WM_INITDIALOG: { // set the window icon SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_ICON1))); SetDlgItemInt(hwnd,IDC_PORT,21, FALSE); //attach the history window to the control history.AttachHistoryWindow(hwnd,IDC_HISTORY); //setup the history window history.SetHistoryLength(500); hFont = CreateFont(14,0,0,0,500,0,0,0,0,0,0,0,0,_T("Courier New")); history.SetFont(hFont); SetDlgItemText(hwnd,IDC_EDIT,_T("localhost")); SetDlgItemText(hwnd,IDC_EDIT2,_T("anonymous"));// User SetDlgItemText(hwnd,IDC_EDIT3,_T("*****@*****.**")); // Password SetDlgItemText(hwnd,IDC_EDIT4,_T("")); // account SetDlgItemText(hwnd,IDC_QUOTE_DATA,_T("HELP")); // the default is to use secure SSL CheckDlgButton (hwnd,IDC_SECURE_SSL,BST_CHECKED); // The default file transfer structure is file CheckRadioButton(hwnd,IDC_RADIO1,IDC_RADIO3,IDC_RADIO1); return 1; } case WM_NCDESTROY: { //delete the font DeleteObject(hFont); return 0; } case WM_COMMAND: { switch(LOWORD(wParam)) { //connect to an FTP site case IDC_CONNECT: { SetCursor(LoadCursor(NULL, IDC_WAIT)); history.SetTextColor(RGB(0,0,255)); history.AddLine("============================================"); history.SetTextColor(RGB(0,0,0)); _TCHAR addr[256]; _TCHAR user [256]; _TCHAR pass [256]; _TCHAR account[256]; int retcode = 11; GetDlgItemText(hwnd, IDC_EDIT,addr,256); GetDlgItemText(hwnd, IDC_EDIT2,user,256); GetDlgItemText(hwnd, IDC_EDIT3,pass,256); GetDlgItemText(hwnd, IDC_EDIT4,account,256); if (_tcslen(addr) > 0) { ftpClient.setsMode(IsDlgButtonChecked(hwnd, IDC_SECURE_SSL) ? CUT_FTPClient::FTPES : CUT_FTPClient::FTP); ftpClient.SetControlPort (GetDlgItemInt(hwnd,IDC_PORT, &bTrans, FALSE)); retcode = ftpClient.FTPConnect(addr,user,pass,account); if( retcode == UTE_SUCCESS){ history.AddLine("Connect Success"); int index = 0; // v4.2 changed - was while(1) for(;;){ pbuf = (LPSTR)ftpClient.GetMultiLineResponse(index); index++; if(pbuf != NULL) history.AddLine(pbuf); else break; } } else{ // ***************** Change to alarm color ************ history.SetTextColor(RGB(255,0,0)); history.AddLine(ftpClient.GetLastResponse ()); history.AddLine(CUT_ERR::GetErrorString (retcode)); history.SetTextColor(RGB(0,0,0)); // change back to black text color } } SetCursor(LoadCursor(NULL, IDC_ARROW)); return 1; } //disconnect from an FTP site case IDC_DISCONNECT: { SetCursor(LoadCursor(NULL, IDC_WAIT)); history.SetTextColor(RGB(0,0,255)); history.AddLine("============================================"); history.SetTextColor(RGB(0,0,0)); if(ftpClient.Close() == UTE_SUCCESS) history.AddLine("Disconnect Successful"); else{ history.SetTextColor(RGB(255,0,0)); history.AddLine("Disconnect Failed"); history.SetTextColor(RGB(0,0,0)); } SetCursor(LoadCursor(NULL, IDC_ARROW)); return 1; } case IDC_QUOTE: { _TCHAR quote[MAX_PATH]; int retcode = 0; GetDlgItemText(hwnd, IDC_QUOTE_DATA,quote,MAX_PATH-1); if ((retcode = ftpClient.Quote(quote)) == UTE_SUCCESS) { int index = 0; // v4.2 changed - was while(1) for(;;){ pbuf = (LPSTR)ftpClient.GetMultiLineResponse(index); index++; if(pbuf != NULL) history.AddLine(pbuf); else break; } }else{ history.AddLine(ftpClient.GetLastResponse ()); history.AddLine(CUT_ERR::GetErrorString (retcode)); } return 1; } // Send a file to the remote host case IDC_SENDFILE: { SetCursor(LoadCursor(NULL, IDC_WAIT)); history.SetTextColor(RGB(0,0,255)); history.AddLine("============================================"); history.SetTextColor(RGB(0,0,0)); int retCode; _TCHAR filetosend[256]; _TCHAR fileToSave[256]; GetDlgItemText(hwnd, IDC_FILENAME,filetosend,256); GetDlgItemText(hwnd, IDC_SAVEAS,fileToSave,256); retCode = ftpClient.SendFile(filetosend, fileToSave); if (retCode == UTE_SUCCESS) history.AddLine("Send File Successful"); else{ history.SetTextColor(RGB(255,0,0)); history.AddLine("Send File Failed!"); history.SetTextColor(RGB(0,0,0)); } SetCursor(LoadCursor(NULL, IDC_ARROW)); return 1; } case IDC_TEST_REST: { _TCHAR filetoget[MAX_PATH]; _TCHAR filetosave[MAX_PATH]; GetDlgItemText(hwnd, IDC_FILENAME,filetoget,MAX_PATH); GetDlgItemText(hwnd, IDC_SAVEAS,filetosave,MAX_PATH); int retCode = ftpClient.ResumeReceiveFile(filetoget,filetosave); if (retCode == UTE_SUCCESS) history.AddLine("Receive file"); else{ history.SetTextColor(RGB(255,0,0)); history.AddLine( CUT_ERR::GetErrorString (retCode)); history.SetTextColor(RGB(0,0,0)); } return 1; } // Get file from the remote host case IDC_RETRFILE: { SetCursor(LoadCursor(NULL, IDC_WAIT)); history.SetTextColor(RGB(0,0,255)); history.AddLine("============================================"); history.SetTextColor(RGB(0,0,0)); int retCode; _TCHAR filetoget[MAX_PATH]; _TCHAR filetosave[MAX_PATH]; _TCHAR buf[MAX_PATH]; GetDlgItemText(hwnd, IDC_FILENAME,filetoget,MAX_PATH); GetDlgItemText(hwnd, IDC_SAVEAS,filetosave,MAX_PATH); if(*filetoget == '\0') { history.AddLine("No file name entered"); return 1; } if(*filetosave == ('\0')) { _tcscpy(filetosave, filetoget); } retCode = ftpClient.ReceiveFile(filetoget, filetosave); if (retCode == UTE_SUCCESS) { history.AddLine("Receive File Successful"); _sntprintf(buf, sizeof(buf),_T("File was saved to %s"),filetosave); history.AddLine(buf ); } else { history.SetTextColor(RGB(255,0,0)); history.AddLine(CUT_ERR::GetErrorString (retCode)); history.SetTextColor(RGB(0,0,0)); } SetCursor(LoadCursor(NULL, IDC_ARROW)); return 1; } // the FILE transfer structure was selected case IDC_RADIO1: CheckRadioButton(hwnd,IDC_RADIO1,IDC_RADIO3,IDC_RADIO1); ftpClient.SetTransferStructure(0); break; // The RECORD transfer structure was selected case IDC_RADIO2: CheckRadioButton(hwnd,IDC_RADIO1,IDC_RADIO3,IDC_RADIO2); ftpClient.SetTransferStructure(1); break; case IDC_RADIO3: // The PAGE transfer structure was selected CheckRadioButton(hwnd,IDC_RADIO1,IDC_RADIO3,IDC_RADIO3); ftpClient.SetTransferStructure(2); break; // Setting the fire wall mode on or of from when set PASV command will be sent each time we need to call // LIST command // Not all servers support the firewall mode case IDC_FIREWALL: hwndCheck1 = GetDlgItem(hwnd,IDC_FIREWALL); state = (int)SendMessage(hwndCheck1,BM_GETSTATE,0,0l); if(state&BST_CHECKED) ftpClient.SetFireWallMode(TRUE); else ftpClient.SetFireWallMode(FALSE); break; // Select either Image or ASCII for file Transfer type case IDC_TRANSFER_TYPE: hwndCheck1 = GetDlgItem(hwnd,IDC_TRANSFER_MODE); state = (int)SendMessage(hwndCheck1,BM_GETSTATE,0,0l); if(state&BST_CHECKED) ftpClient.SetTransferType(1); // if checked then it is 1 = Image else ftpClient.SetTransferType(0); // if not Selected then it is 0 = ASCII break; // GEt the directory information case IDC_LIST: { SetCursor(LoadCursor(NULL, IDC_WAIT)); history.SetTextColor(RGB(0,0,255)); history.AddLine("============================================"); history.SetTextColor(RGB(0,0,0)); CUT_DIRINFO di; char entry[256]; int RetCode = 0; RetCode = ftpClient.GetDirInfo((char*)NULL); if (RetCode == UTE_SUCCESS) { int count = ftpClient.GetDirInfoCount(); history.AddLine("Current Dir:"); for(int t=0;t<count;t++){ ftpClient.GetDirEntry(t,&di); _snprintf(entry, sizeof(entry),"%s %ld %2.2d/%2.2d/%2.2d %2.2d:%2.2d", di.fileName,di.fileSize,di.year,di.month, di.day,di.hour,di.minute); history.AddLine(entry); } } else { history.SetTextColor(RGB(255,0,0)); history.AddLine(CUT_ERR::GetErrorString (RetCode)); history.SetTextColor(RGB(0,0,0)); } SetCursor(LoadCursor(NULL, IDC_ARROW)); return 1; } // Call the ChDir command to change the directory case IDC_CHANGEDIR: { SetCursor(LoadCursor(NULL, IDC_WAIT)); history.SetTextColor(RGB(0,0,255)); history.AddLine(_T("============================================")); history.SetTextColor(RGB(0,0,0)); CUT_DIRINFO di; char entry[256]; _TCHAR dir[256]; int valRet; GetDlgItemText(hwnd, IDC_DIRCHANGE,dir,256); if (_tcslen(dir)> 0) { valRet = ftpClient.ChDir(dir); if (valRet != UTE_SUCCESS){ history.SetTextColor(RGB(255,0,0)); history.AddLine(CUT_ERR::GetErrorString (valRet)); history.SetTextColor(RGB(0,0,0)); } else { ftpClient.GetDirInfo((char*)NULL); int count = ftpClient.GetDirInfoCount(); for(int t=0;t<count;t++){ ftpClient.GetDirEntry(t,&di); _snprintf(entry, sizeof(entry),"%s %ld %2.2d/%2.2d/%2.2d %2.2d:%2.2d", di.fileName,di.fileSize,di.year,di.month, di.day,di.hour,di.minute); history.AddLine(entry); } } } // User did not enter any string in the edit box else { _snprintf(entry, sizeof(entry)," Please Enter a directory Name"); history.SetTextColor(RGB(255,0,0)); history.AddLine(entry); history.SetTextColor(RGB(0,0,0)); } SetCursor(LoadCursor(NULL, IDC_ARROW)); return 1; } case IDC_ABOUT: { DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_ABOUT), hwnd, (DLGPROC )AboutProc); return 1; } //exit the program case IDC_EXIT: { EndDialog(hwnd,0); return 1; } } return 0; } case WM_CLOSE: { EndDialog(hwnd,0); return 1; } } return 0; }