This repository has been archived by the owner on Oct 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
237 lines (199 loc) · 6.9 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <boost/beast.hpp>
#include <iostream>
#ifdef WIN32
#include <windows.h>
#else
#include <cstdlib>
#endif
#include <variant.hpp>
#include "client.hpp"
#include "errors.hpp"
#include "mtx.hpp"
//
// Simple usage example of the /login & /sync endpoints which
// will print the stream of messages from all rooms as received by the client.
//
using namespace std;
using namespace mtx::client;
using namespace mtx::events;
using TimelineEvent = mtx::events::collections::TimelineEvents;
void
print_errors(RequestErr err)
{
if (err->status_code != boost::beast::http::status::unknown)
cout << err->status_code << "\n";
if (!err->matrix_error.error.empty())
cout << err->matrix_error.error << "\n";
if (err->error_code)
cout << err->error_code.message() << "\n";
}
// Check if the given event has a textual representation.
bool
is_room_message(const TimelineEvent &event)
{
return mpark::holds_alternative<mtx::events::RoomEvent<msg::Audio>>(event) ||
mpark::holds_alternative<mtx::events::RoomEvent<msg::Emote>>(event) ||
mpark::holds_alternative<mtx::events::RoomEvent<msg::File>>(event) ||
mpark::holds_alternative<mtx::events::RoomEvent<msg::Image>>(event) ||
mpark::holds_alternative<mtx::events::RoomEvent<msg::Notice>>(event) ||
mpark::holds_alternative<mtx::events::RoomEvent<msg::Text>>(event) ||
mpark::holds_alternative<mtx::events::RoomEvent<msg::Video>>(event);
}
// Retrieves the fallback body value from the event.
std::string
get_body(const TimelineEvent &event)
{
if (mpark::holds_alternative<RoomEvent<msg::Audio>>(event))
return mpark::get<RoomEvent<msg::Audio>>(event).content.body;
else if (mpark::holds_alternative<RoomEvent<msg::Emote>>(event))
return mpark::get<RoomEvent<msg::Emote>>(event).content.body;
else if (mpark::holds_alternative<RoomEvent<msg::File>>(event))
return mpark::get<RoomEvent<msg::File>>(event).content.body;
else if (mpark::holds_alternative<RoomEvent<msg::Image>>(event))
return mpark::get<RoomEvent<msg::Image>>(event).content.body;
else if (mpark::holds_alternative<RoomEvent<msg::Notice>>(event))
return mpark::get<RoomEvent<msg::Notice>>(event).content.body;
else if (mpark::holds_alternative<RoomEvent<msg::Text>>(event))
return mpark::get<RoomEvent<msg::Text>>(event).content.body;
else if (mpark::holds_alternative<RoomEvent<msg::Video>>(event))
return mpark::get<RoomEvent<msg::Video>>(event).content.body;
return "";
}
// Retrieves the sender of the event.
std::string
get_sender(const TimelineEvent &event)
{
return mpark::visit([](auto e) { return e.sender; }, event);
}
// Simple print of the message contents.
void
print_message(const TimelineEvent &event)
{
if (is_room_message(event))
cout << get_sender(event) << ": " << get_body(event) << "\n";
}
// Callback to executed after a /sync request completes.
void
sync_handler(shared_ptr<Client> client, const mtx::responses::Sync &res, RequestErr err)
{
if (err) {
cout << "sync error:\n";
print_errors(err);
client->sync(
"",
client->next_batch_token(),
false,
30000,
std::bind(&sync_handler, client, std::placeholders::_1, std::placeholders::_2));
return;
}
for (const auto room : res.rooms.join) {
for (const auto msg : room.second.timeline.events)
print_message(msg);
}
client->set_next_batch_token(res.next_batch);
client->sync(
"",
client->next_batch_token(),
false,
30000,
std::bind(&sync_handler, client, std::placeholders::_1, std::placeholders::_2));
}
// Callback to executed after the first (initial) /sync request completes.
void
initial_sync_handler(shared_ptr<Client> client, const nlohmann::json &res, RequestErr err)
{
if (err) {
cout << "error during initial sync:\n";
print_errors(err);
if (err->status_code != boost::beast::http::status::ok) {
cout << "retrying initial sync ...\n";
client->sync("",
"",
false,
0,
std::bind(&initial_sync_handler,
client,
std::placeholders::_1,
std::placeholders::_2));
}
return;
}
client->set_next_batch_token(res.at("next_batch"));
client->sync(
"",
client->next_batch_token(),
false,
30000,
std::bind(&sync_handler, client, std::placeholders::_1, std::placeholders::_2));
}
#ifdef WIN32
string getpassword(const char *prompt, bool show_asterisk=true)
{
const char BACKSPACE=8;
const char RETURN=13;
string password;
unsigned char ch=0;
cout <<prompt<<endl;
DWORD con_mode;
DWORD dwRead;
HANDLE hIn=GetStdHandle(STD_INPUT_HANDLE);
GetConsoleMode( hIn, &con_mode );
SetConsoleMode( hIn, con_mode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT) );
while(ReadConsoleA( hIn, &ch, 1, &dwRead, NULL) && ch !=RETURN)
{
if(ch==BACKSPACE)
{
if(password.length()!=0)
{
if(show_asterisk)
cout <<"\b \b";
password.resize(password.length()-1);
}
}
else
{
password+=ch;
if(show_asterisk)
cout <<'*';
}
}
cout <<endl;
return password;
}
#else
string getpassword(const char *prompt, bool show_asterisk=true)
{
return getpass("Password: ");
}
#endif
int
main()
{
std::string username, server, password;
cout << "Username: ";
std::getline(std::cin, username);
cout << "HomeServer: ";
std::getline(std::cin, server);
password = getpassword("Password: ");
auto client = std::make_shared<Client>(server);
client->login(
username, password, [client](const mtx::responses::Login &res, RequestErr err) {
if (err) {
cout << "There was an error during login: " << err->matrix_error.error
<< "\n";
return;
}
cout << "Logged in as: " << res.user_id.to_string() << "\n";
client->set_access_token(res.access_token);
client->sync(
"",
"",
false,
0,
std::bind(
&initial_sync_handler, client, std::placeholders::_1, std::placeholders::_2));
});
client->close();
return 0;
}