145 lines
3.7 KiB
C++
145 lines
3.7 KiB
C++
#include "sparrowbot.h"
|
|
#include <iostream>
|
|
#include <stdio.h>
|
|
#include <QStringList>
|
|
|
|
using namespace std;
|
|
|
|
void SparrowBot::receiveMsg(QString str)
|
|
{
|
|
Message msg = Message(str, &users);
|
|
|
|
switch(status)
|
|
{
|
|
case OFFLINE :
|
|
status = ONLINE;
|
|
emit sendMsg(QString("NICK %1\r\nUSER %2 0 * %3").arg(nick).arg(nick).arg(nick));
|
|
break;
|
|
case ONLINE :
|
|
if(str.contains("PING"))
|
|
{
|
|
status = ON_CHAN;
|
|
emit sendMsg(QString("PONG :%1").arg(msg.nick));
|
|
emit sendMsg(QString("JOIN #%1").arg(chan));
|
|
}
|
|
break;
|
|
case ON_CHAN :
|
|
if(str.contains("PING"))
|
|
emit sendMsg(QString("PONG :%1").arg(msg.nick));
|
|
handleMessage(msg);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void SparrowBot::handleMessage(Message msg)
|
|
{
|
|
// si la ligne est un message
|
|
if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) == 0)
|
|
{
|
|
// mise à jour du bot
|
|
if(msg.args.compare("!update") == 0 && msg.src != NULL && msg.src->isOp())
|
|
disconnect("Rebooting for update...");
|
|
|
|
// affiche les gens connectés
|
|
if(msg.args.compare("!list") == 0)
|
|
{
|
|
QString str = "Online users : ";
|
|
for(User* u : users)
|
|
{
|
|
if(u->isOp())
|
|
str.append("@");
|
|
str.append(u->getNick());
|
|
str.append(" ");
|
|
}
|
|
say(str);
|
|
}
|
|
|
|
if(msg.args.compare("plop ?") == 0)
|
|
action("est content");
|
|
|
|
if(msg.args.compare("!attack") == 0)
|
|
{
|
|
joinGame();
|
|
action(QString(" hit Lendemor"));
|
|
}
|
|
}
|
|
|
|
// renommage
|
|
else if(msg.command.compare("NICK", Qt::CaseInsensitive) == 0)
|
|
msg.src->rename(msg.args);
|
|
else if(msg.command.compare("QUIT", Qt::CaseInsensitive) == 0)
|
|
users.remove(msg.src);
|
|
else if(msg.command.compare("PART", Qt::CaseInsensitive) == 0)
|
|
users.remove(msg.src);
|
|
else if(msg.command.compare("KICK", Qt::CaseInsensitive) == 0)
|
|
users.remove(users.getFromNick(msg.target2));
|
|
else if(msg.command.compare("JOIN", Qt::CaseInsensitive) == 0
|
|
&& msg.args.contains(chan))
|
|
{
|
|
users.getOrAdd(msg.nick);
|
|
whois(msg.nick);
|
|
}
|
|
|
|
// 353 = code de la ligne qui liste les nicks connectés
|
|
// on les parse et on effectue un WHOIS sur chaque nick
|
|
else if(msg.command.compare(QString("353"), Qt::CaseInsensitive) == 0)
|
|
{
|
|
// names
|
|
QStringList names = msg.args.split(' ');
|
|
for(QString s : names)
|
|
{
|
|
users.getOrAdd(s);
|
|
whois(s);
|
|
}
|
|
}
|
|
|
|
// 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::whois(QString nick)
|
|
{
|
|
emit sendMsg(QString("WHOIS %1").arg(nick));
|
|
}
|
|
|
|
void SparrowBot::say(QString str)
|
|
{
|
|
emit sendMsg(QString("PRIVMSG #%1 :%2").arg(chan).arg(str));
|
|
}
|
|
|
|
void SparrowBot::action(QString str)
|
|
{
|
|
emit sendMsg(QString("PRIVMSG #%1 :\001ACTION %2\001").arg(chan).arg(str));
|
|
}
|
|
|
|
void SparrowBot::disconnect()
|
|
{
|
|
disconnect("I'll be back");
|
|
}
|
|
|
|
void SparrowBot::disconnect(QString str)
|
|
{
|
|
emit sendMsg(QString("QUIT :%1").arg(str));
|
|
}
|
|
|
|
void SparrowBot::joinGame()
|
|
{
|
|
if(!isInGame)
|
|
{
|
|
emit sendMsg(QString("PRIVMSG SparrowMJ :!join patate"));
|
|
isInGame = true;
|
|
}
|
|
}
|