matrix_thing/util/matrix-send.cpp

111 lines
3.0 KiB
C++

/**
This file is a part of rexy's matrix client
Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "matrix/matrix.hpp"
#include <rexy/string_base.hpp>
#include <cstdlib> //exit
#include <vector>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include "raii/rjp_ptr.hpp"
#include <rexy/string.hpp>
#include <rexy/filerd.hpp>
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){
std::vector<char> data;
data.reserve(256);
for(int in;(in = fgetc(stdin)) != EOF;){
data.push_back(in);
}
data.push_back(0);
auto reply = room.send_message(rexy::static_string(data.data()));
check_netreturn_errors(reply);
}
matrix::auth_data read_auth_file(const char* filename){
rexy::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");
raii::rjp_string pass = rjp_search_member(root.get(), "pass");
raii::rjp_string server = rjp_search_member(root.get(), "server");
raii::rjp_string token = rjp_search_member(root.get(), "token");
raii::rjp_string agent = rjp_search_member(root.get(), "useragent");
return matrix::auth_data{user, pass, server, agent, token};
}
int main(int argc, char** argv){
if(argc < 2){
fprintf(stderr, "Missing argument\n");
return 1;
}
const char* roomid = argv[1];
rexy::string conf = "/etc/matrix-send.conf";
const char* home = getenv("HOME");
if(home){
rexy::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(rexy::static_string(roomid));
if(argc < 3){
do_stdin(room);
}else{
for(int i = 2;i < argc;++i){
if(!strcmp(argv[i], "-")){
do_stdin(room);
}else{
auto reply = room.send_message(rexy::static_string(argv[i]));
check_netreturn_errors(reply);
}
}
}
}