79 lines
2.4 KiB
C++

/**
This file is a part of rexy's matrix bot
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_SYNCER_HPP
#define MATRIX_SYNCER_HPP
#include "raii/curler.hpp"
#include "raii/rjp_ptr.hpp"
#include "raii/rjp_string.hpp"
#include "raii/string.hpp"
#include "matrix/event_info.hpp"
#include "matrix/session_info.hpp"
#include "matrix/client_base.hpp"
#include <cstdlib> //size_t
#include <utility> //forward
#include <functional> //function
#include <memory> //shared_ptr
namespace matrix{
class session;
class syncer : public internal::client_base
{
friend class ::matrix::session;
private:
const std::shared_ptr<internal::session_info> m_ses;
std::function<void(const msg_info&)> m_message_callback;
std::function<void(const membership_info&)> m_membership_callback;
std::function<void(const raii::rjp_ptr&)> m_raw_callback;
raii::rjp_string m_next_batch; //string which tracks where we are in the server history
public:
syncer(std::shared_ptr<internal::session_info>&);
syncer(const syncer& b) = default;
syncer(syncer&& b) = default;
~syncer(void) = default;
syncer& operator=(const syncer&) = default;
syncer& operator=(syncer&&) = default;
template<class Func>
void set_message_callback(Func&& f){
m_message_callback = std::forward<Func>(f);
}
template<class Func>
void set_membership_callback(Func&& f){
m_membership_callback = std::forward<Func>(f);
}
template<class Func>
void set_raw_callback(Func&& f){
m_raw_callback = std::forward<Func>(f);
}
raii::string sync(size_t timeout);
private:
void _handle_membership_events(RJP_value* rooms);
void _handle_other_membership(RJP_value* join);
void _handle_invites(RJP_value* invites);
void _handle_messages(RJP_value* messages);
};
}
#endif