// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet Matrix session. // // K-Server mode is Matrix. This class owns the Quotient connection and nothing // else: login, resume, the sync loop and logout. Rooms and messages belong to // MatrixRoomBridge, and the LAN side is untouched. // // the access token is a bearer credential, kept in the secret store, never in // the config. The Olm pickle key is the exception: libQuotient stores it through // QtKeychain under the application name, which is why that name must never // change. Two rules the states keep: no failure is silent, and every // non-failure state has a deadline; logout clears locally first. #pragma once #include #include #include #include namespace Quotient { class Connection; } namespace koutnet { class AppSettings; class MatrixManager : public QObject { Q_OBJECT // booleans and a status string instead of an enum, since this is a context // property and QML could not name the enumerators anyway Q_PROPERTY(bool loggedIn READ loggedIn NOTIFY stateChanged) Q_PROPERTY(bool busy READ busy NOTIFY stateChanged) // full MXID of the signed-in account, empty when signed out Q_PROPERTY(QString userId READ userId NOTIFY stateChanged) Q_PROPERTY(QString avatarUrl READ avatarUrl NOTIFY profileChanged) Q_PROPERTY(QString displayName READ displayName NOTIFY profileChanged) Q_PROPERTY(QString homeserver READ homeserver NOTIFY stateChanged) Q_PROPERTY(QString statusText READ statusText NOTIFY stateChanged) Q_PROPERTY(QString lastError READ lastError NOTIFY stateChanged) // whether E2EE is actually available, not just requested. read this before // showing a padlock; false with no connection or a failed key store Q_PROPERTY(bool encryptionActive READ encryptionActive NOTIFY encryptionActiveChanged) public: enum class State { LoggedOut, Connecting, Syncing, Online, // was Online and the sync loop is now failing; the token may still be good, // but the interface must stop claiming everything arrives Reconnecting, Failed, }; Q_ENUM(State) // a homeserver that never answers would say "Connecting" forever, so cap it static constexpr int kLoginTimeoutMs = 30000; // syncing needs its own, longer deadline; a resumed or blocked session used // to sit on "Syncing..." until the process died static constexpr int kSyncTimeoutMs = 60000; // keep the connection alive this long after a login timeout, so in-flight // jobs finish on their own static constexpr int kLoginAbandonMs = 60000; // wait this long for the homeserver to confirm a logout before dropping it static constexpr int kLogoutGraceMs = 15000; explicit MatrixManager(AppSettings *settings, QObject *parent = nullptr); ~MatrixManager() override; bool loggedIn() const; bool busy() const; bool encryptionActive() const; // The settings object the session was built with, so the transport reads a // preference without a second copy. Defined out of line: QPointer needs it. AppSettings *settings() const; QString userId() const; QString avatarUrl() const; QString displayName() const; QString homeserver() const; QString statusText() const; QString lastError() const { return m_lastError; } // null until login or resume; MatrixRoomBridge listens for connectionChanged Quotient::Connection *connection() const { return m_connection; } // homeserverUrl may be empty when a full MXID is given; libQuotient then // resolves the server from the domain Q_INVOKABLE void login(const QString &homeserverUrl, const QString &userIdOrLocalpart, const QString &password); Q_INVOKABLE void logout(); // A bearer token from SSO or a bot account. The MXID is inside the token, so // the homeserver cannot be read off the user name; the caller passes it, and // it must be set before this, since libQuotient will not resolve from a token. Q_INVOKABLE void loginWithToken(const QString &homeserverUrl, const QString &token); // the URL the SSO flow opens in a browser; the homeserver answers with a // loginToken turned into a session by loginWithToken(). A random state in the // redirect ties the callback to the attempt that started it. Q_INVOKABLE QString ssoLoginUrl(const QString &homeserverUrl); // Opens ssoLoginUrl() in the browser and wires the koutnet:// redirect back // here, so a successful SSO hands us the token without a copy and paste. Q_INVOKABLE void startSsoLogin(const QString &homeserverUrl); // the second half of SSO when the OS does not route koutnet:// back, and the // manual path for any login token. A pasted token needs no state check - it // never came through the OS routing. Q_INVOKABLE void completeSsoLogin(const QString &token); Q_INVOKABLE void setDisplayName(const QString &displayName); Q_INVOKABLE void setAvatar(const QString &localFilePath); // The banner is Matrix's missing profile field: uploaded once, then sent as // an im.koutnet.banner message into every joined room. The bridge keeps it // off the timeline; a profile card asks the room for the newest one. Q_INVOKABLE void setBanner(const QString &localFilePath); // whether the account keeps an encrypted copy of its keys on the homeserver. // cheap to read and safe to call twice, so the UI can show "restore keys" Q_INVOKABLE bool keyBackupAvailable() const; // true once keyBackupUnlocked() fired; the is- prefix avoids clashing with // the signal of the same name, which would break insert() Q_INVOKABLE bool isKeyBackupUnlocked() const; // tries the typed string as a recovery key first, then as a passphrase. // both go to the homeserver, so the result comes back by signal Q_INVOKABLE void unlockKeyBackup(const QString &recoveryKeyOrPassphrase); // reads the session saved last run; false when there is nothing to resume bool resumeSession(); Q_SIGNALS: void stateChanged(); void profileChanged(); // the connection object changed (not just its state); holders must re-read it void connectionChanged(); // encryptionActive() changed; separate from stateChanged since E2EE settles // after connect and before Online void encryptionActiveChanged(); // a session with no store; the UI must say so instead of implying persistence void sessionNotPersisted(QString reason); // one sentence per failure; statusText shows the same words on the sign-in // page, this is for the main window void sessionError(QString message); // the backed-up room keys were decrypted and loaded; the UI shows a toast void keyBackupUnlocked(); // the typed string did not unlock the backup (wrong key, or none behind it) void keyBackupFailed(QString reason); // the account has no cross-signing keys yet; the UI should offer to set // them up so other devices and users can verify this session void crossSigningRequired(); private: void setState(State state, const QString &error = QString()); Quotient::Connection *makeConnection(); // closes the connection and logs that it was us; libQuotient blames the // network for aborted jobs, so we log the real reason first void retireConnection(const QString &why); void onConnected(); // the OS hands a koutnet://sso?loginToken=... click here once the browser // flow is done; the state is checked before the token is used. void onSsoRedirect(const QUrl &url); void armSyncDeadline(); void onSyncDeadline(); void storeSession(); void clearStoredSession(); void retellBackupAvailability(); QPointer m_settings; Quotient::Connection *m_connection = nullptr; State m_state = State::LoggedOut; QString m_lastError; QTimer m_loginTimeout; // fires when nothing has synced for kSyncTimeoutMs; separate from the login // deadline since a resume hands straight from one to the other QTimer m_syncTimeout; // the last sync error, kept so the deadline can report the real reason QString m_lastSyncError; // a resumed session is not proven by connected(); the first completed sync is, // and this says we are still waiting for it bool m_resuming = false; // whether the room keys were already loaded from backup; kept here because // the handler that did it is gone by the time the UI asks bool m_keyBackupUnlocked = false; // set while login() waits for the server; the password is cleared afterwards QString m_pendingUser; QString m_pendingPassword; // the homeserver startSsoLogin() opened a browser against, remembered so the // redirect handler knows where the returned token belongs QString m_ssoHomeserver; // the state embedded in the redirect of the pending SSO attempt, verified // against the callback before any token is accepted; single use QString m_ssoState; }; } // namespace koutnet