132 lines
3.4 KiB
C++
132 lines
3.4 KiB
C++
#include "basemodule.h"
|
|
#include "message.h"
|
|
#include <QStringList>
|
|
|
|
BaseModule::BaseModule() : Module(), status(OFFLINE)
|
|
{
|
|
|
|
}
|
|
|
|
bool BaseModule::messageHandler(Message msg)
|
|
{
|
|
bool ret = false;
|
|
answer = "";
|
|
if(msg.command.compare("PING", Qt::CaseInsensitive) == 0)
|
|
{
|
|
answer = pong(msg.target);
|
|
ret = true;
|
|
}
|
|
switch(status)
|
|
{
|
|
case OFFLINE :
|
|
status = ONLINE;
|
|
answer = QString("NICK %1\r\nUSER %2 0 * %3\r\n").arg(getNick()).arg(getNick()).arg(getNick());
|
|
ret = true;
|
|
break;
|
|
case ONLINE :
|
|
if(msg.command.compare("376") == 0)
|
|
{
|
|
status = ON_CHAN;
|
|
answer += join(getChan());
|
|
ret = true;
|
|
}
|
|
break;
|
|
case ON_CHAN :
|
|
ret |= onChanHandler(msg);
|
|
break;
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
bool BaseModule::onChanHandler(Message msg)
|
|
{
|
|
bool ret = false;
|
|
User* src = users.getFromNick(msg.nick);
|
|
|
|
// 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 && src != NULL && src->isOp())
|
|
{
|
|
answer += quit("Rebooting for update...");
|
|
ret = true;
|
|
}
|
|
|
|
// affiche les gens connectés
|
|
else 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(" ");
|
|
}
|
|
answer += say(str);
|
|
ret = true;
|
|
}
|
|
}
|
|
|
|
// renommage
|
|
else if(msg.command.compare("NICK", Qt::CaseInsensitive) == 0)
|
|
src->rename(msg.args);
|
|
else if(msg.command.compare("QUIT", Qt::CaseInsensitive) == 0)
|
|
users.remove(src);
|
|
else if(msg.command.compare("PART", Qt::CaseInsensitive) == 0)
|
|
users.remove(src);
|
|
else if(msg.command.compare("KICK", Qt::CaseInsensitive) == 0)
|
|
{
|
|
if(getNick().compare(msg.target2) == 0)
|
|
{
|
|
answer += quit("i've been kicked :(");
|
|
ret = true;
|
|
}
|
|
users.remove(users.getFromNick(msg.target2));
|
|
}
|
|
else if(msg.command.compare("JOIN", Qt::CaseInsensitive) == 0
|
|
&& msg.args.contains(getChan())
|
|
&& getNick().compare(msg.nick) != 0)
|
|
{
|
|
users.getOrAdd(msg.nick);
|
|
answer += whois(msg.nick);
|
|
ret = true;
|
|
}
|
|
|
|
// 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);
|
|
answer += whois(s);
|
|
ret = true;
|
|
}
|
|
}
|
|
|
|
// 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(getChan()))
|
|
{
|
|
u->setOp(s.startsWith('@'));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
QString BaseModule::getName()
|
|
{
|
|
return "base";
|
|
}
|