Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.70 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("@Chrsman.users.quakenet.org", __tmp__));
  98.     AuthsMap __auths;
  99.     HostsMap hosts;
  100.     while (socket->isConnected()) {
  101.         bool isPm = false;
  102.         ChannelsUsers channel_users = getChannelUsers();
  103.         buffer = socket->readLine();
  104.         std::string name = getName(buffer), msg = getMsg(buffer), channel = getChannel(buffer),
  105.             mode = getMode(buffer), mask = getMask(buffer), target = getModeTarget(buffer),
  106.             kicked = getKicked(buffer), host = getHost(buffer), t = getTime();
  107.  
  108.         if (buffer.substr(0, 4) == "PING") {
  109.             socket->write("PO" + buffer.substr(2));
  110.             printf("Received Ping.\n");
  111.         } else if (buffer.find("PRIVMSG") != std::string::npos) {
  112.             if (!authed)
  113.                 continue;
  114.  
  115.             HostsMap::iterator it = hosts.find(name);
  116.             if (it == hosts.end())
  117.                 hosts.insert(std::make_pair(name, host));
  118.             else { //maybe new host.
  119.                 hosts.erase(it);
  120.                 hosts.insert(std::make_pair(name, host));
  121.             }
  122.             std::stringstream ss;
  123.             ss << t;
  124.             for (UsersMap::const_iterator it = users.begin(); it != users.end(); ++it) {
  125.                 if (toLower(it->first) == toLower(name)) {
  126.                     ss << " <" << it->second << name;
  127.                     break;
  128.                 } else {
  129.                     ss << " <" << name;
  130.                 }
  131.             }
  132.             ss << ">" << msg;
  133.             printf("%s\n", ss.str().c_str());
  134.             script->onMsg(name, channel, msg, mask);
  135.             if (channel.substr(0, 1) != "#")
  136.                 isPm = true;
  137.  
  138.             Record *_record = new Record;
  139.             _record->id = g_records->getNextUserId();
  140.             _record->lines_of_text = msg.length();
  141.             if (!g_records->recordExist(name)) {
  142.                 g_records->insertRecord(name, *_record);
  143.             } else {
  144.                 g_records->saveRecord(name, *_record);
  145.             }
  146.             delete _record;
  147.             _record = NULL;
  148.         } else if(buffer.find("MODE") != std::string::npos) {
  149.             printf("%s %s (%s) %s\n", t.c_str(), name.c_str(), mode.c_str(), target.c_str());
  150.             script->onMode(name, target, mode);
  151.             UsersMap::iterator it = users.find(target);
  152.             if (it != users.end()) {
  153.                 users.erase(it);
  154.                 users.insert(std::make_pair(target, mode));
  155.             }
  156.             if(!authed) {
  157.                 socket->write("AUTH " + std::string(BOT_AUTH) + " " + std::string(BOT_PASS));
  158.                 socket->write("MODE " + std::string(BOT_NAME) + " +x");
  159.                 StringVec __msg = splitString(std::string(BOT_CHAN), ",");
  160.                 for (StringVec::const_iterator it = __msg.begin(); it != __msg.end(); ++it)
  161.                     socket->write("JOIN " + *it);
  162.                 joinChannels(socket);
  163.                 authed = true;
  164.             #ifdef _DEBUG
  165.                 printf("Authed.\n");
  166.             #endif
  167.             } else {
  168.                 script->onMode(name, target, mode);
  169.                 Record *_record = new Record;
  170.                 if (mode == "+b") {
  171.                     _record->bans = 1;
  172.                 } else if (mode == "-b") {
  173.                     _record->unbans = 1;
  174.                 }
  175.                 if (!g_records->recordExist(name)) {
  176.                     g_records->insertRecord(name, *_record);
  177.                 } else {
  178.                     g_records->saveRecord(name, *_record);
  179.                 }
  180.                 delete _record;
  181.                 _record = NULL;
  182.             }
  183.         } else if(buffer.find("INVITE") != std::string::npos) {
  184.             printf("%s %s(%s) has invited you to %s\n", t.c_str(), name.c_str(), mask.c_str(), channel.c_str());
  185.             User user;
  186.             user.name = toLower(name);
  187.             if (isAlreadyLoggedIn(user.name)) {
  188.                 user.modes.push_back(MODE_O);
  189.                 user.modes.push_back(MODE_N);
  190.                 g_users->updateMode(&user, channel, "+o");
  191.                 updateChannels(channel);
  192.                 socket->write("JOIN " + channel);
  193.                 if (g_users->isFirstTimeJoin(channel))
  194.                     g_users->first_time_join(&user, channel);
  195.             }
  196.         } else if(buffer.find("NOTICE") != std::string::npos) {
  197.             printf("%s[%s]: %s\n", t.c_str(), name.c_str(), msg.c_str());
  198.         } else if(buffer.find("JOIN") != std::string::npos) {
  199.             printf("%s %s[%s] has joined %s\n", t.c_str(), name.c_str(), mask.c_str(), channel.c_str());
  200.             script->onJoin(name, channel, mask);
  201.             for (ChannelsUsers::const_iterator it = channel_users.begin(); it != channel_users.end(); ++it) {
  202.                 UsersMap __users = it->second;
  203.                 for (UsersMap::const_iterator _user = __users.begin(); _user != __users.end(); ++_user) {
  204.                     if (toLower(_user->second) == toLower(name) && it->first == channel) {
  205.                         User *p = new User;
  206.                         p->name = toLower(_user->second);
  207.                         if (isAlreadyLoggedIn(p->name)) {
  208.                             if (g_users->isOp(p))
  209.                                 g_users->updateMode(p, channel, "+o");
  210.                             else if (g_users->isVoice(p))
  211.                                 g_users->updateMode(p, channel, "+v");
  212.                         }
  213.                     }
  214.                 }
  215.             }
  216.             HostsMap::iterator it = hosts.find(name);
  217.             if (it == hosts.end())
  218.                 hosts.insert(std::make_pair(name, host));
  219.             else { //maybe new host
  220.                 hosts.erase(it);
  221.                 hosts.insert(std::make_pair(name, host));
  222.             }
  223.             users.insert(std::make_pair(name, ""));
  224.             events.insert(std::make_pair(time(NULL) + 900000 / 1000, name));
  225.         } else if(buffer.find("PART") != std::string::npos) {
  226.             printf("%s %s[%s] has left %s\n", t.c_str(), name.c_str(), mask.c_str(), channel.c_str());
  227.             script->onPart(name, channel, mask);
  228.         } else if(buffer.find("QUIT") != std::string::npos) {
  229.             printf("%s %s[%s] has quit (%s)\n", t.c_str(), name.c_str(), mask.c_str(), msg.c_str());
  230.             script->onQuit(name, mask, msg);
  231.             onLogout(name);
  232.             HostsMap::iterator it = hosts.find(name);
  233.             if (it != hosts.end())
  234.                 hosts.erase(it);
  235.  
  236.         } else if(buffer.find("NICK") != std::string::npos) {
  237.             printf("%s %s is now known as %s\n", t.c_str(), name.c_str(), msg.c_str());
  238.             HostsMap::iterator it = hosts.find(name);
  239.             if (it != hosts.end()) {
  240.                 //for sure hes stored.
  241.                 hosts.erase(it);
  242.                 hosts.insert(std::make_pair(msg, host));
  243.             }
  244.         } else if(buffer.find("TOPIC") != std::string::npos) {
  245.             printf("Topic for %s: %s\n", channel.c_str(), msg.c_str());
  246.         } else if(buffer.find("KICK") != std::string::npos) {
  247.             printf("%s %s was kicked by %s\n", t.c_str(), kicked.c_str(), name.c_str());
  248.             HostsMap::iterator it = hosts.find(kicked);
  249.             if (it != hosts.end())
  250.                 hosts.erase(it);
  251.  
  252.         } else if(buffer.find("353") != std::string::npos) {
  253.             Users = splitString(msg, " ");
  254.             printf("Users for %s: ", channel.c_str());
  255.             for (StringVec::iterator it = Users.begin(); it != Users.end(); ++it) {
  256.                 std::string _tmp = (*it).substr(0, 1);
  257.                 if (_tmp == "+" || _tmp == "@" || _tmp == "&" || _tmp == "%")
  258.                     users[(*it).substr(1, (*it).length()-1)] = _tmp;
  259.                 else
  260.                     users[*it] = "";
  261.                 printf("%s ", (*it).c_str());
  262.                 events.insert(std::make_pair(time(NULL) + 900000 / 1000, *it));
  263.             }
  264.             printf("\n");
  265.             channel_users.insert(std::make_pair(channel, users));
  266.             continue;
  267.         } else if (buffer.find("ERROR :Closing Link:") != std::string::npos) {
  268.         #ifdef _WIN32
  269.             system("TibiaBot.exe");
  270.         #else
  271.             system("./TibiaBot");
  272.         #endif
  273.             std::exit(1);
  274.         } else {
  275.             if(!buffer.empty()) {
  276.                 printf("%s\n", buffer.c_str());
  277.                 continue;
  278.             }
  279.         }
  280.         printf("\n");
  281.         for (EventsMap::iterator it = events.begin(); it != events.end(); ++it) {
  282.             if (time(NULL) >= it->first) {
  283.                 if (it->second.find("+") != std::string::npos)
  284.                     it->second.erase(it->second.find("+"));
  285.                 else if (it->second.find("@") != std::string::npos)
  286.                     it->second.erase(it->second.find("@"));
  287.  
  288.                 socket->write("MODE " + channel + " +v " + it->second);
  289.                 events.erase(it);
  290.                 break;
  291.             }
  292.         }
  293.         StringVec params = splitString(msg, " ");
  294.         if (params[0] == "!record") {
  295.             char rec[256];
  296.             if (params.size() == 1) {
  297.                 socket->write("PRIVMSG " + channel + " :Please state a name after !record. Example: !record TibiaBot");
  298.             } else if (g_records->getRecord(rec, params[1])) {
  299.                 socket->write("PRIVMSG " + channel + " :" + std::string(rec));
  300.             } else {
  301.                 socket->write("PRIVMSG " + channel + " :Unable to get record for " + params[1] + ".");
  302.             }
  303.         }
  304.        
  305.         if (host == std::string(BOT_OWNER)) {
  306.             if (params[0] == "!quit") {
  307.                 for (ChannelsUsers::const_iterator _cu = channel_users.begin(); _cu != channel_users.end(); ++_cu) {
  308.                     UsersMap _um = _cu->second;
  309.                     for (UsersMap::iterator _user = _um.begin(); _user != _um.end(); ++_user)
  310.                         onLogout(_user->second);
  311.                 }
  312.                 socket->write("QUIT :" + (params.size() == 1 ? "Requested" : params[1]));
  313.                 exit(1);
  314.             } else if (params[0] == "!clear") {
  315.                 for (_UsersMap::iterator it = g_users->begin(); it != g_users->end(); it++)
  316.                     delete it->second;
  317.  
  318.  
  319.                 g_users->clear();
  320.             }
  321.         }
  322.         if(authed && isPm) {
  323.             if (toLower(params[0]) == "hello") {
  324.                 if (params.size() == 1) {
  325.                     privateMessage(socket, channel, "You must enter a good username.");
  326.                     continue;
  327.                 } else if (params.size() == 2) {
  328.                     privateMessage(socket, channel, "You must enter a good password.");
  329.                     continue;
  330.                 }
  331.  
  332.                 std::string authName = params[1], password = params[2];
  333.                 if (isExist(authName)) {
  334.                     privateMessage(socket, channel, "Username already exists.");
  335.                     continue;
  336.                 }
  337.                 Auth *newAuth = new Auth(toLower(authName), toLower(password), toLower(host));
  338.                 if (newAuth->saveAuth()) {
  339.                     privateMessage(socket, channel, "Your auth has been saved.");
  340.                     AuthPtr __p(newAuth);
  341.                     __auths.insert(std::make_pair(toLower(authName), __p));
  342.                 } else {
  343.                     privateMessage(socket, channel, "Could not save your auth.");
  344.                     continue;
  345.                 }
  346.  
  347.                 std::string c = "auths/" + authName + ".log";
  348.                 FILE *fp = fopen(c.c_str(), "a+");
  349.                 if (fp) {
  350.                     fprintf(fp, "%s is now authed as %s with password %s\n", name.c_str(), authName.c_str(), password.c_str());
  351.                     fclose(fp);
  352.                 }
  353.             } else if (toLower(params[0]) == "chanlev") {
  354.                 if (!isAlreadyLoggedIn(name)) {
  355.                     privateMessage(socket, channel, "You are not authed on me.");
  356.                     continue;
  357.                 }
  358.                 if (params.size() == 1) {
  359.                     privateMessage(socket, channel, "You must specifiy a channel.");
  360.                     continue;
  361.                 } else if (params.size() == 2) {
  362.                     privateMessage(socket, channel, "You must specifiy a name.");
  363.                     continue;
  364.                 } else if (params.size() == 3) {
  365.                     privateMessage(socket, channel, "You must specifiy a mode.");
  366.                     continue;
  367.                 }
  368.                 if (!hasOp(name, params[1])) {
  369.                     privateMessage(socket, channel, params[1] + ": Insufficient privileges");
  370.                     continue;
  371.                 }
  372.                 if (!isAlreadyLoggedIn(params[2])) {
  373.                     privateMessage(socket, channel, "User " + params[2] + " is not authed.");
  374.                     continue;
  375.                 }
  376.                 g_users->updateMode(params[2], params[1], params[3]);
  377.                 g_users->insertUserMode(params[2], params[3], params[1]);
  378.                 privateMessage(socket, channel, "Done. Flags for " + params[2] + " are now: " + params[3] + ".");
  379.             } else if (toLower(params[0]) == "auth") {
  380.                 if (params.size() == 1) {
  381.                     privateMessage(socket, channel, "Please enter your username.");
  382.                     continue;
  383.                 } else if (params.size() == 2) {
  384.                     privateMessage(socket, channel, "Please enter your password.");
  385.                     continue;
  386.                 }
  387.                 std::string authName = toLower(params[1]), password = toLower(params[2]);
  388.                 if (!checkAuth(authName, password)) {
  389.                     privateMessage(socket, channel, "Check your username and password.");
  390.                 } else if (isAlreadyLoggedIn(authName)) {
  391.                     privateMessage(socket, channel, "You are already logged in.");
  392.                 } else if (!checkHost(authName, host)) {
  393.                     privateMessage(socket, channel, "You are not " + authName + ".");
  394.                 } else {
  395.                     privateMessage(socket, channel, "You are now logged in as: " + authName + ".");
  396.                     onLogin(authName);
  397.                 }
  398.  
  399.                 std::string log = "auths/" + authName + ".log";
  400.                 FILE *fp = fopen(log.c_str(), "a+");
  401.                 if (fp) {
  402.                     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());
  403.                     fclose(fp);
  404.                 }
  405.             } else if (toLower(params[0]) == "topic") {
  406.                 if (params.size() == 1) {
  407.                     privateMessage(socket, channel, "Please specifiy a channel.");
  408.                     continue;
  409.                 } else if (params.size() == 2) {
  410.                     privateMessage(socket, channel, "Invalid params.");
  411.                     continue;
  412.                 } else if (!isAlreadyLoggedIn(name)) {
  413.                     privateMessage(socket, channel, "You are not logged in.");
  414.                     continue;
  415.                 }
  416.                 std::string out;
  417.                 for (int i = 2; i < params.size(); i++)
  418.                     out += params[i];
  419.  
  420.                 socket->write("TOPIC " + params[1] + " " + out);
  421.             }
  422.         } else if (authed) {
  423.             _user* p = NULL;
  424.             for (AuthedMap::const_iterator it = auths.begin(); it != auths.end(); ++it) {
  425.                 if (toLower(it->first) == toLower(host))
  426.                     p = it->second;
  427.             }
  428.             if (p && p->group > 5) {
  429.                 if (params[0] == "+bk") {
  430.                     if (params.size() < 1)
  431.                         continue;
  432.                    
  433.                     if(params.size() < 2) {
  434.                         socket->write("KICK " + params[1] + " :Banned.");
  435.                         for (HostsMap::const_iterator it = hosts.begin(); it != hosts.end(); ++it) {
  436.                         if (toLower(it->first) == toLower(params[1]))
  437.                             socket->write("MODE " + channel + " +b *!*" + it->second);
  438.                         }
  439.                     } else {
  440.                     std::string out;
  441.                     for (int i = 2; i < params.size(); i++)
  442.                         out += params[i];
  443.                         socket->write("KICK " + params[1] + " " + out);
  444.                         for (HostsMap::const_iterator it = hosts.begin(); it != hosts.end(); ++it) {
  445.                         if (toLower(it->first) == toLower(params[1]))
  446.                             socket->write("MODE " + channel + " +b *!*" + it->second);
  447.                         }
  448.                     } //else { }
  449.                 }
  450.             }
  451.            
  452.             if (p && p->group > 4) {
  453.                 //
  454.             }
  455.            
  456.             if (p && p->group > 3) {
  457.                 if (toLower(params[0]) == "topic") {
  458.                     if (params.size() == 2) {
  459.                         privateMessage(socket, channel, "Topic is too short!");
  460.                         continue;
  461.                     } else if (!isAlreadyLoggedIn(name)) {
  462.                         privateMessage(socket, channel, "You are not logged in.");
  463.                         continue;
  464.                     }
  465.                 std::string out;
  466.                 for (int i = 2; i < params.size(); i++)
  467.                     out += params[i];
  468.  
  469.                 socket->write("TOPIC " + params[1] + " " + out);
  470.                 }
  471.             }
  472.            
  473.             if (p && p->group > 2) {
  474.                 //
  475.             }
  476.            
  477.             if (p && p->group > 1) {
  478.                 //
  479.             }
  480.         }
  481.     }
  482.     delete socket;
  483.     return 0;
  484. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement