fixed major bug

This commit is contained in:
unknown 2015-05-26 15:03:32 +02:00
parent 49ba5d82df
commit 2f6c2be025
5 changed files with 24 additions and 14 deletions

View File

@ -7,7 +7,7 @@ int main(int argc, char *argv[])
{ {
// set up the socket // set up the socket
SocketIRC sock; SocketIRC sock;
sock.setServer("irc.freenode.net"); sock.setServer("localhost");
sock.setPort(6667); sock.setPort(6667);
// set up the bot // set up the bot

View File

@ -5,14 +5,15 @@
using namespace std; using namespace std;
Message::Message(QString str, UserList users) Message::Message(QString str, UserList* users)
{ {
if(str.startsWith("PING")) if(str.startsWith("PING"))
{ {
command = "PING"; command = "PING";
target = "PING"; target = "PING";
src = NULL; src = NULL;
args = str.split(':')[1]; nick = str.split(':')[1];
args = nick;
} }
else else
{ {
@ -25,8 +26,9 @@ Message::Message(QString str, UserList users)
// the interesting infos before the separator are separated by spaces // the interesting infos before the separator are separated by spaces
QStringList list = str.split(' '); QStringList list = str.split(' ');
// source of the message (creates the user if he is new) // source of the message (creates the user if he is new)
QString nick = list.takeFirst(); QString temp = list.takeFirst();
src = users.getOrAdd(nick.left(nick.indexOf('!'))); nick = temp.left(temp.indexOf('!'));
src = users->getFromNick(nick);
// command issued by the source // command issued by the source
command = list.takeFirst(); command = list.takeFirst();
// target of the command // target of the command

View File

@ -10,11 +10,12 @@ class Message
{ {
public: public:
User* src; User* src;
QString nick;
QString command; QString command;
QString target; QString target;
QString target2; QString target2;
QString args; QString args;
Message(QString str, UserList users); Message(QString str, UserList* users);
~Message(); ~Message();
}; };

View File

@ -7,23 +7,25 @@ using namespace std;
void SparrowBot::receiveMsg(QString str) void SparrowBot::receiveMsg(QString str)
{ {
Message msg = Message(str, users); Message msg = Message(str, &users);
switch(status) switch(status)
{ {
case OFFLINE : case OFFLINE :
status = ONLINE; status = ONLINE;
sendMsg(QString("NICK %1\r\nUSER %2 0 * %3\r\n").arg(nick).arg(nick).arg(nick)); emit sendMsg(QString("NICK %1\r\nUSER %2 0 * %3\r\n").arg(nick).arg(nick).arg(nick));
break; break;
case ONLINE : case ONLINE :
if(str.contains("PING")) if(str.contains("PING"))
{ {
status = ON_CHAN; status = ON_CHAN;
sendMsg(QString("PONG :%1\r\n").arg(msg.args)); emit sendMsg(QString("PONG :%1\r\n").arg(msg.nick));
sendMsg("JOIN #" + chan + "\r\n"); emit sendMsg("JOIN #" + chan + "\r\n");
} }
break; break;
case ON_CHAN : case ON_CHAN :
if(str.contains("PING"))
emit sendMsg(QString("PONG :%1\r\n").arg(msg.nick));
handleMessage(msg); handleMessage(msg);
break; break;
} }
@ -79,7 +81,10 @@ void SparrowBot::handleMessage(Message msg)
users.remove(msg.src); users.remove(msg.src);
else if(msg.command.compare("JOIN", Qt::CaseInsensitive) == 0 else if(msg.command.compare("JOIN", Qt::CaseInsensitive) == 0
&& msg.args.contains(chan)) && msg.args.contains(chan))
emit sendMsg(QString("WHOIS %1\r\n").arg(msg.src->getNick())); {
users.getOrAdd(msg.nick);
emit sendMsg(QString("WHOIS %1\r\n").arg(msg.nick));
}
// 353 = code de la ligne qui liste les nicks connectés // 353 = code de la ligne qui liste les nicks connectés
// on les parse et on effectue un WHOIS sur chaque nick // on les parse et on effectue un WHOIS sur chaque nick
@ -89,9 +94,8 @@ void SparrowBot::handleMessage(Message msg)
QStringList names = msg.args.split(' '); QStringList names = msg.args.split(' ');
for(QString s : names) for(QString s : names)
{ {
User* u = new User(s); users.getOrAdd(s);
users.push_back(u); emit sendMsg(QString("WHOIS %1\r\n").arg(s));
emit sendMsg(QString("WHOIS %1\r\n").arg(u->getNick()));
} }
} }

View File

@ -32,7 +32,10 @@ User* UserList::getOrAdd(QString nick)
{ {
User* u = getFromNick(nick); User* u = getFromNick(nick);
if(u == NULL) if(u == NULL)
{
u = new User(nick); u = new User(nick);
push_back(u);
}
return u; return u;
} }