Update tester program so I don't have to keep remembering to remove my login credentials
This commit is contained in:
parent
25bed730e8
commit
cf4485bab9
1
.gitignore
vendored
1
.gitignore
vendored
@ -13,3 +13,4 @@ lib*.a
|
|||||||
matrix-send
|
matrix-send
|
||||||
.scripts
|
.scripts
|
||||||
windows_bs
|
windows_bs
|
||||||
|
auth_file
|
||||||
|
|||||||
46
src/test.cpp
46
src/test.cpp
@ -55,18 +55,54 @@ void keyboard_fn(matrix::roomcxn cxn, std::atomic_bool& should_quit){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
matrix::auth_data read_auth_file(const char* filename){
|
||||||
|
raii::filerd fp(filename, "r");
|
||||||
|
if(!fp){
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
raii::rjp_ptr root(rjp_parse(fp.read(fp.length())));
|
||||||
|
if(!root.get())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
raii::rjp_string user = rjp_search_member(root.get(), "user", 0).value;
|
||||||
|
raii::rjp_string pass = rjp_search_member(root.get(), "pass", 0).value;
|
||||||
|
raii::rjp_string server = rjp_search_member(root.get(), "server", 0).value;
|
||||||
|
raii::rjp_string token = rjp_search_member(root.get(), "token", 0).value;
|
||||||
|
raii::rjp_string agent = rjp_search_member(root.get(), "useragent", 0).value;
|
||||||
|
|
||||||
|
return matrix::auth_data{user, pass, server, agent, token};
|
||||||
|
}
|
||||||
|
void write_to_auth_file(const char* filename, const matrix::auth_data& auth){
|
||||||
|
raii::rjp_ptr root(rjp_init_json_as(rjp_object()));
|
||||||
|
if(!root.get())
|
||||||
|
return;
|
||||||
|
|
||||||
|
rjp_add_member(root.get(), "user", 4, rjp_string_copy(auth.name));
|
||||||
|
rjp_add_member(root.get(), "pass", 4, rjp_string_copy(auth.pass));
|
||||||
|
rjp_add_member(root.get(), "server", 6, rjp_string_copy(auth.homeserver));
|
||||||
|
rjp_add_member(root.get(), "token", 5, rjp_string_copy(auth.access_token));
|
||||||
|
rjp_add_member(root.get(), "useragent", 9, rjp_string_copy(auth.useragent));
|
||||||
|
raii::rjp_string output;
|
||||||
|
output.reset(rjp_to_json(root.get()));
|
||||||
|
|
||||||
|
raii::filerd fp(filename, "w");
|
||||||
|
if(!fp)
|
||||||
|
return;
|
||||||
|
fp.write(output);
|
||||||
|
}
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
const char* username = "username";
|
matrix::auth_data auth = read_auth_file("auth_file");
|
||||||
const char* password = "password";
|
|
||||||
const char* useragent = "rexy712s test bot";
|
|
||||||
const char* homeserver = "matrix.org";
|
|
||||||
matrix::auth_data auth{username, password, homeserver, useragent};
|
|
||||||
|
|
||||||
matrix::session ses(auth);
|
matrix::session ses(auth);
|
||||||
if(!ses.valid()){
|
if(!ses.valid()){
|
||||||
fprintf(stderr, "Failed to init matrix session!\n");
|
fprintf(stderr, "Failed to init matrix session!\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
if(!auth.access_token){
|
||||||
|
auth.access_token = ses.access_token();
|
||||||
|
write_to_auth_file("auth_file", auth);
|
||||||
|
}
|
||||||
fprintf(stderr, "Succ\n");
|
fprintf(stderr, "Succ\n");
|
||||||
|
|
||||||
std::atomic_bool should_quit = false;
|
std::atomic_bool should_quit = false;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user