1 /***************************************************************************
2 * Copyright (C) 2005-2015 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"
44 class CoreAuthHandler;
50 class AbstractSqlMigrationReader;
51 class AbstractSqlMigrationWriter;
53 class Core : public QObject
58 static Core *instance();
59 static void destroy();
61 static void saveState();
62 static void restoreState();
64 /*** Storage access ***/
65 // These methods are threadsafe.
69 * \param userName The user's login name
70 * \param password The user's uncrypted password
71 * \return The user's ID if valid; 0 otherwise
73 static inline UserId validateUser(const QString &userName, const QString &password) {
74 return instance()->_storage->validateUser(userName, password);
78 //! Change a user's password
80 * \param userId The user's ID
81 * \param password The user's unencrypted new password
82 * \return true, if the password change was successful
84 static bool changeUserPassword(UserId userId, const QString &password);
86 //! Store a user setting persistently
88 * \param userId The users Id
89 * \param settingName The Name of the Setting
90 * \param data The Value
92 static inline void setUserSetting(UserId userId, const QString &settingName, const QVariant &data)
94 instance()->_storage->setUserSetting(userId, settingName, data);
98 //! Retrieve a persistent user setting
100 * \param userId The users Id
101 * \param settingName The Name of the Setting
102 * \param defaultValue Value to return in case it's unset.
103 * \return the Value of the Setting or the default value if it is unset.
105 static inline QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultValue = QVariant())
107 return instance()->_storage->getUserSetting(userId, settingName, defaultValue);
111 /* Identity handling */
112 static inline IdentityId createIdentity(UserId user, CoreIdentity &identity)
114 return instance()->_storage->createIdentity(user, identity);
118 static bool updateIdentity(UserId user, const CoreIdentity &identity)
120 return instance()->_storage->updateIdentity(user, identity);
124 static void removeIdentity(UserId user, IdentityId identityId)
126 instance()->_storage->removeIdentity(user, identityId);
130 static QList<CoreIdentity> identities(UserId user)
132 return instance()->_storage->identities(user);
136 //! Create a Network in the Storage and store it's Id in the given NetworkInfo
137 /** \note This method is thredsafe.
139 * \param user The core user
140 * \param networkInfo a NetworkInfo definition to store the newly created ID in
141 * \return true if successfull.
143 static bool createNetwork(UserId user, NetworkInfo &info);
145 //! Apply the changes to NetworkInfo info to the storage engine
146 /** \note This method is thredsafe.
148 * \param user The core user
149 * \param networkInfo The Updated NetworkInfo
150 * \return true if successfull.
152 static inline bool updateNetwork(UserId user, const NetworkInfo &info)
154 return instance()->_storage->updateNetwork(user, info);
158 //! Permanently remove a Network and all the data associated with it.
159 /** \note This method is thredsafe.
161 * \param user The core user
162 * \param networkId The network to delete
163 * \return true if successfull.
165 static inline bool removeNetwork(UserId user, const NetworkId &networkId)
167 return instance()->_storage->removeNetwork(user, networkId);
171 //! Returns a list of all NetworkInfos for the given UserId user
172 /** \note This method is thredsafe.
174 * \param user The core user
175 * \return QList<NetworkInfo>.
177 static inline QList<NetworkInfo> networks(UserId user)
179 return instance()->_storage->networks(user);
183 //! Get a list of Networks to restore
184 /** Return a list of networks the user was connected at the time of core shutdown
185 * \note This method is threadsafe.
187 * \param user The User Id in question
189 static inline QList<NetworkId> connectedNetworks(UserId user)
191 return instance()->_storage->connectedNetworks(user);
195 //! Update the connected state of a network
196 /** \note This method is threadsafe
198 * \param user The Id of the networks owner
199 * \param networkId The Id of the network
200 * \param isConnected whether the network is connected or not
202 static inline void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected)
204 return instance()->_storage->setNetworkConnected(user, networkId, isConnected);
208 //! Get a hash of channels with their channel keys for a given network
209 /** The keys are channel names and values are passwords (possibly empty)
210 * \note This method is threadsafe
212 * \param user The id of the networks owner
213 * \param networkId The Id of the network
215 static inline QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId)
217 return instance()->_storage->persistentChannels(user, networkId);
221 //! Update the connected state of a channel
222 /** \note This method is threadsafe
224 * \param user The Id of the networks owner
225 * \param networkId The Id of the network
226 * \param channel The name of the channel
227 * \param isJoined whether the channel is connected or not
229 static inline void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined)
231 return instance()->_storage->setChannelPersistent(user, networkId, channel, isJoined);
235 //! Update the key of a channel
236 /** \note This method is threadsafe
238 * \param user The Id of the networks owner
239 * \param networkId The Id of the network
240 * \param channel The name of the channel
241 * \param key The key of the channel (possibly empty)
243 static inline void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key)
245 return instance()->_storage->setPersistentChannelKey(user, networkId, channel, key);
249 //! retrieve last known away message for session restore
250 /** \note This method is threadsafe
252 * \param user The Id of the networks owner
253 * \param networkId The Id of the network
255 static inline QString awayMessage(UserId user, NetworkId networkId)
257 return instance()->_storage->awayMessage(user, networkId);
261 //! Make away message persistent for session restore
262 /** \note This method is threadsafe
264 * \param user The Id of the networks owner
265 * \param networkId The Id of the network
266 * \param awayMsg The current away message of own user
268 static inline void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg)
270 return instance()->_storage->setAwayMessage(user, networkId, awayMsg);
274 //! retrieve last known user mode for session restore
275 /** \note This method is threadsafe
277 * \param user The Id of the networks owner
278 * \param networkId The Id of the network
280 static inline QString userModes(UserId user, NetworkId networkId)
282 return instance()->_storage->userModes(user, networkId);
286 //! Make our user modes persistent for session restore
287 /** \note This method is threadsafe
289 * \param user The Id of the networks owner
290 * \param networkId The Id of the network
291 * \param userModes The current user modes of own user
293 static inline void setUserModes(UserId user, NetworkId networkId, const QString &userModes)
295 return instance()->_storage->setUserModes(user, networkId, userModes);
299 //! Get the unique BufferInfo for the given combination of network and buffername for a user.
300 /** \note This method is threadsafe.
302 * \param user The core user who owns this buffername
303 * \param networkId The network id
304 * \param type The type of the buffer (StatusBuffer, Channel, etc.)
305 * \param buffer The buffer name (if empty, the net's status buffer is returned)
306 * \param create Whether or not the buffer should be created if it doesnt exist
307 * \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
309 static inline BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true)
311 return instance()->_storage->bufferInfo(user, networkId, type, buffer, create);
315 //! Get the unique BufferInfo for a bufferId
316 /** \note This method is threadsafe
317 * \param user The core user who owns this buffername
318 * \param bufferId The id of the buffer
319 * \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
321 static inline BufferInfo getBufferInfo(UserId user, const BufferId &bufferId)
323 return instance()->_storage->getBufferInfo(user, bufferId);
327 //! Store a Message in the storage backend and set it's unique Id.
328 /** \note This method is threadsafe.
330 * \param message The message object to be stored
331 * \return true on success
333 static inline bool storeMessage(Message &message)
335 return instance()->_storage->logMessage(message);
339 //! Store a list of Messages in the storage backend and set their unique Id.
340 /** \note This method is threadsafe.
342 * \param messages The list message objects to be stored
343 * \return true on success
345 static inline bool storeMessages(MessageList &messages)
347 return instance()->_storage->logMessages(messages);
351 //! Request a certain number messages stored in a given buffer.
352 /** \param buffer The buffer we request messages from
353 * \param first if != -1 return only messages with a MsgId >= first
354 * \param last if != -1 return only messages with a MsgId < last
355 * \param limit if != -1 limit the returned list to a max of \limit entries
356 * \return The requested list of messages
358 static inline QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1)
360 return instance()->_storage->requestMsgs(user, bufferId, first, last, limit);
364 //! Request a certain number of messages across all buffers
365 /** \param first if != -1 return only messages with a MsgId >= first
366 * \param last if != -1 return only messages with a MsgId < last
367 * \param limit Max amount of messages
368 * \return The requested list of messages
370 static inline QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1)
372 return instance()->_storage->requestAllMsgs(user, first, last, limit);
376 //! Request a list of all buffers known to a user.
377 /** This method is used to get a list of all buffers we have stored a backlog from.
378 * \note This method is threadsafe.
380 * \param user The user whose buffers we request
381 * \return A list of the BufferInfos for all buffers as requested
383 static inline QList<BufferInfo> requestBuffers(UserId user)
385 return instance()->_storage->requestBuffers(user);
389 //! Request a list of BufferIds for a given NetworkId
390 /** \note This method is threadsafe.
392 * \param user The user whose buffers we request
393 * \param networkId The NetworkId of the network in question
394 * \return List of BufferIds belonging to the Network
396 static inline QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId)
398 return instance()->_storage->requestBufferIdsForNetwork(user, networkId);
402 //! Remove permanently a buffer and it's content from the storage backend
403 /** This call cannot be reverted!
404 * \note This method is threadsafe.
406 * \param user The user who is the owner of the buffer
407 * \param bufferId The bufferId
408 * \return true if successfull
410 static inline bool removeBuffer(const UserId &user, const BufferId &bufferId)
412 return instance()->_storage->removeBuffer(user, bufferId);
417 /** \note This method is threadsafe.
418 * \param user The id of the buffer owner
419 * \param bufferId The bufferId
420 * \param newName The new name of the buffer
421 * \return true if successfull
423 static inline bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName)
425 return instance()->_storage->renameBuffer(user, bufferId, newName);
429 //! Merge the content of two Buffers permanently. This cannot be reversed!
430 /** \note This method is threadsafe.
431 * \param user The id of the buffer owner
432 * \param bufferId1 The bufferId of the remaining buffer
433 * \param bufferId2 The buffer that is about to be removed
434 * \return true if successfulln
436 static inline bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2)
438 return instance()->_storage->mergeBuffersPermanently(user, bufferId1, bufferId2);
442 //! Update the LastSeenDate for a Buffer
443 /** This Method is used to make the LastSeenDate of a Buffer persistent
444 * \note This method is threadsafe.
446 * \param user The Owner of that Buffer
447 * \param bufferId The buffer id
448 * \param MsgId The Message id of the message that has been just seen
450 static inline void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId)
452 return instance()->_storage->setBufferLastSeenMsg(user, bufferId, msgId);
456 //! Get a Hash of all last seen message ids
457 /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
458 * \note This method is threadsafe.
460 * \param user The Owner of the buffers
462 static inline QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user)
464 return instance()->_storage->bufferLastSeenMsgIds(user);
468 //! Update the MarkerLineMsgId for a Buffer
469 /** This Method is used to make the marker line position of a Buffer persistent
470 * \note This method is threadsafe.
472 * \param user The Owner of that Buffer
473 * \param bufferId The buffer id
474 * \param MsgId The Message id where the marker line should be placed
476 static inline void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId)
478 return instance()->_storage->setBufferMarkerLineMsg(user, bufferId, msgId);
482 //! Get a Hash of all marker line message ids
483 /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds
484 * \note This method is threadsafe.
486 * \param user The Owner of the buffers
488 static inline QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user)
490 return instance()->_storage->bufferMarkerLineMsgIds(user);
494 static inline QDateTime startTime() { return instance()->_startTime; }
495 static inline bool isConfigured() { return instance()->_configured; }
496 static bool sslSupported();
497 static QVariantList backendInfo();
499 static QString setup(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData);
501 static inline QTimer &syncTimer() { return instance()->_storageSyncTimer; }
503 inline OidentdConfigGenerator *oidentdConfigGenerator() const { return _oidentdConfigGenerator; }
505 static const int AddClientEventId;
508 //! Make storage data persistent
509 /** \note This method is threadsafe.
512 void setupInternalClientSession(InternalPeer *clientConnection);
513 QString setupCore(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData);
516 //! Sent when a BufferInfo is updated in storage.
517 void bufferInfoUpdated(UserId user, const BufferInfo &info);
519 //! Relay from CoreSession::sessionState(). Used for internal connection only
520 void sessionState(const Protocol::SessionState &sessionState);
523 virtual void customEvent(QEvent *event);
526 bool startListening();
527 void stopListening(const QString &msg = QString());
528 void incomingConnection();
529 void clientDisconnected();
531 bool initStorage(const QString &backend, const QVariantMap &settings, bool setup = false);
533 void socketError(QAbstractSocket::SocketError err, const QString &errorString);
534 void setupClientSession(RemotePeer *, UserId);
536 void changeUserPass(const QString &username);
542 static Core *instanceptr;
544 SessionThread *sessionForUser(UserId userId, bool restoreState = false);
545 void addClientHelper(RemotePeer *peer, UserId uid);
546 //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
547 QString setupCoreForInternalUsage();
549 void registerStorageBackends();
550 bool registerStorageBackend(Storage *);
551 void unregisterStorageBackends();
552 void unregisterStorageBackend(Storage *);
553 bool selectBackend(const QString &backend);
555 void saveBackendSettings(const QString &backend, const QVariantMap &settings);
556 QVariantMap promptForSettings(const Storage *storage);
559 QSet<CoreAuthHandler *> _connectingClients;
560 QHash<UserId, SessionThread *> _sessions;
562 QTimer _storageSyncTimer;
565 SslServer _server, _v6server;
567 QTcpServer _server, _v6server;
570 OidentdConfigGenerator *_oidentdConfigGenerator;
572 QHash<QString, Storage *> _storageBackends;
574 QDateTime _startTime;
578 static AbstractSqlMigrationReader *getMigrationReader(Storage *storage);
579 static AbstractSqlMigrationWriter *getMigrationWriter(Storage *storage);
580 static void stdInEcho(bool on);
581 static inline void enableStdInEcho() { stdInEcho(true); }
582 static inline void disableStdInEcho() { stdInEcho(false); }