Fixes #960 - "Core crashes after deleting a Network without disconnecting the Network...
[quassel.git] / src / core / netsplit.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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 #ifndef NETSPLIT_H
22 #define NETSPLIT_H
23
24 #include <QTimer>
25 #include <QHash>
26 #include <QPair>
27 #include <QStringList>
28
29 class Network;
30
31 class Netsplit : public QObject
32 {
33   Q_OBJECT
34 public:
35   Netsplit(Network *network, QObject *parent = 0);
36
37   inline Network *network() const { return _network; }
38
39   //! Add a user to the netsplit
40   /** Call this method if you noticed a netsplit.
41     * \note This method doesn't check if it really is a netsplit.
42     * \note Check with isNetsplit(const QString &quitMessage) before calling it!
43     *
44     * \param sender   The sender string of the quitting user
45     * \param channels The channels that user shared with us
46     * \param msg      The quit message
47     */
48   void userQuit(const QString &sender, const QStringList &channels, const QString &msg);
49
50   //! Remove a user from the netsplit
51   /** Call this method if a user joined after a netsplit occured.
52     *
53     * \param sender   The sender string of the joined user
54     * \param channel The channel that user shares with us
55     * \return true if user was found in the netsplit
56     */
57   bool userJoined(const QString &sender, const QString &channel);
58
59   //! Check if user has joined since netsplit
60   /** This method shows if a user has already joined after being hit by netsplit
61     * \note The method doesn't check if the user was recorded in the netsplit!
62     *
63     * \param sender   The sender string of the user
64     * \param channel  The channel the user shares with us
65     * \return true if user joined after a netsplit
66     */
67   bool userAlreadyJoined(const QString &sender, const QString &channel);
68
69   //! Add mode to user
70   /** Use this method to buffer userspecific channel modes until netsplitJoin is emitted.
71     *
72     * \param sender   The sender string of the user
73     * \param channel  The channel the user shares with us
74     * \return true if user joined after a netsplit
75     */
76   void addMode(const QString &sender, const QString &channel, const QString &mode);
77
78   //! Check if a string matches the criteria for a netsplit
79   /** \param quitMessage The message to be checked
80     * \return true if the message is a netsplit
81     */
82   static bool isNetsplit(const QString &quitMessage);
83
84 signals:
85   //! A bulk-join of netsplitted users timed out
86   /** Whenever the internal join-timer times out, we consider the bulk-join to be finished and emit that signal
87     * for every channel. This is the end of a netsplit.
88     * \param net     The network
89     * \param channel The IRC channel
90     * \param users   A list of all users that joined that channel
91     * \param modes   A list of all modes the users got set after joining again
92     * \param quitMessage The Quitmessage and thus the servers that got split
93     */
94   void netsplitJoin(Network *net, const QString &channel, const QStringList &users, const QStringList &modes, const QString &quitMessage);
95
96   //! A (probably bulk-) join of netsplitted users.
97   /** If users hit by the split joined before the netsplit is considered over, join the users with a normal join.
98     * \param net     The network
99     * \param channel The IRC channel
100     * \param users   A list of all users that joined that channel
101     * \param modes   A list of all modes the users got set after joining again
102     */
103   void earlyJoin(Network *net, const QString &channel, const QStringList &users, const QStringList &modes);
104
105   //! A bulk-quit of netsplitted users timed out
106   /** Whenever the internal quit-timer times out, we consider the bulk-quit to be finished and emit that signal
107     * for every channel.
108     * \param net     The network
109     * \param channel The IRC channel
110     * \param users   A list of all users that quitted in that channel
111     * \param quitMessage The Quitmessage and thus the servers that got split
112     */
113   void netsplitQuit(Network *net, const QString &channel, const QStringList &users, const QString &quitMessage);
114
115   //! The Netsplit is considered finished
116   /** This signal is emitted right after all netsplitJoin signals have been sent or whenever the
117     * internal timer signals a timeout.
118     * Simply delete the object and remove it from structures when you receive that signal.
119     */
120   void finished();
121
122 private slots:
123   void joinTimeout();
124   void quitTimeout();
125
126 private:
127   Network *_network;
128   QString _quitMsg;
129   // key: channel name
130   // value: senderstring, list of modes
131   QHash<QString, QPair<QStringList, QStringList> > _joins;
132   QHash<QString, QStringList> _quits;
133   QHash<QString, QStringList> _quitsWithMessageSent;
134   bool _sentQuit;
135   QTimer _joinTimer;
136   QTimer _quitTimer;
137   QTimer _discardTimer;
138   int _joinCounter;
139   int _quitCounter;
140 };
141
142 #endif // NETSPLIT_H