the bot should understand JOIN and QUIT
This commit is contained in:
parent
98c8a64d6a
commit
49ba5d82df
@ -12,7 +12,7 @@ Message::Message(QString str, UserList users)
|
|||||||
command = "PING";
|
command = "PING";
|
||||||
target = "PING";
|
target = "PING";
|
||||||
src = NULL;
|
src = NULL;
|
||||||
args = str;
|
args = str.split(':')[1];
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -24,13 +24,15 @@ Message::Message(QString str, UserList users)
|
|||||||
args = str.right(str.size()-(mid+1));
|
args = str.right(str.size()-(mid+1));
|
||||||
// the interesting infos before the separator are separated by spaces
|
// the interesting infos before the separator are separated by spaces
|
||||||
QStringList list = str.split(' ');
|
QStringList list = str.split(' ');
|
||||||
// source of the message
|
// source of the message (creates the user if he is new)
|
||||||
QString nick = list.takeFirst();
|
QString nick = list.takeFirst();
|
||||||
src = users.getFromNick(nick.left(nick.indexOf('!')));
|
src = users.getOrAdd(nick.left(nick.indexOf('!')));
|
||||||
// command issued by the source
|
// command issued by the source
|
||||||
command = list.takeFirst();
|
command = list.takeFirst();
|
||||||
// target of the command
|
// target of the command
|
||||||
target = list.takeFirst();
|
target = list.takeFirst();
|
||||||
|
if(!list.empty())
|
||||||
|
target2 = list.takeFirst();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ public:
|
|||||||
User* src;
|
User* src;
|
||||||
QString command;
|
QString command;
|
||||||
QString target;
|
QString target;
|
||||||
|
QString target2;
|
||||||
QString args;
|
QString args;
|
||||||
Message(QString str, UserList users);
|
Message(QString str, UserList users);
|
||||||
~Message();
|
~Message();
|
||||||
|
@ -5,26 +5,26 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void SparrowBot::receiveMsg(QString msg)
|
void SparrowBot::receiveMsg(QString str)
|
||||||
{
|
{
|
||||||
string str;
|
Message msg = Message(str, users);
|
||||||
if(msg.contains("PING"))
|
|
||||||
{
|
|
||||||
str = "PONG\r\n";
|
|
||||||
emit sendMsg(QString(str.c_str()));
|
|
||||||
}
|
|
||||||
switch(status)
|
switch(status)
|
||||||
{
|
{
|
||||||
case OFFLINE :
|
case OFFLINE :
|
||||||
status = ONLINE;
|
status = ONLINE;
|
||||||
sendRawMessage("NICK " + nick + "\r\nUSER " + nick + " 0 * " + nick + "\r\n");
|
sendMsg(QString("NICK %1\r\nUSER %2 0 * %3\r\n").arg(nick).arg(nick).arg(nick));
|
||||||
break;
|
break;
|
||||||
case ONLINE :
|
case ONLINE :
|
||||||
status = ON_CHAN;
|
if(str.contains("PING"))
|
||||||
sendRawMessage("JOIN #" + chan + "\r\n");
|
{
|
||||||
|
status = ON_CHAN;
|
||||||
|
sendMsg(QString("PONG :%1\r\n").arg(msg.args));
|
||||||
|
sendMsg("JOIN #" + chan + "\r\n");
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case ON_CHAN :
|
case ON_CHAN :
|
||||||
handleMessage(Message(msg, users));
|
handleMessage(msg);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -52,20 +52,20 @@ void SparrowBot::handleMessage(Message msg)
|
|||||||
// mise à jour du bot
|
// mise à jour du bot
|
||||||
if(msg.args.compare("!update") == 0 && msg.src != NULL && msg.src->isOp())
|
if(msg.args.compare("!update") == 0 && msg.src != NULL && msg.src->isOp())
|
||||||
{
|
{
|
||||||
sendRawMessage("QUIT :i'll be back\r\n");
|
emit sendMsg("QUIT :i'll be back\r\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
// affiche les gens connectés
|
// affiche les gens connectés
|
||||||
if(msg.args.compare("!list") == 0)
|
if(msg.args.compare("!list") == 0)
|
||||||
{
|
{
|
||||||
string str = "Online users : ";
|
QString str = "Online users : ";
|
||||||
for(User* u : users)
|
|
||||||
str += u->getNick().toStdString() + " ";
|
|
||||||
say(str);
|
|
||||||
str = "Online ops : ";
|
|
||||||
for(User* u : users)
|
for(User* u : users)
|
||||||
|
{
|
||||||
if(u->isOp())
|
if(u->isOp())
|
||||||
str += u->getNick().toStdString() + " ";
|
str.append("@");
|
||||||
|
str.append(u->getNick());
|
||||||
|
str.append(" ");
|
||||||
|
}
|
||||||
say(str);
|
say(str);
|
||||||
}
|
}
|
||||||
if(msg.args.compare("plop ?") == 0)
|
if(msg.args.compare("plop ?") == 0)
|
||||||
@ -73,8 +73,13 @@ void SparrowBot::handleMessage(Message msg)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// renommage
|
// renommage
|
||||||
else if(msg.command.compare(QString("NICK"), Qt::CaseInsensitive) == 0)
|
else if(msg.command.compare("NICK", Qt::CaseInsensitive) == 0)
|
||||||
msg.src->rename(msg.args);
|
msg.src->rename(msg.args);
|
||||||
|
else if(msg.command.compare("QUIT", Qt::CaseInsensitive) == 0)
|
||||||
|
users.remove(msg.src);
|
||||||
|
else if(msg.command.compare("JOIN", Qt::CaseInsensitive) == 0
|
||||||
|
&& msg.args.contains(chan))
|
||||||
|
emit sendMsg(QString("WHOIS %1\r\n").arg(msg.src->getNick()));
|
||||||
|
|
||||||
// 353 = code de la ligne qui liste les nicks connectés
|
// 353 = code de la ligne qui liste les nicks connectés
|
||||||
// on les parse et on effectue un WHOIS sur chaque nick
|
// on les parse et on effectue un WHOIS sur chaque nick
|
||||||
@ -86,22 +91,33 @@ void SparrowBot::handleMessage(Message msg)
|
|||||||
{
|
{
|
||||||
User* u = new User(s);
|
User* u = new User(s);
|
||||||
users.push_back(u);
|
users.push_back(u);
|
||||||
sendRawMessage("WHOIS "+u->getNick().toStdString()+"\r\n");
|
emit sendMsg(QString("WHOIS %1\r\n").arg(u->getNick()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 319 = code de la ligne de réponse au whois qui indique les chans d'un user, et son statut dessus
|
||||||
|
else if(msg.command.compare(QString("319"), Qt::CaseInsensitive) == 0)
|
||||||
|
{
|
||||||
|
User* u = users.getOrAdd(msg.target2);
|
||||||
|
QStringList chanList = msg.args.split(' ');
|
||||||
|
for(QString s : chanList)
|
||||||
|
{
|
||||||
|
if(s.endsWith(chan))
|
||||||
|
{
|
||||||
|
u->setOp(s.startsWith('@'));
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SparrowBot::say(string str)
|
void SparrowBot::say(QString str)
|
||||||
{
|
{
|
||||||
sendRawMessage("PRIVMSG #" + chan + " :" + str + "\r\n");
|
emit sendMsg(QString("PRIVMSG #%1 :%2\r\n").arg(chan).arg(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SparrowBot::action(string str)
|
void SparrowBot::action(QString str)
|
||||||
{
|
{
|
||||||
sendRawMessage("PRIVMSG #" + chan + " :ACTION " + str + "\r\n");
|
emit sendMsg(QString("PRIVMSG #%1 :ACTION %2\r\n").arg(chan).arg(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SparrowBot::sendRawMessage(string str)
|
|
||||||
{
|
|
||||||
emit sendMsg(QString(str.c_str()));
|
|
||||||
}
|
|
||||||
|
11
sparrowbot.h
11
sparrowbot.h
@ -11,20 +11,19 @@ class SparrowBot : public QObject
|
|||||||
|
|
||||||
enum{OFFLINE, ONLINE, ON_CHAN};
|
enum{OFFLINE, ONLINE, ON_CHAN};
|
||||||
|
|
||||||
std::string nick;
|
QString nick;
|
||||||
std::string chan;
|
QString chan;
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
UserList users;
|
UserList users;
|
||||||
public:
|
public:
|
||||||
SparrowBot() : nick("SparrowBot"), chan("epicsparrow"), status(OFFLINE) {}
|
SparrowBot() : nick("SparrowBot"), chan("epicsparrow"), status(OFFLINE) {}
|
||||||
SparrowBot(std::string nick_, std::string chan_) : nick(nick_), chan(chan_), status(OFFLINE) {}
|
SparrowBot(QString nick_, QString chan_) : nick(nick_), chan(chan_), status(OFFLINE) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void handleMessage(Message msg);
|
void handleMessage(Message msg);
|
||||||
void say(std::string str);
|
void say(QString str);
|
||||||
void action(std::string str);
|
void action(QString str);
|
||||||
void sendRawMessage(std::string str);
|
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void receiveMsg(QString msg);
|
void receiveMsg(QString msg);
|
||||||
|
19
user.cpp
19
user.cpp
@ -23,6 +23,19 @@ bool User::isOp()
|
|||||||
return op;
|
return op;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void User::setOp(bool newStatus)
|
||||||
|
{
|
||||||
|
op = newStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
User* UserList::getOrAdd(QString nick)
|
||||||
|
{
|
||||||
|
User* u = getFromNick(nick);
|
||||||
|
if(u == NULL)
|
||||||
|
u = new User(nick);
|
||||||
|
return u;
|
||||||
|
}
|
||||||
|
|
||||||
User* UserList::getFromNick(QString nick)
|
User* UserList::getFromNick(QString nick)
|
||||||
{
|
{
|
||||||
for(User* u : *this)
|
for(User* u : *this)
|
||||||
@ -30,3 +43,9 @@ User* UserList::getFromNick(QString nick)
|
|||||||
return u;
|
return u;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UserList::remove(User* user)
|
||||||
|
{
|
||||||
|
erase(std::remove(begin(), end(), user), end());
|
||||||
|
delete(user);
|
||||||
|
}
|
||||||
|
3
user.h
3
user.h
@ -14,12 +14,15 @@ public:
|
|||||||
void rename(QString newNick);
|
void rename(QString newNick);
|
||||||
QString getNick();
|
QString getNick();
|
||||||
bool isOp();
|
bool isOp();
|
||||||
|
void setOp(bool newStatus);
|
||||||
};
|
};
|
||||||
|
|
||||||
class UserList : public std::vector<User*>
|
class UserList : public std::vector<User*>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
User* getOrAdd(QString nick);
|
||||||
User* getFromNick(QString nick);
|
User* getFromNick(QString nick);
|
||||||
|
void remove(User* user);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // USER_H
|
#endif // USER_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user