| 1 | #include <iostream> |
| 2 | #include <variant> |
| 3 | |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | #include "mtx.hpp" |
| 7 | #include "mtxclient/http/client.hpp" |
| 8 | #include "mtxclient/http/errors.hpp" |
| 9 | |
| 10 | // |
| 11 | // Simple usage example of the /login & /sync endpoints which |
| 12 | // will print the stream of messages from all rooms as received by the client. |
| 13 | // |
| 14 | |
| 15 | using namespace std; |
| 16 | using namespace mtx::client; |
| 17 | using namespace mtx::http; |
| 18 | using namespace mtx::events; |
| 19 | |
| 20 | using TimelineEvent = mtx::events::collections::TimelineEvents; |
| 21 | |
| 22 | namespace { |
| 23 | std::shared_ptr<Client> client = nullptr; |
| 24 | } |
| 25 | |
| 26 | void |
| 27 | print_errors(RequestErr err) |
| 28 | { |
| 29 | if (err->status_code) |
| 30 | cout << err->status_code << "\n"; |
| 31 | if (!err->matrix_error.error.empty()) |
| 32 | cout << err->matrix_error.error << "\n"; |
| 33 | if (err->error_code) |
| 34 | cout << err->error_code << "\n"; |
| 35 | } |
| 36 | |
| 37 | // Check if the given event has a textual representation. |
| 38 | bool |
| 39 | is_room_message(const TimelineEvent &e) |
| 40 | { |
| 41 | return (std::holds_alternative<mtx::events::RoomEvent<msg::Audio>>(e)) || |
| 42 | (std::holds_alternative<mtx::events::RoomEvent<msg::Emote>>(e)) || |
| 43 | (std::holds_alternative<mtx::events::RoomEvent<msg::File>>(e)) || |
| 44 | (std::holds_alternative<mtx::events::RoomEvent<msg::Image>>(e)) || |
| 45 | (std::holds_alternative<mtx::events::RoomEvent<msg::Notice>>(e)) || |
| 46 | (std::holds_alternative<mtx::events::RoomEvent<msg::Text>>(e)) || |
| 47 | (std::holds_alternative<mtx::events::RoomEvent<msg::Video>>(e)); |
| 48 | } |
| 49 | |
| 50 | // Retrieves the fallback body value from the event. |
| 51 | std::string |
| 52 | get_body(const TimelineEvent &e) |
| 53 | { |
| 54 | if (auto ev = std::get_if<RoomEvent<msg::Audio>>(&e); ev != nullptr) |
| 55 | return ev->content.body; |
| 56 | else if (auto ev = std::get_if<RoomEvent<msg::Emote>>(&e); ev != nullptr) |
| 57 | return ev->content.body; |
| 58 | else if (auto ev = std::get_if<RoomEvent<msg::File>>(&e); ev != nullptr) |
| 59 | return ev->content.body; |
| 60 | else if (auto ev = std::get_if<RoomEvent<msg::Image>>(&e); ev != nullptr) |
| 61 | return ev->content.body; |
| 62 | else if (auto ev = std::get_if<RoomEvent<msg::Notice>>(&e); ev != nullptr) |
| 63 | return ev->content.body; |
| 64 | else if (auto ev = std::get_if<RoomEvent<msg::Text>>(&e); ev != nullptr) |
| 65 | return ev->content.body; |
| 66 | else if (auto ev = std::get_if<RoomEvent<msg::Video>>(&e); ev != nullptr) |
| 67 | return ev->content.body; |
| 68 | |
| 69 | return ""; |
| 70 | } |
| 71 | |
| 72 | // Retrieves the sender of the event. |
| 73 | std::string |
| 74 | get_sender(const TimelineEvent &event) |
| 75 | { |
| 76 | return std::visit([](const auto &e) { return e.sender; }, event); |
| 77 | } |
| 78 | |
| 79 | // Simple print of the message contents. |
| 80 | void |
| 81 | print_message(const TimelineEvent &event) |
| 82 | { |
| 83 | if (is_room_message(event)) |
| 84 | cout << get_sender(event) << ": " << get_body(event) << "\n"; |
| 85 | } |
| 86 | |
| 87 | // Callback to executed after a /sync request completes. |
| 88 | void |
| 89 | sync_handler(const mtx::responses::Sync &res, RequestErr err) |
| 90 | { |
| 91 | SyncOpts opts; |
| 92 | |
| 93 | if (err) { |
| 94 | cout << "sync error:\n"; |
| 95 | print_errors(err); |
| 96 | opts.since = client->next_batch_token(); |
| 97 | client->sync(opts, &sync_handler); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | for (const auto &room : res.rooms.join) { |
| 102 | for (const auto &msg : room.second.timeline.events) |
| 103 | print_message(msg); |
| 104 | } |
| 105 | |
| 106 | opts.since = res.next_batch; |
| 107 | client->set_next_batch_token(res.next_batch); |
| 108 | client->sync(opts, &sync_handler); |
| 109 | } |
| 110 | |
| 111 | // Callback to executed after the first (initial) /sync request completes. |
| 112 | void |
| 113 | initial_sync_handler(const mtx::responses::Sync &res, RequestErr err) |
| 114 | { |
| 115 | SyncOpts opts; |
| 116 | |
| 117 | if (err) { |
| 118 | cout << "error during initial sync:\n"; |
| 119 | print_errors(err); |
| 120 | |
| 121 | if (err->status_code != 200) { |
| 122 | cout << "retrying initial sync ...\n"; |
| 123 | opts.timeout = 0; |
| 124 | client->sync(opts, &initial_sync_handler); |
| 125 | } |
| 126 | |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | opts.since = res.next_batch; |
| 131 | client->set_next_batch_token(res.next_batch); |
| 132 | client->sync(opts, &sync_handler); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | login_handler(const mtx::responses::Login &res, RequestErr err) |
| 137 | { |
| 138 | if (err) { |
| 139 | cout << "There was an error during login: " << err->matrix_error.error << "\n"; |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | cout << "Logged in as: " << res.user_id.to_string() << "\n"; |
| 144 | SyncOpts opts; |
| 145 | opts.timeout = 0; |
| 146 | |
| 147 | client->set_access_token(res.access_token); |
| 148 | client->sync(opts, &initial_sync_handler); |
| 149 | } |
| 150 | |
| 151 | int |
| 152 | main() |
| 153 | { |
| 154 | std::string username, server, password; |
| 155 | |
| 156 | cout << "Username: "; |
| 157 | std::getline(std::cin, username); |
| 158 | |
| 159 | cout << "HomeServer: "; |
| 160 | std::getline(std::cin, server); |
| 161 | |
| 162 | password = getpass("Password: "); |
| 163 | |
| 164 | client = std::make_shared<Client>(server); |
| 165 | client->login(username, password, &login_handler); |
| 166 | client->close(); |
| 167 | |
| 168 | return 0; |
| 169 | } |