the bot should understand JOIN and QUIT
This commit is contained in:
		
							parent
							
								
									98c8a64d6a
								
							
						
					
					
						commit
						49ba5d82df
					
				@ -12,7 +12,7 @@ Message::Message(QString str, UserList users)
 | 
			
		||||
        command = "PING";
 | 
			
		||||
        target = "PING";
 | 
			
		||||
        src = NULL;
 | 
			
		||||
        args = str;
 | 
			
		||||
        args = str.split(':')[1];
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
@ -24,13 +24,15 @@ Message::Message(QString str, UserList users)
 | 
			
		||||
        args = str.right(str.size()-(mid+1));
 | 
			
		||||
        // the interesting infos before the separator are separated by spaces
 | 
			
		||||
        QStringList list = str.split(' ');
 | 
			
		||||
        // source of the message
 | 
			
		||||
        // source of the message (creates the user if he is new)
 | 
			
		||||
        QString nick = list.takeFirst();
 | 
			
		||||
        src = users.getFromNick(nick.left(nick.indexOf('!')));
 | 
			
		||||
        src = users.getOrAdd(nick.left(nick.indexOf('!')));
 | 
			
		||||
        // command issued by the source
 | 
			
		||||
        command = list.takeFirst();
 | 
			
		||||
        // target of the command
 | 
			
		||||
        target = list.takeFirst();
 | 
			
		||||
        if(!list.empty())
 | 
			
		||||
            target2 = list.takeFirst();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -12,6 +12,7 @@ public:
 | 
			
		||||
    User* src;
 | 
			
		||||
    QString command;
 | 
			
		||||
    QString target;
 | 
			
		||||
    QString target2;
 | 
			
		||||
    QString args;
 | 
			
		||||
    Message(QString str, UserList users);
 | 
			
		||||
    ~Message();
 | 
			
		||||
 | 
			
		||||
@ -5,26 +5,26 @@
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
 | 
			
		||||
void SparrowBot::receiveMsg(QString msg)
 | 
			
		||||
