Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _WINSOCK_DEPRECATED_NO_WARNINGS
- #define _CRT_SECURE_NO_WARNINGS
- #include <winsock2.h>
- #include <Windows.h>
- #include <conio.h>
- #include <iostream>
- #include <string>
- #include <ctime>
- #include <fstream>
- using namespace std;
- #define MAX_MSG 128 // max message length
- #define COMM_SUFF "\r\n"
- #define COMM_USER "USER "
- #define COMM_PASS "PASS "
- #define COMM_STAT "STAT\r\n"
- #define COMM_LIST "LIST\r\n"
- #define COMM_QUIT "QUIT\r\n"
- #define COMM_RETR "RETR "
- #define COMM_TOP "TOP "
- #define COMM_DELETE "DELE "
- #define COMM_NOOP "NOOP\r\n"
- //string username = "[email protected]", password = "Testpop3", hostname = "pop3.wp.pl", prefix = "maildrop has ";
- string username, password, hostname, prefix;
- //int port = 110, checkingTime = 3;
- int port, checkingTime;
- int readConfigFile(string filename);
- void printCurrentTime();
- int checkNumberofEamils(string hostname, int port,string prefix, string login, string password);
- int getNumberofEmails(char *bufor, int len);
- int main()
- {
- readConfigFile("app.config");
- bool flagFirst = true;
- int secondsCounter = 0;
- int count = -1;
- int previousCount = -1;
- int total = 0;
- while (!_kbhit() || _getch() != 'q')
- {
- if (secondsCounter % checkingTime == 0)
- {
- count = checkNumberofEamils(hostname, 110, prefix, username, password);
- if (flagFirst)
- {
- flagFirst = false;
- previousCount = count;
- std::cout << "Sprawdzanie poczty dla " << username << " na " << hostname << " co " << checkingTime <<" s" << endl;
- printCurrentTime();
- std::cout << "Ilosc odebranych maili: " << count << endl << endl;
- }
- else if (previousCount != count)
- {
- if (previousCount < count)
- {
- total += (count - previousCount);
- printCurrentTime();
- std::cout << "Odebrano " << (count - previousCount) << " nowy(ch) email(i)" << endl;
- }
- previousCount = count;
- }
- }
- secondsCounter++;
- Sleep(1000);
- }
- std::cout << endl;
- printCurrentTime();
- std::cout << "Zakonczono sprawdzanie poczty dla " << username << " na " << hostname << endl;
- std::cout << "Ilosc odebranych maili podczas sesji: " << total << endl;
- _getch();
- return 0;
- }
- int readConfigFile(string filename)
- {
- fstream file;
- file.open(filename, ios::in);
- if (!file.good()) return -1;
- getline(file, username);
- getline(file, password);
- getline(file, hostname);
- getline(file, prefix);
- string tmp;
- getline(file, tmp);
- port = atoi(tmp.c_str());
- getline(file,tmp);
- checkingTime = atoi(tmp.c_str());
- file.close();
- return 0;
- }
- void printCurrentTime()
- {
- time_t t = time(0); // get time now
- struct tm * now = localtime(&t);
- std::cout << "[" << (now->tm_hour < 10 ? "0" : "") << now->tm_hour << ":" << (now->tm_min < 10 ? "0" : "")
- << now->tm_min << ":" << (now->tm_sec < 10 ? "0" : "") << now->tm_sec << "] ";
- }
- int checkNumberofEamils(string hostname, int port, string prefix, string login, string password)
- {
- WSADATA wsaData;
- int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (result != NO_ERROR)
- printf("Initialization error.\n");
- SOCKET sock_no; // SOCKET no
- struct sockaddr_in sok = { 0 }; // SOCKET struct
- struct hostent *hp;
- string command, s, msg_num;
- //string user = login;//"EMAIL_USERNAME";
- //string passw = password;// "EMAIL_PASSWORD";
- char buff[MAX_MSG]; // pop-3 output buffer
- sock_no = socket(AF_INET, SOCK_STREAM, 0); // init socket
- sok.sin_family = AF_INET;
- int n_bytes = 0; // num of return bytes
- s = hostname;
- hp = gethostbyname(s.c_str()); // get server IP address
- if (hp != NULL)
- {
- struct in_addr **list = (struct in_addr**) hp->h_addr_list;
- sok.sin_addr.s_addr = inet_addr(inet_ntoa(*list[0]));
- }
- else
- {
- return -1;
- }
- sok.sin_port = (unsigned short int) htons(port);
- //..........................................................
- // connect to pop-3 server and logon
- //..........................................................
- int info = 0;
- if ((info = connect(sock_no, (sockaddr *)&sok, sizeof(sockaddr_in))) != 0)
- {
- n_bytes = recv(sock_no, buff, MAX_MSG, 0); // skip first server answer
- }
- command = COMM_USER + login + COMM_SUFF; // send "user" command
- n_bytes = send(sock_no, command.data(), command.size(), 0);
- // get server answer
- n_bytes = recv(sock_no, buff, MAX_MSG, 0);
- // send PASS command
- command = COMM_PASS + password + COMM_SUFF;
- n_bytes = send(sock_no, command.data(), command.size(), 0);
- n_bytes = recv(sock_no, buff, MAX_MSG, 0); // get server answer
- //..........................................................
- // get list of messages; get number of messages in inbox
- //..........................................................
- command = COMM_STAT;
- n_bytes = send(sock_no, command.data(), command.size(), 0);
- n_bytes = recv(sock_no, buff, MAX_MSG, 0);
- //for (int i = 0; i < n_bytes; i++) std::cout << buff[i];
- int iloscMaili = getNumberofEmails(buff, n_bytes);
- //std::cout << endl << iloscMaili << endl;
- //..........................................................
- // quit and disconnect pop-3 server; send "QUIT" command
- //..........................................................
- command = COMM_QUIT;
- n_bytes = ::send(sock_no, command.data(), command.size(), 0);
- ::closesocket(sock_no);
- //getchar();
- return iloscMaili;
- }
- int getNumberofEmails(char *bufor, int len)
- {
- int start = 0;
- for (int i = 0; i < len; i++)
- {
- if (bufor[i] == prefix[0])
- {
- unsigned int j;
- for (j = 1; j < prefix.length(); j++)
- {
- if (bufor[i + j] != prefix[j]) break;
- }
- if (j != prefix.length()) continue;
- start = i + j;
- }
- }
- if (start == 0) return -1;
- int tmp = 0;
- for (int i = start; i < len; i++)
- {
- if (bufor[i] >= '0' && bufor[i] <= '9')
- {
- tmp = tmp * 10 + (bufor[i] - '0');
- }
- else if (bufor[i] == ' ')start++;
- else if (i == start) return -2;
- else return tmp;
- }
- return -3;
- }
Advertisement
Add Comment
Please, Sign In to add comment