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/rjp_string.hpp"
|
||||
#include "raii/filerd.hpp"
|
||||
#include "raii/rjp_ptr.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
@ -120,19 +121,21 @@ namespace matrix{
|
||||
|
||||
struct event_info
|
||||
{
|
||||
const raii::rjp_string& roomid;
|
||||
const raii::rjp_string& sender;
|
||||
const raii::rjp_string& eventid;
|
||||
const raii::rjp_string roomid;
|
||||
const raii::rjp_string sender;
|
||||
const raii::rjp_string eventid;
|
||||
const raii::rjp_string eventtype;
|
||||
const int serverts;
|
||||
const int age;
|
||||
};
|
||||
struct msg_info : public event_info
|
||||
{
|
||||
const msgtype type;
|
||||
const raii::rjp_string& body;
|
||||
const msgtype type = msg::other;
|
||||
const raii::rjp_string body;
|
||||
};
|
||||
struct membership_info : public event_info
|
||||
{
|
||||
const raii::rjp_string& recipient;
|
||||
const raii::rjp_string recipient;
|
||||
};
|
||||
|
||||
//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 membership_info&)> m_membership_callback;
|
||||
std::function<void(const client&, const raii::rjp_ptr&)> m_raw_callback;
|
||||
|
||||
public:
|
||||
client(const auth_data& a, const raii::string_base& useragent);
|
||||
@ -277,6 +281,10 @@ namespace matrix{
|
||||
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);
|
||||
void logout(void);
|
||||
|
||||
|
||||
100
src/matrix.cpp
100
src/matrix.cpp
@ -22,7 +22,6 @@
|
||||
|
||||
#include "raii/curl_llist.hpp"
|
||||
#include "raii/static_string.hpp"
|
||||
#include "raii/rjp_ptr.hpp"
|
||||
#include "raii/filerd.hpp"
|
||||
#include "fat_strings.hpp"
|
||||
#include "raii/util.hpp"
|
||||
@ -459,6 +458,9 @@ namespace matrix{
|
||||
return reply;
|
||||
m_next_batch = res.value;
|
||||
|
||||
if(m_raw_callback != nullptr){
|
||||
m_raw_callback(*this, root);
|
||||
}
|
||||
res = rjp_search_member(root.get(), "rooms", 0);
|
||||
if(res.value){
|
||||
if(m_message_callback != nullptr)
|
||||
@ -482,6 +484,54 @@ namespace matrix{
|
||||
if(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){
|
||||
for(RJP_value* roomid = rjp_get_member(join);roomid;roomid = rjp_next_member(roomid)){
|
||||
RJP_search_res res = rjp_search_member(roomid, "timeline", 0);
|
||||
@ -489,22 +539,11 @@ namespace matrix{
|
||||
res = rjp_search_member(res.value, "events", 0);
|
||||
if(!res.value) continue;
|
||||
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"};
|
||||
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);
|
||||
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;
|
||||
membership_info minfo = _membership_info_from_json(event, roomid);
|
||||
if(!minfo.roomid) continue;
|
||||
if(minfo.eventtype != "m.room.member"_ss) continue;
|
||||
|
||||
raii::rjp_string event_str = results[1].value;
|
||||
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});
|
||||
m_membership_callback(*this, minfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -515,13 +554,9 @@ namespace matrix{
|
||||
res = rjp_search_member(res.value, "events", 0);
|
||||
if(!res.value) continue;
|
||||
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"};
|
||||
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);
|
||||
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)});
|
||||
membership_info minfo = _membership_info_from_json(event, roomid);
|
||||
if(!minfo.roomid) continue;
|
||||
m_membership_callback(*this, minfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -534,23 +569,8 @@ namespace matrix{
|
||||
res = rjp_search_member(res.value, "events", 0);
|
||||
if(!res.value) continue;
|
||||
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"};
|
||||
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);
|
||||
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)});
|
||||
msg_info minfo = _message_info_from_json(event, roomid);
|
||||
m_message_callback(*this, minfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,6 +27,7 @@
|
||||
#include <chrono>
|
||||
|
||||
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
|
||||
{
|
||||
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){
|
||||
sync_reply = client.sync(30000);
|
||||
}
|
||||
@ -50,7 +50,7 @@ void keyboard_fn(matrix::client client, std::atomic_bool& should_quit){
|
||||
should_quit = true;
|
||||
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::client matclient(auth, useragent);
|
||||
auto sync_reply = matclient.sync(0); //initial sync
|
||||
|
||||
std::atomic_bool should_quit = false;
|
||||
std::thread sync_thread(sync_fn, matclient, std::ref(should_quit));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user