1 /***************************************************************************
2 * Copyright (C) 2005-2015 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
27 Netsplit::Netsplit(Network *network, QObject *parent)
29 _network(network), _quitMsg(""), _sentQuit(false), _joinCounter(0), _quitCounter(0)
31 _discardTimer.setSingleShot(true);
32 _joinTimer.setSingleShot(true);
33 _quitTimer.setSingleShot(true);
35 connect(&_discardTimer, SIGNAL(timeout()), this, SIGNAL(finished()));
37 connect(&_joinTimer, SIGNAL(timeout()), this, SLOT(joinTimeout()));
38 connect(&_quitTimer, SIGNAL(timeout()), this, SLOT(quitTimeout()));
40 // wait for a maximum of 1 hour until we discard the netsplit
41 _discardTimer.start(3600000);
45 void Netsplit::userQuit(const QString &sender, const QStringList &channels, const QString &msg)
47 if (_quitMsg.isEmpty())
49 foreach(QString channel, channels) {
50 _quits[channel].append(sender);
53 // now let's wait 10s to finish the netsplit-quit
54 _quitTimer.start(10000);
58 bool Netsplit::userJoined(const QString &sender, const QString &channel)
60 if (!_quits.contains(channel))
63 QStringList &users = _quits[channel];
65 QStringList::iterator userIter;
66 const QString senderNick = nickFromMask(sender);
67 for (userIter = users.begin(); userIter != users.end(); ++userIter) {
68 if (nickFromMask(*userIter) == senderNick)
71 if (userIter == users.end())
74 _joins[channel].first.append(*userIter);
75 _joins[channel].second.append(QString());
77 users.erase(userIter);
80 _quits.remove(channel);
84 if (_quits.empty()) // all users joined already - no need to wait
86 else // wait 30s to finish the netsplit-join
87 _joinTimer.start(30000);
93 bool Netsplit::userAlreadyJoined(const QString &sender, const QString &channel)
95 if (_joins.value(channel).first.contains(sender))
101 void Netsplit::addMode(const QString &sender, const QString &channel, const QString &mode)
103 if (!_joins.contains(channel))
105 int idx = _joins.value(channel).first.indexOf(sender);
108 _joins[channel].second[idx].append(mode);
112 bool Netsplit::isNetsplit(const QString &quitMessage)
114 // check if we find some common chars that disqualify the netsplit as such
115 if (quitMessage.contains(':') || quitMessage.contains('/'))
118 // now test if message consists only of two dns names as the RFC requests
119 // but also allow the commonly used "*.net *.split"
120 QRegExp hostRx("^(?:[\\w\\d-.]+|\\*)\\.[\\w\\d-]+\\s(?:[\\w\\d-.]+|\\*)\\.[\\w\\d-]+$");
121 if (hostRx.exactMatch(quitMessage))
128 void Netsplit::joinTimeout()
135 QHash<QString, QPair<QStringList, QStringList> >::iterator it;
138 Try to catch server jumpers.
139 If we have too few joins for a netsplit-quit,
140 we assume that the users manually changed servers and join them
141 without ending the netsplit.
142 A netsplit is assumed over only if at least 1/3 of all quits had their corresponding
145 if (_joinCounter < _quitCounter/3) {
146 for (it = _joins.begin(); it != _joins.end(); ++it)
147 emit earlyJoin(network(), it.key(), it.value().first, it.value().second);
149 // we don't care about those anymore
152 // restart the timer with 5min timeout
153 // This might happen a few times if netsplit lasts longer.
154 // As soon as another user joins, the timer is set to a shorter timeout again.
155 _joinTimer.start(300000);
159 // send netsplitJoin for every recorded channel
160 for (it = _joins.begin(); it != _joins.end(); ++it)
161 emit netsplitJoin(network(), it.key(), it.value().first, it.value().second, _quitMsg);
163 _discardTimer.stop();
168 void Netsplit::quitTimeout()
170 // send netsplitQuit for every recorded channel
171 QHash<QString, QStringList>::iterator channelIter;
172 for (channelIter = _quits.begin(); channelIter != _quits.end(); ++channelIter) {
173 QStringList usersToSend;
175 foreach(QString user, channelIter.value()) {
176 if (!_quitsWithMessageSent.value(channelIter.key()).contains(user)) {
178 _quitsWithMessageSent[channelIter.key()].append(user);
181 // not yet sure how that could happen, but never send empty netsplit-quits
183 if (!usersToSend.isEmpty())
184 emit netsplitQuit(network(), channelIter.key(), usersToSend, _quitMsg);