added raw callback, cleanup up other callbacks a bit
This commit is contained in:
parent
74c8948139
commit
02a43ba1b9
@ -23,6 +23,7 @@
|
|||||||
#include "raii/string.hpp"
|
#include "raii/string.hpp"
|
||||||
#include "raii/rjp_string.hpp"
|
#include "raii/rjp_string.hpp"
|
||||||
#include "raii/filerd.hpp"
|
#include "raii/filerd.hpp"
|
||||||
|
#include "raii/rjp_ptr.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
@ -120,19 +121,21 @@ namespace matrix{
|
|||||||
|
|
||||||
struct event_info
|
struct event_info
|
||||||
{
|
{
|
||||||
const raii::rjp_string& roomid;
|
const raii::rjp_string roomid;
|
||||||
const raii::rjp_string& sender;
|
const raii::rjp_string sender;
|
||||||
const raii::rjp_string& eventid;
|
const raii::rjp_string eventid;
|
||||||
|
const raii::rjp_string eventtype;
|
||||||
|
const int serverts;
|
||||||
const int age;
|
const int age;
|
||||||
};
|
};
|
||||||
struct msg_info : public event_info
|
struct msg_info : public event_info
|
||||||
{
|
{
|
||||||
const msgtype type;
|
const msgtype type = msg::other;
|
||||||
const raii::rjp_string& body;
|
const raii::rjp_string body;
|
||||||
};
|
};
|
||||||
struct membership_info : public event_info
|
struct membership_info : public event_info
|
||||||
{
|
{
|
||||||
const raii::rjp_string& recipient;
|
const raii::rjp_string recipient;
|
||||||
};
|
};
|
||||||
|
|
||||||
//main class
|
//main class
|
||||||
@ -209,6 +212,7 @@ namespace matrix{
|
|||||||
|
|
||||||
std::function<void(const client&,const msg_info&)> m_message_callback;
|
std::function<void(const client&,const msg_info&)> m_message_callback;
|
||||||
std::function<void(const client&, const membership_info&)> m_membership_callback;
|
std::function<void(const client&, const membership_info&)> m_membership_callback;
|
||||||
|
std::function<void(const client&, const raii::rjp_ptr&)> m_raw_callback;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
client(const auth_data& a, const raii::string_base& useragent);
|
client(const auth_data& a, const raii::string_base& useragent);
|
||||||
@ -277,6 +281,10 @@ namespace matrix{
|
|||||||
void set_membership_callback(Func&& f){
|
void set_membership_callback(Func&& f){
|
||||||
m_membership_callback = std::forward<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);
|
raii::string sync(size_t timeout);
|
||||||
void logout(void);
|
void logout(void);
|
||||||
|
|
||||||
|
|||||||
100
src/matrix.cpp
100
src/matrix.cpp
@ -22,7 +22,6 @@
|
|||||||
|
|
||||||
#include "raii/curl_llist.hpp"
|
#include "raii/curl_llist.hpp"
|
||||||
#include "raii/static_string.hpp"
|
#include "raii/static_string.hpp"
|
||||||
#include "raii/rjp_ptr.hpp"
|
|
||||||
#include "raii/filerd.hpp"
|
#include "raii/filerd.hpp"
|
||||||
#include "fat_strings.hpp"
|
#include "fat_strings.hpp"
|
||||||
#include "raii/util.hpp"
|
#include "raii/util.hpp"
|
||||||
@ -459,6 +458,9 @@ namespace matrix{
|
|||||||
return reply;
|
return reply;
|
||||||
m_next_batch = res.value;
|
m_next_batch = res.value;
|
||||||
|
|
||||||
|
if(m_raw_callback != nullptr){
|
||||||
|
m_raw_callback(*this, root);
|
||||||
|
}
|
||||||
res = rjp_search_member(root.get(), "rooms", 0);
|
res = rjp_search_member(root.get(), "rooms", 0);
|
||||||
if(res.value){
|
if(res.value){
|
||||||
if(m_message_callback != nullptr)
|
if(m_message_callback != nullptr)
|
||||||
@ -482,6 +484,54 @@ namespace matrix{
|
|||||||
if(res.value)
|
if(res.value)
|
||||||
_handle_other_membership(res.value);
|
_handle_other_membership(res.value);
|
||||||
}
|
}
|
||||||
|
static membership_info _membership_info_from_json(RJP_value* event, RJP_value* roomid){
|
||||||
|
static constexpr const char* search_terms[] = {"event_id", "sender", "state_key", "unsigned", "type", "origin_server_ts"};
|
||||||
|
static constexpr size_t num_searches = sizeof(search_terms)/sizeof(search_terms[0]);
|
||||||
|
RJP_search_res results[num_searches] = {};
|
||||||
|
rjp_search_members(event, num_searches, search_terms, results, 0);
|
||||||
|
for(size_t i = 0;i < num_searches;++i){
|
||||||
|
if(!results[i].value)
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
RJP_search_res age = rjp_search_member(results[3].value, "age", 0);
|
||||||
|
if(!age.value) return {};
|
||||||
|
|
||||||
|
raii::rjp_string room_str = rjp_member_name(roomid);
|
||||||
|
raii::rjp_string sender_str = results[1].value;
|
||||||
|
raii::rjp_string event_str = results[0].value;
|
||||||
|
raii::rjp_string eventtype_str = results[4].value;
|
||||||
|
raii::rjp_string rec_str = results[2].value;
|
||||||
|
|
||||||
|
return membership_info{std::move(room_str), std::move(sender_str),
|
||||||
|
std::move(event_str), std::move(eventtype_str),
|
||||||
|
rjp_value_integer(results[5].value), rjp_value_integer(age.value),
|
||||||
|
std::move(rec_str)};
|
||||||
|
}
|
||||||
|
static msg_info _message_info_from_json(RJP_value* event, RJP_value* roomid){
|
||||||
|
static constexpr const char* searches[] = {"sender", "content", "event_id", "unsigned", "type", "origin_server_ts"};
|
||||||
|
static constexpr size_t num_searches = sizeof(searches)/sizeof(searches[0]);
|
||||||
|
RJP_search_res results[num_searches] = {};
|
||||||
|
rjp_search_members(event, num_searches, searches, results, 0);
|
||||||
|
for(size_t i = 0;i < num_searches;++i){
|
||||||
|
if(!results[i].value)
|
||||||
|
return msg_info{};
|
||||||
|
}
|
||||||
|
RJP_search_res msg = rjp_search_member(results[1].value, "msgtype", 0);
|
||||||
|
if(!msg.value) return {};
|
||||||
|
RJP_search_res body = rjp_search_member(results[1].value, "body", 0);
|
||||||
|
if(!body.value) return {};
|
||||||
|
RJP_search_res age = rjp_search_member(results[3].value, "age", 0);
|
||||||
|
if(!age.value) return msg_info{};
|
||||||
|
raii::rjp_string room_str = rjp_member_name(roomid);
|
||||||
|
raii::rjp_string sender_str = results[0].value;
|
||||||
|
raii::rjp_string eventid_str = results[2].value;
|
||||||
|
raii::rjp_string eventtype_str = results[3].value;
|
||||||
|
raii::rjp_string msgbody_str = body.value;
|
||||||
|
return msg_info{std::move(room_str), std::move(sender_str),
|
||||||
|
std::move(eventid_str), std::move(eventtype_str),
|
||||||
|
rjp_value_integer(results[5].value), rjp_value_integer(age.value),
|
||||||
|
msg::from_str(rjp_value_string(msg.value)), std::move(msgbody_str)};
|
||||||
|
}
|
||||||
void client::_handle_other_membership(RJP_value* join){
|
void client::_handle_other_membership(RJP_value* join){
|
||||||
for(RJP_value* roomid = rjp_get_member(join);roomid;roomid = rjp_next_member(roomid)){
|
for(RJP_value* roomid = rjp_get_member(join);roomid;roomid = rjp_next_member(roomid)){
|
||||||
RJP_search_res res = rjp_search_member(roomid, "timeline", 0);
|
RJP_search_res res = rjp_search_member(roomid, "timeline", 0);
|
||||||
@ -489,22 +539,11 @@ namespace matrix{
|
|||||||
res = rjp_search_member(res.value, "events", 0);
|
res = rjp_search_member(res.value, "events", 0);
|
||||||
if(!res.value) continue;
|
if(!res.value) continue;
|
||||||
for(RJP_value* event = rjp_get_element(res.value);event;event = rjp_next_element(event)){
|
for(RJP_value* event = rjp_get_element(res.value);event;event = rjp_next_element(event)){
|
||||||
static constexpr const char* search_terms[] = {"content", "event_id", "sender", "state_key", "unsigned"};
|
membership_info minfo = _membership_info_from_json(event, roomid);
|
||||||
static constexpr size_t num_searches = sizeof(search_terms)/sizeof(search_terms[0]);
|
if(!minfo.roomid) continue;
|
||||||
RJP_search_res results[num_searches] = {};
|
if(minfo.eventtype != "m.room.member"_ss) continue;
|
||||||
rjp_search_members(event, num_searches, search_terms, results, 0);
|
|
||||||
if(!results[0].value || !results[1].value || !results[2].value || !results[3].value || !results[4].value) continue;
|
|
||||||
RJP_search_res age = rjp_search_member(results[4].value, "age", 0);
|
|
||||||
if(!age.value) continue;
|
|
||||||
RJP_search_res type = rjp_search_member(results[0].value, "membership", 0);
|
|
||||||
if(!type.value) continue;
|
|
||||||
|
|
||||||
raii::rjp_string event_str = results[1].value;
|
m_membership_callback(*this, minfo);
|
||||||
raii::rjp_string sender_str = results[2].value;
|
|
||||||
raii::rjp_string rec_str = results[3].value;
|
|
||||||
raii::rjp_string room_str = rjp_member_name(roomid);
|
|
||||||
|
|
||||||
m_membership_callback(*this, membership_info{room_str, sender_str, event_str, rjp_value_integer(age.value), rec_str});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -515,13 +554,9 @@ namespace matrix{
|
|||||||
res = rjp_search_member(res.value, "events", 0);
|
res = rjp_search_member(res.value, "events", 0);
|
||||||
if(!res.value) continue;
|
if(!res.value) continue;
|
||||||
for(RJP_value* event = rjp_get_element(res.value);event;event = rjp_next_element(event)){
|
for(RJP_value* event = rjp_get_element(res.value);event;event = rjp_next_element(event)){
|
||||||
static constexpr const char* search_terms[] = {"event_id", "sender", "state_key", "unsigned"};
|
membership_info minfo = _membership_info_from_json(event, roomid);
|
||||||
static constexpr size_t num_searches = sizeof(search_terms)/sizeof(search_terms[0]);
|
if(!minfo.roomid) continue;
|
||||||
RJP_search_res results[num_searches] = {};
|
m_membership_callback(*this, minfo);
|
||||||
rjp_search_members(event, num_searches, search_terms, results, 0);
|
|
||||||
if(!results[0].value || !results[1].value || !results[2].value || !results[3].value) continue;
|
|
||||||
raii::rjp_string room_str = rjp_member_name(roomid);
|
|
||||||
m_membership_callback(*this, membership_info{room_str, raii::rjp_string(results[1].value), raii::rjp_string(results[0].value), rjp_value_integer(results[3].value), raii::rjp_string(results[2].value)});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -534,23 +569,8 @@ namespace matrix{
|
|||||||
res = rjp_search_member(res.value, "events", 0);
|
res = rjp_search_member(res.value, "events", 0);
|
||||||
if(!res.value) continue;
|
if(!res.value) continue;
|
||||||
for(RJP_value* event = rjp_get_element(res.value);event;event = rjp_next_element(event)){
|
for(RJP_value* event = rjp_get_element(res.value);event;event = rjp_next_element(event)){
|
||||||
static constexpr const char* searches[] = {"sender", "content", "event_id", "unsigned"};
|
msg_info minfo = _message_info_from_json(event, roomid);
|
||||||
static constexpr size_t num_searches = sizeof(searches)/sizeof(searches[0]);
|
m_message_callback(*this, minfo);
|
||||||
RJP_search_res results[num_searches] = {};
|
|
||||||
rjp_search_members(event, num_searches, searches, results, 0);
|
|
||||||
if(!results[0].value || !results[1].value || !results[2].value || !results[3].value) continue;
|
|
||||||
if(!strcmp(rjp_value_string(results[0].value), m_userid)) continue;
|
|
||||||
RJP_search_res msg = rjp_search_member(results[1].value, "msgtype", 0);
|
|
||||||
if(!msg.value) continue;
|
|
||||||
RJP_search_res body = rjp_search_member(results[1].value, "body", 0);
|
|
||||||
if(!body.value) continue;
|
|
||||||
RJP_search_res age = rjp_search_member(results[3].value, "age", 0);
|
|
||||||
if(!age.value) continue;
|
|
||||||
raii::rjp_string sender_str = results[0].value;
|
|
||||||
raii::rjp_string room_str = rjp_member_name(roomid);
|
|
||||||
raii::rjp_string eventid_str = results[2].value;
|
|
||||||
_send_read_receipt(room_str, eventid_str);
|
|
||||||
m_message_callback(*this, msg_info{room_str, sender_str, eventid_str, rjp_value_integer(age.value), msg::from_str(rjp_value_string(msg.value)), raii::rjp_string(body.value)});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -27,6 +27,7 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
void sync_fn(matrix::client client, std::atomic_bool& should_quit){
|
void sync_fn(matrix::client client, std::atomic_bool& should_quit){
|
||||||
|
auto sync_reply = client.sync(0);
|
||||||
client.set_message_callback([&](const matrix::client& client, const matrix::msg_info& msg)->void
|
client.set_message_callback([&](const matrix::client& client, const matrix::msg_info& msg)->void
|
||||||
{
|
{
|
||||||
if(msg.body == "!exit"_ss){
|
if(msg.body == "!exit"_ss){
|
||||||
@ -37,7 +38,6 @@ void sync_fn(matrix::client client, std::atomic_bool& should_quit){
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
auto sync_reply = client.sync(0);
|
|
||||||
while(!should_quit){
|
while(!should_quit){
|
||||||
sync_reply = client.sync(30000);
|
sync_reply = client.sync(30000);
|
||||||
}
|
}
|
||||||
@ -50,7 +50,7 @@ void keyboard_fn(matrix::client client, std::atomic_bool& should_quit){
|
|||||||
should_quit = true;
|
should_quit = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
client.send_message("!QbNLZNFSsqUXqQwtJp:rexy712.chickenkiller.com"_ss , raii::static_string(buffer));
|
client.send_message("!cEIFONpAlHTbGBUFAE:rexy712.chickenkiller.com"_ss , raii::static_string(buffer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,7 +62,6 @@ int main(){
|
|||||||
matrix::auth_data auth{username, password, homeserver};
|
matrix::auth_data auth{username, password, homeserver};
|
||||||
|
|
||||||
matrix::client matclient(auth, useragent);
|
matrix::client matclient(auth, useragent);
|
||||||
auto sync_reply = matclient.sync(0); //initial sync
|
|
||||||
|
|
||||||
std::atomic_bool should_quit = false;
|
std::atomic_bool should_quit = false;
|
||||||
std::thread sync_thread(sync_fn, matclient, std::ref(should_quit));
|
std::thread sync_thread(sync_fn, matclient, std::ref(should_quit));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user