Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "auth.h"
- #include "connection.h"
- #include "defines.h"
- #include "tools.h"
- #include "scripting.h"
- #include "users.h"
- #include "records.h"
- #include <iostream>
- #include <cstdio>
- #include <ctime>
- #include <exception>
- #include <cstdlib>
- #include <boost/thread.hpp>
- #include <boost/bind.hpp>
- #include <sstream>
- #ifdef WIN32
- #include <windows.h>
- #else
- #include <pthread.h>
- #pragma comment(lib, "pthread")
- #endif
- #ifdef WIN32
- DWORD threadFunc(LPVOID p)
- {
- ::Sleep(100);
- return 1; //shutdown thread
- }
- #else
- void *threadFunc(void *p)
- {
- char *msg = (char*)p;
- printf("%s", msg);
- }
- #endif
- void privateMessage(Connection* socket, const std::string& channel, const std::string& msg)
- {
- socket->write("NOTICE " + channel + " :" + msg);
- }
- extern Users *g_users;
- extern Records *g_records;
- struct _user
- {
- short group;
- };
- int main(int argc, char **argv)
- {
- #ifdef WIN32
- DWORD threadId[6];
- for (int i = 0; i < 5; ++i) {
- if (CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&threadFunc, (LPVOID)&i, 0, &threadId[i]) == NULL) {
- printf("Failed to create thread %d\nStopping....\n", i+1);
- break;
- }
- printf("Created Thread %d\n", i+1);
- }
- #else
- pthread_t threads[6];
- int p[5];
- char message[50];
- for (int i = 0; i < 5; ++i) {
- sprintf(message, "Created Thread %d\n", i+1);
- p[i] = pthread_create(&threads[i], NULL, threadFunc, (void*)message);
- if (p[i] != 0) {
- printf("Failed to create thread %d\nStopping....\n", i+1);
- break;
- }
- pthread_join(threads[i], NULL);
- }
- #endif
- Connection *socket = new Connection(BOT_SERVER, BOT_PORT);
- boost::thread thread(boost::bind(&Connection::connectToHost, socket));
- thread.join();
- if (!socket->isConnected())
- return 1;
- Script* script = new Script(socket);
- g_users->setConnection(socket);
- std::string buffer;
- UsersMap users;
- EventsMap events;
- StringVec Users;
- socket->write("USER " + std::string(BOT_NAME) + " * * :" + std::string(BOT_NAME));
- socket->write("NICK " + std::string(BOT_NAME));
- bool authed = false;
- createDir("auths");
- script->loadFile(std::string(BOT_SCRIPTING_FILE));
- AuthedMap auths;
- _user* __tmp__ = new _user;
- __tmp__->group = 10;
- auths.insert(std::make_pair("@Xeonz.users.quakenet.org", __tmp__));
- std::string functions = "";
- AuthsMap __auths;
- HostsMap hosts;
- while (socket->isConnected()) {
- bool isPm = false;
- ChannelsUsers channel_users = getChannelUsers();
- buffer = socket->readLine();
- std::string name = getName(buffer), msg = getMsg(buffer), channel = getChannel(buffer),
- mode = getMode(buffer), mask = getMask(buffer), target = getModeTarget(buffer),
- kicked = getKicked(buffer), host = getHost(buffer), t = getTime();
- if (buffer.substr(0, 4) == "PING") {
- socket->write("PO" + buffer.substr(2));
- printf("Received Ping.\n");
- } else if (buffer.find("PRIVMSG") != std::string::npos) {
- HostsMap::iterator it = hosts.find(name);
- if (it == hosts.end())
- hosts.insert(std::make_pair(name, host));
- else { //maybe new host.
- hosts.erase(it);
- hosts.insert(std::make_pair(name, host));
- }
- std::stringstream ss;
- ss << t;
- for (UsersMap::const_iterator it = users.begin(); it != users.end(); ++it) {
- if (toLower(it->first) == toLower(name)) {
- ss << " <" << it->second << name;
- break;
- }
- }
- if (ss.str().find(name) == std::string::npos)
- ss << " <" << name;
- ss << ">" << msg;
- printf("%s\n", ss.str().c_str());
- script->onMsg(name, channel, msg, mask);
- if (channel.substr(0, 1) != "#") {
- isPm = true;
- #ifdef _DEBUG
- printf("Received a private message.\n");
- #endif
- }
- Record *_record = new Record;
- _record->id = g_records->getNextUserId();
- _record->lines_of_text = msg.length();
- if (!g_records->recordExist(name)) {
- g_records->insertRecord(name, *_record);
- #ifdef _DEBUG
- printf("Inserting record...\n");
- #endif
- } else {
- #ifdef _DEBUG
- printf("Saving record....\n");
- #endif
- g_records->saveRecord(name, *_record);
- }
- delete _record;
- _record = NULL;
- } else if(buffer.find("MODE") != std::string::npos) {
- printf("%s %s (%s) %s\n", t.c_str(), name.c_str(), mode.c_str(), target.c_str());
- script->onMode(name, target, mode);
- UsersMap::iterator it = users.find(target);
- if (it != users.end()) {
- users.erase(it);
- users.insert(std::make_pair(target, mode));
- }
- if(!authed) {
- socket->write("AUTH " + std::string(BOT_AUTH) + " " + std::string(BOT_PASS));
- socket->write("MODE " + std::string(BOT_NAME) + " +x");
- StringVec __msg = splitString(std::string(BOT_CHAN), ",");
- for (StringVec::const_iterator it = __msg.begin(); it != __msg.end(); ++it)
- socket->write("JOIN " + *it);
- joinChannels(socket);
- authed = true;
- #ifdef _DEBUG
- printf("Authed.\n");
- #endif
- } else {
- printf("%s %s (%s) %s\n", t.c_str(), name.c_str(), mode.c_str(), target.c_str());
- script->onMode(name, target, mode);
- Record *_record = new Record;
- if (mode.find("+b") != std::string::npos) {
- _record->bans = 1;
- } else if (mode.find("-b") != std::string::npos) {
- _record->unbans = 1;
- }
- if (!g_records->recordExist(name)) {
- g_records->insertRecord(name, *_record);
- #ifdef _DEBUG
- printf("Inserting record...\n");
- #endif
- } else {
- #ifdef _DEBUG
- printf("Saving record....\n");
- #endif
- g_records->saveRecord(name, *_record);
- }
- delete _record;
- _record = NULL;
- }
- } else if(buffer.find("INVITE") != std::string::npos) {
- printf("%s %s(%s) has invited you to %s\n", t.c_str(), name.c_str(), mask.c_str(), channel.c_str());
- User user;
- user.name = toLower(name);
- if (isAlreadyLoggedIn(user.name)) {
- user.modes.push_back(MODE_O);
- user.modes.push_back(MODE_N);
- g_users->updateMode(&user, channel, "+o");
- updateChannels(channel);
- socket->write("JOIN " + channel);
- if (g_users->isFirstTimeJoin(channel))
- g_users->first_time_join(&user, channel);
- #ifdef _DEBUG
- else
- printf("Channel is already in use, not updating.\n");
- #endif
- }
- #ifdef _DEBUG
- else
- printf("User %s not logged in, not joining/updating.\n", toLower(name).c_str());
- #endif
- } else if(buffer.find("NOTICE") != std::string::npos) {
- printf("%s[%s]: %s\n", t.c_str(), name.c_str(), msg.c_str());
- } else if(buffer.find("JOIN") != std::string::npos) {
- printf("%s %s[%s] has joined %s\n", t.c_str(), name.c_str(), mask.c_str(), channel.c_str());
- script->onJoin(name, channel, mask);
- for (ChannelsUsers::const_iterator it = channel_users.begin(); it != channel_users.end(); ++it) {
- UsersMap __users = it->second;
- for (UsersMap::const_iterator _user = __users.begin(); _user != __users.end(); ++_user) {
- if (toLower(_user->second) == toLower(name) && it->first == channel) {
- User *p = new User;
- p->name = toLower(_user->second);
- if (isAlreadyLoggedIn(p->name)) {
- if (g_users->isOp(p))
- g_users->updateMode(p, channel, "+o");
- else if (g_users->isVoice(p))
- g_users->updateMode(p, channel, "+v");
- #ifdef _DEBUG
- else
- printf("User %s has no flags, not updating.\n", toLower(_user->second).c_str());
- #endif
- }
- #ifdef _DEBUG
- else
- printf("User %s is not logged in, not updating.\n", toLower(name).c_str());
- #endif
- }
- #ifdef _DEBUG
- else
- printf("User %s not found.\n", toLower(name).c_str());
- #endif
- }
- }
- HostsMap::iterator it = hosts.find(name);
- if (it == hosts.end())
- hosts.insert(std::make_pair(name, host));
- else { //maybe new host
- hosts.erase(it);
- hosts.insert(std::make_pair(name, host));
- }
- users.insert(std::make_pair(name, ""));
- events.insert(std::make_pair(time(NULL) + 900000 / 1000, name));
- } else if(buffer.find("PART") != std::string::npos) {
- printf("%s %s[%s] has left %s\n", t.c_str(), name.c_str(), mask.c_str(), channel.c_str());
- script->onPart(name, channel, mask);
- } else if(buffer.find("QUIT") != std::string::npos) {
- printf("%s %s[%s] has quit (%s)\n", t.c_str(), name.c_str(), mask.c_str(), msg.c_str());
- script->onQuit(name, mask, msg);
- onLogout(name);
- HostsMap::iterator it = hosts.find(name);
- if (it != hosts.end())
- hosts.erase(it);
- } else if(buffer.find("NICK") != std::string::npos) {
- printf("%s %s is now known as %s\n", t.c_str(), name.c_str(), msg.c_str());
- HostsMap::iterator it = hosts.find(name);
- if (it != hosts.end()) {
- //for sure hes stored.
- hosts.erase(it);
- hosts.insert(std::make_pair(msg, host));
- }
- } else if(buffer.find("TOPIC") != std::string::npos) {
- printf("Topic for %s: %s\n", channel.c_str(), msg.c_str());
- } else if(buffer.find("KICK") != std::string::npos) {
- printf("%s %s was kicked by %s\n", t.c_str(), kicked.c_str(), name.c_str());
- HostsMap::iterator it = hosts.find(kicked);
- if (it != hosts.end())
- hosts.erase(it);
- } else if(buffer.find("353") != std::string::npos) {
- Users = splitString(msg, " ");
- printf("Users for %s: ", channel.c_str());
- for (StringVec::iterator it = Users.begin(); it != Users.end(); ++it) {
- std::string _tmp = (*it).substr(0, 1);
- if (_tmp == "+" || _tmp == "@" || _tmp == "&" || _tmp == "%")
- users[(*it).substr(1, (*it).length()-1)] = _tmp;
- else
- users[*it] = "";
- printf("%s ", (*it).c_str());
- events.insert(std::make_pair(time(NULL) + 900000 / 1000, *it));
- }
- printf("\n");
- channel_users.insert(std::make_pair(channel, users));
- } else if (buffer.find("ERROR :Closing Link:") != std::string::npos) {
- #ifdef _WIN32
- system("TibiaBot.exe");
- #else
- system("./TibiaBot");
- #endif
- std::exit(1);
- } else {
- if(!buffer.empty())
- printf("%s\n", buffer.c_str());
- }
- for (EventsMap::iterator it = events.begin(); it != events.end(); ++it) {
- if (time(NULL) >= it->first) {
- if (it->second.find("+") != std::string::npos)
- it->second.erase(it->second.find("+"));
- else if (it->second.find("@") != std::string::npos)
- it->second.erase(it->second.find("@"));
- socket->write("MODE " + channel + " +v " + it->second);
- events.erase(it);
- break;
- }
- }
- StringVec params = splitString(msg, " ");
- if (params[0] == "!record") {
- char rec[256];
- if (g_records->getRecord(rec, params[1])) {
- socket->write("PRIVMSG " + channel + " :" + std::string(rec));
- } else {
- socket->write("PRIVMSG " + channel + " :Unable to get record for " + params[1] + ".");
- }
- }
- if (host == std::string(BOT_OWNER)) {
- if (params[0] == "!quit") {
- for (ChannelsUsers::const_iterator _cu = channel_users.begin(); _cu != channel_users.end(); ++_cu) {
- UsersMap _um = _cu->second;
- for (UsersMap::iterator _user = _um.begin(); _user != _um.end(); ++_user)
- onLogout(_user->second);
- }
- socket->write("QUIT :" + (params.size() == 1 ? "Requested" : params[1]));
- exit(1);
- } else if (params[0] == "!clear") {
- for (_UsersMap::iterator it = g_users->begin(); it != g_users->end(); it++)
- delete it->second;
- g_users->clear();
- }
- }
- if(authed && isPm) {
- if (toLower(params[0]) == "hello") {
- if (params.size() == 1) {
- privateMessage(socket, channel, "You must enter a good username.");
- continue;
- } else if (params.size() == 2) {
- privateMessage(socket, channel, "You must enter a good password.");
- continue;
- }
- std::string authName = params[1], password = params[2];
- if (isExist(authName)) {
- privateMessage(socket, channel, "Username already exists.");
- continue;
- }
- Auth *newAuth = new Auth(toLower(authName), toLower(password), toLower(host));
- if (newAuth->saveAuth()) {
- privateMessage(socket, channel, "Your auth has been saved.");
- AuthPtr __p(newAuth);
- __auths.insert(std::make_pair(toLower(authName), __p));
- } else {
- privateMessage(socket, channel, "Could not save your auth.");
- continue;
- }
- std::string c = "auths/" + authName + ".log";
- FILE *fp = fopen(c.c_str(), "a+");
- if (fp) {
- fprintf(fp, "%s is now authed as %s with password %s\n", name.c_str(), authName.c_str(), password.c_str());
- fclose(fp);
- }
- } else if (toLower(params[0]) == "topic") {
- if (params.size() == 1) {
- privateMessage(socket, channel, "Please specifiy a channel.");
- continue;
- } else if (params.size() == 2) {
- privateMessage(socket, channel, "Invalid params.");
- continue;
- } else if (!isAlreadyLoggedIn(name)) {
- privateMessage(socket, channel, "You are not logged in.");
- continue;
- }
- std::string out;
- for (int i = 2; i < params.size(); i++)
- out += params[i];
- socket->write("TOPIC " + params[1] + " " + out);
- } else if (toLower(params[0]) == "chanlev") {
- if (!isAlreadyLoggedIn(name)) {
- privateMessage(socket, channel, "You are not authed on me.");
- continue;
- }
- if (params.size() == 1) {
- privateMessage(socket, channel, "You must specifiy a channel.");
- continue;
- } else if (params.size() == 2) {
- privateMessage(socket, channel, "You must specifiy a name.");
- continue;
- } else if (params.size() == 3) {
- privateMessage(socket, channel, "You must specifiy a mode.");
- continue;
- }
- if (!hasOp(name, params[1])) {
- privateMessage(socket, channel, params[1] + ": Insufficient privileges");
- continue;
- }
- if (!isAlreadyLoggedIn(params[2])) {
- privateMessage(socket, channel, "User " + params[2] + " is not authed.");
- continue;
- }
- g_users->updateMode(params[2], params[1], params[3]);
- g_users->insertUserMode(params[2], params[3], params[1]);
- privateMessage(socket, channel, "Done. Flags for " + params[2] + " are now: " + params[3] + ".");
- } else if (toLower(params[0]) == "auth") {
- if (params.size() == 1) {
- privateMessage(socket, channel, "Please enter your username.");
- continue;
- } else if (params.size() == 2) {
- privateMessage(socket, channel, "Please enter your password.");
- continue;
- }
- std::string authName = toLower(params[1]), password = toLower(params[2]);
- if (!checkAuth(authName, password)) {
- privateMessage(socket, channel, "Check your username and password.");
- } else if (isAlreadyLoggedIn(authName)) {
- privateMessage(socket, channel, "You are already logged in.");
- } else if (!checkHost(authName, host)) {
- privateMessage(socket, channel, "You are not " + authName + ".");
- } else {
- privateMessage(socket, channel, "You are now logged in as: " + authName + ".");
- onLogin(authName);
- }
- std::string log = "auths/" + authName + ".log";
- FILE *fp = fopen(log.c_str(), "a+");
- if (fp) {
- 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());
- fclose(fp);
- }
- }
- } else if (authed) {
- _user* p = NULL;
- for (AuthedMap::const_iterator it = auths.begin(); it != auths.end(); ++it) {
- if (toLower(it->first) == toLower(host))
- p = it->second;
- }
- if (p && p->group > 5) {
- if (params[0] == "+bk") {
- if (params.size() < 1)
- continue;
- socket->write("KICK " + params[1] + " :Banned.");
- for (HostsMap::const_iterator it = hosts.begin(); it != hosts.end(); ++it) {
- if (toLower(it->first) == toLower(params[1]))
- socket->write("MODE " + channel + " +b *!*" + it->second);
- }
- }
- }
- }
- }
- delete socket;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement