SparrowBot/sparrowbot.cpp
2015-05-26 14:20:03 +02:00

124 lines
3.3 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
sendMsg(QString("NICK %1\r\nUSER %2 0 * %3\r\n").arg(nick).arg(nick).arg(nick));
break;
case ONLINE :
if(str.contains("PING"))
{
status = ON_CHAN;
sendMsg(QString("PONG :%1\r\n").arg(msg.args));
sendMsg("JOIN #" + chan + "\r\n");
}
break;
case ON_CHAN :
handleMessage(msg);
break;
}
}
void SparrowBot::forceStatus(int newStatus)
{
emit changeSocketStatus(newStatus);
if(newStatus)
{
//status = ONLINE;
//sendRawMessage("NICK " + nick + "\r\nUSER " + nick + " 0 * " + nick + "\r\n");
}
else
{
status = OFFLINE;
users.clear();
}
}
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())
{
emit sendMsg("QUIT :i'll be back\r\n");
exit(0);
}
// 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");
}
// 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("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
// 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)
{
User* u = new User(s);
users.push_back(u);
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(QString str)
{
emit sendMsg(QString("PRIVMSG #%1 :%2\r\n").arg(chan).arg(str));
}
void SparrowBot::action(QString str)
{
emit sendMsg(QString("PRIVMSG #%1 :ACTION %2\r\n").arg(chan).arg(str));
}