#include #include #include #include using namespace ost; using namespace std; int main(int argc, char** argv) { if (argc<4) { cout << "USAGE: " << argv[0] << " serveur_pop login password" << endl; return (1); } string AddrStr(argv[1]); AddrStr += ":110"; tcpstream MyStream(AddrStr.c_str()); // buffer de 512 par défaut ça tombe bien // c'est la limite fixée par la RFC 1939 char ServData[513]; // +1 pour le zéro terminal string CliData; // Connexion MyStream.get(ServData, 513); MyStream.istream::ignore(); cout << "S: " << ServData << endl; // Authentification CliData = "USER "; CliData += argv[2]; CliData += "\r\n"; MyStream << CliData; cout << "C: " << CliData; MyStream.sync(); MyStream.get(ServData, 513); MyStream.istream::ignore(); cout << "S: " << ServData << endl; CliData = "PASS "; CliData += argv[3]; CliData += "\r\n"; MyStream << CliData; cout << "C: " << CliData; MyStream.sync(); MyStream.get(ServData, 513); MyStream.istream::ignore(); cout << "S: " << ServData << endl; if (!strncmp(ServData, "+OK", 3)) { CliData = "STAT\r\n"; MyStream << CliData; cout << "C: " << CliData; MyStream.sync(); MyStream.get(ServData, 513); MyStream.istream::ignore(); cout << "S: " << ServData << endl; istringstream ServDataStream(ServData); string Result; int NbMsg; ServDataStream >> Result; if (Result=="+OK") { ServDataStream >> NbMsg; } int idMsg = 0; while (idMsg++ < NbMsg) { // Récupération du message // Attention RETR est une réponse multilignes // Donc terminée par un '.\r\n' ostringstream CliDataStream; CliDataStream << "RETR " << idMsg << "\r\n"; MyStream << CliDataStream.str(); cout << "C: " << CliDataStream.str(); MyStream.sync(); MyStream.get(ServData, 513); cout << "S: " << ServData << endl; if (!strncmp(ServData, "+OK", 3)) { while (MyStream.get(ServData, 513)) { cout << "S: " << ServData << endl; if (!strcmp(ServData, ".\r")) break; MyStream.istream::ignore(); } } } } // Déconnexion CliData = "QUIT\r\n"; MyStream << CliData; cout << "C: " << CliData; MyStream.sync(); MyStream.get(ServData, 513); MyStream.istream::ignore(); cout << "S: " << ServData << endl; cout << "Exiting ..." << endl; MyStream.close(); }