/** 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 . */ #include "matrix/matrix.hpp" #include #include //exit #include #include #include #include #include #include "raii/rjp_ptr.hpp" #include #include void check_netreturn_errors(const matrix::netreturn& 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 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()), RJP_PARSE_ALL_EXT, NULL)); 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); } } } }