void SparrowBot::receiveMsg(QString str)
 | 
			
		||||
{
 | 
			
		||||
    string str;
 | 
			
		||||
    if(msg.contains("PING"))
 | 
			
		||||
    {
 | 
			
		||||
        str = "PONG\r\n";
 | 
			
		||||
        emit sendMsg(QString(str.c_str()));
 | 
			
		||||
    }
 | 
			
		||||
    Message msg = Message(str, users);
 | 
			
		||||
 | 
			
		||||
    switch(status)
 | 
			
		||||
    {
 | 
			
		||||
    case OFFLINE :
 | 
			
		||||
        status = ONLINE;
 | 
			
		||||
        sendRawMessage("NICK " + nick + "\r\nUSER " + nick + " 0 * " + nick + "\r\n");
 | 
			
		||||
        sendMsg(QString("NICK %1\r\nUSER %2 0 * %3\r\n").arg(nick).arg(nick).arg(nick));
 | 
			
		||||
        break;
 | 
			
		||||
    case ONLINE :
 | 
			
		||||
        status = ON_CHAN;
 | 
			
		||||
        sendRawMessage("JOIN #" + chan + "\r\n");
 | 
			
		||||
        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(Message(msg, users));
 | 
			
		||||
        handleMessage(msg);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -52,20 +52,20 @@ void SparrowBot::handleMessage(Message msg)
 | 
			
		||||
        // mise à jour du bot
 | 
			
		||||
        if(msg.args.compare("!update") == 0 && msg.src != NULL && msg.src->isOp())
 | 
			
		||||
        {
 | 
			
		||||
            sendRawMessage("QUIT :i'll be back\r\n");
 | 
			
		||||
            emit sendMsg("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 : ";
 | 
			
		||||
            QString str = "Online users : ";
 | 
			
		||||
            for(User* u : users)
 | 
			
		||||
            {
 | 
			
		||||
                if(u->isOp())
 | 
			
		||||
                    str += u->getNick().toStdString() + " ";
 | 
			
		||||
                    str.append("@");
 | 
			
		||||
                str.append(u->getNick());
 | 
			
		||||
                str.append(" ");
 | 
			
		||||
            }
 | 
			
		||||
            say(str);
 | 
			
		||||
        }
 | 
			
		||||
        if(msg.args.compare("plop ?") == 0)
 | 
			
		||||
@ -73,8 +73,13 @@ void SparrowBot::handleMessage(Message msg)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // renommage
 | 
			
		||||
    else if(msg.command.compare(QString("NICK"), Qt::CaseInsensitive) == 0)
 | 
			
		||||
    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
 | 
			
		||||
@ -86,22 +91,33 @@ void SparrowBot::handleMessage(Message msg)
 | 
			
		||||
        {
 | 
			
		||||
            User* u = new User(s);
 | 
			
		||||
            users.push_back(u);
 | 
			
		||||
            sendRawMessage("WHOIS "+u->getNick().toStdString()+"\r\n");
 | 
			
		||||
            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(string str)
 | 
			
		||||
void SparrowBot::say(QString str)
 | 
			
		||||
{
 | 
			
		||||
    sendRawMessage("PRIVMSG #" + chan + " :" + str + "\r\n");
 | 
			
		||||
    emit sendMsg(QString("PRIVMSG #%1 :%2\r\n").arg(chan).arg(str));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SparrowBot::action(string str)
 | 
			
		||||
void SparrowBot::action(QString str)
 | 
			
		||||
{
 | 
			
		||||
    sendRawMessage("PRIVMSG #" + chan + " :ACTION " + str + "\r\n");
 | 
			
		||||
    emit sendMsg(QString("PRIVMSG #%1 :ACTION %2\r\n").arg(chan).arg(str));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SparrowBot::sendRawMessage(string str)
 | 
			
		||||
{
 | 
			
		||||
    emit sendMsg(QString(str.c_str()));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										11
									
								
								sparrowbot.h
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								sparrowbot.h
									
									
									
									
									
								
							@ -11,20 +11,19 @@ class SparrowBot : public QObject
 | 
			
		||||
 | 
			
		||||
    enum{OFFLINE, ONLINE, ON_CHAN};
 | 
			
		||||
 | 
			
		||||
    std::string nick;
 | 
			
		||||
    std::string chan;
 | 
			
		||||
    QString nick;
 | 
			
		||||
    QString chan;
 | 
			
		||||
    int status;
 | 
			
		||||
 | 
			
		||||
    UserList users;
 | 
			
		||||
public:
 | 
			
		||||
    SparrowBot() : nick("SparrowBot"), chan("epicsparrow"), status(OFFLINE) {}
 | 
			
		||||
    SparrowBot(std::string nick_, std::string chan_) : nick(nick_), chan(chan_), status(OFFLINE) {}
 | 
			
		||||
    SparrowBot(QString nick_, QString chan_) : nick(nick_), chan(chan_), status(OFFLINE) {}
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    void handleMessage(Message msg);
 | 
			
		||||
    void say(std::string str);
 | 
			
		||||
    void action(std::string str);
 | 
			
		||||
    void sendRawMessage(std::string str);
 | 
			
		||||
    void say(QString str);
 | 
			
		||||
    void action(QString str);
 | 
			
		||||
 | 
			
		||||
public slots:
 | 
			
		||||
    void receiveMsg(QString msg);
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										19
									
								
								user.cpp
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								user.cpp
									
									
									
									
									
								
							@ -23,6 +23,19 @@ bool User::isOp()
 | 
			
		||||
    return op;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void User::setOp(bool newStatus)
 | 
			
		||||
{
 | 
			
		||||
    op = newStatus;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
User* UserList::getOrAdd(QString nick)
 | 
			
		||||
{
 | 
			
		||||
    User* u = getFromNick(nick);
 | 
			
		||||
    if(u == NULL)
 | 
			
		||||
        u = new User(nick);
 | 
			
		||||
    return u;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
User* UserList::getFromNick(QString nick)
 | 
			
		||||
{
 | 
			
		||||
    for(User* u : *this)
 | 
			
		||||
@ -30,3 +43,9 @@ User* UserList::getFromNick(QString nick)
 | 
			
		||||
            return u;
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void UserList::remove(User* user)
 | 
			
		||||
{
 | 
			
		||||
    erase(std::remove(begin(), end(), user), end());
 | 
			
		||||
    delete(user);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user