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