Fix segfault in matrix::connection, fix typo in netreturn generation, update matrix-send utility
This commit is contained in:
+59
-13
@@ -23,11 +23,20 @@
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <unistd.h>
|
||||
#include "raii/rjp_ptr.hpp"
|
||||
#include "raii/static_string.hpp"
|
||||
#include "raii/string.hpp"
|
||||
|
||||
[[noreturn]] void usage(int status){
|
||||
printf("Usage: matrix-send STRING\n");
|
||||
printf("\tSends message to designated room as the designated user\n");
|
||||
exit(status);
|
||||
void check_netreturn_errors(const matrix::netreturn<raii::rjp_string>& n){
|
||||
if(n.has_httperror()){
|
||||
fprintf(stderr, "http error %d\n", n.httpstatus());
|
||||
}
|
||||
if(n.mxerrorcode()){
|
||||
fprintf(stderr, "matrix error code %s\n", n.mxerrorcode().get());
|
||||
fprintf(stderr, "matrix error %s\n", n.mxerror().get());
|
||||
}
|
||||
}
|
||||
|
||||
void do_stdin(const matrix::roomcxn& room){
|
||||
@@ -37,28 +46,65 @@ void do_stdin(const matrix::roomcxn& room){
|
||||
data.push_back(in);
|
||||
}
|
||||
data.push_back(0);
|
||||
room.send_message(raii::static_string(data.data()));
|
||||
auto reply = room.send_message(raii::static_string(data.data()));
|
||||
check_netreturn_errors(reply);
|
||||
}
|
||||
|
||||
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};
|
||||
}
|
||||
|
||||
int main(int argc, char** argv){
|
||||
const char* username = "username";
|
||||
const char* password = "password";
|
||||
const char* useragent = "rexy712s test bot";
|
||||
const char* homeserver = "matrix.org";
|
||||
const char* roomid = "!room:matrix.org";
|
||||
matrix::auth_data auth{username, password, homeserver, useragent};
|
||||
if(argc < 2){
|
||||
fprintf(stderr, "Missing argument\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char* roomid = argv[1];
|
||||
raii::string conf = "/etc/matrix-send.conf";
|
||||
const char* home = getenv("HOME");
|
||||
|
||||
if(home){
|
||||
raii::string home_conf = home + "/.config/matrix-send.conf"_ss;
|
||||
if(access(home_conf.get(), F_OK) != -1){
|
||||
conf = std::move(home_conf);
|
||||
}
|
||||
}
|
||||
matrix::auth_data auth = read_auth_file(conf.get());
|
||||
|
||||
matrix::session ses(auth);
|
||||
if(!ses.valid()){
|
||||
fprintf(stderr, "Failed to init matrix session!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto client = ses.spawn_client();
|
||||
auto room = client.spawn_room(raii::static_string(roomid));
|
||||
if(argc < 2 || !strcmp(argv[1], "-")){
|
||||
|
||||
if(argc < 3){
|
||||
do_stdin(room);
|
||||
}else{
|
||||
room.send_message(raii::static_string(argv[1]));
|
||||
for(int i = 2;i < argc;++i){
|
||||
if(!strcmp(argv[i], "-")){
|
||||
do_stdin(room);
|
||||
}else{
|
||||
auto reply = room.send_message(raii::static_string(argv[i]));
|
||||
check_netreturn_errors(reply);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user