Remove legacy core/client protocol version checks
[quassel.git] / src / core / core.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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 _CORE_H_
22 #define _CORE_H_
23
24 #include <QDateTime>
25 #include <QMutex>
26 #include <QString>
27 #include <QVariant>
28 #include <QTimer>
29
30 #ifdef HAVE_SSL
31 #  include <QSslSocket>
32 #  include "sslserver.h"
33 #else
34 #  include <QTcpSocket>
35 #  include <QTcpServer>
36 #endif
37
38 #include "bufferinfo.h"
39 #include "message.h"
40 #include "sessionthread.h"
41 #include "types.h"
42
43 class CoreSession;
44 class SessionThread;
45 class Storage;
46 struct NetworkInfo;
47
48 class Core : public QObject {
49   Q_OBJECT
50
51   public:
52     static Core * instance();
53     static void destroy();
54
55     static void saveState();
56     static void restoreState();
57
58     /*** Storage access ***/
59     // These methods are threadsafe.
60
61     //! Store a user setting persistently
62     /**
63      * \param userId       The users Id
64      * \param settingName  The Name of the Setting
65      * \param data         The Value
66      */
67     static void setUserSetting(UserId userId, const QString &settingName, const QVariant &data);
68
69     //! Retrieve a persistent user setting
70     /**
71      * \param userId       The users Id
72      * \param settingName  The Name of the Setting
73      * \param default      Value to return in case it's unset.
74      * \return the Value of the Setting or the default value if it is unset.
75      */
76     static QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant());
77
78
79     //! Create a Network in the Storage and store it's Id in the given NetworkInfo
80     /** \note This method is thredsafe.
81      *
82      *  \param user        The core user
83      *  \param networkInfo a NetworkInfo definition to store the newly created ID in
84      *  \return true if successfull.
85      */
86     static bool createNetwork(UserId user, NetworkInfo &info);
87
88     //! Apply the changes to NetworkInfo info to the storage engine
89     /** \note This method is thredsafe.
90      *
91      *  \param user        The core user
92      *  \param networkInfo The Updated NetworkInfo
93      *  \return true if successfull.
94      */
95     static bool updateNetwork(UserId user, const NetworkInfo &info);
96
97     //! Permanently remove a Network and all the data associated with it.
98     /** \note This method is thredsafe.
99      *
100      *  \param user        The core user
101      *  \param networkId   The network to delete
102      *  \return true if successfull.
103      */
104     static bool removeNetwork(UserId user, const NetworkId &networkId);
105
106     //! Returns a list of all NetworkInfos for the given UserId user
107     /** \note This method is thredsafe.
108      *
109      *  \param user        The core user
110      *  \return QList<NetworkInfo>.
111      */
112     static QList<NetworkInfo> networks(UserId user);
113
114     //! Get the NetworkId for a network name.
115     /** \note This method is threadsafe.
116      *
117      *  \param user    The core user
118      *  \param network The name of the network
119      *  \return The NetworkId corresponding to the given network.
120      */
121     static NetworkId networkId(UserId user, const QString &network);
122
123     //! Get a list of Networks to restore
124     /** Return a list of networks the user was connected at the time of core shutdown
125      *  \note This method is threadsafe.
126      *
127      *  \param user  The User Id in question
128      */
129     static QList<NetworkId> connectedNetworks(UserId user);
130
131     //! Update the connected state of a network
132     /** \note This method is threadsafe
133      *
134      *  \param user        The Id of the networks owner
135      *  \param networkId   The Id of the network
136      *  \param isConnected whether the network is connected or not
137      */
138     static void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected);
139
140     //! Get a hash of channels with their channel keys for a given network
141     /** The keys are channel names and values are passwords (possibly empty)
142      *  \note This method is threadsafe
143      *
144      *  \param user       The id of the networks owner
145      *  \param networkId  The Id of the network
146      */
147     static QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId);
148
149     //! Update the connected state of a channel
150     /** \note This method is threadsafe
151      *
152      *  \param user       The Id of the networks owner
153      *  \param networkId  The Id of the network
154      *  \param channel    The name of the channel
155      *  \param isJoined   whether the channel is connected or not
156      */
157     static void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined);
158
159     //! Update the key of a channel
160     /** \note This method is threadsafe
161      *
162      *  \param user       The Id of the networks owner
163      *  \param networkId  The Id of the network
164      *  \param channel    The name of the channel
165      *  \param key        The key of the channel (possibly empty)
166      */
167     static void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key);
168
169     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
170     /** \note This method is threadsafe.
171      *
172      *  \param user      The core user who owns this buffername
173      *  \param networkId The network id
174      *  \param type      The type of the buffer (StatusBuffer, Channel, etc.)
175      *  \param buffer    The buffer name (if empty, the net's status buffer is returned)
176      *  \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
177      */
178     static BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type, const QString &buffer = "");
179
180     //! Get the unique BufferInfo for a bufferId
181     /** \note This method is threadsafe
182      *  \param user      The core user who owns this buffername
183      *  \param bufferId  The id of the buffer
184      *  \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
185      */
186     static BufferInfo getBufferInfo(UserId user, const BufferId &bufferId);
187
188
189     //! Store a Message in the backlog.
190     /** \note This method is threadsafe.
191      *
192      *  \param msg  The message object to be stored
193      *  \return The globally unique id for the stored message
194      */
195     static MsgId storeMessage(const Message &message);
196
197     //! Request a certain number (or all) messages stored in a given buffer.
198     /** \note This method is threadsafe.
199      *
200      *  \param buffer   The buffer we request messages from
201      *  \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
202      *  \param offset   Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
203      *  \return The requested list of messages
204      */
205     static QList<Message> requestMsgs(UserId user, BufferId buffer, int lastmsgs = -1, int offset = -1);
206
207     //! Request messages stored in a given buffer since a certain point in time.
208     /** \note This method is threadsafe.
209      *
210      *  \param buffer   The buffer we request messages from
211      *  \param since    Only return messages newer than this point in time
212      *  \param offset   Do not return messages with MsgId >= offset, if offset >= 0
213      *  \return The requested list of messages
214      */
215     static QList<Message> requestMsgs(UserId user, BufferId buffer, QDateTime since, int offset = -1);
216
217     //! Request a range of messages stored in a given buffer.
218     /** \note This method is threadsafe.
219      *
220      *  \param buffer   The buffer we request messages from
221      *  \param first    Return messages with first <= MsgId <= last
222      *  \param last     Return messages with first <= MsgId <= last
223      *  \return The requested list of messages
224      */
225     static QList<Message> requestMsgRange(UserId user, BufferId buffer, int first, int last);
226
227     //! Request a list of all buffers known to a user.
228     /** This method is used to get a list of all buffers we have stored a backlog from.
229      *  \note This method is threadsafe.
230      *
231      *  \param user  The user whose buffers we request
232      *  \return A list of the BufferInfos for all buffers as requested
233      */
234     static QList<BufferInfo> requestBuffers(UserId user);
235
236
237     //! Request a list of BufferIds for a given NetworkId
238     /** \note This method is threadsafe.
239     *
240     *  \param user  The user whose buffers we request
241     *  \param networkId  The NetworkId of the network in question
242     *  \return List of BufferIds belonging to the Network
243     */
244     static QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId);
245
246     //! Remove permanently a buffer and it's content from the storage backend
247     /** This call cannot be reverted!
248      *  \note This method is threadsafe.
249      *
250      *  \param user      The user who is the owner of the buffer
251      *  \param bufferId  The bufferId
252      *  \return true if successfull
253      */
254     static bool removeBuffer(const UserId &user, const BufferId &bufferId);
255
256     //! Rename a Buffer
257     /** \note This method is threadsafe.
258      *  \param user      The id of the buffer owner
259      *  \param networkId The id of the network the buffer belongs to
260      *  \param newName   The new name of the buffer
261      *  \param oldName   The previous name of the buffer
262      *  \return the BufferId of the affected buffer or an invalid BufferId if not successfull
263      */
264     static BufferId renameBuffer(const UserId &user, const NetworkId &networkId, const QString &newName, const QString &oldName);
265
266     //! Update the LastSeenDate for a Buffer
267     /** This Method is used to make the LastSeenDate of a Buffer persistent
268      *  \note This method is threadsafe.
269      *
270      * \param user      The Owner of that Buffer
271      * \param bufferId  The buffer id
272      * \param MsgId     The Message id of the message that has been just seen
273      */
274     static void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId);
275
276     //! Get a Hash of all last seen message ids
277     /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
278      *  \note This method is threadsafe.
279      *
280      * \param user      The Owner of the buffers
281      */
282     static QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user);
283
284   const QDateTime &startTime() const { return _startTime; }
285
286   public slots:
287     //! Make storage data persistent
288     /** \note This method is threadsafe.
289      */
290     void syncStorage();
291
292   signals:
293     //! Sent when a BufferInfo is updated in storage.
294     void bufferInfoUpdated(UserId user, const BufferInfo &info);
295
296   private slots:
297     bool startListening();
298     void stopListening();
299     void incomingConnection();
300     void clientHasData();
301     void clientDisconnected();
302
303     bool initStorage(QVariantMap dbSettings, bool setup = false);
304
305 #ifdef HAVE_SSL
306     void sslErrors(const QList<QSslError> &errors);
307 #endif
308     void socketError(QAbstractSocket::SocketError);
309
310   private:
311     Core();
312     ~Core();
313     void init();
314     static Core *instanceptr;
315
316     SessionThread *createSession(UserId userId, bool restoreState = false);
317     void setupClientSession(QTcpSocket *socket, UserId uid);
318     void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
319     //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
320     QString setupCore(const QVariant &setupData);
321
322     bool registerStorageBackend(Storage *);
323     void unregisterStorageBackend(Storage *);
324
325     QHash<UserId, SessionThread *> sessions;
326     Storage *storage;
327     QTimer _storageSyncTimer;
328
329 #ifdef HAVE_SSL
330   SslServer _server, _v6server;
331 #else
332   QTcpServer _server, _v6server;
333 #endif
334
335     QHash<QTcpSocket *, quint32> blocksizes;
336     QHash<QTcpSocket *, QVariantMap> clientInfo;
337
338     QHash<QString, Storage *> _storageBackends;
339
340     QDateTime _startTime;
341
342     bool configured;
343
344     static QMutex mutex;
345 };
346
347 #endif