stoneharry

Untitled

May 15th, 2013
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <curl.h>
  5. #include <sstream>
  6. #include <vector>
  7. #include "sha1.h"
  8. #include <fstream>
  9.  
  10. using namespace std;
  11.  
  12. #define URL_GetMessages "http://eoc.dispersion-wow.com/scripts/OfflineChat/GetMessages.php"
  13. #define URL_SendMessage "http://eoc.dispersion-wow.com/scripts/OfflineChat/SendChatMessage.php"
  14. #define URL_Authenticate "http://eoc.dispersion-wow.com/scripts/OfflineChat/Authenticate.php"
  15. #define Session_Storage "OfflineChat_Session.db"
  16.  
  17. bool shuttingdown = false;
  18. bool thread1exit = false;
  19. bool thread2exit = false;
  20. const long n = 0x1971425;
  21. int MessageSpam = 0;
  22. long lastClock = 0;
  23.  
  24. string username = "";
  25.  
  26. vector<int> IDsRead;
  27.  
  28. static size_t data_write(void* buf, size_t size, size_t nmemb, void* userp)
  29. {
  30.     if(userp)
  31.     {
  32.         std::ostream& os = *static_cast<std::ostream*>(userp);
  33.         std::streamsize len = size * nmemb;
  34.         if(os.write(static_cast<char*>(buf), len))
  35.             return len;
  36.     }
  37.     return 0;
  38. }
  39.  
  40. CURLcode curl_read(const std::string& url, std::ostream& os, long timeout = 5)
  41. {
  42.     CURLcode code(CURLE_FAILED_INIT);
  43.     CURL* curl = curl_easy_init();
  44.  
  45.     if(curl)
  46.     {
  47.         if(CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &data_write))
  48.         && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L))
  49.         && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L))
  50.         && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_FILE, &os))
  51.         && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout))
  52.         && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_COOKIEFILE, Session_Storage))
  53.         && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_COOKIEJAR, Session_Storage))
  54.         && CURLE_OK == (code = curl_easy_setopt(curl, CURLOPT_URL, url.c_str())))
  55.         {
  56.             code = curl_easy_perform(curl);
  57.         }
  58.         curl_easy_cleanup(curl);
  59.     }
  60.     return code;
  61. }
  62.  
  63. bool authenticate(string password)
  64. {
  65.     ostringstream oss;
  66.     string URL = URL_Authenticate;
  67.     URL += "?user=" + username + "&pass=" + password;
  68.     curl_read(URL, oss);
  69.     string html = oss.str();
  70.  
  71.     if (html.length() > 0)
  72.         if (atoi(html.c_str()) == n)
  73.             return true;
  74.     return false;
  75. }
  76.  
  77. void SendChatMessage(string message)
  78. {
  79.     MessageSpam++;
  80.     if (clock() > lastClock + 10000)
  81.     {
  82.         if (MessageSpam > 10)
  83.         {
  84.             shuttingdown = true;
  85.             return;
  86.         }
  87.         MessageSpam = 0;
  88.         lastClock = clock();
  89.     }
  90.     ostringstream oss;
  91.     string destination = URL_SendMessage;
  92.     int position = message.find(" ");
  93.     while (position != string::npos)
  94.     {
  95.         message.replace(position, 1, "||");
  96.         position = message.find(" ", position + 1);
  97.     }
  98.     destination += "?user=" + username;
  99.     destination += "&message=" + message;
  100.     curl_read(destination, oss);
  101. }
  102.  
  103. void HandleMessage(string message)
  104. {
  105.     if (message.length() < 1)
  106.         return;
  107.     for (unsigned int i = 0; i < message.length() - 2; i++)
  108.     {
  109.         if (message.at(i) == '|' && message.at(i+1) == '|')
  110.         {
  111.             i = i + 2;
  112.         }
  113.         else
  114.         {
  115.             continue;
  116.         }
  117.         string output = "";
  118.         string ID = "";
  119.  
  120.         bool advance = true;
  121.         while (advance)
  122.         {
  123.             ID += message.at(i);
  124.             i++;
  125.             if (message.at(i) == '|' && message.at(i+1) == '|')
  126.                 advance = false;
  127.         }
  128.         i += 2;
  129.         advance = true;
  130.         int intID = atoi(ID.c_str());
  131.         int size = IDsRead.size();
  132.         bool found = false;
  133.         for (int j = 0; j < size; j++)
  134.         {
  135.             if (IDsRead[j] == intID)
  136.             {
  137.                 found = true;
  138.                 break;
  139.             }
  140.         }
  141.         while (advance)
  142.         {
  143.             output += message.at(i);
  144.             i++;
  145.             if (message.at(i) == '|' && message.at(i+1) == '|')
  146.                 advance = false;
  147.         }
  148.         i += 2;
  149.         output += " ";
  150.         advance = true;
  151.         while (advance)
  152.         {
  153.             if (message.at(i) == '|' && message.at(i+1) == 'c' && message.at(i+2) == 'f') // Handle links
  154.             {
  155.                 while (message.at(i) != '[')
  156.                     i++;
  157.                 while (message.at(i) != ']')
  158.                 {
  159.                     output += message.at(i);
  160.                     i++;
  161.                 }
  162.                 output += message.at(i);
  163.                 while (message.at(i) != '|' && message.at(i+1) != 'r')
  164.                     i++;
  165.                 i += 3;
  166.             }
  167.             else
  168.             {
  169.                 output += message.at(i); // Normal message
  170.             }
  171.             i++;
  172.             if (message.at(i) == '|' && message.at(i+1) == '|')
  173.                 advance = false;
  174.         }
  175.         i += 20; // skip date
  176.  
  177.         if (!found)
  178.         {
  179.             IDsRead.push_back(intID);
  180.             cout << output << endl;
  181.         }
  182.     }
  183. }
  184.  
  185. string getMessages()
  186. {
  187.     ostringstream oss;
  188.     curl_read(URL_GetMessages, oss);
  189.     string html = oss.str();
  190.     return html;
  191. }
  192.  
  193. DWORD WINAPI threadOne(LPVOID args)
  194. {
  195.     int ConnTimeout = 0;
  196.     while (!shuttingdown)
  197.     {
  198.         string message = getMessages();
  199.         if (message.length() < 1)
  200.         {
  201.             if (ConnTimeout == 4)
  202.             {
  203.                 printf("\n-- Webserver has completely stopped responding. Closing program. --\n\n");
  204.                 shuttingdown = true;
  205.                 continue;
  206.             }
  207.             printf("\n-- Warning: Website did not respond for more than 5 seconds. --\n\n");
  208.             ConnTimeout++;
  209.         }
  210.         ConnTimeout = 0;
  211.         HandleMessage(message);
  212.         Sleep(1000);
  213.     }
  214.     thread1exit = true;
  215.     return NULL;
  216. }
  217.  
  218. DWORD WINAPI threadTwo(LPVOID args)
  219. {
  220.     string input;
  221.     int length;
  222.     while (!shuttingdown)
  223.     {
  224.         getline(cin, input);
  225.         length = input.length();
  226.         if (length > 0)
  227.         {
  228.             if (length > 255)
  229.             {
  230.                 printf("\n-- Message too long to send. Keep under 255 characters. --\n");
  231.                 continue;
  232.             }
  233.             SendChatMessage(input);
  234.             printf("[Webchat] [%s] %s\n", username.c_str(), input.c_str());
  235.         }
  236.     }
  237.     thread2exit = true;
  238.     return NULL;
  239. }
  240.  
  241. string StringToUpper(const string & s)
  242. {
  243.     string ret(s.size(), char());
  244.     for(unsigned int i = 0; i < s.size(); ++i)
  245.         ret[i] = (s[i] <= 'z' && s[i] >= 'a') ? s[i]-('a'-'A') : s[i];
  246.     return ret;
  247. }
  248.  
  249. inline void SafeShutdown()
  250. {
  251.     curl_global_cleanup();
  252.     remove(Session_Storage);
  253.  
  254.     ofstream myfile;
  255.     myfile.open(Session_Storage, ios::trunc);
  256.     myfile << "# Session Database\n";
  257.     myfile.close();
  258. }
  259.  
  260. BOOL WINAPI ConsoleHandler(DWORD CEvent)
  261. {
  262.     switch(CEvent)
  263.     {
  264.         case CTRL_C_EVENT:
  265.             SafeShutdown();
  266.             break;
  267.         case CTRL_BREAK_EVENT:
  268.             SafeShutdown();
  269.             break;
  270.         case CTRL_CLOSE_EVENT:
  271.             SafeShutdown();
  272.             break;
  273.         case CTRL_LOGOFF_EVENT:
  274.             SafeShutdown();
  275.             break;
  276.         case CTRL_SHUTDOWN_EVENT:
  277.             SafeShutdown();
  278.             break;
  279.     }
  280.     return TRUE;
  281. }
  282.  
  283.  
  284. int main(void)
  285. {
  286.     printf("\nPlease input your username: ");
  287.     cin >> username;
  288.     printf("\nPlease input your password: ");
  289.     string password = "";
  290.     cin >> password;
  291.  
  292.     password = StringToUpper(username) + ":" + StringToUpper(password);
  293.  
  294.     unsigned char hash[20];
  295.     char hexstring[41];
  296.     sha1::calc(password.c_str(), password.length(), hash);
  297.     sha1::toHexString(hash, hexstring);
  298.    
  299.     password = hexstring;
  300.  
  301.     curl_global_init(CURL_GLOBAL_ALL);
  302.  
  303.     if (!authenticate(password))
  304.     {
  305.         printf("\nFailed to authenticate. Aborting...\n");
  306.         SafeShutdown();
  307.         Sleep(2000);
  308.         return -1;
  309.     }
  310.  
  311.     printf("\nSuccessfully authenticated.\n");
  312.  
  313.     /*
  314.     printf("\nEnter your desired display name: ");
  315.     cin >> username;
  316.     printf("\n");
  317.     */
  318.  
  319.     string message = getMessages();
  320.     if (message.length() < 1)
  321.     {
  322.         printf("\nFailed to connect to server.\n");
  323.         SafeShutdown();
  324.         Sleep(2000);
  325.         return -1;
  326.     }
  327.     HandleMessage(message);
  328.  
  329.     CreateThread(NULL, 0, threadOne, NULL, 0, NULL);
  330.     CreateThread(NULL, 0, threadTwo, NULL, 0, NULL);
  331.  
  332.     while (!shuttingdown || !thread1exit || !thread2exit)
  333.     {
  334.         if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
  335.         {
  336.             SafeShutdown();
  337.             return -1;
  338.         }
  339.         Sleep(1000);
  340.     }
  341.  
  342.     SafeShutdown();
  343.  
  344.     return 0;
  345. }
Advertisement
Add Comment
Please, Sign In to add comment