QssParser: Interpret "oblique" as italic
[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     //! Get the auth username associated with a userId
560     /** \param user  The user to retrieve the username for
561      *  \return      The username for the user
562      */
563     static inline QString getAuthUserName(UserId user) {
564         return instance()->_storage->getAuthUserName(user);
565     }
566
567     //! Get a usable sysident for the given user in oidentd-strict mode
568     /** \param user    The user to retrieve the sysident for
569      *  \return The authusername
570      */
571     QString strictSysIdent(UserId user) const;
572
573
574     //! Get a Hash of all last seen message ids
575     /** This Method is called when the Quassel Core is started to restore the lastSeenMsgIds
576      *  \note This method is threadsafe.
577      *
578      * \param user      The Owner of the buffers
579      */
580     static inline QHash<BufferId, MsgId> bufferLastSeenMsgIds(UserId user)
581     {
582         return instance()->_storage->bufferLastSeenMsgIds(user);
583     }
584
585
586     //! Update the MarkerLineMsgId for a Buffer
587     /** This Method is used to make the marker line position of a Buffer persistent
588      *  \note This method is threadsafe.
589      *
590      * \param user      The Owner of that Buffer
591      * \param bufferId  The buffer id
592      * \param MsgId     The Message id where the marker line should be placed
593      */
594     static inline void setBufferMarkerLineMsg(UserId user, const BufferId &bufferId, const MsgId &msgId)
595     {
596         return instance()->_storage->setBufferMarkerLineMsg(user, bufferId, msgId);
597     }
598
599
600     //! Get a Hash of all marker line message ids
601     /** This Method is called when the Quassel Core is started to restore the MarkerLineMsgIds
602      *  \note This method is threadsafe.
603      *
604      * \param user      The Owner of the buffers
605      */
606     static inline QHash<BufferId, MsgId> bufferMarkerLineMsgIds(UserId user)
607     {
608         return instance()->_storage->bufferMarkerLineMsgIds(user);
609     }
610
611     //! Update the BufferActivity for a Buffer
612     /** This Method is used to make the activity state of a Buffer persistent
613      *  \note This method is threadsafe.
614      *
615      * \param user      The Owner of that Buffer
616      * \param bufferId  The buffer id
617      * \param MsgId     The Message id where the marker line should be placed
618      */
619     static inline void setBufferActivity(UserId user, BufferId bufferId, Message::Types activity) {
620         return instance()->_storage->setBufferActivity(user, bufferId, activity);
621     }
622
623
624     //! Get a Hash of all buffer activity states
625     /** This Method is called when the Quassel Core is started to restore the BufferActivity
626      *  \note This method is threadsafe.
627      *
628      * \param user      The Owner of the buffers
629      */
630     static inline QHash<BufferId, Message::Types> bufferActivities(UserId user) {
631         return instance()->_storage->bufferActivities(user);
632     }
633
634     //! Get the bitset of buffer activity states for a buffer
635     /** This method is used to load the activity state of a buffer when its last seen message changes.
636      *  \note This method is threadsafe.
637      *
638      * \param bufferId The buffer
639      * \param lastSeenMsgId     The last seen message
640      */
641     static inline Message::Types bufferActivity(BufferId bufferId, MsgId lastSeenMsgId) {
642         return instance()->_storage->bufferActivity(bufferId, lastSeenMsgId);
643     }
644
645     //! Update the highlight count for a Buffer
646     /** This Method is used to make the highlight count state of a Buffer persistent
647      *  \note This method is threadsafe.
648      *
649      * \param user      The Owner of that Buffer
650      * \param bufferId  The buffer id
651      * \param MsgId     The Message id where the marker line should be placed
652      */
653     static inline void setHighlightCount(UserId user, BufferId bufferId, int highlightCount) {
654         return instance()->_storage->setHighlightCount(user, bufferId, highlightCount);
655     }
656
657
658     //! Get a Hash of all highlight count states
659     /** This Method is called when the Quassel Core is started to restore the highlight count
660      *  \note This method is threadsafe.
661      *
662      * \param user      The Owner of the buffers
663      */
664     static inline QHash<BufferId, int> highlightCounts(UserId user) {
665         return instance()->_storage->highlightCounts(user);
666     }
667     //! Get the highlight count states for a buffer
668     /** This method is used to load the highlight count of a buffer when its last seen message changes.
669      *  \note This method is threadsafe.
670      *
671      * \param bufferId The buffer
672      * \param lastSeenMsgId     The last seen message
673      */
674     static inline int highlightCount(BufferId bufferId, MsgId lastSeenMsgId) {
675         return instance()->_storage->highlightCount(bufferId, lastSeenMsgId);
676     }
677
678     static inline QDateTime startTime() { return instance()->_startTime; }
679     static inline bool isConfigured() { return instance()->_configured; }
680
681     /**
682      * Whether or not strict ident mode is enabled, locking users' idents to Quassel username
683      *
684      * @return True if strict mode enabled, otherwise false
685      */
686     static inline bool strictIdentEnabled() { return instance()->_strictIdentEnabled; }
687
688     static bool sslSupported();
689
690     static QVariantList backendInfo();
691     static QVariantList authenticatorInfo();
692
693     static QString setup(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData, const QString &authenticator, const QVariantMap &authSetupMap);
694
695     static inline QTimer *syncTimer() { return &instance()->_storageSyncTimer; }
696
697     inline OidentdConfigGenerator *oidentdConfigGenerator() const { return _oidentdConfigGenerator; }
698     inline IdentServer *identServer() const { return _identServer; }
699
700     static const int AddClientEventId;
701
702 signals:
703     //! Sent when a BufferInfo is updated in storage.
704     void bufferInfoUpdated(UserId user, const BufferInfo &info);
705
706     //! Relay from CoreSession::sessionState(). Used for internal connection only
707     void sessionState(const Protocol::SessionState &sessionState);
708
709     //! Emitted when database schema upgrade starts or ends
710     void dbUpgradeInProgress(bool inProgress);
711
712     //! Emitted when a fatal error was encountered during async initialization
713     void exitRequested(int exitCode, const QString &reason);
714
715 public slots:
716     void initAsync();
717
718     /** Persist storage.
719      *
720      * @note This method is threadsafe.
721      */
722     void syncStorage();
723
724     /**
725      * Reload SSL certificates used for connection with clients.
726      *
727      * @return True if certificates reloaded successfully, otherwise false.
728      */
729     bool reloadCerts();
730
731     void cacheSysIdent();
732
733     QString setupCore(const QString &adminUser, const QString &adminPassword, const QString &backend, const QVariantMap &setupData, const QString &authenticator, const QVariantMap &authSetupMap);
734
735     void connectInternalPeer(QPointer<InternalPeer> peer);
736
737 protected:
738     void customEvent(QEvent *event) override;
739
740 private slots:
741     bool startListening();
742     void stopListening(const QString &msg = QString());
743     void incomingConnection();
744     void clientDisconnected();
745
746     bool initStorage(const QString &backend, const QVariantMap &settings,
747                      const QProcessEnvironment &environment, bool loadFromEnvironment,
748                      bool setup = false);
749     bool initAuthenticator(const QString &backend, const QVariantMap &settings,
750                            const QProcessEnvironment &environment, bool loadFromEnvironment,
751                            bool setup = false);
752
753     void socketError(QAbstractSocket::SocketError err, const QString &errorString);
754     void setupClientSession(RemotePeer *, UserId);
755
756     bool changeUserPass(const QString &username);
757
758 private:
759     SessionThread *sessionForUser(UserId userId, bool restoreState = false);
760     void addClientHelper(RemotePeer *peer, UserId uid);
761     //void processCoreSetup(QTcpSocket *socket, QVariantMap &msg);
762     QString setupCoreForInternalUsage();
763     void setupInternalClientSession(QPointer<InternalPeer> peer);
764
765     bool createUser();
766
767     template<typename Storage>
768     void registerStorageBackend();
769
770     template<typename Authenticator>
771     void registerAuthenticator();
772
773     void registerStorageBackends();
774     void registerAuthenticators();
775
776     DeferredSharedPtr<Storage>       storageBackend(const QString& backendId) const;
777     DeferredSharedPtr<Authenticator> authenticator(const QString& authenticatorId) const;
778
779     bool selectBackend(const QString &backend);
780     bool selectAuthenticator(const QString &backend);
781
782     bool saveBackendSettings(const QString &backend, const QVariantMap &settings);
783     void saveAuthenticatorSettings(const QString &backend, const QVariantMap &settings);
784
785     void saveState();
786     void restoreState();
787
788     template<typename Backend>
789     QVariantMap promptForSettings(const Backend *backend);
790
791 private:
792     static Core *_instance;
793     QSet<CoreAuthHandler *> _connectingClients;
794     QHash<UserId, SessionThread *> _sessions;
795     DeferredSharedPtr<Storage>       _storage;        ///< Active storage backend
796     DeferredSharedPtr<Authenticator> _authenticator;  ///< Active authenticator
797     QMap<UserId, QString> _authUserNames;
798
799     QTimer _storageSyncTimer;
800
801 #ifdef HAVE_SSL
802     SslServer _server, _v6server;
803 #else
804     QTcpServer _server, _v6server;
805 #endif
806
807     OidentdConfigGenerator *_oidentdConfigGenerator {nullptr};
808
809     std::vector<DeferredSharedPtr<Storage>>       _registeredStorageBackends;
810     std::vector<DeferredSharedPtr<Authenticator>> _registeredAuthenticators;
811
812     QDateTime _startTime;
813
814     IdentServer *_identServer {nullptr};
815
816     bool _initialized{false};
817     bool _configured{false};
818
819     QPointer<InternalPeer> _pendingInternalConnection;
820
821     /// Whether or not strict ident mode is enabled, locking users' idents to Quassel username
822     bool _strictIdentEnabled;
823
824     static std::unique_ptr<AbstractSqlMigrationReader> getMigrationReader(Storage *storage);
825     static std::unique_ptr<AbstractSqlMigrationWriter> getMigrationWriter(Storage *storage);
826     static void stdInEcho(bool on);
827     static inline void enableStdInEcho() { stdInEcho(true); }
828     static inline void disableStdInEcho() { stdInEcho(false); }
829 };