SparrowBot/sparrowbot.cpp
2015-05-21 15:26:18 +02:00

96 lines
2.1 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));
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;
}
}
void SparrowBot::handleMessage(Message msg)
{
//char* str = msg.args.toStdString().c_str();
if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) == 0)
{
// message
if(msg.args.compare("!destroy") == 0)
{
sendRawMessage("QUIT :i'll be back\r\n");
status = OFFLINE;
emit changeSocketStatus(false);
}
if(msg.args.compare("!list") == 0)
{
string str = "Online users : ";
for(User u : users)
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 = 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()));
}