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