#include "sparrowbot.h" #include #include #include using namespace std; void SparrowBot::receiveMsg(QString msg) { string str; if(msg.contains("PING")) { str = "PONG\r\n"; emit sendMsg(QString(str.c_str())); } switch(status) { case OFFLINE : status = ONLINE; sendRawMessage("NICK " + nick + "\r\nUSER " + nick + " 0 * " + nick + "\r\n"); break; case ONLINE : status = ON_CHAN; sendRawMessage("JOIN #" + chan + "\r\n"); break; case ON_CHAN : handleMessage(Message(msg, users)); 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()) { sendRawMessage("QUIT :i'll be back\r\n"); exit(0); } // affiche les gens connectés if(msg.args.compare("!list") == 0) { string str = "Online users : "; 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); } if(msg.args.compare("plop ?") == 0) action("est content"); } // renommage else if(msg.command.compare(QString("NICK"), Qt::CaseInsensitive) == 0) msg.src->rename(msg.args); // 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); sendRawMessage("WHOIS "+u->getNick().toStdString()+"\r\n"); } } } void SparrowBot::say(string str) { sendRawMessage("PRIVMSG #" + chan + " :" + str + "\r\n"); } void SparrowBot::action(string str) { sendRawMessage("PRIVMSG #" + chan + " :ACTION " + str + "\r\n"); } void SparrowBot::sendRawMessage(string str) { emit sendMsg(QString(str.c_str())); }