SparrowBot/sparrowbot.cpp
2015-05-21 19:50:54 +02:00

100 lines
2.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 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)
{
if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) == 0)
{
// message
if(msg.args.compare("!update") == 0 && msg.src != NULL && msg.src->isOp())
{
sendRawMessage("QUIT :i'll be back\r\n");
exit(0);
}
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.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()));
}