1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
30 # include <QSslSocket>
31 # include "sslserver.h"
33 # include <QTcpSocket>
34 # include <QTcpServer>
38 #include "bufferinfo.h"
40 #include "sessionthread.h"
48 class Core : public QObject {
52 static Core * instance();
53 static void destroy();
55 static void saveState();
56 static void restoreState();
58 /*** Storage access ***/
59 // These methods are threadsafe.
61 //! Store a user setting persistently
63 * \param userId The users Id
64 * \param settingName The Name of the Setting
65 * \param data The Value
67 static inline void setUserSetting(UserId userId, const QString &settingName, const QVariant &data) {
68 instance()->_storage->setUserSetting(userId, settingName, data);
71 //! Retrieve a persistent user setting
73 * \param userId The users Id
74 * \param settingName The Name of the Setting
75 * \param default Value to return in case it's unset.
76 * \return the Value of the Setting or the default value if it is unset.
78 static inline QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &data = QVariant()) {
79 return instance()->_storage->getUserSetting(userId, settingName, data);
82 /* Identity handling */
83 static inline IdentityId createIdentity(UserId user, CoreIdentity &identity) {
84 return instance()->_storage->createIdentity(user, identity);
86 static bool updateIdentity(UserId user, const CoreIdentity &identity) {
87 return instance()->_storage->updateIdentity(user, identity);
89 static void removeIdentity(UserId user, IdentityId identityId) {
90 instance()->_storage->removeIdentity(user, identityId);
92 static QList<CoreIdentity> identities(UserId user) {
93 return instance()->_storage->identities(user);
96 //! Create a Network in the Storage and store it's Id in the given NetworkInfo
97 /** \note This method is thredsafe.
99 * \param user The core user
100 * \param networkInfo a NetworkInfo definition to store the newly created ID in
101 * \return true if successfull.
103 static bool createNetwork(UserId user, NetworkInfo &info);
105 //! Apply the changes to NetworkInfo info to the storage engine
106 /** \note This method is thredsafe.
108 * \param user The core user
109 * \param networkInfo The Updated NetworkInfo
110 * \return true if successfull.
112 static inline bool updateNetwork(UserId user, const NetworkInfo &info) {
113 return instance()->_storage->updateNetwork(user, info);
116 //! Permanently remove a Network and all the data associated with it.
117 /** \note This method is thredsafe.
119 * \param user The core user
120 * \param networkId The network to delete
121 * \return true if successfull.
123 static inline bool removeNetwork(UserId user, const NetworkId &networkId) {
124 return instance()->_storage->removeNetwork(user, networkId);
127 //! Returns a list of all NetworkInfos for the given UserId user
128 /** \note This method is thredsafe.
130 * \param user The core user
131 * \return QList<NetworkInfo>.
133 static inline QList<NetworkInfo> networks(UserId user) {
134 return instance()->_storage->networks(user);
137 //! Get a list of Networks to restore
138 /** Return a list of networks the user was connected at the time of core shutdown
139 * \note This method is threadsafe.
141 * \param user The User Id in question
143 static inline QList<NetworkId> connectedNetworks(UserId user) {
144 return instance()->_storage->connectedNetworks(user);
147 //! Update the connected state of a network
148 /** \note This method is threadsafe
150 * \param user The Id of the networks owner
151 * \param networkId The Id of the network
152 * \param isConnected whether the network is connected or not
154 static inline void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected) {
155 return instance()->_storage->setNetworkConnected(user, networkId, isConnected);
158 //! Get a hash of channels with their channel keys for a given network
159 /** The keys are channel names and values are passwords (possibly empty)
160 * \note This method is threadsafe
162 * \param user The id of the networks owner
163 * \param networkId The Id of the network
165 static inline QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId) {
166 return instance()->_storage->persistentChannels(user, networkId);
169 //! Update the connected state of a channel
170 /** \note This method is threadsafe
172 * \param user The Id of the networks owner
173 * \param networkId The Id of the network
174 * \param channel The name of the channel
175 * \param isJoined whether the channel is connected or not
177 static inline void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined) {
178 return instance()->_storage->setChannelPersistent(user, networkId, channel, isJoined);
181 //! Update the key of a channel
182 /** \note This method is threadsafe
184 * \param user The Id of the networks owner
185 * \param networkId The Id of the network
186 * \param channel The name of the channel
187 * \param key The key of the channel (possibly empty)
189 static inline void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key) {
190 return instance()->_storage->setPersistentChannelKey(user, networkId, channel, key);
193 //! retrieve last known away message for session restore
194 /** \note This method is threadsafe
196 * \param user The Id of the networks owner
197 * \param networkId The Id of the network
199 static inline QString awayMessage(UserId user, NetworkId networkId) {
200 return instance()->_storage->awayMessage(user, networkId);
203 //! Make away message persistent for session restore
204 /** \note This method is threadsafe
206 * \param user The Id of the networks owner
207 * \param networkId The Id of the network
208 * \param awayMsg The current away message of own user
210 static inline void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg) {
211 return instance()->_storage->setAwayMessage(user, networkId, awayMsg);
214 //! retrieve last known user mode for session restore
215 /** \note This method is threadsafe
217 * \param user The Id of the networks owner
218 * \param networkId The Id of the network
220 static inline QString userModes(UserId user, NetworkId networkId) {
221 return instance()->_storage->userModes(user, networkId);
224 //! Make our user modes persistent for session restore
225 /** \note This method is threadsafe
227 * \param user The Id of the networks owner
228 * \param networkId The Id of the network
229 * \param userModes The current user modes of own user
231 static inline void setUserModes(UserId user, NetworkId networkId, const QString &userModes) {
232 return instance()->_storage->setUserModes(user, networkId, userModes);
235 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
236 /** \note This method is threadsafe.
238 * \param user The core user who owns this buffername
239 * \param networkId The network id
240 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
241 * \param buffer The buffer name (if empty, the net's status buffer is returned)
242 * \param create Whether or not the buffer should be created if it doesnt exist
243 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
245 static inline BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true) {
246 return instance()->_storage->bufferInfo(user, networkId, type, buffer, create);
249 //! Get the unique BufferInfo for a bufferId
250 /** \note This method is threadsafe
251 * \param user The core user who owns this buffername
252 * \param bufferId The id of the buffer
253 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
255 static inline BufferInfo getBufferInfo(UserId user, const BufferId &bufferId) {
256 return instance()->_storage->getBufferInfo(user, bufferId);
259 //! Store a Message in the storage backend and set it's unique Id.
260 /** \note This method is threadsafe.
262 * \param message The message object to be stored
263 * \return true on success
265 static inline bool storeMessage(Message &message) {
266 return instance()->_storage->logMessage(message);
269 //! Store a list of Messages in the storage backend and set their unique Id.
270 /** \note This method is threadsafe.
272 * \param messages The list message objects to be stored
273 * \return true on success
275 static inline bool storeMessages(MessageList &messages) {
276 return instance()->_storage->logMessages(messages);
279 //! Request a certain number messages stored in a given buffer.
280 /** \param buffer The buffer we request messages from
281 * \param first if != -1 return only messages with a MsgId >= first
282 * \param last if != -1 return only messages with a MsgId < last
283 * \param limit if != -1 limit the returned list to a max of \limit entries
284 * \return The requested list of messages
286 static inline QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1) {
287 return instance()->_storage->requestMsgs(user, bufferId, first, last, limit);
290 //! Request a certain number of messages across all buffers
291 /** \param first if != -1 return only messages with a MsgId >= first
292 * \param last if != -1 return only messages with a MsgId < last
293 * \param limit Max amount of messages
294 * \return The requested list of messages
296 static inline QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1) {
297 return instance()->_storage->requestAllMsgs(user, first, last, limit);
300 //! Request a list of all buffers known to a user.
301 /** This method is used to get a list of all buffers we have stored a backlog from.
302 * \note This method is threadsafe.
304 * \param user The user whose buffers we request
305 * \return A list of the BufferInfos for all buffers as requested
307 static inline QList<BufferInfo> requestBuffers(UserId user) {
308 return instance()->_storage->requestBuffers(user);
311 //! Request a list of BufferIds for a given NetworkId
312 /** \note This method is threadsafe.
314 * \param user The user whose buffers we request
315 * \param networkId The NetworkId of the network in question
316 * \return List of BufferIds belonging to the Network
318 static inline QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId) {
319 return instance()->_storage->requestBufferIdsForNetwork(user, networkId);
322 //! Remove permanently a buffer and it's content from the storage backend
323 /** This call cannot be reverted!
324 * \note This method is threadsafe.
326 * \param user The user who is the owner of the buffer
327 * \param bufferId The bufferId
328 * \return true if successfull
330 static inline bool removeBuffer(const UserId &user, const BufferId &bufferId) {
331 return instance()->_storage->removeBuffer(user, bufferId);
335 /** \note This method is threadsafe.
336 * \param user The id of the buffer owner
337 * \param bufferId The bufferId
338 * \param newName The new name of the buffer
339 * \return true if successfull
341 static inline bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName) {
342 return instance()->_storage->renameBuffer(user, bufferId, newName);
345 //! Merge the content of two Buffers permanently. This cannot be reversed!
346 /** \note This method is threadsafe.
347 * \param user The id of the buffer owner
348 * \param bufferId1 The bufferId of the remaining buffer
349 * \param bufferId2 The buffer that is about to be removed
350 * \return true if successfulln
352 static inline bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2) {
353 return instance()->_storage->mergeBuffersPermanently(user, bufferId1, bufferId2);
356 //! Update the LastSeenDate for a Buffer
357 /** This Method is used to make the LastSeenDate of a Buffer persistent
358 * \note This method is threadsafe.
360 * \param user The Owner of that Buffer
361 * \param bufferId The buffer id
362 * \param MsgId The Message id of the message that has been just seen
364 static inline void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId) {
365 return instance()->_storage->setBufferLastSeenMsg(user, bufferId, msgId);
368 //! Get a Hash of all last seen message ids
369 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
370 * \note This method is threadsafe.
372 * \param user The Owner of the buffers
374 static inline QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user) {
375 return instance()->_storage->bufferLastSeenMsgIds(user);
378 const QDateTime &startTime() const { return _startTime; }
380 static inline QTimer &syncTimer() { return instance()->_storageSyncTimer; }
383 //! Make storage data persistent
384 /** \note This method is threadsafe.
387 void setupInternalClientSession(SignalProxy *proxy);
389 //! Sent when a BufferInfo is updated in storage.
390 void bufferInfoUpdated(UserId user, const BufferInfo &info);
392 //! Relay From CoreSession::sessionState(const QVariant &). Used for internal connection only
393 void sessionState(const QVariant &);
396 bool startListening();
397 void stopListening(const QString &msg = QString());
398 void incomingConnection();
399 void clientHasData();
400 void clientDisconnected();
402 bool initStorage(const QString &backend, QVariantMap settings, bool setup = false);
403 bool initStorage(QVariantMap dbSettings, bool setup = false);
406 void sslErrors(const QList<QSslError> &errors);
408 void socketError(QAbstractSocket::SocketError);
414 static Core *instanceptr;
416 SessionThread *createSession(UserId userId, bool restoreState = false);
417 void setupClientSession(QTcpSocket *socket, UserId uid);
418 void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
419 //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
420 QString setupCoreForInternalUsage();
421 QString setupCore(QVariantMap setupData);
423 void registerStorageBackends();
424 bool registerStorageBackend(Storage *);
425 void unregisterStorageBackends();
426 void unregisterStorageBackend(Storage *);
427 bool migrateBackend(const QString &backend);
428 bool switchBackend(const QString &backend);
429 void saveBackendSettings(const QString &backend, const QVariantMap &settings);
430 QVariantMap promptForSettings(const Storage *storage);
432 QHash<UserId, SessionThread *> sessions;
434 QTimer _storageSyncTimer;
437 SslServer _server, _v6server;
439 QTcpServer _server, _v6server;
442 QHash<QTcpSocket *, quint32> blocksizes;
443 QHash<QTcpSocket *, QVariantMap> clientInfo;
445 QHash<QString, Storage *> _storageBackends;
447 QDateTime _startTime;