Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.55 KB | None | 0 0
  1. #include "auth.h"
  2. #include "connection.h"
  3. #include "defines.h"
  4. #include "tools.h"
  5. #include "scripting.h"
  6. #include "users.h"
  7. #include "records.h"
  8.  
  9. #include <iostream>
  10. #include <cstdio>
  11. #include <ctime>
  12. #include <exception>
  13. #include <cstdlib>
  14. #include <boost/thread.hpp>
  15. #include <boost/bind.hpp>
  16. #include <sstream>
  17.  
  18. #ifdef WIN32
  19.     #include <windows.h>
  20. #else
  21.     #include <pthread.h>
  22.     #pragma comment(lib, "pthread")
  23. #endif
  24.  
  25. #ifdef WIN32
  26.     DWORD threadFunc(LPVOID p)
  27.     {
  28.         ::Sleep(100);
  29.         return 1; //shutdown thread
  30.     }
  31. #else
  32.     void *threadFunc(void *p)
  33.     {
  34.         char *msg = (char*)p;
  35.         printf("%s", msg);
  36.     }
  37. #endif
  38.  
  39. void privateMessage(Connection* socket, const std::string& channel, const std::string& msg)
  40. {
  41.     socket->write("NOTICE " + channel + " :" + msg);
  42. }
  43.  
  44. extern Users *g_users;
  45. extern Records *g_records;
  46.  
  47. struct _user
  48. {
  49.     short group;
  50. };
  51.  
  52. int main(int argc, char **argv)
  53. {
  54. #ifdef WIN32
  55.     DWORD threadId[6];
  56.     for (int i = 0; i < 5; ++i) {
  57.         if (CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&threadFunc, (LPVOID)&i, 0, &threadId[i]) == NULL) {
  58.             printf("Failed to create thread %d\nStopping....\n", i+1);
  59.             break;
  60.         }
  61.         printf("Created Thread %d\n", i+1);
  62.     }
  63. #else
  64.     pthread_t threads[6];
  65.     int p[5];
  66.     char message[50];
  67.     for (int i = 0; i < 5; ++i) {
  68.         sprintf(message, "Created Thread %d\n", i+1);
  69.         p[i] = pthread_create(&threads[i], NULL, threadFunc, (void*)message);
  70.         if (p[i] != 0) {
  71.             printf("Failed to create thread %d\nStopping....\n", i+1);
  72.             break;
  73.         }
  74.         pthread_join(threads[i], NULL);
  75.     }
  76. #endif
  77.     Connection *socket = new Connection(BOT_SERVER, BOT_PORT);
  78.     boost::thread thread(boost::bind(&Connection::connectToHost, socket));
  79.     thread.join();
  80.     if (!socket->isConnected())
  81.         return 1;
  82.  
  83.     Script* script = new Script(socket);
  84.     g_users->setConnection(socket);
  85.     std::string buffer;
  86.     UsersMap users;
  87.     EventsMap events;
  88.     StringVec Users;
  89.     socket->write("USER " + std::string(BOT_NAME) + " * * :" + std::string(BOT_NAME));
  90.     socket->write("NICK " + std::string(BOT_NAME));
  91.     bool authed = false;
  92.     createDir("auths");
  93.     script->loadFile(std::string(BOT_SCRIPTING_FILE));
  94.     AuthedMap auths;
  95.     _user* __tmp__ = new _user;
  96.     __tmp__->group = 10;
  97.     auths.insert(std::make_pair("@Xeonz.users.quakenet.org", __tmp__));
  98.     std::string functions = "";
  99.     AuthsMap __auths;
  100.     HostsMap hosts;
  101.     while (socket->isConnected()) {
  102.         bool isPm = false;
  103.         ChannelsUsers channel_users = getChannelUsers();
  104.         buffer = socket->readLine();
  105.         std::string name = getName(buffer), msg = getMsg(buffer), channel = getChannel(buffer),
  106.             mode = getMode(buffer), mask = getMask(buffer), target = getModeTarget(buffer),
  107.             kicked = getKicked(buffer), host = getHost(buffer), t = getTime();
  108.  
  109.         if (buffer.substr(0, 4) == "PING") {
  110.             socket->write("PO" + buffer.substr(2));
  111.             printf("Received Ping.\n");
  112.         } else if (buffer.find("PRIVMSG") != std::string::npos) {
  113.             HostsMap::iterator it = hosts.find(name);
  114.             if (it == hosts.end())
  115.                 hosts.insert(std::make_pair(name, host));
  116.             else { //maybe new host.
  117.                 hosts.erase(it);
  118.                 hosts.insert(std::make_pair(name, host));
  119.             }
  120.             std::stringstream ss;
  121.             ss << t;
  122.             for (UsersMap::const_iterator it = users.begin(); it != users.end(); ++it) {
  123.                 if (toLower(it->first) == toLower(name)) {
  124.                     ss << " <" << it->second << name;
  125.                     break;
  126.                 }
  127.             }
  128.             if (ss.str().find(name) == std::string::npos)
  129.                 ss << " <" << name;
  130.  
  131.             ss << ">" << msg;
  132.             printf("%s\n", ss.str().c_str());
  133.             script->onMsg(name, channel, msg, mask);
  134.             if (channel.substr(0, 1) != "#") {
  135.                 isPm = true;
  136.             #ifdef _DEBUG
  137.                 printf("Received a private message.\n");
  138.             #endif
  139.             }
  140.  
  141.             Record *_record = new Record;
  142.             _record->id = g_records->getNextUserId();
  143.             _record->lines_of_text = msg.length();
  144.             if (!g_records->recordExist(name)) {
  145.                 g_records->insertRecord(name, *_record);
  146.             #ifdef _DEBUG
  147.                 printf("Inserting record...\n");
  148.             #endif
  149.             } else {
  150.             #ifdef _DEBUG
  151.                 printf("Saving record....\n");
  152.             #endif
  153.                 g_records->saveRecord(name, *_record);
  154.             }
  155.             delete _record;
  156.             _record = NULL;
  157.         } else if(buffer.find("MODE") != std::string::npos) {
  158.             printf("%s %s (%s) %s\n", t.c_str(), name.c_str(), mode.c_str(), target.c_str());
  159.             script->onMode(name, target, mode);
  160.             UsersMap::iterator it = users.find(target);
  161.             if (it != users.end()) {
  162.                 users.erase(it);
  163.                 users.insert(std::make_pair(target, mode));
  164.             }
  165.             if(!authed) {
  166.                 socket->write("AUTH " + std::string(BOT_AUTH) + " " + std::string(BOT_PASS));
  167.                 socket->write("MODE " + std::string(BOT_NAME) + " +x");
  168.                 StringVec __msg = splitString(std::string(BOT_CHAN), ",");
  169.                 for (StringVec::const_iterator it = __msg.begin(); it != __msg.end(); ++it)
  170.                     socket->write("JOIN " + *it);
  171.                 joinChannels(socket);
  172.                 authed = true;
  173.             #ifdef _DEBUG
  174.                 printf("Authed.\n");
  175.             #endif
  176.             } else {
  177.                 printf("%s %s (%s) %s\n", t.c_str(), name.c_str(), mode.c_str(), target.c_str());
  178.                 script->onMode(name, target, mode);
  179.                 Record *_record = new Record;
  180.                 if (mode.find("+b") != std::string::npos) {
  181.                     _record->bans = 1;
  182.                 } else if (mode.find("-b") != std::string::npos) {
  183.                     _record->unbans = 1;
  184.                 }
  185.                 if (!g_records->recordExist(name)) {
  186.                     g_records->insertRecord(name, *_record);
  187.                 #ifdef _DEBUG
  188.                     printf("Inserting record...\n");
  189.                 #endif
  190.                 } else {
  191.                 #ifdef _DEBUG
  192.                     printf("Saving record....\n");
  193.                 #endif
  194.                     g_records->saveRecord(name, *_record);
  195.                 }
  196.                 delete _record;
  197.                 _record = NULL;
  198.             }
  199.         } else if(buffer.find("INVITE") != std::string::npos) {
  200.             printf("%s %s(%s) has invited you to %s\n", t.c_str(), name.c_str(), mask.c_str(), channel.c_str());
  201.             User user;
  202.             user.name = toLower(name);
  203.             if (isAlreadyLoggedIn(user.name)) {
  204.                 user.modes.push_back(MODE_O);
  205.                 user.modes.push_back(MODE_N);
  206.                 g_users->updateMode(&user, channel, "+o");
  207.                 updateChannels(channel);
  208.                 socket->write("JOIN " + channel);
  209.                 if (g_users->isFirstTimeJoin(channel))
  210.                     g_users->first_time_join(&user, channel);
  211.             #ifdef _DEBUG
  212.                 else
  213.                     printf("Channel is already in use, not updating.\n");
  214.             #endif
  215.             }
  216.         #ifdef _DEBUG
  217.             else
  218.                 printf("User %s not logged in, not joining/updating.\n", toLower(name).c_str());
  219.         #endif
  220.         } else if(buffer.find("NOTICE") != std::string::npos) {
  221.             printf("%s[%s]: %s\n", t.c_str(), name.c_str(), msg.c_str());
  222.         } else if(buffer.find("JOIN") != std::string::npos) {
  223.             printf("%s %s[%s] has joined %s\n", t.c_str(), name.c_str(), mask.c_str(), channel.c_str());
  224.             script->onJoin(name, channel, mask);
  225.             for (ChannelsUsers::const_iterator it = channel_users.begin(); it != channel_users.end(); ++it) {
  226.                 UsersMap __users = it->second;
  227.                 for (UsersMap::const_iterator _user = __users.begin(); _user != __users.end(); ++_user) {
  228.                     if (toLower(_user->second) == toLower(name) && it->first == channel) {
  229.                         User *p = new User;
  230.                         p->name = toLower(_user->second);
  231.                         if (isAlreadyLoggedIn(p->name)) {
  232.                             if (g_users->isOp(p))
  233.                                 g_users->updateMode(p, channel, "+o");
  234.                             else if (g_users->isVoice(p))
  235.                                 g_users->updateMode(p, channel, "+v");
  236.                             #ifdef _DEBUG
  237.                             else
  238.                                 printf("User %s has no flags, not updating.\n", toLower(_user->second).c_str());
  239.                             #endif
  240.                         }
  241.                     #ifdef _DEBUG
  242.                         else
  243.                             printf("User %s is not logged in, not updating.\n", toLower(name).c_str());
  244.                     #endif
  245.                     }
  246.                 #ifdef _DEBUG
  247.                     else
  248.                         printf("User %s not found.\n", toLower(name).c_str());
  249.                 #endif
  250.                 }
  251.             }
  252.             HostsMap::iterator it = hosts.find(name);
  253.             if (it == hosts.end())
  254.                 hosts.insert(std::make_pair(name, host));
  255.             else { //maybe new host
  256.                 hosts.erase(it);
  257.                 hosts.insert(std::make_pair(name, host));
  258.             }
  259.             users.insert(std::make_pair(name, ""));
  260.             events.insert(std::make_pair(time(NULL) + 900000 / 1000, name));
  261.         } else if(buffer.find("PART") != std::string::npos) {
  262.             printf("%s %s[%s] has left %s\n", t.c_str(), name.c_str(), mask.c_str(), channel.c_str());
  263.             script->onPart(name, channel, mask);
  264.         } else if(buffer.find("QUIT") != std::string::npos) {
  265.             printf("%s %s[%s] has quit (%s)\n", t.c_str(), name.c_str(), mask.c_str(), msg.c_str());
  266.             script->onQuit(name, mask, msg);
  267.             onLogout(name);
  268.             HostsMap::iterator it = hosts.find(name);
  269.             if (it != hosts.end())
  270.                 hosts.erase(it);
  271.  
  272.         } else if(buffer.find("NICK") != std::string::npos) {
  273.             printf("%s %s is now known as %s\n", t.c_str(), name.c_str(), msg.c_str());
  274.             HostsMap::iterator it = hosts.find(name);
  275.             if (it != hosts.end()) {
  276.                 //for sure hes stored.
  277.                 hosts.erase(it);
  278.                 hosts.insert(std::make_pair(msg, host));
  279.             }
  280.         } else if(buffer.find("TOPIC") != std::string::npos) {
  281.             printf("Topic for %s: %s\n", channel.c_str(), msg.c_str());
  282.         } else if(buffer.find("KICK") != std::string::npos) {
  283.             printf("%s %s was kicked by %s\n", t.c_str(), kicked.c_str(), name.c_str());
  284.             HostsMap::iterator it = hosts.find(kicked);
  285.             if (it != hosts.end())
  286.                 hosts.erase(it);
  287.  
  288.         } else if(buffer.find("353") != std::string::npos) {
  289.             Users = splitString(msg, " ");
  290.             printf("Users for %s: ", channel.c_str());
  291.             for (StringVec::iterator it = Users.begin(); it != Users.end(); ++it) {
  292.                 std::string _tmp = (*it).substr(0, 1);
  293.                 if (_tmp == "+" || _tmp == "@" || _tmp == "&" || _tmp == "%")
  294.                     users[(*it).substr(1, (*it).length()-1)] = _tmp;
  295.                 else
  296.                     users[*it] = "";
  297.                 printf("%s ", (*it).c_str());
  298.                 events.insert(std::make_pair(time(NULL) + 900000 / 1000, *it));
  299.             }
  300.             printf("\n");
  301.             channel_users.insert(std::make_pair(channel, users));
  302.         } else if (buffer.find("ERROR :Closing Link:") != std::string::npos) {
  303.         #ifdef _WIN32
  304.             system("TibiaBot.exe");
  305.         #else
  306.             system("./TibiaBot");
  307.         #endif
  308.             std::exit(1);
  309.         } else {
  310.             if(!buffer.empty())
  311.                 printf("%s\n", buffer.c_str());
  312.         }
  313.         for (EventsMap::iterator it = events.begin(); it != events.end(); ++it) {
  314.             if (time(NULL) >= it->first) {
  315.                 if (it->second.find("+") != std::string::npos)
  316.                     it->second.erase(it->second.find("+"));
  317.                 else if (it->second.find("@") != std::string::npos)
  318.                     it->second.erase(it->second.find("@"));
  319.  
  320.                 socket->write("MODE " + channel + " +v " + it->second);
  321.                 events.erase(it);
  322.                 break;
  323.             }
  324.         }
  325.         StringVec params = splitString(msg, " ");
  326.         if (params[0] == "!record") {
  327.             char rec[256];
  328.             if (g_records->getRecord(rec, params[1])) {
  329.                 socket->write("PRIVMSG " + channel + " :" + std::string(rec));
  330.             } else {
  331.                 socket->write("PRIVMSG " + channel + " :Unable to get record for " + params[1] + ".");
  332.             }
  333.         }
  334.         if (host == std::string(BOT_OWNER)) {
  335.             if (params[0] == "!quit") {
  336.                 for (ChannelsUsers::const_iterator _cu = channel_users.begin(); _cu != channel_users.end(); ++_cu) {
  337.                     UsersMap _um = _cu->second;
  338.                     for (UsersMap::iterator _user = _um.begin(); _user != _um.end(); ++_user)
  339.                         onLogout(_user->second);
  340.                 }
  341.                 socket->write("QUIT :" + (params.size() == 1 ? "Requested" : params[1]));
  342.                 exit(1);
  343.             } else if (params[0] == "!clear") {
  344.                 for (_UsersMap::iterator it = g_users->begin(); it != g_users->end(); it++)
  345.                     delete it->second;
  346.  
  347.  
  348.                 g_users->clear();
  349.             }
  350.         }
  351.         if(authed && isPm) {
  352.             if (toLower(params[0]) == "hello") {
  353.                 if (params.size() == 1) {
  354.                     privateMessage(socket, channel, "You must enter a good username.");
  355.                     continue;
  356.                 } else if (params.size() == 2) {
  357.                     privateMessage(socket, channel, "You must enter a good password.");
  358.                     continue;
  359.                 }
  360.  
  361.                 std::string authName = params[1], password = params[2];
  362.                 if (isExist(authName)) {
  363.                     privateMessage(socket, channel, "Username already exists.");
  364.                     continue;
  365.                 }
  366.                 Auth *newAuth = new Auth(toLower(authName), toLower(password), toLower(host));
  367.                 if (newAuth->saveAuth()) {
  368.                     privateMessage(socket, channel, "Your auth has been saved.");
  369.                     AuthPtr __p(newAuth);
  370.                     __auths.insert(std::make_pair(toLower(authName), __p));
  371.                 } else {
  372.                     privateMessage(socket, channel, "Could not save your auth.");
  373.                     continue;
  374.                 }
  375.  
  376.                 std::string c = "auths/" + authName + ".log";
  377.                 FILE *fp = fopen(c.c_str(), "a+");
  378.                 if (fp) {
  379.                     fprintf(fp, "%s is now authed as %s with password %s\n", name.c_str(), authName.c_str(), password.c_str());
  380.                     fclose(fp);
  381.                 }
  382.             } else if (toLower(params[0]) == "topic") {
  383.                 if (params.size() == 1) {
  384.                     privateMessage(socket, channel, "Please specifiy a channel.");
  385.                     continue;
  386.                 } else if (params.size() == 2) {
  387.                     privateMessage(socket, channel, "Invalid params.");
  388.                     continue;
  389.                 } else if (!isAlreadyLoggedIn(name)) {
  390.                     privateMessage(socket, channel, "You are not logged in.");
  391.                     continue;
  392.                 }
  393.                 std::string out;
  394.                 for (int i = 2; i < params.size(); i++)
  395.                     out += params[i];
  396.  
  397.                 socket->write("TOPIC " + params[1] + " " + out);
  398.             } else if (toLower(params[0]) == "chanlev") {
  399.                 if (!isAlreadyLoggedIn(name)) {
  400.                     privateMessage(socket, channel, "You are not authed on me.");
  401.                     continue;
  402.                 }
  403.                 if (params.size() == 1) {
  404.                     privateMessage(socket, channel, "You must specifiy a channel.");
  405.                     continue;
  406.                 } else if (params.size() == 2) {
  407.                     privateMessage(socket, channel, "You must specifiy a name.");
  408.                     continue;
  409.                 } else if (params.size() == 3) {
  410.                     privateMessage(socket, channel, "You must specifiy a mode.");
  411.                     continue;
  412.                 }
  413.                 if (!hasOp(name, params[1])) {
  414.                     privateMessage(socket, channel, params[1] + ": Insufficient privileges");
  415.                     continue;
  416.                 }
  417.                 if (!isAlreadyLoggedIn(params[2])) {
  418.                     privateMessage(socket, channel, "User " + params[2] + " is not authed.");
  419.                     continue;
  420.                 }
  421.                 g_users->updateMode(params[2], params[1], params[3]);
  422.                 g_users->insertUserMode(params[2], params[3], params[1]);
  423.                 privateMessage(socket, channel, "Done. Flags for " + params[2] + " are now: " + params[3] + ".");
  424.             } else if (toLower(params[0]) == "auth") {
  425.                 if (params.size() == 1) {
  426.                     privateMessage(socket, channel, "Please enter your username.");
  427.                     continue;
  428.                 } else if (params.size() == 2) {
  429.                     privateMessage(socket, channel, "Please enter your password.");
  430.                     continue;
  431.                 }
  432.                 std::string authName = toLower(params[1]), password = toLower(params[2]);
  433.                 if (!checkAuth(authName, password)) {
  434.                     privateMessage(socket, channel, "Check your username and password.");
  435.                 } else if (isAlreadyLoggedIn(authName)) {
  436.                     privateMessage(socket, channel, "You are already logged in.");
  437.                 } else if (!checkHost(authName, host)) {
  438.                     privateMessage(socket, channel, "You are not " + authName + ".");
  439.                 } else {
  440.                     privateMessage(socket, channel, "You are now logged in as: " + authName + ".");
  441.                     onLogin(authName);
  442.                 }
  443.  
  444.                 std::string log = "auths/" + authName + ".log";
  445.                 FILE *fp = fopen(log.c_str(), "a+");
  446.                 if (fp) {
  447.                     fprintf(fp, "%s is now logged in as %s[Acc: %s, Password: %s]\n", name.c_str(), authName.c_str(), authName.c_str(), password.c_str());
  448.                     fclose(fp);
  449.                 }
  450.             }
  451.         } else if (authed) {
  452.             _user* p = NULL;
  453.             for (AuthedMap::const_iterator it = auths.begin(); it != auths.end(); ++it) {
  454.                 if (toLower(it->first) == toLower(host))
  455.                     p = it->second;
  456.             }
  457.             if (p && p->group > 5) {
  458.                 if (params[0] == "+bk") {
  459.                     if (params.size() < 1)
  460.                         continue;
  461.  
  462.                     socket->write("KICK " + params[1] + " :Banned.");
  463.                     for (HostsMap::const_iterator it = hosts.begin(); it != hosts.end(); ++it) {
  464.                         if (toLower(it->first) == toLower(params[1]))
  465.                             socket->write("MODE " + channel + " +b *!*" + it->second);
  466.                     }
  467.                 }
  468.             }
  469.         }
  470.     }
  471.     delete socket;
  472.     return 0;
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement