added missing macros
[quassel.git] / src / core / netsplit.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "netsplit.h"
22
23 #include <QRegExp>
24
25 Netsplit::Netsplit()
26     : _quitMsg(""), _sentQuit(false)
27 {
28   _discardTimer.setSingleShot(true);
29   _joinTimer.setSingleShot(true);
30   _quitTimer.setSingleShot(true);
31
32   connect(&_discardTimer, SIGNAL(timeout()), this, SIGNAL(finished()));
33
34   connect(&_joinTimer, SIGNAL(timeout()), this, SLOT(joinTimeout()));
35   connect(&_quitTimer, SIGNAL(timeout()), this, SLOT(quitTimeout()));
36
37   // wait for a maximum of 1 hour until we discard the netsplit
38   _discardTimer.start(3600000);
39 }
40
41 void Netsplit::userQuit(const QString &sender, const QStringList &channels, const QString &msg)
42 {
43   if(_quitMsg.isEmpty())
44     _quitMsg = msg;
45   foreach(QString channel, channels) {
46     _quits[channel].append(sender);
47   }
48   // now let's wait 5s to finish the netsplit-quit
49   _quitTimer.start(5000);
50 }
51
52 bool Netsplit::userJoined(const QString &sender, const QString &channel) {
53   if(!_quits.contains(channel))
54     return false;
55
56   QStringList &users = _quits[channel];
57   int idx = users.indexOf(sender);
58   if(idx == -1)
59     return false;
60
61   _joins[channel].append(users.takeAt(idx));
62   if(users.empty())
63     _quits.remove(channel);
64
65   // now let's wait 5s to finish the netsplit-join
66   _joinTimer.start(5000);
67   return true;
68 }
69
70 bool Netsplit::isNetsplit(const QString &quitMessage)
71 {
72   // check if we find some common chars that disqualify the netsplit as such
73   if(quitMessage.contains(':') || quitMessage.contains('/'))
74     return false;
75
76   // now test if message consists only of two dns names as the RFC requests
77   // but also allow the commonly used "*.net *.split"
78   QRegExp hostRx("^(?:[\\w\\d-.]+|\\*)\\.[\\w\\d-]+\\s(?:[\\w\\d-.]+|\\*)\\.[\\w\\d-]+$");
79   if(hostRx.exactMatch(quitMessage))
80     return true;
81
82   return false;
83 }
84
85 void Netsplit::joinTimeout()
86 {
87   if(!_sentQuit) {
88     _quitTimer.stop();
89     quitTimeout();
90   }
91   QHash<QString, QStringList>::iterator it;
92   for(it = _joins.begin(); it != _joins.end(); ++it)
93     emit netsplitJoin(it.key(), it.value(),_quitMsg);
94   _joins.clear();
95   _discardTimer.stop();
96   emit finished();
97 }
98
99 void Netsplit::quitTimeout()
100 {
101   QHash<QString, QStringList>::iterator it;
102   for(it = _quits.begin(); it != _quits.end(); ++it)
103     emit netsplitQuit(it.key(), it.value(),_quitMsg);
104   _sentQuit = true;
105 }