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
|
#include <stdio.h>
#include <stdarg.h>
#include <mjson.h>
#include <matrix.h>
#define SERVER "https://matrix.org"
#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO"
#define DEVICE_ID "MAZNCCZLBR"
#define USER_ID "@pscho:matrix.org"
#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org"
#define BUFFER_SIZE 1024
#define NUMBER_ARGS 10
void
GetCommand(
char * cmd,
int * nargs,
char ** args
) {
int index = 0;
int c;
*nargs = 0;
printf("> ");
while ((c = getchar()), c != ' ' && c != '\n')
cmd[index++] = c;
cmd[index] = '\0';
if (c == '\n')
return;
*nargs = 1;
index = 0;
char * arg = args[0];
while ((c = getchar()), c != '\n') {
if (c == ' ') {
arg[index] = '\0';
arg = args[(*nargs)++];
index = 0;
continue;
}
arg[index++] = c;
}
arg[index] = '\0';
}
bool
CheckCommand(
const char * cmd,
const char * str
) {
if (strlen(cmd) != strlen(str))
return false;
for (size_t i = 0; i < strlen(cmd); i++) {
if (cmd[i] != str[i])
return false;
}
return true;
}
void
Usage(
const char * cmd,
const char * args
) {
printf("Usage: %s %s\n", cmd, args);
}
void
ExecuteCommand(
MatrixClient * client,
const char * cmd,
int nargs, char ** args
) {
#define CHECK_ARGS(N, ARGS) if (nargs != N) { Usage(cmd, ARGS); return; }
/**/ if (CheckCommand(cmd, "devicekey")) {
printf("%s\n", client->deviceKey);
}
else if (CheckCommand(cmd, "genkeys")) {
CHECK_ARGS(1, "<number of keys>")
MatrixClientGenerateOnetimeKeys(client, atoi(args[0]));
}
else if (CheckCommand(cmd, "uploadkeys")) {
MatrixClientUploadOnetimeKeys(client);
}
else if (CheckCommand(cmd, "onetimekeys")) {
static char buffer[1024];
olm_account_one_time_keys(client->olmAccount.account, buffer, 1024);
printf("%s\n", buffer);
}
else if (CheckCommand(cmd, "getkeys")) {
MatrixClientRequestDeviceKeys(client);
for (int i = 0; i < client->numDevices; i++)
printf("id: %s key: %s\n",
client->devices[i].deviceId,
client->devices[i].deviceKey);
}
else if (CheckCommand(cmd, "todevice")) {
static char buffer[30000];
MatrixClientSync(client,
buffer, 30000);
const char * todevice;
int todeviceLen;
mjson_find(buffer, 30000,
"$.to_device",
&todevice, &todeviceLen);
static char prettyBuffer[10000];
struct mjson_fixedbuf fb = { prettyBuffer, 10000, 0 };
mjson_pretty(todevice, todeviceLen,
" ", mjson_print_fixed_buf, &fb);
printf("%.*s\n", fb.len, fb.ptr);
}
else if (CheckCommand(cmd, "save")) {
CHECK_ARGS(1, "<filename>")
MatrixClientSave(client, args[0]);
}
else if (CheckCommand(cmd, "load")) {
CHECK_ARGS(1, "<filename>")
MatrixClientLoad(client, args[0]);
}
else if (CheckCommand(cmd, "send")) {
CHECK_ARGS(2, "<room_id> <message>")
static char body[1024];
snprintf(body, 1024,
"{\"body\":\"%s\",\"msgtype\":\"m.text\"}",
args[1]);
MatrixClientSendEvent(client,
args[0],
"m.room.message",
body);
}
else if (CheckCommand(cmd, "setuserid")) {
CHECK_ARGS(1, "<user_id>")
MatrixClientSetUserId(client, args[0]);
}
else if (CheckCommand(cmd, "getuserid")) {
printf("User ID: %s\n", client->userId);
}
else if (CheckCommand(cmd, "sendencrypted")) {
CHECK_ARGS(2, "<room_id> <message>")
static char body[1024];
snprintf(body, 1024,
"{\"body\":\"%s\",\"msgtype\":\"m.text\"}",
args[1]);
MatrixClientSendEventEncrypted(client,
args[0],
"m.room.message",
body);
}
else if (CheckCommand(cmd, "sharesession")) {
CHECK_ARGS(2, "<user_id> <device_id>")
MatrixClientShareMegolmOutSession(&client,
args[0],
args[1],
&client->megolmOutSessions[0]);
}
else if (CheckCommand(cmd, "savesession")) {
CHECK_ARGS(2, "<filename> <key>")
MatrixMegolmOutSessionSave(
&client->megolmOutSessions[0],
args[0],
args[1]);
}
else if (CheckCommand(cmd, "loadsession")) {
CHECK_ARGS(2, "<filename> <key>")
MatrixMegolmOutSessionLoad(
&client->megolmOutSessions[0],
args[0],
args[1]);
}
#undef CHECK_ARGS
}
int
main(void)
{
MatrixClient client;
MatrixClientInit(&client,
SERVER);
MatrixHttpInit(&client);
MatrixClientSetAccessToken(&client,
ACCESS_TOKEN);
MatrixClientSetDeviceId(&client,
DEVICE_ID);
MatrixClientSetUserId(&client,
USER_ID);
static char cmd[BUFFER_SIZE];
static char args_[NUMBER_ARGS][BUFFER_SIZE];
char * args[NUMBER_ARGS];
for (int i = 0; i < NUMBER_ARGS; i++)
args[i] = args_[i];
int nargs;
do {
GetCommand(cmd, &nargs, args);
ExecuteCommand(&client, cmd, nargs, args);
} while (strcmp(cmd, "exit") != 0);
MatrixHttpDeinit(&client);
return 0;
}
|