| 1 | #include <filesystem> |
| 2 | #include <fstream> |
| 3 | #include <iostream> |
| 4 | #include <variant> |
| 5 | |
| 6 | #include <unistd.h> |
| 7 | |
| 8 | #include "mtx.hpp" |
| 9 | #include "mtxclient/http/client.hpp" |
| 10 | #include "mtxclient/http/errors.hpp" |
| 11 | |
| 12 | // |
| 13 | // Simple usage example of the /login & /sync endpoints which |
| 14 | // will print the stream of messages from all rooms as received by the client. |
| 15 | // |
| 16 | |
| 17 | using namespace std; |
| 18 | using namespace mtx::client; |
| 19 | using namespace mtx::http; |
| 20 | using namespace mtx::events; |
| 21 | |
| 22 | using TimelineEvent = mtx::events::collections::TimelineEvents; |
| 23 | |
| 24 | std::string room_id_to_download_from; |
| 25 | |
| 26 | namespace { |
| 27 | std::shared_ptr<Client> client = nullptr; |
| 28 | } |
| 29 | |
| 30 | void |
| 31 | print_errors(RequestErr err) |
| 32 | { |
| 33 | if (err->status_code) |
| 34 | cout << err->status_code << "\n"; |
| 35 | if (!err->matrix_error.error.empty()) |
| 36 | cout << err->matrix_error.error << "\n"; |
| 37 | if (err->error_code) |
| 38 | cout << err->error_code << "\n"; |
| 39 | } |
| 40 | |
| 41 | // Check if the given event has a textual representation. |
| 42 | bool |
| 43 | is_room_message(const TimelineEvent &e) |
| 44 | { |
| 45 | return (std::holds_alternative<mtx::events::RoomEvent<msg::Audio>>(e)) || |
| 46 | (std::holds_alternative<mtx::events::RoomEvent<msg::Emote>>(e)) || |
| 47 | (std::holds_alternative<mtx::events::RoomEvent<msg::File>>(e)) || |
| 48 | (std::holds_alternative<mtx::events::RoomEvent<msg::Image>>(e)) || |
| 49 | (std::holds_alternative<mtx::events::RoomEvent<msg::Notice>>(e)) || |
| 50 | (std::holds_alternative<mtx::events::RoomEvent<msg::Text>>(e)) || |
| 51 | (std::holds_alternative<mtx::events::RoomEvent<msg::Video>>(e)); |
| 52 | } |
| 53 | |
| 54 | // Retrieves the fallback body value from the event. |
| 55 | std::string |
| 56 | get_body(const TimelineEvent &e) |
| 57 | { |
| 58 | if (auto ev = std::get_if<RoomEvent<msg::Audio>>(&e); ev != nullptr) |
| 59 | return ev->content.body; |
| 60 | else if (auto ev = std::get_if<RoomEvent<msg::Emote>>(&e); ev != nullptr) |
| 61 | return ev->content.body; |
| 62 | else if (auto ev = std::get_if<RoomEvent<msg::File>>(&e); ev != nullptr) |
| 63 | return ev->content.body; |
| 64 | else if (auto ev = std::get_if<RoomEvent<msg::Image>>(&e); ev != nullptr) |
| 65 | return ev->content.body; |
| 66 | else if (auto ev = std::get_if<RoomEvent<msg::Notice>>(&e); ev != nullptr) |
| 67 | return ev->content.body; |
| 68 | else if (auto ev = std::get_if<RoomEvent<msg::Text>>(&e); ev != nullptr) |
| 69 | return ev->content.body; |
| 70 | else if (auto ev = std::get_if<RoomEvent<msg::Video>>(&e); ev != nullptr) |
| 71 | return ev->content.body; |
| 72 | |
| 73 | return ""; |
| 74 | } |
| 75 | |
| 76 | // Retrieves the fallback body value from the event. |
| 77 | std::string |
| 78 | get_url(const TimelineEvent &e) |
| 79 | { |
| 80 | if (auto ev = std::get_if<RoomEvent<msg::Audio>>(&e); ev != nullptr) |
| 81 | return ev->content.url; |
| 82 | else if (auto ev = std::get_if<RoomEvent<msg::File>>(&e); ev != nullptr) |
| 83 | return ev->content.url; |
| 84 | else if (auto ev = std::get_if<RoomEvent<msg::Image>>(&e); ev != nullptr) |
| 85 | return ev->content.url; |
| 86 | else if (auto ev = std::get_if<RoomEvent<msg::Video>>(&e); ev != nullptr) |
| 87 | return ev->content.url; |
| 88 | |
| 89 | return ""; |
| 90 | } |
| 91 | |
| 92 | // Retrieves the sender of the event. |
| 93 | std::string |
| 94 | get_sender(const TimelineEvent &event) |
| 95 | { |
| 96 | return std::visit([](auto e) { return e.sender; }, event); |
| 97 | } |
| 98 | |
| 99 | // Simple print of the message contents. |
| 100 | void |
| 101 | print_message(const TimelineEvent &event) |
| 102 | { |
| 103 | if (is_room_message(event)) |
| 104 | cout << get_sender(event) << ": " << get_body(event) << "\n"; |
| 105 | |
| 106 | if (auto url = get_url(event); !url.empty()) { |
| 107 | cout << "found url: " << url << "\n"; |
| 108 | |
| 109 | client->download(url, |
| 110 | [url](const std::string &data, |
| 111 | const std::string &content_type, |
| 112 | const std::string &original_filename, |
| 113 | RequestErr err) { |
| 114 | if (err) { |
| 115 | cout << "download error:\n"; |
| 116 | print_errors(err); |
| 117 | return; |
| 118 | } |
| 119 | std::string_view urlv = url; |
| 120 | urlv.remove_prefix(std::size("mxc://") - 1); |
| 121 | std::filesystem::path p(urlv); |
| 122 | std::filesystem::create_directories(p.parent_path()); |
| 123 | |
| 124 | ofstream file(p, std::ios::binary); |
| 125 | file.write(data.data(), data.size()); |
| 126 | |
| 127 | if (file.good()) |
| 128 | cout << "Wrote to: " << p << ", original filename was '" |
| 129 | << original_filename << "', content_type '" << content_type |
| 130 | << "'\n"; |
| 131 | else |
| 132 | cout << "Write to '" << p << "' failed!\n"; |
| 133 | }); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Callback to executed after a /sync request completes. |
| 138 | void |
| 139 | message_handler(const mtx::responses::Messages &res, RequestErr err) |
| 140 | { |
| 141 | MessagesOpts opts; |
| 142 | opts.room_id = room_id_to_download_from; |
| 143 | opts.limit = 300; |
| 144 | opts.from = res.end; |
| 145 | |
| 146 | if (err) { |
| 147 | cout << "messages error:\n"; |
| 148 | print_errors(err); |
| 149 | opts.from = res.start; |
| 150 | client->messages(opts, &message_handler); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | for (const auto &msg : res.chunk) |
| 155 | print_message(msg); |
| 156 | |
| 157 | if (res.chunk.empty()) { |
| 158 | cout << "No more messages, exiting.\n"; |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | client->messages(opts, &message_handler); |
| 163 | } |
| 164 | |
| 165 | // Callback to executed after the first (initial) /sync request completes. |
| 166 | void |
| 167 | initial_sync_handler(const mtx::responses::Sync &res, RequestErr err) |
| 168 | { |
| 169 | MessagesOpts opts; |
| 170 | opts.room_id = room_id_to_download_from; |
| 171 | opts.limit = 300; |
| 172 | |
| 173 | if (err) { |
| 174 | cout << "error during initial sync:\n"; |
| 175 | print_errors(err); |
| 176 | |
| 177 | if (err->status_code != 200) { |
| 178 | cout << "retrying initial sync ...\n"; |
| 179 | SyncOpts sopts; |
| 180 | sopts.timeout = 0; |
| 181 | client->sync(sopts, &initial_sync_handler); |
| 182 | } |
| 183 | |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (res.rooms.join.count(opts.room_id) == 0) { |
| 188 | cout << "room doesn't exist?"; |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | opts.from = res.next_batch; |
| 193 | client->messages(opts, message_handler); |
| 194 | } |
| 195 | |
| 196 | void |
| 197 | login_handler(const mtx::responses::Login &res, RequestErr err) |
| 198 | { |
| 199 | if (err) { |
| 200 | cout << "There was an error during login: " << err->matrix_error.error << "\n"; |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | cout << "Logged in as: " << res.user_id.to_string() << "\n"; |
| 205 | |
| 206 | cout << "give room id to download all media from: "; |
| 207 | std::getline(std::cin, room_id_to_download_from); |
| 208 | |
| 209 | SyncOpts opts; |
| 210 | opts.timeout = 0; |
| 211 | |
| 212 | client->set_access_token(res.access_token); |
| 213 | client->sync(opts, &initial_sync_handler); |
| 214 | } |
| 215 | |
| 216 | int |
| 217 | main() |
| 218 | { |
| 219 | std::string username, server, password; |
| 220 | |
| 221 | cout << "Username: "; |
| 222 | std::getline(std::cin, username); |
| 223 | |
| 224 | cout << "HomeServer: "; |
| 225 | std::getline(std::cin, server); |
| 226 | |
| 227 | password = getpass("Password: "); |
| 228 | |
| 229 | client = std::make_shared<Client>(server); |
| 230 | client->login(username, password, &login_handler); |
| 231 | client->close(); |
| 232 | |
| 233 | return 0; |
| 234 | } |