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