From b86a0c18455f91404acc4b2432315f1e3f61bdf5 Mon Sep 17 00:00:00 2001 From: Anselme Date: Thu, 21 May 2015 19:50:54 +0200 Subject: [PATCH] the bot now recognises ops --- message.cpp | 7 ++++--- message.h | 6 ++++-- sparrowbot.cpp | 19 ++++++++++++------- sparrowbot.h | 3 +-- user.cpp | 16 ++++++++++++++-- user.h | 10 +++++++++- 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/message.cpp b/message.cpp index 773c08a..dc10e24 100644 --- a/message.cpp +++ b/message.cpp @@ -5,13 +5,13 @@ using namespace std; -Message::Message(QString str) +Message::Message(QString str, UserList users) { if(str.startsWith("PING")) { command = "PING"; target = "PING"; - src = str.right(str.size()-(str.indexOf(':')+1)); + src = NULL; args = str; } else @@ -25,7 +25,8 @@ Message::Message(QString str) // the interesting infos before the separator are separated by spaces QStringList list = str.split(' '); // source of the message - src = list.takeFirst(); + QString nick = list.takeFirst(); + src = users.getFromNick(nick.left(nick.indexOf('!'))); // command issued by the source command = list.takeFirst(); // target of the command diff --git a/message.h b/message.h index 032e5ca..d87c494 100644 --- a/message.h +++ b/message.h @@ -3,15 +3,17 @@ #include #include +#include +#include "user.h" class Message { public: - QString src; + User* src; QString command; QString target; QString args; - Message(QString str); + Message(QString str, UserList users); ~Message(); }; diff --git a/sparrowbot.cpp b/sparrowbot.cpp index 704c84b..fad1474 100644 --- a/sparrowbot.cpp +++ b/sparrowbot.cpp @@ -24,7 +24,7 @@ void SparrowBot::receiveMsg(QString msg) sendRawMessage("JOIN #" + chan + "\r\n"); break; case ON_CHAN : - handleMessage(Message(msg)); + handleMessage(Message(msg, users)); break; } } @@ -40,18 +40,18 @@ void SparrowBot::forceStatus(int newStatus) else { status = OFFLINE; + users.clear(); } } void SparrowBot::handleMessage(Message msg) { - //char* str = msg.args.toStdString().c_str(); if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) == 0) { // message - if(msg.args.compare("!update") == 0) + if(msg.args.compare("!update") == 0 && msg.src != NULL && msg.src->isOp()) { sendRawMessage("QUIT :i'll be back\r\n"); exit(0); @@ -59,8 +59,13 @@ void SparrowBot::handleMessage(Message msg) if(msg.args.compare("!list") == 0) { string str = "Online users : "; - for(User u : users) - str += u.getNick().toStdString() + " "; + for(User* u : users) + str += u->getNick().toStdString() + " "; + say(str); + str = "Online ops : "; + for(User* u : users) + if(u->isOp()) + str += u->getNick().toStdString() + " "; say(str); } } @@ -71,9 +76,9 @@ void SparrowBot::handleMessage(Message msg) QStringList names = msg.args.split(' '); for(QString s : names) { - User u = User(s); + User* u = new User(s); users.push_back(u); - sendRawMessage("WHOIS "+u.getNick().toStdString()+"\r\n"); + sendRawMessage("WHOIS "+u->getNick().toStdString()+"\r\n"); } } } diff --git a/sparrowbot.h b/sparrowbot.h index dfe37f7..c4d541a 100644 --- a/sparrowbot.h +++ b/sparrowbot.h @@ -4,7 +4,6 @@ #include "user.h" #include "message.h" #include -#include class SparrowBot : public QObject { @@ -16,7 +15,7 @@ class SparrowBot : public QObject std::string chan; int status; - std::vector users; + UserList users; public: SparrowBot() : nick("SparrowBot"), chan("epicsparrow"), status(OFFLINE) {} SparrowBot(std::string nick_, std::string chan_) : nick(nick_), chan(chan_), status(OFFLINE) {} diff --git a/user.cpp b/user.cpp index a2b51f2..a1bfa5f 100644 --- a/user.cpp +++ b/user.cpp @@ -2,8 +2,8 @@ User::User(QString str) { - isOp = (str.at(0) == '@'); - if(isOp) + op = (str.at(0) == '@'); + if(op) str = str.remove(0, 1); nick = str; } @@ -13,3 +13,15 @@ QString User::getNick() return nick; } +bool User::isOp() +{ + return op; +} + +User* UserList::getFromNick(QString nick) +{ + for(User* u : *this) + if(nick.compare(u->getNick()) == 0) + return u; + return NULL; +} diff --git a/user.h b/user.h index ef6f981..f13e487 100644 --- a/user.h +++ b/user.h @@ -2,15 +2,23 @@ #define USER_H #include +#include class User { QString nick; - bool isOp; + bool op; public: User(QString str); QString getNick(); + bool isOp(); +}; + +class UserList : public std::vector +{ +public: + User* getFromNick(QString nick); }; #endif // USER_H