Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.12 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 (params[0] == "!record") {
  306.             char rec[256];
  307.             if (params.size() == 1) {
  308.                 socket->write("PRIVMSG " + channel + " :Please state a name after !record. Example: !record TibiaBot");
  309.             } else if (g_records->getRecord(rec, params[1])) {
  310.                 socket->write("PRIVMSG " + channel + " :" + std::string(rec));
  311.             } else {
  312.                 socket->write("PRIVMSG " + channel + " :Unable to get record for " + params[1] + ".");
  313.             }
  314.         }
  315.        
  316.         if (host == std::string(BOT_OWNER)) {
  317.             if (params[0] == "!quit") {
  318.                 for (ChannelsUsers::const_iterator _cu = channel_users.begin(); _cu != channel_users.end(); ++_cu) {
  319.                     UsersMap _um = _cu->second;
  320.                     for (UsersMap::iterator _user = _um.begin(); _user != _um.end(); ++_user)
  321.                         onLogout(_user->second);
  322.                 }
  323.                 socket->write("QUIT :" + (params.size() == 1 ? "Requested" : params[1]));
  324.                 exit(1);
  325.             } else if (params[0] == "!clear") {
  326.                 for (_UsersMap::iterator it = g_users->begin(); it != g_users->end(); it++)
  327.                     delete it->second;
  328.  
  329.  
  330.                 g_users->clear();
  331.             }
  332.         }
  333.         if(authed && isPm) {
  334.             if (toLower(params[0]) == "hello") {
  335.                 if (params.size() == 1) {
  336.                     privateMessage(socket, channel, "You must enter a good username.");
  337.                     continue;
  338.                 } else if (params.size() == 2) {
  339.                     privateMessage(socket, channel, "You must enter a good password.");
  340.                     continue;
  341.                 }
  342.  
  343.                 std::string authName = params[1], password = params[2];
  344.                 if (isExist(authName)) {
  345.                     privateMessage(socket, channel, "Username already exists.");
  346.                     continue;
  347.                 }
  348.                 Auth *newAuth = new Auth(toLower(authName), toLower(password), toLower(host));
  349.                 if (newAuth->saveAuth()) {
  350.                     privateMessage(socket, channel, "Your auth has been saved.");
  351.                     AuthPtr __p(newAuth);
  352.                     __auths.insert(std::make_pair(toLower(authName), __p));
  353.                 } else {
  354.                     privateMessage(socket, channel, "Could not save your auth.");
  355.                     continue;
  356.                 }
  357.  
  358.                 std::string c = "auths/" + authName + ".log";
  359.                 FILE *fp = fopen(c.c_str(), "a+");
  360.                 if (fp) {
  361.                     fprintf(fp, "%s is now authed as %s with password %s\n", name.c_str(), authName.c_str(), password.c_str());
  362.                     fclose(fp);
  363.                 }
  364.             } else if (toLower(params[0]) == "chanlev") {
  365.                 if (!isAlreadyLoggedIn(name)) {
  366.                     privateMessage(socket, channel, "You are not authed on me.");
  367.                     continue;
  368.                 }
  369.                 if (params.size() == 1) {
  370.                     privateMessage(socket, channel, "You must specifiy a channel.");
  371.                     continue;
  372.                 } else if (params.size() == 2) {
  373.                     privateMessage(socket, channel, "You must specifiy a name.");
  374.                     continue;
  375.                 } else if (params.size() == 3) {
  376.                     privateMessage(socket, channel, "You must specifiy a mode.");
  377.                     continue;
  378.                 }
  379.                 if (!hasOp(name, params[1])) {
  380.                     privateMessage(socket, channel, params[1] + ": Insufficient privileges");
  381.                     continue;
  382.                 }
  383.                 if (!isAlreadyLoggedIn(params[2])) {
  384.                     privateMessage(socket, channel, "User " + params[2] + " is not authed.");
  385.                     continue;
  386.                 }
  387.                 g_users->updateMode(params[2], params[1], params[3]);
  388.                 g_users->insertUserMode(params[2], params[3], params[1]);
  389.                 privateMessage(socket, channel, "Done. Flags for " + params[2] + " are now: " + params[3] + ".");
  390.             } else if (toLower(params[0]) == "auth") {
  391.                 if (params.size() == 1) {
  392.                     privateMessage(socket, channel, "Please enter your username.");
  393.                     continue;
  394.                 } else if (params.size() == 2) {
  395.                     privateMessage(socket, channel, "Please enter your password.");
  396.                     continue;
  397.                 }
  398.                 std::string authName = toLower(params[1]), password = toLower(params[2]);
  399.                 if (!checkAuth(authName, password)) {
  400.                     privateMessage(socket, channel, "Check your username and password.");
  401.                 } else if (isAlreadyLoggedIn(authName)) {
  402.                     privateMessage(socket, channel, "You are already logged in.");
  403.                 } else if (!checkHost(authName, host)) {
  404.                     privateMessage(socket, channel, "You are not " + authName + ".");
  405.                 } else {
  406.                     privateMessage(socket, channel, "You are now logged in as: " + authName + ".");
  407.                     onLogin(authName);
  408.                 }
  409.  
  410.                 std::string log = "auths/" + authName + ".log";
  411.                 FILE *fp = fopen(log.c_str(), "a+");
  412.                 if (fp) {
  413.                     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());
  414.                     fclose(fp);
  415.                 }
  416.             } else if (toLower(params[0]) == "topic") {
  417.                 if (params.size() == 1) {
  418.                     privateMessage(socket, channel, "Please specifiy a channel.");
  419.                     continue;
  420.                 } else if (params.size() == 2) {
  421.                     privateMessage(socket, channel, "Invalid params.");
  422.                     continue;
  423.                 } else if (!isAlreadyLoggedIn(name)) {
  424.                     privateMessage(socket, channel, "You are not logged in.");
  425.                     continue;
  426.                 }
  427.                 std::string out;
  428.                 for (int i = 2; i < params.size(); i++)
  429.                     out += params[i];
  430.  
  431.                 socket->write("TOPIC " + params[1] + " " + out);
  432.             }
  433.         } else if (authed) {
  434.             _user* p = NULL;
  435.             for (AuthedMap::const_iterator it = auths.begin(); it != auths.end(); ++it) {
  436.                 if (toLower(it->first) == toLower(host))
  437.                     p = it->second;
  438.             }
  439.             if (p && p->group > 5) {
  440.                 if (params[0] == "+bk") {
  441.                     if (params.size() < 1)
  442.                         continue;
  443.                    
  444.                     if(params.size() < 2) {
  445.                         socket->write("KICK " + params[1] + " :Banned.");
  446.                         for (HostsMap::const_iterator it = hosts.begin(); it != hosts.end(); ++it) {
  447.                         if (toLower(it->first) == toLower(params[1]))
  448.                             socket->write("MODE " + channel + " +b *!*" + it->second);
  449.                         }
  450.                     } else {
  451.                     std::string out;
  452.                     for (int i = 2; i < params.size(); i++)
  453.                         out += params[i];
  454.                         socket->write("KICK " + params[1] + " " + out);
  455.                         for (HostsMap::const_iterator it = hosts.begin(); it != hosts.end(); ++it) {
  456.                         if (toLower(it->first) == toLower(params[1]))
  457.                             socket->write("MODE " + channel + " +b *!*" + it->second);
  458.                         }
  459.                     } //else { }
  460.                 }
  461.             }
  462.            
  463.             if (p && p->group > 4) {
  464.                 //
  465.             }
  466.            
  467.             if (p && p->group > 3) {
  468.                 if (toLower(params[0]) == "topic") {
  469.                     } if (params.size() == 2) {
  470.                         privateMessage(socket, channel, "Topic is too short!");
  471.                         continue;
  472.                     } else if (!isAlreadyLoggedIn(name)) {
  473.                         privateMessage(socket, channel, "You are not logged in.");
  474.                         continue;
  475.                     }
  476.                 std::string out;
  477.                 for (int i = 2; i < params.size(); i++)
  478.                     out += params[i];
  479.  
  480.                 socket->write("TOPIC " + params[1] + " " + out);
  481.             }
  482.            
  483.             if (p && p->group > 2) {
  484.                 //
  485.             }
  486.            
  487.             if (p && p->group > 1) {
  488.                 //
  489.             }
  490.         }
  491.     }
  492.     delete socket;
  493.     return 0;
  494. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement