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