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