| 1 | #include <unistd.h> |
| 2 | |
| 3 | #include <iostream> |
| 4 | #include <variant> |
| 5 | |
| 6 | #include <nlohmann/json.hpp> |
| 7 | |
| 8 | #include <mtx.hpp> |
| 9 | #include <mtx/identifiers.hpp> |
| 10 | |
| 11 | #include "mtxclient/http/client.hpp" |
| 12 | #include "mtxclient/http/errors.hpp" |
| 13 | |
| 14 | // |
| 15 | // Simple example bot that will accept any invite. |
| 16 | // |
| 17 | |
| 18 | using namespace std; |
| 19 | using namespace mtx::client; |
| 20 | using namespace mtx::http; |
| 21 | using namespace mtx::events; |
| 22 | using namespace mtx::identifiers; |
| 23 | |
| 24 | using TimelineEvent = mtx::events::collections::TimelineEvents; |
| 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 sender of the event. |
| 77 | std::string |
| 78 | get_sender(const TimelineEvent &event) |
| 79 | { |
| 80 | return std::visit([](const auto &e) { return e.sender; }, event); |
| 81 | } |
| 82 | |
| 83 | void |
| 84 | parse_messages(const mtx::responses::Sync &res, bool parse_repeat_cmd = false) |
| 85 | { |
| 86 | for (const auto &room : res.rooms.invite) { |
| 87 | auto room_id = room.first; |
| 88 | |
| 89 | printf("joining room %s\n", room_id.c_str()); |
| 90 | client->join_room(room_id, [room_id](const mtx::responses::RoomId &obj, RequestErr e) { |
| 91 | if (e) { |
| 92 | print_errors(e); |
| 93 | printf("failed to join room %s\n", room_id.c_str()); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | printf("joined room \n%s\n", obj.room_id.c_str()); |
| 98 | |
| 99 | mtx::events::msg::Text text; |
| 100 | text.body = "Thanks for the invitation!"; |
| 101 | |
| 102 | client->send_room_message<mtx::events::msg::Text>( |
| 103 | room_id, text, [room_id](const mtx::responses::EventId &, RequestErr e) { |
| 104 | if (e) { |
| 105 | print_errors(e); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | printf("sent message to %s\n", room_id.c_str()); |
| 110 | }); |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | if (!parse_repeat_cmd) |
| 115 | return; |
| 116 | |
| 117 | for (const auto &room : res.rooms.join) { |
| 118 | const std::string repeat_cmd = "!repeat"; |
| 119 | const std::string room_id = room.first; |
| 120 | |
| 121 | for (const auto &e : room.second.timeline.events) { |
| 122 | if (!is_room_message(e)) |
| 123 | continue; |
| 124 | |
| 125 | auto body = get_body(e); |
| 126 | if (body.find(repeat_cmd) != 0) |
| 127 | continue; |
| 128 | |
| 129 | auto word = std::string(body.begin() + repeat_cmd.size(), body.end()); |
| 130 | auto user = get_sender(e); |
| 131 | |
| 132 | mtx::events::msg::Text text; |
| 133 | text.body = user + ": " + word; |
| 134 | |
| 135 | client->send_room_message<mtx::events::msg::Text>( |
| 136 | room_id, text, [room_id](const mtx::responses::EventId &, RequestErr e) { |
| 137 | if (e) { |
| 138 | print_errors(e); |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | printf("sent message to %s\n", room_id.c_str()); |
| 143 | }); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // Callback to executed after a /sync request completes. |
| 149 | void |
| 150 | sync_handler(const mtx::responses::Sync &res, RequestErr err) |
| 151 | { |
| 152 | SyncOpts opts; |
| 153 | |
| 154 | if (err) { |
| 155 | cout << "sync error:\n"; |
| 156 | print_errors(err); |
| 157 | opts.since = client->next_batch_token(); |
| 158 | client->sync(opts, &sync_handler); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | parse_messages(res, true); |
| 163 | |
| 164 | opts.since = res.next_batch; |
| 165 | client->set_next_batch_token(res.next_batch); |
| 166 | client->sync(opts, &sync_handler); |
| 167 | } |
| 168 | |
| 169 | // Callback to executed after the first (initial) /sync request completes. |
| 170 | void |
| 171 | initial_sync_handler(const mtx::responses::Sync &res, RequestErr err) |
| 172 | { |
| 173 | SyncOpts opts; |
| 174 | |
| 175 | if (err) { |
| 176 | cout << "error during initial sync:\n"; |
| 177 | print_errors(err); |
| 178 | |
| 179 | if (err->status_code != 200) { |
| 180 | cout << "retrying initial sync ...\n"; |
| 181 | opts.timeout = 0; |
| 182 | client->sync(opts, &initial_sync_handler); |
| 183 | } |
| 184 | |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | parse_messages(res); |
| 189 | |
| 190 | opts.since = res.next_batch; |
| 191 | client->set_next_batch_token(res.next_batch); |
| 192 | client->sync(opts, &sync_handler); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | login_handler(const mtx::responses::Login &, RequestErr err) |
| 197 | { |
| 198 | if (err) { |
| 199 | printf("login error\n"); |
| 200 | print_errors(err); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | printf("user_id: %s\n", client->user_id().to_string().c_str()); |
| 205 | printf("device_id: %s\n", client->device_id().c_str()); |
| 206 | |
| 207 | SyncOpts opts; |
| 208 | opts.timeout = 0; |
| 209 | client->sync(opts, &initial_sync_handler); |
| 210 | } |
| 211 | |
| 212 | int |
| 213 | main() |
| 214 | { |
| 215 | std::string username, server, password; |
| 216 | |
| 217 | cout << "username: "; |
| 218 | std::getline(std::cin, username); |
| 219 | |
| 220 | cout << "server: "; |
| 221 | std::getline(std::cin, server); |
| 222 | |
| 223 | password = getpass("password: "); |
| 224 | |
| 225 | client = std::make_shared<Client>(server); |
| 226 | client->login(username, password, login_handler); |
| 227 | client->close(); |
| 228 | |
| 229 | return 0; |
| 230 | } |