/** This file is a part of rexy's matrix client Copyright (C) 2019-2020 rexy712 This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ #ifndef MATRIX_SESSION_HPP #define MATRIX_SESSION_HPP #include "matrix/client.hpp" #include "matrix/session_info.hpp" #include "matrix/auth.hpp" #include "raii/curler.hpp" #include "raii/string.hpp" #include "matrix/connection.hpp" #include //pair namespace matrix{ //Manages connection to homeserver and user login. //Shares states with any spawned clients and syncers. class session : public connection { private: bool m_valid = false; public: /* * Create a session object that does not initialize a connection to a homeserver * User will need to create a connection manually through the local setter interface, then call login() */ session(void); /* * Create a session object that will attempt to initialize a connection to a homeserver * using the details in the auth_data structure. If the access token given is invalid, a * new one will be retrieved. Use valid() to check if the session is successfully created. */ session(const auth_data&); //Copy and move constructors session(const session&) = default; session(session&&) = default; /* * NOT thread safe. * Invalidates the session. All spawned clients and syncers will be invalidated as well. */ ~session(void); //Copy and move assignment session& operator=(const session&) = default; session& operator=(session&&) = default; /* * NOT thread safe. * Set the useragent for the session and all spawned clients and syncers. */ void set_useragent(const raii::string_base&); void set_useragent(raii::string&&); /* * NOT thread safe. * Set the access token for the session and all spawned clients and syncers. * Note this will not check if the access token is valid or not, nor will urls be updated. */ void set_access_token(const raii::string_base&); void set_access_token(raii::string&&); /* * NOT thread safe. * Set the homeserver for the session and all spawned clients and syncers. * Note this will not check if the homeserver is valid or not, nor will urs be updated. */ void set_homeserver(const raii::string_base&); void set_homeserver(raii::string&&); /* * NOT thread safe. * Manually log in to a homeserver using the current access token. * This will regenerate the urls and ensure access token and homeserver are valid. * Returns: true if success, false otherwise. */ netreturn login(void); /* * NOT thread safe. * Manually log in to a homeserver first using the current access token, then trying the * username and password as a backup. This will regenerate the urls and ensure access token and * homeserver are valid. * Returns: true if success, false otherwise. */ netreturn login(const raii::string_base& username, const raii::string_base& pass); /* * Logout of the current connection to the homeserver. Will invalidate the access token on the server side. * Returns: true if the logout was successful, false otherwise. */ netreturn logout(void); /* * Returns: true if the session is successfully connected to the homeserver. */ bool valid(void)const; netreturn change_password(const raii::string_base& oldpass, const raii::string_base& newpass); /* * Create a matrix::client which shares login details with this session. * Note the client and session will share memory for generated urls, homeserver, access_token, and * useragent. Changing any of these fields after spawning a client MUST be synchronized across threads. * Returns: a new matrix::client. */ client spawn_client(void)const; /* * NOT thread safe. * Invalidates the session and all spawned clients and syncers. * All generated urls will be discarded; homeserver, access token, and useragent will be discarded; * and valid() will return false. */ void invalidate(void); private: netreturn _do_login(const raii::string_base& username, const raii::string_base& pass); void _populate_session_info(const auth_data& a); netreturn _get_userid(void); raii::string _request_access_token(const raii::string_base& name, const raii::string_base& pass, const raii::string_base& loginurl)const; netreturn> _get_new_access_token(const raii::string_base& name, const raii::string_base& pass, const raii::string_base& loginurl); }; } #endif