matrix_thing/include/matrix/connection.hpp

82 lines
2.8 KiB
C++

/**
This file is a part of r0nk, atlas_moon, and 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_CONNECTION_HPP
#define MATRIX_CONNECTION_HPP
#include "raii/curler.hpp"
#include "raii/string.hpp"
#include "raii/rjp_string.hpp"
#include "matrix/session_info.hpp"
#include <memory> //shared_ptr
namespace matrix{
//Base class of all interactive network classes.
class connection
{
protected:
mutable raii::curler m_curl;
std::shared_ptr<internal::session_info> m_ses;
protected:
connection(const std::shared_ptr<internal::session_info>&);
connection(const connection&) = default;
connection(connection&&) = default;
connection& operator=(const connection&) = default;
connection& operator=(connection&&) = default;
public:
~connection(void) = default;
/*
* NOT thread safe.
* Returns: the current access token.
*/
const raii::rjp_string& access_token(void)const;
/*
* NOT thread safe
* Returns: the logged in user's userid.
*/
const raii::rjp_string& userid(void)const;
/*
* NOT thread safe
* Returns: the current useragent.
*/
const raii::string& useragent(void)const;
/*
* Returns: the http status code of the last operation performed.
*/
long http_status(void)const;
protected:
void _set_curl_useragent(const raii::string_base& useragent);
static size_t _post_reply_curl_callback(char* ptr, size_t size, size_t nmemb, void* userdata);
raii::string _get_curl(const raii::string_base& url)const;
raii::string _post_curl(const raii::string_base& postdata, const raii::string_base& url, const raii::curl_llist& header)const;
raii::string _put_curl(const raii::string_base& postdata, const raii::string_base& url, const raii::curl_llist& header)const;
raii::rjp_string _post_and_find(const raii::string_base& data, const raii::string_base& url, const raii::curl_llist& header, const raii::string_base& target)const;
raii::rjp_string _get_and_find(const raii::string_base& url, const raii::string_base& search)const;
raii::rjp_string _curl_reply_search(const raii::string_base& reply, const raii::string_base& search)const;
void _set_curl_defaults(const raii::string_base& useragent)const;
};
}
#endif