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