Fix segfault in matrix::connection, fix typo in netreturn generation, update matrix-send utility
This commit is contained in:
parent
3c0c9b89be
commit
aa06b2d540
1
doc/TODO
1
doc/TODO
@ -1,6 +1,7 @@
|
|||||||
general/other:
|
general/other:
|
||||||
5:thorough error checking
|
5:thorough error checking
|
||||||
1:use libmagic to determine file types for uploading?
|
1:use libmagic to determine file types for uploading?
|
||||||
|
1:cross platform matrix-send
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
8:fix fat_strings.cpp; use C style string allocation/catenation
|
8:fix fat_strings.cpp; use C style string allocation/catenation
|
||||||
|
|||||||
15
makefile
15
makefile
@ -23,10 +23,11 @@ OBJDIR::=obj
|
|||||||
DEPDIR::=$(OBJDIR)/dep
|
DEPDIR::=$(OBJDIR)/dep
|
||||||
LIBDIR::=lib
|
LIBDIR::=lib
|
||||||
INCLUDE_DIRS::=include
|
INCLUDE_DIRS::=include
|
||||||
CXXFLAGS::=-g -std=c++17 -Wall -pedantic -Wextra
|
CXXFLAGS::=-std=c++17 -Wall -pedantic -Wextra
|
||||||
EXT::=cpp
|
EXT::=cpp
|
||||||
MAIN_LIBRARY::=rmatrix
|
MAIN_LIBRARY::=rmatrix
|
||||||
SHARED?=1
|
SHARED?=0
|
||||||
|
RELEASE?=0
|
||||||
|
|
||||||
ifneq ($(WINDOWS),1)
|
ifneq ($(WINDOWS),1)
|
||||||
CXX::=g++
|
CXX::=g++
|
||||||
@ -58,9 +59,11 @@ else
|
|||||||
INTERNAL_MAIN_LIBRARY::=lib$(MAIN_LIBRARY).a
|
INTERNAL_MAIN_LIBRARY::=lib$(MAIN_LIBRARY).a
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(RELEASE),1)
|
||||||
all: CXXFLAGS+=-O0
|
CXXFLAGS+=-O2
|
||||||
release: CXXFLAGS+=-O2
|
else
|
||||||
|
CXXFLAGS+=-O0 -g
|
||||||
|
endif
|
||||||
|
|
||||||
memchk: LDFLAGS+=-fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
|
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
|
memchk: CXXFLAGS+=-O0 -fsanitize=address -fno-omit-frame-pointer -fno-optimize-sibling-calls
|
||||||
@ -107,9 +110,7 @@ $(INTERNAL_MAIN_LIBRARY): $(OBJECTS)
|
|||||||
endif #shared
|
endif #shared
|
||||||
|
|
||||||
.PHONY: memchk
|
.PHONY: memchk
|
||||||
.PHONY: release
|
|
||||||
memchk: all
|
memchk: all
|
||||||
release: all
|
|
||||||
|
|
||||||
.PHONY: utils
|
.PHONY: utils
|
||||||
utils: matrix-send
|
utils: matrix-send
|
||||||
|
|||||||
@ -194,6 +194,8 @@ namespace matrix{
|
|||||||
if(!root)
|
if(!root)
|
||||||
return {};
|
return {};
|
||||||
RJP_search_res res = rjp_search_member(root.get(), target.get(), 0);
|
RJP_search_res res = rjp_search_member(root.get(), target.get(), 0);
|
||||||
|
if(!res.value)
|
||||||
|
return {};
|
||||||
if(rjp_value_type(res.value) != json_string)
|
if(rjp_value_type(res.value) != json_string)
|
||||||
return {};
|
return {};
|
||||||
return raii::rjp_string(res.value);
|
return raii::rjp_string(res.value);
|
||||||
@ -222,7 +224,7 @@ namespace matrix{
|
|||||||
netreturn_base connection::_create_netreturn(const raii::rjp_ptr& root, int httpstatus){
|
netreturn_base connection::_create_netreturn(const raii::rjp_ptr& root, int httpstatus){
|
||||||
if(!root)
|
if(!root)
|
||||||
return netreturn_base("Invalid JSON"_ss, "Invalid JSON"_ss, -1);
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -94,7 +94,11 @@ namespace matrix::sync{
|
|||||||
return m_root.get();
|
return m_root.get();
|
||||||
}
|
}
|
||||||
raii::rjp_string response::raw_str(void)const{
|
raii::rjp_string response::raw_str(void)const{
|
||||||
|
#ifndef RJP_FORMAT_NONE
|
||||||
return raii::rjp_string(rjp_to_json(m_root.get()));
|
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_value* response::_find_room_list(const char* segment)const{
|
||||||
RJP_search_res res = rjp_search_member(m_root.get(), "rooms", 0);
|
RJP_search_res res = rjp_search_member(m_root.get(), "rooms", 0);
|
||||||
|
|||||||
@ -23,11 +23,20 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdio>
|
#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){
|
void check_netreturn_errors(const matrix::netreturn<raii::rjp_string>& n){
|
||||||
printf("Usage: matrix-send STRING\n");
|
if(n.has_httperror()){
|
||||||
printf("\tSends message to designated room as the designated user\n");
|
fprintf(stderr, "http error %d\n", n.httpstatus());
|
||||||
exit(status);
|
}
|
||||||
|
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){
|
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(in);
|
||||||
}
|
}
|
||||||
data.push_back(0);
|
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){
|
int main(int argc, char** argv){
|
||||||
const char* username = "username";
|
if(argc < 2){
|
||||||
const char* password = "password";
|
fprintf(stderr, "Missing argument\n");
|
||||||
const char* useragent = "rexy712s test bot";
|
return 1;
|
||||||
const char* homeserver = "matrix.org";
|
}
|
||||||
const char* roomid = "!room:matrix.org";
|
|
||||||
matrix::auth_data auth{username, password, homeserver, useragent};
|
|
||||||
|
|
||||||
|
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);
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto client = ses.spawn_client();
|
auto client = ses.spawn_client();
|
||||||
auto room = client.spawn_room(raii::static_string(roomid));
|
auto room = client.spawn_room(raii::static_string(roomid));
|
||||||
if(argc < 2 || !strcmp(argv[1], "-")){
|
|
||||||
|
if(argc < 3){
|
||||||
do_stdin(room);
|
do_stdin(room);
|
||||||
}else{
|
}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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user