7da4f2272cfd7cd4f4d28d0d42327f6a15a0cd62
[quassel.git] / src / core / core.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <memory>
24 #include <vector>
25
26 #include <QDateTime>
27 #include <QPointer>
28 #include <QString>
29 #include <QVariant>
30 #include <QTimer>
31
32 #ifdef HAVE_SSL
33 #  include <QSslSocket>
34 #  include "sslserver.h"
35 #else
36 #  include <QTcpSocket>
37 #  include <QTcpServer>
38 #endif
39
40 #include "authenticator.h"
41 #include "bufferinfo.h"
42 #include "deferredptr.h"
43 #include "message.h"
44 #include "oidentdconfiggenerator.h"
45 #include "sessionthread.h"
46 #include "storage.h"
47 #include "types.h"
48 #include "identserver.h"
49
50 class CoreAuthHandler;
51 class CoreSession;
52 class InternalPeer;
53 class SessionThread;
54 class SignalProxy;
55
56 struct NetworkInfo;
57
58 class AbstractSqlMigrationReader;
59 class AbstractSqlMigrationWriter;
60
61 class Core : public QObject
62 {
63     Q_OBJECT
64
65 public:
66     static Core *instance();
67
68     Core();
69     ~Core() override;
70
71     /*** Storage access ***/
72     // These methods are threadsafe.
73
74     //! Validate user
75     /**
76      * \param userName The user's login name
77      * \param password The user's uncrypted password
78      * \return The user's ID if valid; 0 otherwise
79      */
80     static inline UserId validateUser(const QString &userName, const QString &password) {
81         return instance()->_storage->validateUser(userName, password);
82     }
83
84     //! Authenticate user against auth backend
85     /**
86      * \param userName The user's login name
87      * \param password The user's uncrypted password
88      * \return The user's ID if valid; 0 otherwise
89      */
90     static inline UserId authenticateUser(const QString &userName, const QString &password) {
91         return instance()->_authenticator->validateUser(userName, password);
92     }
93
94     //! Add a new user, exposed so auth providers can call this without being the storage.
95     /**
96      * \param userName The user's login name
97      * \param password The user's uncrypted password
98      * \param authenticator The name of the auth provider service used to log the user in, defaults to "Database".
99      * \return The user's ID if valid; 0 otherwise
100      */
101     static inline UserId addUser(const QString &userName, const QString &password, const QString &authenticator = "Database") {
102         return instance()->_storage->addUser(userName, password, authenticator);
103     }
104
105     //! Does a comparison test against the authenticator in the database and the authenticator currently in use for a UserID.
106     /**
107      * \param userid The user's ID (note: not login name).
108      * \param authenticator The name of the auth provider service used to log the user in, defaults to "Database".
109      * \return True if the userid was configured with the passed authenticator, false otherwise.
110      */
111     static inline bool checkAuthProvider(const UserId userid, const QString &authenticator) {
112         return instance()->_storage->getUserAuthenticator(userid) == authenticator;
113     }
114
115     //! Change a user's password
116     /**
117      * \param userId     The user's ID
118      * \param password   The user's unencrypted new password
119      * \return true, if the password change was successful
120      */
121     static bool changeUserPassword(UserId userId, const QString &password);
122
123     //! Check if we can change a user password.
124     /**
125      * \param userID     The user's ID
126      * \return true, if we can change their password, false otherwise
127      */
128     static bool canChangeUserPassword(UserId userId);
129
130     //! Store a user setting persistently
131     /**
132      * \param userId       The users Id
133      * \param settingName  The Name of the Setting
134      * \param data         The Value
135      */
136     static inline void setUserSetting(UserId userId, const QString &settingName, const QVariant &data)
137     {
138         instance()->_storage->setUserSetting(userId, settingName, data);
139     }
140
141
142     //! Retrieve a persistent user setting
143     /**
144      * \param userId       The users Id
145      * \param settingName  The Name of the Setting
146      * \param defaultValue Value to return in case it's unset.
147      * \return the Value of the Setting or the default value if it is unset.
148      */
149     static inline QVariant getUserSetting(UserId userId, const QString &settingName, const QVariant &defaultValue = QVariant())
150     {
151         return instance()->_storage->getUserSetting(userId, settingName, defaultValue);
152     }
153
154
155     /* Identity handling */
156     static inline IdentityId createIdentity(UserId user, CoreIdentity &identity)
157     {
158         return instance()->_storage->createIdentity(user, identity);
159     }
160
161
162     static bool updateIdentity(UserId user, const CoreIdentity &identity)
163     {
164         return instance()->_storage->updateIdentity(user, identity);
165     }
166
167
168     static void removeIdentity(UserId user, IdentityId identityId)
169     {
170         instance()->_storage->removeIdentity(user, identityId);
171     }
172
173
174     static QList<CoreIdentity> identities(UserId user)
175     {
176         return instance()->_storage->identities(user);
177     }
178
179
180     //! Create a Network in the Storage and store it's Id in the given NetworkInfo
181     /** \note This method is thredsafe.
182      *
183      *  \param user        The core user
184      *  \param networkInfo a NetworkInfo definition to store the newly created ID in
185      *  \return true if successfull.
186      */
187     static bool createNetwork(UserId user, NetworkInfo &info);
188
189     //! Apply the changes to NetworkInfo info to the storage engine
190     /** \note This method is thredsafe.
191      *
192      *  \param user        The core user
193      *  \param networkInfo The Updated NetworkInfo
194      *  \return true if successfull.
195      */
196     static inline bool updateNetwork(UserId user, const NetworkInfo &info)
197     {
198         return instance()->_storage->updateNetwork(user, info);
199     }
200
201
202     //! Permanently remove a Network and all the data associated with it.
203     /** \note This method is thredsafe.
204      *
205      *  \param user        The core user
206      *  \param networkId   The network to delete
207      *  \return true if successfull.
208      */
209     static inline bool removeNetwork(UserId user, const NetworkId &networkId)
210     {
211         return instance()->_storage->removeNetwork(user, networkId);
212     }
213
214
215     //! Returns a list of all NetworkInfos for the given UserId user
216     /** \note This method is thredsafe.
217      *
218      *  \param user        The core user
219      *  \return QList<NetworkInfo>.
220      */
221     static inline QList<NetworkInfo> networks(UserId user)
222     {
223         return instance()->_storage->networks(user);
224     }
225
226
227     //! Get a list of Networks to restore
228     /** Return a list of networks the user was connected at the time of core shutdown
229      *  \note This method is threadsafe.
230      *
231      *  \param user  The User Id in question
232      */
233     static inline QList<NetworkId> connectedNetworks(UserId user)
234     {
235         return instance()->_storage->connectedNetworks(user);
236     }
237
238
239     //! Update the connected state of a network
240     /** \note This method is threadsafe
241      *
242      *  \param user        The Id of the networks owner
243      *  \param networkId   The Id of the network
244      *  \param isConnected whether the network is connected or not
245      */
246     static inline void setNetworkConnected(UserId user, const NetworkId &networkId, bool isConnected)
247     {
248         return instance()->_storage->setNetworkConnected(user, networkId, isConnected);
249     }
250
251
252     //! Get a hash of channels with their channel keys for a given network
253     /** The keys are channel names and values are passwords (possibly empty)
254      *  \note This method is threadsafe
255      *
256      *  \param user       The id of the networks owner
257      *  \param networkId  The Id of the network
258      */
259     static inline QHash<QString, QString> persistentChannels(UserId user, const NetworkId &networkId)
260     {
261         return instance()->_storage->persistentChannels(user, networkId);
262     }
263
264
265     //! Update the connected state of a channel
266     /** \note This method is threadsafe
267      *
268      *  \param user       The Id of the networks owner
269      *  \param networkId  The Id of the network
270      *  \param channel    The name of the channel
271      *  \param isJoined   whether the channel is connected or not
272      */
273     static inline void setChannelPersistent(UserId user, const NetworkId &networkId, const QString &channel, bool isJoined)
274     {
275         return instance()->_storage->setChannelPersistent(user, networkId, channel, isJoined);
276     }
277
278
279     //! Get a hash of buffers with their ciphers for a given network
280     /** The keys are channel names and values are ciphers (possibly empty)
281      *  \note This method is threadsafe
282      *
283      *  \param user       The id of the networks owner
284      *  \param networkId  The Id of the network
285      */
286     static inline QHash<QString, QByteArray> bufferCiphers(UserId user, const NetworkId &networkId)
287     {
288         return instance()->_storage->bufferCiphers(user, networkId);
289     }
290
291
292     //! Update the cipher of a buffer
293     /** \note This method is threadsafe
294      *
295      *  \param user        The Id of the networks owner
296      *  \param networkId   The Id of the network
297      *  \param bufferName The Cname of the buffer
298      *  \param cipher      The cipher for the buffer
299      */
300     static inline void setBufferCipher(UserId user, const NetworkId &networkId, const QString &bufferName, const QByteArray &cipher)
301     {
302         return instance()->_storage->setBufferCipher(user, networkId, bufferName, cipher);
303     }
304
305
306     //! Update the key of a channel
307     /** \note This method is threadsafe
308      *
309      *  \param user       The Id of the networks owner
310      *  \param networkId  The Id of the network
311      *  \param channel    The name of the channel
312      *  \param key        The key of the channel (possibly empty)
313      */
314     static inline void setPersistentChannelKey(UserId user, const NetworkId &networkId, const QString &channel, const QString &key)
315     {
316         return instance()->_storage->setPersistentChannelKey(user, networkId, channel, key);
317     }
318
319
320     //! retrieve last known away message for session restore
321     /** \note This method is threadsafe
322      *
323      *  \param user       The Id of the networks owner
324      *  \param networkId  The Id of the network
325      */
326     static inline QString awayMessage(UserId user, NetworkId networkId)
327     {
328         return instance()->_storage->awayMessage(user, networkId);
329     }
330
331
332     //! Make away message persistent for session restore
333     /** \note This method is threadsafe
334      *
335      *  \param user       The Id of the networks owner
336      *  \param networkId  The Id of the network
337      *  \param awayMsg    The current away message of own user
338      */
339     static inline void setAwayMessage(UserId user, NetworkId networkId, const QString &awayMsg)
340     {
341         return instance()->_storage->setAwayMessage(user, networkId, awayMsg);
342     }
343
344
345     //! retrieve last known user mode for session restore
346     /** \note This method is threadsafe
347      *
348      *  \param user       The Id of the networks owner
349      *  \param networkId  The Id of the network
350      */
351     static inline QString userModes(UserId user, NetworkId networkId)
352     {
353         return instance()->_storage->userModes(user, networkId);
354     }
355
356
357     //! Make our user modes persistent for session restore
358     /** \note This method is threadsafe
359      *
360      *  \param user       The Id of the networks owner
361      *  \param networkId  The Id of the network
362      *  \param userModes  The current user modes of own user
363      */
364     static inline void setUserModes(UserId user, NetworkId networkId, const QString &userModes)
365     {
366         return instance()->_storage->setUserModes(user, networkId, userModes);
367     }
368
369
370     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
371     /** \note This method is threadsafe.
372      *
373      *  \param user      The core user who owns this buffername
374      *  \param networkId The network id
375      *  \param type      The type of the buffer (StatusBuffer, Channel, etc.)
376      *  \param buffer    The buffer name (if empty, the net's status buffer is returned)
377      *  \param create    Whether or not the buffer should be created if it doesnt exist
378      *  \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
379      */
380     static inline BufferInfo bufferInfo(UserId user, const NetworkId &networkId, BufferInfo::Type type, const QString &buffer = "", bool create = true)
381     {
382         return instance()->_storage->bufferInfo(user, networkId, type, buffer, create);
383     }
384
385
386     //! Get the unique BufferInfo for a bufferId
387     /** \note This method is threadsafe
388      *  \param user      The core user who owns this buffername
389      *  \param bufferId  The id of the buffer
390      *  \return The BufferInfo corresponding to the given buffer id, or an invalid BufferInfo if not found.
391      */
392     static inline BufferInfo getBufferInfo(UserId user, const BufferId &bufferId)
393     {
394         return instance()->_storage->getBufferInfo(user, bufferId);
395     }
396
397
398     //! Store a Message in the storage backend and set it's unique Id.
399     /** \note This method is threadsafe.
400      *
401      *  \param message The message object to be stored
402      *  \return true on success
403      */
404     static inline bool storeMessage(Message &message)
405     {
406         return instance()->_storage->logMessage(message);
407     }
408
409
410     //! Store a list of Messages in the storage backend and set their unique Id.
411     /** \note This method is threadsafe.
412      *
413      *  \param messages The list message objects to be stored
414      *  \return true on success
415      */
416     static inline bool storeMessages(MessageList &messages)
417     {
418         return instance()->_storage->logMessages(messages);
419     }
420
421
422     //! Request a certain number messages stored in a given buffer.
423     /** \param buffer   The buffer we request messages from
424      *  \param first    if != -1 return only messages with a MsgId >= first
425      *  \param last     if != -1 return only messages with a MsgId < last
426      *  \param limit    if != -1 limit the returned list to a max of \limit entries
427      *  \return The requested list of messages
428      */
429     static inline QList<Message> requestMsgs(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1, int limit = -1)
430     {
431         return instance()->_storage->requestMsgs(user, bufferId, first, last, limit);
432     }
433
434
435     //! Request a certain number messages stored in a given buffer, matching certain filters
436     /** \param buffer   The buffer we request messages from
437      *  \param first    if != -1 return only messages with a MsgId >= first
438      *  \param last     if != -1 return only messages with a MsgId < last
439      *  \param limit    if != -1 limit the returned list to a max of \limit entries
440      *  \param type     The Message::Types that should be returned
441      *  \return The requested list of messages
442      */
443     static inline QList<Message> requestMsgsFiltered(UserId user, BufferId bufferId, MsgId first = -1, MsgId last = -1,
444                                                      int limit = -1, Message::Types type = Message::Types{-1},
445                                                      Message::Flags flags = Message::Flags{-1})
446     {
447         return instance()->_storage->requestMsgsFiltered(user, bufferId, first, last, limit, type, flags);
448     }
449
450
451     //! Request a certain number of messages across all buffers
452     /** \param first    if != -1 return only messages with a MsgId >= first
453      *  \param last     if != -1 return only messages with a MsgId < last
454      *  \param limit    Max amount of messages
455      *  \return The requested list of messages
456      */
457     static inline QList<Message> requestAllMsgs(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1)
458     {
459         return instance()->_storage->requestAllMsgs(user, first, last, limit);
460     }
461
462
463     //! Request a certain number of messages across all buffers, matching certain filters
464     /** \param first    if != -1 return only messages with a MsgId >= first
465      *  \param last     if != -1 return only messages with a MsgId < last
466      *  \param limit    Max amount of messages
467      *  \param type     The Message::Types that should be returned
468      *  \return The requested list of messages
469      */
470     static inline QList<Message> requestAllMsgsFiltered(UserId user, MsgId first = -1, MsgId last = -1, int limit = -1,
471                                                         Message::Types type = Message::Types{-1},
472                                                         Message::Flags flags = Message::Flags{-1})
473     {
474         return instance()->_storage->requestAllMsgsFiltered(user, first, last, limit, type, flags);
475     }
476
477
478     //! Request a list of all buffers known to a user.
479     /** This method is used to get a list of all buffers we have stored a backlog from.
480      *  \note This method is threadsafe.
481      *
482      *  \param user  The user whose buffers we request
483      *  \return A list of the BufferInfos for all buffers as requested
484      */
485     static inline QList<BufferInfo> requestBuffers(UserId user)
486     {
487         return instance()->_storage->requestBuffers(user);
488     }
489
490
491     //! Request a list of BufferIds for a given NetworkId
492     /** \note This method is threadsafe.
493      *
494      *  \param user  The user whose buffers we request
495      *  \param networkId  The NetworkId of the network in question
496      *  \return List of BufferIds belonging to the Network
497      */
498     static inline QList<BufferId> requestBufferIdsForNetwork(UserId user, NetworkId networkId)
499     {
500         return instance()->_storage->requestBufferIdsForNetwork(user, networkId);
501     }
502
503
504     //! Remove permanently a buffer and it's content from the storage backend
505     /** This call cannot be reverted!
506      *  \note This method is threadsafe.
507      *
508      *  \param user      The user who is the owner of the buffer
509      *  \param bufferId  The bufferId
510      *  \return true if successfull
511      */
512     static inline bool removeBuffer(const UserId &user, const BufferId &bufferId)
513     {
514         return instance()->_storage->removeBuffer(user, bufferId);
515     }
516
517
518     //! Rename a Buffer
519     /** \note This method is threadsafe.
520      *  \param user      The id of the buffer owner
521      *  \param bufferId  The bufferId
522      *  \param newName   The new name of the buffer
523      *  \return true if successfull
524      */
525     static inline bool renameBuffer(const UserId &user, const BufferId &bufferId, const QString &newName)
526     {
527         return instance()->_storage->renameBuffer(user, bufferId, newName);
528     }
529
530
531     //! Merge the content of two Buffers permanently. This cannot be reversed!
532     /** \note This method is threadsafe.
533      *  \param user      The id of the buffer owner
534      *  \param bufferId1 The bufferId of the remaining buffer
535      *  \param bufferId2 The buffer that is about to be removed
536      *  \return true if successfulln
537      */
538     static inline bool mergeBuffersPermanently(const UserId &user, const BufferId &bufferId1, const BufferId &bufferId2)
539     {
540         return instance()->_storage->mergeBuffersPermanently(user, bufferId1, bufferId2);
541     }
542
543
544     //! Update the LastSeenDate for a Buffer
545     /** This Method is used to make the LastSeenDate of a Buffer persistent
546      *  \note This method is threadsafe.
547      *
548      * \param user      The Owner of that Buffer
549      * \param bufferId  The buffer id
550      * \param MsgId     The Message id of the message that has been just seen
551      */
552     static inline void setBufferLastSeenMsg(UserId user, const BufferId &bufferId, const MsgId &msgId)
553     {
554         return instance()->_storage->setBufferLastSeenMsg(user, bufferId, msgId);
555     }
556
557     //! Get the auth username associated with a userId
558     /** \param user  The user to retrieve the username for
559      *  \return      The username for the user
560      */
561     static inline QString getAuthUserName(UserId user) {
562         return instance()->_storage->getAuthUserName(user);
563     }
564
565     //! Get a usable sysident for the given user in oidentd-strict mode
566     /** \param user    The user to retrieve the sysident for
567      *  \return The authusername
568      */
569     QString strictSysIdent(UserId user) const;
570
571
572     //! Get a Hash of all last seen message ids
573     /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
574      *  \note This method is threadsafe.
575      *
576      * \param user      The Owner of the buffers
577      */
578     static inline QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user)
579     {
580         return instance()->_storage->bufferLastSeenMsgIds(user);
581     }
582
583
584     //! Update the MarkerLineMsgId for a Buffer
585     /** This Method is used to make the marker line position of a Buffer persistent
586      *  \note This method is threadsafe.
587      *
588      * \param user      The Owner of that Buffer
589      * \param bufferId  The buffer id
590      * \param MsgId     The Message id where the marker line should be placed
591      */
592     static inline void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId)
593     {
594         return instance()->_storage->setBufferMarkerLineMsg(user, bufferId, msgId);
595     }
596
597
598     //! Get a Hash of all marker line message ids
599     /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds
600      *  \note This method is threadsafe.
601      *
602      * \param user      The Owner of the buffers
603      */
604     static inline QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user)
605     {
606         return instance()->_storage->bufferMarkerLineMsgIds(user);
607     }
608
609     //! Update the BufferActivity for a Buffer
610     /** This Method is used to make the activity state of a Buffer persistent
611      *  \note This method is threadsafe.
612      *
613      * \param user      The Owner of that Buffer
614      * \param bufferId  The buffer id
615      * \param MsgId     The Message id where the marker line should be placed
616      */
617     static inline void setBufferActivity(UserId user, BufferId bufferId, Message::Types activity) {
618         return instance()->_storage->setBufferActivity(user, bufferId, activity);
619     }
620
621
622     //! Get a Hash of all buffer activity states
623     /** This Method is called when the Quassel Core is started to restore the BufferActivity
624      *  \note This method is threadsafe.
625      *
626      * \param user      The Owner of the buffers
627      */
628     static inline QHash<BufferId, Message::Types> bufferActivities(UserId user) {
629         return instance()->_storage->bufferActivities(user);
630     }
631
632     //! Get the bitset of buffer activity states for a buffer
633     /** This method is used to load the activity state of a buffer when its last seen message changes.
634      *  \note This method is threadsafe.
635      *
636      * \param bufferId The buffer
637      * \param lastSeenMsgId     The last seen message
638      */
639     static inline Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) {
640         return instance()->_storage->bufferActivity(bufferId, lastSeenMsgId);
641     }
642
643     //! Update the highlight count for a Buffer
644     /** This Method is used to make the highlight count state of a Buffer persistent
645      *  \note This method is threadsafe.
646      *
647      * \param user      The Owner of that Buffer
648      * \param bufferId  The buffer id
649      * \param MsgId     The Message id where the marker line should be placed
650      */
651     static inline void setHighlightCount(UserId user, BufferId bufferId, int highlightCount) {
652         return instance()->_storage->setHighlightCount(user, bufferId, highlightCount);
653     }
654
655
656     //! Get a Hash of all highlight count states
657     /** This Method is called when the Quassel Core is started to restore the highlight count
658      *  \note This method is threadsafe.
659      *
660      * \param user      The Owner of the buffers
661      */
662     static inline QHash<BufferId, int> highlightCounts(UserId user) {
663         return instance()->_storage->highlightCounts(user);
664     }
665     //! Get the highlight count states for a buffer
666     /** This method is used to load the highlight count of a buffer when its last seen message changes.
667      *  \note This method is threadsafe.
668      *
669      * \param bufferId The buffer
670      * \param lastSeenMsgId     The last seen message
671      */
672     static inline int highlightCount(BufferId bufferId, MsgId lastSeenMsgId) {
673         return instance()->_storage->highlightCount(bufferId, lastSeenMsgId);
674     }
675
676     static inline QDateTime startTime() { return instance()->_startTime; }
677     static inline bool isConfigured() { return instance()->_configured; }
678
679     /**
680      * Whether or not strict ident mode is enabled, locking users' idents to Quassel username
681      *
682      * @return True if strict mode enabled, otherwise false
683      */
684     static inline bool strictIdentEnabled() { return instance()->_strictIdentEnabled; }
685
686     static bool sslSupported();
687
688     static QVariantList backendInfo();
689     static QVariantList authenticatorInfo();
690
691     static QString setup(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData, const QString &authenticator, const QVariantMap &authSetupMap);
692
693     static inline QTimer *syncTimer() { return &instance()->_storageSyncTimer; }
694
695     inline OidentdConfigGenerator *oidentdConfigGenerator() const { return _oidentdConfigGenerator; }
696     inline IdentServer *identServer() const { return _identServer; }
697
698     static const int AddClientEventId;
699
700 signals:
701     //! Sent when a BufferInfo is updated in storage.
702     void bufferInfoUpdated(UserId user, const BufferInfo &info);
703
704     //! Relay from CoreSession::sessionState(). Used for internal connection only
705     void sessionState(const Protocol::SessionState &sessionState);
706
707     //! Emitted when database schema upgrade starts or ends
708     void dbUpgradeInProgress(bool inProgress);
709
710 public slots:
711     bool init();
712
713     /** Persist storage.
714      *
715      * @note This method is threadsafe.
716      */
717     void syncStorage();
718
719     /**
720      * Reload SSL certificates used for connection with clients.
721      *
722      * @return True if certificates reloaded successfully, otherwise false.
723      */
724     bool reloadCerts();
725
726     void cacheSysIdent();
727
728     QString setupCore(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData, const QString &authenticator, const QVariantMap &authSetupMap);
729
730     void connectInternalPeer(QPointer<InternalPeer> peer);
731
732 protected:
733     void customEvent(QEvent *event) override;
734
735 private slots:
736     bool startListening();
737     void stopListening(const QString &msg = QString());
738     void incomingConnection();
739     void clientDisconnected();
740
741     bool initStorage(const QString &backend, const QVariantMap &settings,
742                      const QProcessEnvironment &environment, bool loadFromEnvironment,
743                      bool setup = false);
744     bool initAuthenticator(const QString &backend, const QVariantMap &settings,
745                            const QProcessEnvironment &environment, bool loadFromEnvironment,
746                            bool setup = false);
747
748     void socketError(QAbstractSocket::SocketError err, const QString &errorString);
749     void setupClientSession(RemotePeer *, UserId);
750
751     bool changeUserPass(const QString &username);
752
753 private:
754     SessionThread *sessionForUser(UserId userId, bool restoreState = false);
755     void addClientHelper(RemotePeer *peer, UserId uid);
756     //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
757     QString setupCoreForInternalUsage();
758     void setupInternalClientSession(QPointer<InternalPeer> peer);
759
760     bool createUser();
761
762     template<typename Storage>
763     void registerStorageBackend();
764
765     template<typename Authenticator>
766     void registerAuthenticator();
767
768     void registerStorageBackends();
769     void registerAuthenticators();
770
771     DeferredSharedPtr<Storage>       storageBackend(const QString& backendId) const;
772     DeferredSharedPtr<Authenticator> authenticator(const QString& authenticatorId) const;
773
774     bool selectBackend(const QString &backend);
775     bool selectAuthenticator(const QString &backend);
776
777     bool saveBackendSettings(const QString &backend, const QVariantMap &settings);
778     void saveAuthenticatorSettings(const QString &backend, const QVariantMap &settings);
779
780     void saveState();
781     void restoreState();
782
783     template<typename Backend>
784     QVariantMap promptForSettings(const Backend *backend);
785
786 private:
787     static Core *_instance;
788     QSet<CoreAuthHandler *> _connectingClients;
789     QHash<UserId, SessionThread *> _sessions;
790     DeferredSharedPtr<Storage>       _storage;        ///< Active storage backend
791     DeferredSharedPtr<Authenticator> _authenticator;  ///< Active authenticator
792     QMap<UserId, QString> _authUserNames;
793
794     QTimer _storageSyncTimer;
795
796 #ifdef HAVE_SSL
797     SslServer _server, _v6server;
798 #else
799     QTcpServer _server, _v6server;
800 #endif
801
802     OidentdConfigGenerator *_oidentdConfigGenerator {nullptr};
803
804     std::vector<DeferredSharedPtr<Storage>>       _registeredStorageBackends;
805     std::vector<DeferredSharedPtr<Authenticator>> _registeredAuthenticators;
806
807     QDateTime _startTime;
808
809     IdentServer *_identServer {nullptr};
810     
811     bool _initialized{false};
812     bool _configured{false};
813
814     QPointer<InternalPeer> _pendingInternalConnection;
815
816     /// Whether or not strict ident mode is enabled, locking users' idents to Quassel username
817     bool _strictIdentEnabled;
818
819     static std::unique_ptr<AbstractSqlMigrationReader> getMigrationReader(Storage *storage);
820     static std::unique_ptr<AbstractSqlMigrationWriter> getMigrationWriter(Storage *storage);
821     static void stdInEcho(bool on);
822     static inline void enableStdInEcho() { stdInEcho(true); }
823     static inline void disableStdInEcho() { stdInEcho(false); }
824 };