Fix segfault in matrix::connection, fix typo in netreturn generation, update matrix-send utility

This commit is contained in:
rexy712 2019-11-26 13:04:09 -08:00
parent 3c0c9b89be
commit aa06b2d540
5 changed files with 75 additions and 21 deletions

View File

@ -1,6 +1,7 @@
general/other:
5:thorough error checking
1:use libmagic to determine file types for uploading?
1:cross platform matrix-send
matrix:
8:fix fat_strings.cpp; use C style string allocation/catenation

View File

@ -23,10 +23,11 @@ OBJDIR::=obj
DEPDIR::=$(OBJDIR)/dep
LIBDIR::=lib
INCLUDE_DIRS::=include
CXXFLAGS::=-g -std=c++17 -Wall -pedantic -Wextra
CXXFLAGS::=-std=c++17 -Wall -pedantic -Wextra
EXT::=cpp
MAIN_LIBRARY::=rmatrix
SHARED?=1
SHARED?=0
RELEASE?=0
ifneq ($(WINDOWS),1)
CXX::=g++
@ -58,9 +59,11 @@ else
INTERNAL_MAIN_LIBRARY::=lib$(MAIN_LIBRARY).a
endif
all: CXXFLAGS+=-O0
release: CXXFLAGS+=-O2
ifeq ($(RELEASE),1)
CXXFLAGS+=-O2
else
CXXFLAGS+=-O0 -g
endif
memchk: LDFLAGS+=-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
memchk: CXXFLAGS+=-O0 -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
@ -107,9 +110,7 @@ $(INTERNAL_MAIN_LIBRARY): $(OBJECTS)
endif #shared
.PHONY: memchk
.PHONY: release
memchk: all
release: all
.PHONY: utils
utils: matrix-send

View File

@ -194,6 +194,8 @@ namespace matrix{
if(!root)
return {};
RJP_search_res res = rjp_search_member(root.get(), target.get(), 0);
if(!res.value)
return {};
if(rjp_value_type(res.value) != json_string)
return {};
return raii::rjp_string(res.value);
@ -222,7 +224,7 @@ namespace matrix{
netreturn_base connection::_create_netreturn(const raii::rjp_ptr& root, int httpstatus){
if(!root)
return netreturn_base("Invalid JSON"_ss, "Invalid JSON"_ss, -1);
return netreturn_base(rjp_search_member(root.get(), "error", 0).value, rjp_search_member(root.get(), "errorcode", 0).value, httpstatus);
return netreturn_base(rjp_search_member(root.get(), "error", 0).value, rjp_search_member(root.get(), "errcode", 0).value, httpstatus);
}
}

View File

@ -94,7 +94,11 @@ namespace matrix::sync{
return m_root.get();
}
raii::rjp_string response::raw_str(void)const{
#ifndef RJP_FORMAT_NONE
return raii::rjp_string(rjp_to_json(m_root.get()));
#else
return raii::rjp_string(rjp_to_json(m_root.get(), RJP_FORMAT_NONE));
#endif
}
RJP_value* response::_find_room_list(const char* segment)const{
RJP_search_res res = rjp_search_member(m_root.get(), "rooms", 0);

View File

@ -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);
}
}
}
}