/** 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 . */ #ifndef MATRIX_CLIENT_HPP #define MATRIX_CLIENT_HPP #include "raii/string.hpp" #include "raii/rjp_string.hpp" #include "raii/filerd.hpp" #include "matrix/session_info.hpp" #include "matrix/upload_info.hpp" #include "matrix/connection.hpp" #include "matrix/roomcxn.hpp" #include "matrix/netreturn.hpp" #include "matrix/rest/client_url_list.hpp" #include //vector #include //shared_ptr #include //size_t namespace matrix{ //Class to interact with the homeserver of the logged in user. //Shares state with the spawning session. class client : public connection { private: std::shared_ptr m_urls; public: client(const std::shared_ptr&); //Copy and move ctor client(const client& b) = default; client(client&& b) = default; //Dtor ~client(void) = default; //Copy and move assignment client& operator=(const client&) = default; client& operator=(client&&) = default; /* * Sets the display name on the homeserver. * Note name must be safe to be placed in a json string. * Returns: true if the operation was successful. */ netreturn set_display_name(const raii::string_base&); /* * Sets the url of the user's profile picture on the homeserver. * Note url must be safe to be placed in a json string. * Returns: true if the operation was successful. */ netreturn set_profile_picture(const raii::string_base&); netreturn set_presence(const raii::string_base& status); netreturn get_presence(const raii::string_base& userid); /* * Gets the display name of the logged in user from the homeserver. * Returns: the display name on success, an empty string on failure. */ netreturn get_display_name(void)const; /* * Gets the profile picture url of the logged in user from the homeserver. * Returns: the profile picture url on success, an empty string on failure. */ netreturn get_profile_picture(void)const; /* * Lookup a room id given a room alias. * Returns: the room id of the associated room on success, empty string on failure. */ netreturn room_alias_to_id(const raii::string_base& alias)const; /* * Get a list of rooms the logged in user is a member of. * Returns: a vector of room id strings. */ netreturn> list_rooms(void)const; /* * Create a new room on the logged in user's homeserver. Assign an alias to the room. * Returns: the room id of the newly created room on success, an empty string on failure. */ netreturn create_room(const raii::string_base& name, const raii::string_base& alias)const; /* * Create a new matrix::roomcxn object for the given roomid. * The roomcxn will share login information with this client. Note that changing useragent, * access token, or homeserver after creating a roomcxn MUST be synchronized between threads. * Returns: a new matrix::roomcxn for the given roomid. */ roomcxn spawn_room(const raii::string_base& roomid)const; roomcxn spawn_room(raii::string&& roomid)const; //NOTE alias equivalents just give the uploaded file a name other than the filename. /* * Upload a file as a raw file. * Takes file data as argument. * Returns: file_info struct containing the url of the file on the homeserver. * Note check the file_info::fileurl field to check if the upload succeeded. */ netreturn upload_file(const file_details& file)const; /* * Upload a file as an image. * Returns: image_info struct containing the url of the image on the homeserver. * Note check the image_info::fileurl field to check if the upload succeeded. */ netreturn upload_image(const image_details& file)const; /* * Upload data as a video. * Returns: video_info struct containing the url of the image on the homeserver. * Note check the video_info::fileurl field to check if the upload succeeded. */ netreturn upload_video(const video_details& file)const; /* * Upload a file as an audio file. * Returns: audio_info struct containing the url of the image on the homeserver. * Note check the audio_info::fileurl field to check if the upload succeeded. */ netreturn upload_audio(const audio_details& file)const; netreturn create_thumbnail(uploaded_image& info)const; netreturn create_thumbnail(uploaded_video& video)const; raii::binary download_file(const raii::string_base& url); template bool download_file(const raii::string_base& url, DLHandler&& dl){ _get_curl_setup(m_urls->file_download(m_ses->homeserver, url)); m_curl.setwritefun(_download_dispatch); m_curl.setwritedata(&dl); if(!_perform_curl()) return false; return true; } template bool download_file(const raii::string_base& server, const raii::string_base& media_id, DLHandler&& dl){ raii::string url = "mxc://"_ss + server + "/"_ss + media_id; return download_file(url, std::forward(dl)); } private: template static size_t _download_dispatch(char* ptr, size_t size, size_t nmemb, void* userdata){ return reinterpret_cast(userdata)(ptr, size, nmemb, nullptr); } netreturn _upload_file(const file_details& file, const raii::curl_llist& header)const; }; } #endif