144 lines
5.3 KiB
C++
144 lines
5.3 KiB
C++
/**
|
|
This file is a part of rexy's matrix client
|
|
Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef MATRIX_SESSION_HPP
|
|
#define MATRIX_SESSION_HPP
|
|
|
|
#include "matrix/client.hpp"
|
|
#include "matrix/syncer.hpp"
|
|
#include "matrix/session_info.hpp"
|
|
#include "matrix/auth.hpp"
|
|
#include "raii/curler.hpp"
|
|
#include "raii/string.hpp"
|
|
#include "matrix/connection.hpp"
|
|
#include <utility> //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<void> 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<void> 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<void> logout(void);
|
|
/*
|
|
* Returns: true if the session is successfully connected to the homeserver.
|
|
*/
|
|
bool valid(void)const;
|
|
|
|
/*
|
|
* 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;
|
|
/*
|
|
* Create a matrix::syncer which shares login details with this session.
|
|
* Note the syncer and session will share memory for generated urls, homeserver, access_token, and
|
|
* useragent. Changing any of these fields after spawning a syncer MUST be synchronized across threads.
|
|
* Returns: a new matrix::syncer.
|
|
*/
|
|
syncer spawn_syncer(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<void> _do_login(const raii::string_base& username, const raii::string_base& pass);
|
|
void _populate_session_info(const auth_data& a);
|
|
netreturn<raii::rjp_string> _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<std::pair<raii::rjp_string,raii::rjp_string>> _get_new_access_token(const raii::string_base& name, const raii::string_base& pass, const raii::string_base& loginurl);
|
|
};
|
|
}
|
|
|
|
#endif
|