1 /***************************************************************************
2 * Copyright (C) 2005-2012 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
30 # include <QSslSocket>
31 # include "sslserver.h"
33 # include <QTcpSocket>
34 # include <QTcpServer>
37 #include "bufferinfo.h"
39 #include "oidentdconfiggenerator.h"
40 #include "sessionthread.h"
49 class AbstractSqlMigrationReader;
50 class AbstractSqlMigrationWriter;
52 class Core : public QObject
57 static Core *instance();
58 static void destroy();
60 static void saveState();
61 static void restoreState();
63 /*** Storage access ***/
64 // These methods are threadsafe.
66 //! Store a user setting persistently
68 * \param userId The users Id
69 * \param settingName The Name of the Setting
70 * \param data The Value
72 static inline void setUserSetting(UserId userId, const QString &settingName, const QVariant &data)
74 instance()->_storage->setUserSetting(userId, settingName, data);
78 //! Retrieve a persistent user setting
80 * \param userId The users Id
81 * \param settingName The Name of the Setting
82 * \param defaultValue Value to return in case it's unset.
83 * \return the Value of the Setting or the default value if it is unset.
85 static inline QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultValue = QVariant())
87 return instance()->_storage->getUserSetting(userId, settingName, defaultValue);
91 /* Identity handling */
92 static inline IdentityId createIdentity(UserId user, CoreIdentity &identity)
94 return instance()->_storage->createIdentity(user, identity);
98 static bool updateIdentity(UserId user, const CoreIdentity &identity)
100 return instance()->_storage->updateIdentity(user, identity);
104 static void removeIdentity(UserId user, IdentityId identityId)
106 instance()->_storage->removeIdentity(user, identityId);
110 static QList<CoreIdentity> identities(UserId user)
112 return instance()->_storage->identities(user);
116 //! Create a Network in the Storage and store it's Id in the given NetworkInfo
117 /** \note This method is thredsafe.
119 * \param user The core user
120 * \param networkInfo a NetworkInfo definition to store the newly created ID in
121 * \return true if successfull.
123 static bool createNetwork(UserId user, NetworkInfo &info);
125 //! Apply the changes to NetworkInfo info to the storage engine
126 /** \note This method is thredsafe.
128 * \param user The core user
129 * \param networkInfo The Updated NetworkInfo
130 * \return true if successfull.
132 static inline bool updateNetwork(UserId user, const NetworkInfo &info)
134 return instance()->_storage->updateNetwork(user, info);
138 //! Permanently remove a Network and all the data associated with it.
139 /** \note This method is thredsafe.
141 * \param user The core user
142 * \param networkId The network to delete
143 * \return true if successfull.
145 static inline bool removeNetwork(UserId user, const NetworkId &networkId)
147 return instance()->_storage->removeNetwork(user, networkId);
151 //! Returns a list of all NetworkInfos for the given UserId user
152 /** \note This method is thredsafe.
154 * \param user The core user
155 * \return QList<NetworkInfo>.
157 static inline QList<NetworkInfo> networks(UserId user)
159 return instance()->_storage->networks(user);
163 //! Get a list of Networks to restore
164 /** Return a list of networks the user was connected at the time of core shutdown
165 * \note This method is threadsafe.
167 * \param user The User Id in question
169 static inline QList<NetworkId> connectedNetworks(UserId user)
171 return instance()->_storage->connectedNetworks(user);
175 //! Update the connected state of a network
176 /** \note This method is threadsafe
178 * \param user The Id of the networks owner
179 * \param networkId The Id of the network
180 * \param isConnected whether the network is connected or not
182 static inline void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected)
184 return instance()->_storage->setNetworkConnected(user, networkId, isConnected);
188 //! Get a hash of channels with their channel keys for a given network
189 /** The keys are channel names and values are passwords (possibly empty)
190 * \note This method is threadsafe
192 * \param user The id of the networks owner
193 * \param networkId The Id of the network
195 static inline QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId)
197 return instance()->_storage->persistentChannels(user, networkId);
201 //! Update the connected state of a channel
202 /** \note This method is threadsafe
204 * \param user The Id of the networks owner
205 * \param networkId The Id of the network
206 * \param channel The name of the channel
207 * \param isJoined whether the channel is connected or not
209 static inline void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined)
211 return instance()->_storage->setChannelPersistent(user, networkId, channel, isJoined);
215 //! Update the key of a channel
216 /** \note This method is threadsafe
218 * \param user The Id of the networks owner
219 * \param networkId The Id of the network
220 * \param channel The name of the channel
221 * \param key The key of the channel (possibly empty)
223 static inline void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key)
225 return instance()->_storage->setPersistentChannelKey(user, networkId, channel, key);
229 //! retrieve last known away message for session restore
230 /** \note This method is threadsafe
232 * \param user The Id of the networks owner
233 * \param networkId The Id of the network
235 static inline QString awayMessage(UserId user, NetworkId networkId)
237 return instance()->_storage->awayMessage(user, networkId);
241 //! Make away message persistent for session restore
242 /** \note This method is threadsafe
244 * \param user The Id of the networks owner
245 * \param networkId The Id of the network
246 * \param awayMsg The current away message of own user
248 static inline void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg)
250 return instance()->_storage->setAwayMessage(user, networkId, awayMsg);
254 //! retrieve last known user mode for session restore
255 /** \note This method is threadsafe
257 * \param user The Id of the networks owner
258 * \param networkId The Id of the network
260 static inline QString userModes(UserId user, NetworkId networkId)
262 return instance()->_storage->userModes(user, networkId);
266 //! Make our user modes persistent for session restore
267 /** \note This method is threadsafe
269 * \param user The Id of the networks owner
270 * \param networkId The Id of the network
271 * \param userModes The current user modes of own user
273 static inline void setUserModes(UserId user, NetworkId networkId, const QString &userModes)
275 return instance()->_storage->setUserModes(user, networkId, userModes);
279 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
280 /** \note This method is threadsafe.
282 * \param user The core user who owns this buffername
283 * \param networkId The network id
284 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
285 * \param buffer The buffer name (if empty, the net's status buffer is returned)
286 * \param create Whether or not the buffer should be created if it doesnt exist
287 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
289 static inline BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true)
291 return instance()->_storage->bufferInfo(user, networkId, type, buffer, create);
295 //! Get the unique BufferInfo for a bufferId
296 /** \note This method is threadsafe
297 * \param user The core user who owns this buffername
298 * \param bufferId The id of the buffer
299 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
301 static inline BufferInfo getBufferInfo(UserId user, const BufferId &bufferId)
303 return instance()->_storage->getBufferInfo(user, bufferId);
307 //! Store a Message in the storage backend and set it's unique Id.
308 /** \note This method is threadsafe.
310 * \param message The message object to be stored
311 * \return true on success
313 static inline bool storeMessage(Message &message)
315 return instance()->_storage->logMessage(message);
319 //! Store a list of Messages in the storage backend and set their unique Id.
320 /** \note This method is threadsafe.
322 * \param messages The list message objects to be stored
323 * \return true on success
325 static inline bool storeMessages(MessageList &messages)
327 return instance()->_storage->logMessages(messages);
331 //! Request a certain number messages stored in a given buffer.
332 /** \param buffer The buffer we request messages from
333 * \param first if != -1 return only messages with a MsgId >= first
334 * \param last if != -1 return only messages with a MsgId < last
335 * \param limit if != -1 limit the returned list to a max of \limit entries
336 * \return The requested list of messages
338 static inline QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1)
340 return instance()->_storage->requestMsgs(user, bufferId, first, last, limit);
344 //! Request a certain number of messages across all buffers
345 /** \param first if != -1 return only messages with a MsgId >= first
346 * \param last if != -1 return only messages with a MsgId < last
347 * \param limit Max amount of messages
348 * \return The requested list of messages
350 static inline QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1)
352 return instance()->_storage->requestAllMsgs(user, first, last, limit);
356 //! Request a list of all buffers known to a user.
357 /** This method is used to get a list of all buffers we have stored a backlog from.
358 * \note This method is threadsafe.
360 * \param user The user whose buffers we request
361 * \return A list of the BufferInfos for all buffers as requested
363 static inline QList<BufferInfo> requestBuffers(UserId user)
365 return instance()->_storage->requestBuffers(user);
369 //! Request a list of BufferIds for a given NetworkId
370 /** \note This method is threadsafe.
372 * \param user The user whose buffers we request
373 * \param networkId The NetworkId of the network in question
374 * \return List of BufferIds belonging to the Network
376 static inline QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId)
378 return instance()->_storage->requestBufferIdsForNetwork(user, networkId);
382 //! Remove permanently a buffer and it's content from the storage backend
383 /** This call cannot be reverted!
384 * \note This method is threadsafe.
386 * \param user The user who is the owner of the buffer
387 * \param bufferId The bufferId
388 * \return true if successfull
390 static inline bool removeBuffer(const UserId &user, const BufferId &bufferId)
392 return instance()->_storage->removeBuffer(user, bufferId);
397 /** \note This method is threadsafe.
398 * \param user The id of the buffer owner
399 * \param bufferId The bufferId
400 * \param newName The new name of the buffer
401 * \return true if successfull
403 static inline bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName)
405 return instance()->_storage->renameBuffer(user, bufferId, newName);
409 //! Merge the content of two Buffers permanently. This cannot be reversed!
410 /** \note This method is threadsafe.
411 * \param user The id of the buffer owner
412 * \param bufferId1 The bufferId of the remaining buffer
413 * \param bufferId2 The buffer that is about to be removed
414 * \return true if successfulln
416 static inline bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2)
418 return instance()->_storage->mergeBuffersPermanently(user, bufferId1, bufferId2);
422 //! Update the LastSeenDate for a Buffer
423 /** This Method is used to make the LastSeenDate of a Buffer persistent
424 * \note This method is threadsafe.
426 * \param user The Owner of that Buffer
427 * \param bufferId The buffer id
428 * \param MsgId The Message id of the message that has been just seen
430 static inline void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId)
432 return instance()->_storage->setBufferLastSeenMsg(user, bufferId, msgId);
436 //! Get a Hash of all last seen message ids
437 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
438 * \note This method is threadsafe.
440 * \param user The Owner of the buffers
442 static inline QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user)
444 return instance()->_storage->bufferLastSeenMsgIds(user);
448 //! Update the MarkerLineMsgId for a Buffer
449 /** This Method is used to make the marker line position of a Buffer persistent
450 * \note This method is threadsafe.
452 * \param user The Owner of that Buffer
453 * \param bufferId The buffer id
454 * \param MsgId The Message id where the marker line should be placed
456 static inline void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId)
458 return instance()->_storage->setBufferMarkerLineMsg(user, bufferId, msgId);
462 //! Get a Hash of all marker line message ids
463 /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds
464 * \note This method is threadsafe.
466 * \param user The Owner of the buffers
468 static inline QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user)
470 return instance()->_storage->bufferMarkerLineMsgIds(user);
474 const QDateTime &startTime() const { return _startTime; }
476 static inline QTimer &syncTimer() { return instance()->_storageSyncTimer; }
478 inline OidentdConfigGenerator *oidentdConfigGenerator() const { return _oidentdConfigGenerator; }
480 static const int AddClientEventId;
483 //! Make storage data persistent
484 /** \note This method is threadsafe.
487 void setupInternalClientSession(SignalProxy *proxy);
490 //! Sent when a BufferInfo is updated in storage.
491 void bufferInfoUpdated(UserId user, const BufferInfo &info);
493 //! Relay From CoreSession::sessionState(const QVariant &). Used for internal connection only
494 void sessionState(const QVariant &);
497 virtual void customEvent(QEvent *event);
500 bool startListening();
501 void stopListening(const QString &msg = QString());
502 void incomingConnection();
503 void clientHasData();
504 void clientDisconnected();
506 bool initStorage(const QString &backend, QVariantMap settings, bool setup = false);
507 bool initStorage(QVariantMap dbSettings, bool setup = false);
510 void sslErrors(const QList<QSslError> &errors);
512 void socketError(QAbstractSocket::SocketError);
518 static Core *instanceptr;
520 SessionThread *createSession(UserId userId, bool restoreState = false);
521 void setupClientSession(QTcpSocket *socket, UserId uid);
522 void addClientHelper(QTcpSocket *socket, UserId uid);
523 void processClientMessage(QTcpSocket *socket, const QVariantMap &msg);
524 //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
525 QString setupCoreForInternalUsage();
526 QString setupCore(QVariantMap setupData);
528 void registerStorageBackends();
529 bool registerStorageBackend(Storage *);
530 void unregisterStorageBackends();
531 void unregisterStorageBackend(Storage *);
532 bool selectBackend(const QString &backend);
534 void changeUserPass(const QString &username);
535 void saveBackendSettings(const QString &backend, const QVariantMap &settings);
536 QVariantMap promptForSettings(const Storage *storage);
538 QHash<UserId, SessionThread *> sessions;
540 QTimer _storageSyncTimer;
543 SslServer _server, _v6server;
545 QTcpServer _server, _v6server;
548 OidentdConfigGenerator *_oidentdConfigGenerator;
550 QHash<QTcpSocket *, quint32> blocksizes;
551 QHash<QTcpSocket *, QVariantMap> clientInfo;
553 QHash<QString, Storage *> _storageBackends;
555 QDateTime _startTime;
559 static AbstractSqlMigrationReader *getMigrationReader(Storage *storage);
560 static AbstractSqlMigrationWriter *getMigrationWriter(Storage *storage);
561 static void stdInEcho(bool on);
562 static inline void enableStdInEcho() { stdInEcho(true); }
563 static inline void disableStdInEcho() { stdInEcho(false); }