Pi0trek

pop3_rfc_socket.RC

Apr 27th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 KB | None | 0 0
  1. #define _WINSOCK_DEPRECATED_NO_WARNINGS
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <winsock2.h>
  4. #include <Windows.h>
  5. #include <conio.h>
  6. #include <iostream>
  7. #include <string>
  8. #include <ctime>
  9. #include <fstream>
  10.  
  11. using namespace std;
  12.  
  13. #define MAX_MSG         128                   // max message length
  14. #define COMM_SUFF       "\r\n"
  15. #define COMM_USER       "USER "
  16. #define COMM_PASS       "PASS "
  17. #define COMM_STAT       "STAT\r\n"
  18. #define COMM_LIST       "LIST\r\n"
  19. #define COMM_QUIT       "QUIT\r\n"
  20. #define COMM_RETR       "RETR "
  21. #define COMM_TOP        "TOP "
  22. #define COMM_DELETE     "DELE "
  23. #define COMM_NOOP       "NOOP\r\n"
  24.  
  25. //string username = "[email protected]", password = "Testpop3", hostname = "pop3.wp.pl", prefix = "maildrop has ";
  26. string username, password, hostname, prefix;
  27. //int port = 110, checkingTime = 3;
  28. int port, checkingTime;
  29.  
  30. int readConfigFile(string filename);
  31. void printCurrentTime();
  32. int checkNumberofEamils(string hostname, int port,string prefix, string login, string password);
  33. int getNumberofEmails(char *bufor, int len);
  34.  
  35. int main()
  36. {
  37.     readConfigFile("app.config");
  38.  
  39.     bool flagFirst = true;
  40.     int secondsCounter = 0;
  41.  
  42.     int count = -1;
  43.     int previousCount = -1;
  44.     int total = 0;
  45.     while (!_kbhit() || _getch() != 'q')
  46.     {
  47.         if (secondsCounter % checkingTime == 0)
  48.         {
  49.             count = checkNumberofEamils(hostname, 110, prefix, username, password);
  50.  
  51.             if (flagFirst)
  52.             {
  53.                 flagFirst = false;
  54.                 previousCount = count;
  55.                 std::cout << "Sprawdzanie poczty dla " << username << " na " << hostname << " co " << checkingTime <<" s" << endl;
  56.                 printCurrentTime();
  57.                 std::cout << "Ilosc odebranych maili: " << count << endl << endl;
  58.             }
  59.             else if (previousCount != count)
  60.             {
  61.                 if (previousCount < count)
  62.                 {
  63.                     total += (count - previousCount);
  64.                     printCurrentTime();
  65.                     std::cout << "Odebrano " << (count - previousCount) << " nowy(ch) email(i)" << endl;
  66.                 }
  67.                 previousCount = count;
  68.             }
  69.         }
  70.  
  71.         secondsCounter++;
  72.         Sleep(1000);
  73.     }
  74.  
  75.     std::cout << endl;
  76.     printCurrentTime();
  77.     std::cout << "Zakonczono sprawdzanie poczty dla " << username << " na " << hostname << endl;
  78.     std::cout << "Ilosc odebranych maili podczas sesji: " << total << endl;
  79.  
  80.     _getch();
  81.     return 0;
  82. }
  83.  
  84. int readConfigFile(string filename)
  85. {
  86.     fstream file;
  87.     file.open(filename, ios::in);
  88.     if (!file.good()) return -1;
  89.     getline(file, username);
  90.     getline(file, password);
  91.     getline(file, hostname);
  92.     getline(file, prefix);
  93.     string tmp;
  94.     getline(file, tmp);
  95.     port = atoi(tmp.c_str());
  96.     getline(file,tmp);
  97.     checkingTime = atoi(tmp.c_str());
  98.     file.close();
  99.     return 0;
  100. }
  101.  
  102. void printCurrentTime()
  103. {
  104.     time_t t = time(0);   // get time now
  105.     struct tm * now = localtime(&t);
  106.  
  107.     std::cout << "[" << (now->tm_hour < 10 ? "0" : "") << now->tm_hour << ":" << (now->tm_min < 10 ? "0" : "")
  108.         << now->tm_min << ":" << (now->tm_sec < 10 ? "0" : "") << now->tm_sec << "] ";
  109. }
  110.  
  111. int checkNumberofEamils(string hostname, int port, string prefix, string login, string password)
  112. {
  113.     WSADATA wsaData;
  114.  
  115.     int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
  116.     if (result != NO_ERROR)
  117.         printf("Initialization error.\n");
  118.  
  119.     SOCKET sock_no;                             // SOCKET no
  120.     struct sockaddr_in sok = { 0 };               // SOCKET struct
  121.     struct hostent *hp;
  122.     string command, s, msg_num;
  123.     //string user = login;//"EMAIL_USERNAME";
  124.     //string passw = password;// "EMAIL_PASSWORD";
  125.     char buff[MAX_MSG];                         // pop-3 output buffer
  126.     sock_no = socket(AF_INET, SOCK_STREAM, 0); // init socket
  127.     sok.sin_family = AF_INET;
  128.     int n_bytes = 0;                            // num of return bytes
  129.  
  130.     s = hostname;
  131.     hp = gethostbyname(s.c_str());             // get server IP address
  132.     if (hp != NULL)
  133.     {
  134.         struct in_addr **list = (struct in_addr**) hp->h_addr_list;
  135.         sok.sin_addr.s_addr = inet_addr(inet_ntoa(*list[0]));
  136.     }
  137.     else
  138.     {
  139.         return -1;
  140.     }
  141.     sok.sin_port = (unsigned short int) htons(port);
  142.  
  143.     //..........................................................
  144.     // connect to pop-3 server and logon
  145.     //..........................................................
  146.     int info = 0;
  147.     if ((info = connect(sock_no, (sockaddr *)&sok, sizeof(sockaddr_in))) != 0)
  148.     {
  149.         n_bytes = recv(sock_no, buff, MAX_MSG, 0); // skip first server answer
  150.     }
  151.     command = COMM_USER + login + COMM_SUFF;    // send "user" command
  152.     n_bytes = send(sock_no, command.data(), command.size(), 0);
  153.  
  154.     // get server answer
  155.     n_bytes = recv(sock_no, buff, MAX_MSG, 0);
  156.  
  157.     // send PASS command
  158.     command = COMM_PASS + password + COMM_SUFF;
  159.     n_bytes = send(sock_no, command.data(), command.size(), 0);
  160.  
  161.     n_bytes = recv(sock_no, buff, MAX_MSG, 0); // get server answer
  162.  
  163.  //..........................................................
  164.  // get list of messages; get number of messages in inbox
  165.  //..........................................................
  166.     command = COMM_STAT;
  167.  
  168.     n_bytes = send(sock_no, command.data(), command.size(), 0);
  169.     n_bytes = recv(sock_no, buff, MAX_MSG, 0);
  170.     //for (int i = 0; i < n_bytes; i++) std::cout << buff[i];
  171.     int iloscMaili = getNumberofEmails(buff, n_bytes);
  172.     //std::cout << endl << iloscMaili << endl;
  173.  
  174.  
  175.  
  176.     //..........................................................
  177.     // quit and disconnect pop-3 server; send "QUIT" command
  178.     //..........................................................
  179.     command = COMM_QUIT;
  180.     n_bytes = ::send(sock_no, command.data(), command.size(), 0);
  181.     ::closesocket(sock_no);
  182.    
  183.     //getchar();
  184.     return iloscMaili;
  185. }
  186.  
  187. int getNumberofEmails(char *bufor, int len)
  188. {
  189.     int start = 0;
  190.  
  191.     for (int i = 0; i < len; i++)
  192.     {
  193.         if (bufor[i] == prefix[0])
  194.         {
  195.             unsigned int j;
  196.             for (j = 1; j < prefix.length(); j++)
  197.             {
  198.                 if (bufor[i + j] != prefix[j]) break;
  199.             }
  200.             if (j != prefix.length()) continue;
  201.             start = i + j;
  202.         }
  203.     }
  204.     if (start == 0) return -1;
  205.     int tmp = 0;
  206.     for (int i = start; i < len; i++)
  207.     {
  208.         if (bufor[i] >= '0' && bufor[i] <= '9')
  209.         {
  210.             tmp = tmp * 10 + (bufor[i] - '0');
  211.         }
  212.         else if (bufor[i] == ' ')start++;
  213.         else if (i == start) return -2;
  214.         else return tmp;
  215.     }
  216.     return -3;
  217. }
Advertisement
Add Comment
Please, Sign In to add comment