/** This file is a part of 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 . */ #include "matrix/roomcxn.hpp" #include "raii/static_string.hpp" #include "matrix/fat_strings.hpp" #include "matrix/json_targets.hpp" #include "raii/util.hpp" #include "raii/rjp_ptr.hpp" #include //move namespace matrix{ roomcxn::roomcxn(const std::shared_ptr& ses, const raii::string_base& roomid): roomcxn(ses, raii::string(roomid)){} roomcxn::roomcxn(const std::shared_ptr& ses, raii::string&& roomid): connection(ses), m_roomid(std::move(roomid)), m_urls(ses->homeserver, ses->access_token, m_curl.encode(m_roomid), ses->userid){} netreturn roomcxn::join(void)const{ raii::string response = _post_curl(raii::string(), m_urls.join(m_ses->homeserver, m_ses->access_token, m_roomid), raii::curl_llist()); return _create_netreturn(response, http_status()); } netreturn roomcxn::leave(void)const{ raii::string response = _post_curl(raii::string(), m_urls.leave(m_ses->homeserver, m_ses->access_token, m_roomid), raii::curl_llist()); return _create_netreturn(response, http_status()); } netreturn roomcxn::forget(void)const{ raii::string response = _post_curl(raii::string(), m_urls.forget(m_ses->homeserver, m_ses->access_token, m_roomid), raii::curl_llist()); return _create_netreturn(response, http_status()); } netreturn roomcxn::accept_invite(void)const{ return join(); } netreturn roomcxn::reject_invite(void)const{ return leave(); } netreturn> roomcxn::members(void)const{ netreturn> retval; raii::string resp = _get_curl(m_urls.room_members()); if(!resp) return _create_netreturn(resp, http_status()); raii::rjp_ptr root(rjp_parse(resp.get())); if(!root) return _create_netreturn(root, http_status()); RJP_search_res res = rjp_search_member(root.get(), json::rooms::joined(), 0); if(!res.value) return _create_netreturn(root, http_status()); for(RJP_value* mem = rjp_get_member(res.value);mem;mem = rjp_next_member(mem)){ raii::rjp_string tmp = raii::rjp_string_from_key(mem); retval.value().emplace_back(std::move(tmp)); } return retval; } netreturn roomcxn::invite(const raii::string_base& userid){ raii::string json(json::_userid(userid)); printf("%s\n", json.get()); raii::string response = _post_curl(json, m_urls.invite(), raii::curl_llist()); return _create_netreturn(response, http_status()); } netreturn roomcxn::uninvite(const raii::string_base& userid, const raii::string_base& reason){ return kick(userid, reason); } netreturn roomcxn::kick(const raii::string_base& userid, const raii::string_base& reason){ raii::string response; if(reason){ response = _post_curl(json::_userid_reason(userid, reason), m_urls.kick(), raii::curl_llist()); }else{ response = _post_curl(json::_userid(userid), m_urls.kick(), raii::curl_llist()); } return _create_netreturn(response, http_status()); } netreturn roomcxn::ban(const raii::string_base& userid, const raii::string_base& reason){ raii::string response; if(reason){ response = _post_curl(json::_userid_reason(userid, reason), m_urls.ban(), raii::curl_llist()); }else{ response = _post_curl(json::_userid(userid), m_urls.ban(), raii::curl_llist()); } return _create_netreturn(response, http_status()); } netreturn roomcxn::unban(const raii::string_base& userid){ raii::string response = _post_curl(json::_userid(userid), m_urls.unban(), raii::curl_llist()); return _create_netreturn(response, http_status()); } netreturn roomcxn::send_custom_event(const raii::string_base& event, const raii::string_base& eventtype)const{ raii::string reply = _post_curl(event, m_urls.send(m_ses->homeserver, m_ses->access_token, m_curl.encode(m_roomid), eventtype), raii::curl_llist()); if(!reply) return _create_netreturn(reply, http_status()); raii::rjp_ptr root(rjp_parse(reply)); netreturn retval = _create_netreturn(root, http_status()); retval.value() = _curl_reply_search(root, json::rooms::eventid()); return retval; } netreturn roomcxn::send_message(const raii::string_base& text)const{ return _send_message(json::_message_body(text)); } netreturn roomcxn::send_file(const uploaded_file& file)const{ return _send_message(json::_file_body(file)); } netreturn roomcxn::send_image(const uploaded_image& image)const{ return _send_message(json::_image_body(image)); } netreturn roomcxn::send_video(const uploaded_video& video)const{ return _send_message(json::_video_body(video)); } netreturn roomcxn::send_audio(const uploaded_audio& audio)const{ return _send_message(json::_audio_body(audio)); } netreturn roomcxn::send_typing(bool active, int timeout)const{ return _create_netreturn(_put_curl(json::_typing(active, timeout), m_urls.typing(), raii::curl_llist()), http_status()); } netreturn roomcxn::send_read_receipt(const raii::string_base& eventid)const{ return _create_netreturn(_post_curl(raii::string(), m_urls.read_receipt(m_ses->homeserver, m_ses->access_token, m_curl.encode(m_roomid), m_curl.encode(eventid)), raii::curl_llist()), http_status()); } netreturn roomcxn::redact_event(const raii::string_base& eventid, const raii::string_base& reason)const{ return _put_and_find(json::_redact(reason), m_urls.redact(m_ses->homeserver, m_ses->access_token, m_curl.encode(m_roomid), m_curl.encode(eventid)), raii::curl_llist(), json::rooms::eventid()); } netreturn roomcxn::redact_event(const raii::string_base& eventid)const{ return redact_event(eventid, "No reason given"_ss); } netreturn roomcxn::_get_events(int amount, raii::static_string direction, const raii::string_base& from, const raii::string_base& to){ raii::string reply = _get_curl(m_urls.messages(m_ses->homeserver, m_ses->access_token, m_curl.encode(m_roomid), from, to, direction, amount)); if(!reply) return _create_netreturn(reply, http_status()); raii::rjp_ptr root(rjp_parse(reply.get())); if(!root.get()) _create_netreturn(root, http_status()); netreturn retval = _create_netreturn(root, http_status()); RJP_value* chunk = rjp_search_member(root.get(), json::rooms::chunk(), 0).value; if(!chunk) return retval; retval.value() = sync::roomcxn_message_event_list(root, rjp_get_element(chunk), m_roomid); return retval; } netreturn roomcxn::get_events_forward(int amount){ return _get_events(amount, "f"_ss, raii::string(), raii::string()); } netreturn roomcxn::get_events_backward(int amount){ return _get_events(amount, "b"_ss, raii::string(), raii::string()); } netreturn roomcxn::get_events_forward(int amount, const raii::string_base& from, const raii::string_base& to){ return _get_events(amount, "f"_ss, from, to); } netreturn roomcxn::get_events_backward(int amount, const raii::string_base& from, const raii::string_base& to){ return _get_events(amount, "b"_ss, from, to); } void roomcxn::regenerate_urls(void){ m_urls.repopulate(m_ses->homeserver, m_ses->access_token, m_ses->userid, m_roomid); } netreturn roomcxn::upgrade(int version)const{ return _post_and_find(json::_room_upgrade(version), m_urls.upgrade(m_ses->homeserver, m_ses->access_token, m_roomid), raii::curl_llist(), json::rooms::eventid()); } netreturn roomcxn::_send_message(const raii::string_base& msg)const{ return send_custom_event(msg, "m.room.message"_ss); } }