www.pudn.com > FTPServerClient.zip > interface.cpp


#include "interface.h"
#include "unistd.h"
using namespace std;

Interface::Interface() {
	client = NULL;
	passive = false;
}

void Interface::start() {
	while(true) {
		cout << "ftp" << (client ? ":"+string(dir) : "") + "> ";

		string command;
		getline(cin, command);

		try {
			if(command.substr(0, 5)=="open ") {
				if(client) {
					client->quit();
					delete client;
				}

				if(logon(command.substr(5))) {
					if(client->syst()!=215) continue;
					if(client->port(rand())!=200) continue;
					client->pwd(dir);
				}

			} else if(command=="close") {
				if(client) {
					client->quit();
					delete client;
					client = NULL;
				}

			} else if(command=="passive") {
				passive = !passive;
				cout << "\t[M] Passive mode " << (passive ? "on" : "off") << endl;

			} else if(command=="ascii") {
				if(client) client->type(Tcp::ASCII);

			} else if(command=="binary") {
				if(client) client->type(Tcp::IMAGE);

			} else if(command=="pwd") {
				if(client) client->pwd(dir);

			} else if(command.substr(0, 2)=="ls") {
				if(command.length()==2) {
					if(client) {
						if(passive) client->pasv();
						client->list();
					}
				} else if(command.length()>3 && command[2]==' ') {
					if(client) {
						if(passive) client->pasv();
						client->list(command.substr(3));
					}
				} else
					cerr << "ERROR: Invalid command" << endl;

			} else if(command.substr(0, 3)=="cd ") {
				if(client && client->cwd(command.substr(3))==250)
					dir.cd(command.substr(3));

			} else if(command.substr(0, 4)=="get ") {
					if(client) {
						if(passive) client->pasv();
						client->retr(command.substr(4));
					}

			} else if(command.substr(0, 4)=="put ") {
					if(client)
						if(command.substr(4, 6)=="ftp://") {
							// transfer between servers
							unsigned int slash = command.find('/', 10);

							Client* receiver = client;
							if(!logon(command.substr(10, slash-10))) continue;

							if(receiver->connect(client))
								receiver->transfer(client, command.substr(slash));

							client->quit();
							delete client;
							client = receiver;
						} else {
							if(passive) client->pasv();
							client->stor(command.substr(4));
						}

			} else if(command.substr(0, 7)=="append ") {
					if(client) {
						if(passive) client->pasv();
						client->appe(command.substr(7));
					}

			} else if(command.substr(0, 4)=="help") {
				if(command.length()==4) {
					if(client) client->help();
				} else if(command.length()>5 && command[4]==' ') {
					if(client) client->help(command.substr(5));
				} else
					cerr << "ERROR: Invalid command" << endl;

			} else if(command=="quit") {
				if(client) {
					client->quit();
					delete client;
				}

				return;

			} else
				cerr << "ERROR: Invalid command" << endl;

		} catch(const char* message) {
			cerr << "ERROR: " << message << endl;
		}
	}
}

bool Interface::logon(const string& url) {
	unsigned int colon = url.find(':');
	if(colon==string::npos)
		client = new Client(url);
	else
		client = new Client(url.substr(0, colon), atoi(url.substr(colon+1).c_str()));

	cout << "Username: ";
	string username;
	getline(cin, username);

	int reply = username.length() ? client->user(username): client->user();
	if(reply==331)
		reply = client->pass(getpass("Password: "));

	if(reply==230)
		return true;
	else {
		delete client;
		client = NULL;
		return false;
	}
}

int main() {
	Interface interface;
	interface.start();
	return 0;
}