diff --git a/include/matrix/client.hpp b/include/matrix/client.hpp index d376a7b..dc36b7a 100644 --- a/include/matrix/client.hpp +++ b/include/matrix/client.hpp @@ -25,7 +25,6 @@ #include "raii/filerd.hpp" #include "matrix/session_info.hpp" #include "matrix/upload_info.hpp" -#include "matrix/event_info.hpp" #include "matrix/client_base.hpp" #include //vector #include //shared_ptr @@ -70,8 +69,10 @@ namespace matrix{ bool join_room(const raii::string_base& roomid)const; bool leave_room(const raii::string_base& roomid)const; - bool accept_invite(const membership_info& invite)const; - bool reject_invite(const membership_info& invite)const; + [[deprecated("Use client::join_room instead")]] + bool accept_invite(const raii::string_base& roomid)const; + [[deprecated("Use client::leave_room instead")]] + bool reject_invite(const raii::string_base& roomid)const; //other network void logout(void); diff --git a/include/matrix/event_info.hpp b/include/matrix/event_info.hpp deleted file mode 100644 index a98c0a7..0000000 --- a/include/matrix/event_info.hpp +++ /dev/null @@ -1,103 +0,0 @@ -/** - 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 . -*/ - -#ifndef MATRIX_EVENT_INFO_HPP -#define MATRIX_EVENT_INFO_HPP - -#include "raii/rjp_string.hpp" -#include //strcmp - -namespace matrix{ - //message handling structs - //enumerate message type but also give a string representation - class msgtype{ - private: - const char* m_str; - const int m_num; - public: - constexpr msgtype(const char* s, int n): - m_str(s), m_num(n){} - constexpr msgtype(const msgtype&) = default; - ~msgtype(void) = default; - constexpr msgtype& operator=(const msgtype&) = default; - - constexpr bool operator==(const msgtype& m){ - return m_num == m.m_num; - } - constexpr bool operator!=(const msgtype& m){ - return m_num != m.m_num; - } - constexpr const char* str(void)const{ - return m_str; - } - constexpr operator int(void)const{ - return m_num; - } - }; - //class enumeration - struct msg{ - private: - enum _types{ - _text, _audio, _video, _file, _image, _other - }; - public: - constexpr static msgtype text = msgtype("text", _text); - constexpr static msgtype audio = msgtype("audio", _audio); - constexpr static msgtype video = msgtype("video", _video); - constexpr static msgtype file = msgtype("file", _file); - constexpr static msgtype image = msgtype("image", _image); - constexpr static msgtype other = msgtype("other", _other); - - constexpr static const msgtype& from_str(const char* str){ - if(!strcmp(str, "m.text")){ - return text; - }else if(!strcmp(str, "m.audio")){ - return audio; - }else if(!strcmp(str, "m.video")){ - return video; - }else if(!strcmp(str, "m.file")){ - return file; - }else if(!strcmp(str, "m.image")){ - return image; - }else{ - return other; - } - } - }; - - struct event_info - { - 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 = msg::other; - const raii::rjp_string body; - }; - struct membership_info : public event_info - { - const raii::rjp_string recipient; - }; -} - -#endif diff --git a/include/matrix/session.hpp b/include/matrix/session.hpp index 236f8dc..9f3c159 100644 --- a/include/matrix/session.hpp +++ b/include/matrix/session.hpp @@ -45,6 +45,8 @@ namespace matrix{ syncer& get_syncer(void); const syncer& get_syncer(void)const; + bool valid(void)const; + void invalidate(void); private: void _populate_session_info(const auth_data& a); diff --git a/include/matrix/syncer.hpp b/include/matrix/syncer.hpp index 60f7613..bdaa5af 100644 --- a/include/matrix/syncer.hpp +++ b/include/matrix/syncer.hpp @@ -20,15 +20,11 @@ #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 //size_t -#include //forward -#include //function #include //shared_ptr namespace matrix{ @@ -38,10 +34,6 @@ namespace matrix{ friend class ::matrix::session; private: const std::shared_ptr m_ses; - - std::function m_message_callback; - std::function m_membership_callback; - std::function m_raw_callback; raii::rjp_string m_next_batch; //string which tracks where we are in the server history public: @@ -53,25 +45,7 @@ namespace matrix{ syncer& operator=(const syncer&) = default; syncer& operator=(syncer&&) = default; - template - void set_message_callback(Func&& f){ - m_message_callback = std::forward(f); - } - template - void set_membership_callback(Func&& f){ - m_membership_callback = std::forward(f); - } - template - void set_raw_callback(Func&& f){ - m_raw_callback = std::forward(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); }; } diff --git a/src/matrix/client.cpp b/src/matrix/client.cpp index 4338e05..9cf1ec8 100644 --- a/src/matrix/client.cpp +++ b/src/matrix/client.cpp @@ -105,11 +105,11 @@ namespace matrix{ bool client::leave_room(const raii::string_base& roomid)const{ return _post_curl(raii::string(), m_ses->urls.leave_room(m_ses->homeserver, m_ses->access_token, m_curl.encode(roomid)), raii::curl_llist()); } - bool client::accept_invite(const membership_info& invite)const{ - return join_room(invite.roomid); + bool client::accept_invite(const raii::string_base& roomid)const{ + return join_room(roomid); } - bool client::reject_invite(const membership_info& invite)const{ - return leave_room(invite.roomid); + bool client::reject_invite(const raii::string_base& roomid)const{ + return leave_room(roomid); } //other network diff --git a/src/matrix/session.cpp b/src/matrix/session.cpp index 7f0cc5c..d62eb32 100644 --- a/src/matrix/session.cpp +++ b/src/matrix/session.cpp @@ -53,6 +53,10 @@ namespace matrix{ return m_sync; } + bool session::valid(void)const{ + return m_ses->access_token; + } + void session::invalidate(void){ m_ses->useragent.reset(); m_ses->homeserver.reset(); diff --git a/src/matrix/syncer.cpp b/src/matrix/syncer.cpp index 97e3557..6c4146d 100644 --- a/src/matrix/syncer.cpp +++ b/src/matrix/syncer.cpp @@ -22,7 +22,6 @@ #include "raii/util.hpp" namespace matrix{ - syncer::syncer(std::shared_ptr& ses): client_base(), m_ses(ses){} @@ -41,116 +40,6 @@ namespace matrix{ return reply; m_next_batch = res.value; - if(m_raw_callback != nullptr){ - m_raw_callback(root); - } - res = rjp_search_member(root.get(), "rooms", 0); - if(res.value){ - if(m_message_callback != nullptr) - _handle_messages(res.value); - if(m_membership_callback != nullptr) - _handle_membership_events(res.value); - } return reply; } - - - 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 syncer::_handle_membership_events(RJP_value* rooms){ - RJP_search_res res = rjp_search_member(rooms, "invite", 0); - if(res.value) - _handle_invites(res.value); - res = rjp_search_member(rooms, "join", 0); - if(res.value) - _handle_other_membership(res.value); - } - void syncer::_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); - if(!res.value) continue; - 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)){ - membership_info minfo = _membership_info_from_json(event, roomid); - if(!minfo.roomid) continue; - if(minfo.eventtype != "m.room.member"_ss) continue; - - m_membership_callback(minfo); - } - } - } - void syncer::_handle_invites(RJP_value* invites){ - for(RJP_value* roomid = rjp_get_member(invites);roomid;roomid = rjp_next_member(roomid)){ - RJP_search_res res = rjp_search_member(roomid, "invite_state", 0); - if(!res.value) continue; - 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)){ - membership_info minfo = _membership_info_from_json(event, roomid); - if(!minfo.roomid) continue; - m_membership_callback(minfo); - } - } - } - void syncer::_handle_messages(RJP_value* messages){ - RJP_search_res res = rjp_search_member(messages, "join", 0); - if(!res.value) return; - for(RJP_value* roomid = rjp_get_member(res.value);roomid;roomid = rjp_next_member(roomid)){ - res = rjp_search_member(roomid, "timeline", 0); - if(!res.value) continue; - 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)){ - msg_info minfo = _message_info_from_json(event, roomid); - m_message_callback(minfo); - } - } - } } diff --git a/src/test.cpp b/src/test.cpp index 0fb3b39..415f715 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -29,15 +29,9 @@ void sync_fn(matrix::syncer& syn, std::atomic_bool& should_quit){ auto sync_reply = syn.sync(0); - syn.set_message_callback([&](const matrix::msg_info& msg)->void{ - printf("%s\n", msg.body.get()); - if(!strcmp(msg.body.get(), "!exit")){ - should_quit = true; - } - }); - while(!should_quit){ sync_reply = syn.sync(30000); + printf("%s\n", sync_reply.get()); } } void keyboard_fn(matrix::client& client, std::atomic_bool& should_quit){ @@ -60,6 +54,11 @@ int main(){ matrix::auth_data auth{username, password, homeserver, useragent}; matrix::session ses(auth); + if(!ses.valid()){ + fprintf(stderr, "Failed to init matrix session!\n"); + return 1; + } + fprintf(stderr, "Succ\n"); std::atomic_bool should_quit = false; std::thread sync_thread(sync_fn, std::ref(ses.get_syncer()), std::ref(should_quit));