Updated to new rjp api

This commit is contained in:
rexy712 2020-02-24 14:29:46 -08:00
parent 098642152e
commit d2f88d439c
59 changed files with 539 additions and 222 deletions

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -57,7 +57,7 @@ namespace matrix::sync{
private: private:
raii::rjp_object_iterator m_room; raii::rjp_object_iterator m_room;
public: public:
constexpr room_iterator(RJP_value* r):m_room(r){} room_iterator(RJP_value* r):m_room(r){}
constexpr bool operator==(const room_iterator& r){ constexpr bool operator==(const room_iterator& r){
return r.m_room == m_room; return r.m_room == m_room;
} }
@ -68,7 +68,7 @@ namespace matrix::sync{
++m_room; ++m_room;
return *this; return *this;
} }
constexpr room_event_response operator*(void){ room_event_response operator*(void){
return room_event_response(*m_room); return room_event_response(*m_room);
} }
}; };
@ -84,8 +84,8 @@ namespace matrix::sync{
private: private:
iterator m_room; iterator m_room;
public: public:
constexpr room_list(RJP_value* room):m_room(room){} room_list(RJP_value* room):m_room(room){}
constexpr room_list(const room_list& r):m_room(r.m_room){} room_list(const room_list& r):m_room(r.m_room){}
constexpr iterator& begin(void){ constexpr iterator& begin(void){
return m_room; return m_room;
@ -93,7 +93,7 @@ namespace matrix::sync{
constexpr const_iterator& begin(void)const{ constexpr const_iterator& begin(void)const{
return m_room; return m_room;
} }
constexpr const_iterator end(void)const{ const_iterator end(void)const{
return const_iterator(nullptr); return const_iterator(nullptr);
} }
}; };
@ -106,10 +106,16 @@ namespace matrix::sync{
device_list(RJP_value* root): device_list(RJP_value* root):
m_root(root){} m_root(root){}
event_list left(void)const{ event_list left(void)const{
return rjp_get_element(rjp_search_member(m_root, "left", 0).value); RJP_array_iterator it;
RJP_value* ev = rjp_search_member(m_root, "left");
rjp_init_array_iterator(&it, ev);
return rjp_array_iterator_current(&it);
} }
event_list changed(void)const{ event_list changed(void)const{
return rjp_get_element(rjp_search_member(m_root, "changed", 0).value); RJP_array_iterator it;
RJP_value* ev = rjp_search_member(m_root, "changed");
rjp_init_array_iterator(&it, ev);
return rjp_array_iterator_current(&it);
} }
}; };

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -20,75 +20,117 @@
#define RJP_ITERATOR_HPP #define RJP_ITERATOR_HPP
#include <rjp.h> #include <rjp.h>
#include <utility> //exchange
namespace raii{ namespace raii{
class rjp_object_iterator class rjp_object_iterator
{ {
public: public:
using iterator = RJP_value*; using iterator = RJP_object_iterator;
using const_iterator = const iterator; using const_iterator = const iterator;
private: private:
RJP_value* m_value; RJP_value* m_root;
RJP_object_iterator m_obj = {};
public: public:
constexpr rjp_object_iterator(RJP_value* v):m_value(v){} rjp_object_iterator(RJP_value* v):m_root(v){
constexpr rjp_object_iterator(const rjp_object_iterator& e):m_value(e.m_value){} if(m_root)
constexpr rjp_object_iterator& operator=(const rjp_object_iterator& e){ rjp_init_object_iterator(&m_obj, m_root);
m_value = e.m_value; }
rjp_object_iterator(const rjp_object_iterator& e):
rjp_object_iterator(e.m_root){}
rjp_object_iterator(rjp_object_iterator&& e):
m_root(std::exchange(e.m_root, nullptr)),
m_obj(e.m_obj)
{
e.m_obj = RJP_object_iterator{0};
}
rjp_object_iterator& operator=(rjp_object_iterator&& e){
std::swap(m_root, e.m_root);
std::swap(m_obj, e.m_obj);
return *this; return *this;
} }
rjp_object_iterator& operator=(const rjp_object_iterator& e){
rjp_object_iterator it(e);
*this = std::move(it);
return *this;
}
~rjp_object_iterator(void){
if(m_root)
rjp_delete_object_iterator(&m_obj);
}
constexpr bool operator==(const rjp_object_iterator& e)const{ constexpr bool operator==(const rjp_object_iterator& e)const{
return m_value == e.m_value; RJP_value* l = m_root, *r = e.m_root;
if(l)
l = rjp_object_iterator_current(&m_obj);
if(r)
r = rjp_object_iterator_current(&e.m_obj);
return l == r;
} }
constexpr bool operator!=(const rjp_object_iterator& e)const{ constexpr bool operator!=(const rjp_object_iterator& e)const{
return m_value != e.m_value; return !(*this == e);
} }
rjp_object_iterator& operator++(void); rjp_object_iterator& operator++(void);
constexpr RJP_value* operator*(void){ RJP_value* operator*(void){
return m_value; return rjp_object_iterator_current(&m_obj);
} }
constexpr const RJP_value* operator*(void)const{ const RJP_value* operator*(void)const{
return m_value; return rjp_object_iterator_current(&m_obj);
}
constexpr RJP_value& operator->(void){
return *m_value;
}
constexpr const RJP_value& operator->(void)const{
return *m_value;
} }
}; };
class rjp_array_iterator class rjp_array_iterator
{ {
public: public:
using iterator = RJP_value*; using iterator = RJP_array_iterator*;
using const_iterator = const iterator; using const_iterator = const iterator;
private: private:
RJP_value* m_value; RJP_value* m_root;
RJP_array_iterator m_arr;
public: public:
constexpr rjp_array_iterator(RJP_value* v):m_value(v){} rjp_array_iterator(RJP_value* v):m_root(v){
constexpr rjp_array_iterator(const rjp_array_iterator& e):m_value(e.m_value){} if(m_root)
constexpr rjp_array_iterator& operator=(const rjp_array_iterator& e){ rjp_init_array_iterator(&m_arr, m_root);
m_value = e.m_value; }
rjp_array_iterator(rjp_array_iterator&& e):
m_root(std::exchange(e.m_root, nullptr))
{
m_arr = RJP_array_iterator{0};
}
rjp_array_iterator(const rjp_array_iterator& e):
rjp_array_iterator(e.m_root){}
rjp_array_iterator& operator=(rjp_array_iterator&& e){
std::swap(m_root, e.m_root);
std::swap(m_arr, e.m_arr);
return *this; return *this;
} }
rjp_array_iterator& operator=(const rjp_array_iterator& e){
rjp_array_iterator it(e);
*this = std::move(it);
return *this;
}
~rjp_array_iterator(void){
if(m_root)
rjp_delete_array_iterator(&m_arr);
}
constexpr bool operator==(const rjp_array_iterator& e)const{ constexpr bool operator==(const rjp_array_iterator& e)const{
return m_value == e.m_value; RJP_value* l = m_root, *r = e.m_root;
if(l)
l = rjp_array_iterator_current(&m_arr);
if(r)
r = rjp_array_iterator_current(&e.m_arr);
return l == r;
} }
constexpr bool operator!=(const rjp_array_iterator& e)const{ constexpr bool operator!=(const rjp_array_iterator& e)const{
return m_value != e.m_value; return !(*this == e);
} }
rjp_array_iterator& operator++(void); rjp_array_iterator& operator++(void);
constexpr RJP_value* operator*(void){ RJP_value* operator*(void){
return m_value; return rjp_array_iterator_current(&m_arr);
} }
constexpr const RJP_value* operator*(void)const{ const RJP_value* operator*(void)const{
return m_value; return rjp_array_iterator_current(&m_arr);
}
constexpr RJP_value& operator->(void){
return *m_value;
}
constexpr const RJP_value& operator->(void)const{
return *m_value;
} }
}; };

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -48,19 +48,30 @@ namespace raii{
using string_intermediary<detail::rjp_allocator>::string_intermediary; using string_intermediary<detail::rjp_allocator>::string_intermediary;
rjp_string(RJP_value* r): rjp_string(RJP_value* r):
string_intermediary<detail::rjp_allocator>(r ? std::exchange(r->string.value, nullptr) : nullptr, r ? r->string.length : 0, r ? r->string.length : 0){} string_intermediary<detail::rjp_allocator>(r ? rjp_get_string(r)->value : nullptr,
r ? rjp_get_string(r)->length : 0,
r ? rjp_get_string(r)->length : 0)
{
if(r){
RJP_string* str = rjp_get_string(r);
str->value = nullptr;
str->length = 0;
rjp_set_null(r);
}
}
using string_intermediary<detail::rjp_allocator>::operator=; using string_intermediary<detail::rjp_allocator>::operator=;
rjp_string& operator=(RJP_value* r){ rjp_string& operator=(RJP_value* r){
if(!r) if(!r)
return *this; return *this;
reset(); reset();
m_data = std::exchange(r->string.value, nullptr); RJP_string* str = rjp_get_string(r);
m_length = r->string.length; m_data = std::exchange(str->value, nullptr);
m_cap = r->string.length; m_length = str->length;
m_cap = str->length;
str->length = 0;
return *this; return *this;
} }
}; };
rjp_string rjp_string_from_key(RJP_value* val);
} }
#endif #endif

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -11,7 +11,7 @@
#You should have received a copy of the GNU General Public License #You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>. #along with this program. If not, see <http://www.gnu.org/licenses/>.
#Copyright 2018-2019 rexy712 #Copyright 2018-2020 rexy712
ifeq ($(OS),Windows_NT) ifeq ($(OS),Windows_NT)

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -72,13 +72,16 @@ namespace matrix{
if(!root) if(!root)
return retval; return retval;
RJP_search_res res = rjp_search_member(root.get(), json::keys::joinedrooms(), 0); RJP_value* res = rjp_search_member(root.get(), json::keys::joinedrooms());
if(!res.value) if(!res)
return retval; return retval;
for(RJP_value* v = rjp_get_element(res.value);v;v = rjp_next_element(v)){ RJP_array_iterator it;
rjp_init_array_iterator(&it, res);
for(RJP_value* v = rjp_array_iterator_current(&it);v;v = rjp_array_iterator_next(&it)){
retval.value().emplace_back(v); retval.value().emplace_back(v);
} }
rjp_delete_array_iterator(&it);
return retval; return retval;
} }
@ -182,8 +185,8 @@ namespace matrix{
if(!root) if(!root)
return retval; return retval;
RJP_search_res res = rjp_search_member(root.get(), "next_batch", 0); RJP_value* res = rjp_search_member(root.get(), "next_batch");
if(!res.value) if(!res)
return retval; return retval;
retval.value() = sync::client_response(root.release()); retval.value() = sync::client_response(root.release());
return retval; return retval;
@ -228,10 +231,10 @@ namespace matrix{
netreturn<uploaded_file> retval = _create_netreturn(root, http_status()); netreturn<uploaded_file> retval = _create_netreturn(root, http_status());
if(!root) if(!root)
return retval; return retval;
RJP_search_res res = rjp_search_member(root.get(), json::keys::contenturi(), 0); RJP_value* res = rjp_search_member(root.get(), json::keys::contenturi());
if(!res.value) if(!res)
return retval; return retval;
retval.value().m_fileurl = res.value; retval.value().m_fileurl = res;
retval.value().m_filename = file.name; retval.value().m_filename = file.name;
retval.value().m_filesize = file.data.size(); retval.value().m_filesize = file.data.size();
return retval; return retval;

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -193,12 +193,12 @@ namespace matrix{
raii::rjp_string connection::_curl_reply_search(const raii::rjp_ptr& root, const raii::string_base& target)const{ raii::rjp_string connection::_curl_reply_search(const raii::rjp_ptr& root, const raii::string_base& target)const{
if(!root) if(!root)
return {}; return {};
RJP_search_res res = rjp_search_member(root.get(), target.get(), 0); RJP_value* res = rjp_search_member(root.get(), target.get());
if(!res.value) if(!res)
return {}; return {};
if(rjp_value_type(res.value) != json_string) if(rjp_value_type(res) != rjp_json_string)
return {}; return {};
return raii::rjp_string(res.value); return raii::rjp_string(res);
} }
void connection::_set_curl_defaults(const raii::string_base& useragent)const{ void connection::_set_curl_defaults(const raii::string_base& useragent)const{
@ -224,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(), "errcode", 0).value, httpstatus); return netreturn_base(rjp_search_member(root.get(), "error"), rjp_search_member(root.get(), "errcode"), httpstatus);
} }
} }

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -24,14 +24,14 @@ namespace matrix::sync{
//Event base //Event base
event::event(RJP_value* ev): event::event(RJP_value* ev):
m_event(ev), m_event(ev),
m_type(rjp_search_member(ev, json::keys::event::type(), 0).value), m_type(ev ? rjp_search_member(ev, json::keys::event::type()) : nullptr),
m_content(rjp_search_member(ev, json::keys::event::content(), 0).value){} m_content(ev ? rjp_search_member(ev, json::keys::event::content()) : nullptr){}
event::event(event&& ev)noexcept: event::event(event&& ev)noexcept:
m_event(std::exchange(ev.m_event, nullptr)), m_event(std::exchange(ev.m_event, nullptr)),
m_type(std::exchange(ev.m_type, nullptr)), m_type(std::exchange(ev.m_type, nullptr)),
m_content(std::exchange(ev.m_content, nullptr)){} m_content(std::exchange(ev.m_content, nullptr)){}
raii::static_string event::type(void)const&{ raii::static_string event::type(void)const&{
return raii::static_string(rjp_value_string(m_type), rjp_value_string_length(m_type)); return raii::static_string(rjp_get_cstring(m_type)->value, rjp_get_cstring(m_type)->length);
} }
raii::rjp_string event::type(void)&&{ raii::rjp_string event::type(void)&&{
return raii::rjp_string(std::exchange(m_type, nullptr)); return raii::rjp_string(std::exchange(m_type, nullptr));
@ -51,12 +51,16 @@ namespace matrix::sync{
//Room event //Room event
room_event::room_event(RJP_value* ev, const raii::string_base& roomid): room_event::room_event(RJP_value* ev, const raii::string_base& roomid):
room_event_base(roomid), event(ev), room_event_base(roomid), event(ev)
m_id(rjp_search_member(ev, json::keys::event::eventid(), 0).value), {
m_sender(rjp_search_member(ev, json::keys::event::sender(), 0).value), if(!ev)
m_unsigned(rjp_search_member(ev, json::keys::event::extra(), 0).value), return;
m_redacts(rjp_search_member(ev, json::keys::event::redacts(), 0).value), m_id = rjp_search_member(ev, json::keys::event::eventid());
m_origin_server_ts(rjp_value_integer(rjp_search_member(ev, json::keys::event::origin_server_ts(), 0).value)){} m_sender = rjp_search_member(ev, json::keys::event::sender());
m_unsigned = rjp_search_member(ev, json::keys::event::extra());
m_redacts = rjp_search_member(ev, json::keys::event::redacts());
m_origin_server_ts = rjp_get_int(rjp_search_member(ev, json::keys::event::origin_server_ts()));
}
room_event::room_event(room_event&& ev)noexcept: room_event::room_event(room_event&& ev)noexcept:
room_event_base(std::move(ev)), event(std::move(ev)), room_event_base(std::move(ev)), event(std::move(ev)),
m_id(std::exchange(ev.m_id, nullptr)), m_id(std::exchange(ev.m_id, nullptr)),
@ -65,14 +69,14 @@ namespace matrix::sync{
m_redacts(std::exchange(ev.m_redacts, nullptr)), m_redacts(std::exchange(ev.m_redacts, nullptr)),
m_origin_server_ts(ev.m_origin_server_ts){} m_origin_server_ts(ev.m_origin_server_ts){}
raii::static_string room_event::eventid(void)const&{ raii::static_string room_event::eventid(void)const&{
return raii::static_string(rjp_value_string(m_id), rjp_value_string_length(m_id)); return raii::static_string(rjp_get_cstring(m_id)->value, rjp_get_cstring(m_id)->length);
} }
raii::rjp_string room_event::eventid(void)&&{ raii::rjp_string room_event::eventid(void)&&{
return raii::rjp_string(std::exchange(m_id, nullptr)); return raii::rjp_string(std::exchange(m_id, nullptr));
} }
raii::static_string room_event::sender(void)const&{ raii::static_string room_event::sender(void)const&{
return raii::static_string(rjp_value_string(m_sender), rjp_value_string_length(m_sender)); return raii::static_string(rjp_get_cstring(m_sender)->value, rjp_get_cstring(m_sender)->length);
} }
raii::rjp_string room_event::sender(void)&&{ raii::rjp_string room_event::sender(void)&&{
return raii::rjp_string(std::exchange(m_sender, nullptr)); return raii::rjp_string(std::exchange(m_sender, nullptr));
@ -89,7 +93,7 @@ namespace matrix::sync{
raii::static_string room_event::redacts(void)const&{ raii::static_string room_event::redacts(void)const&{
if(!m_redacts) if(!m_redacts)
return raii::static_string(); return raii::static_string();
return raii::static_string(rjp_value_string(m_redacts), rjp_value_string_length(m_redacts)); return raii::static_string(rjp_get_cstring(m_redacts)->value, rjp_get_cstring(m_redacts)->length);
} }
raii::rjp_string room_event::redacts(void)&&{ raii::rjp_string room_event::redacts(void)&&{
if(!m_redacts) if(!m_redacts)
@ -99,22 +103,24 @@ namespace matrix::sync{
//Room state event //Room state event
room_state_event::room_state_event(RJP_value* ev, const raii::string_base& roomid): room_state_event::room_state_event(RJP_value* ev, const raii::string_base& roomid):
room_event(ev, roomid), room_event(ev, roomid)
m_state_key(rjp_search_member(ev, json::keys::event::state_key(), 0).value),
m_prev_content(rjp_search_member(ev, json::keys::event::prev_content(), 0).value)
{ {
if(!ev)
return;
m_state_key = rjp_search_member(ev, json::keys::event::state_key());
m_prev_content = rjp_search_member(ev, json::keys::event::prev_content());
if(!m_prev_content && m_unsigned){ if(!m_prev_content && m_unsigned){
m_prev_content = rjp_search_member(m_unsigned, json::keys::event::prev_content(), 0).value; m_prev_content = rjp_search_member(m_unsigned, json::keys::event::prev_content());
} }
} }
raii::static_string room_state_event::state_key(void)const&{ raii::static_string room_state_event::state_key(void)const&{
return raii::static_string(rjp_value_string(m_state_key), rjp_value_string_length(m_state_key)); return raii::static_string(rjp_get_cstring(m_state_key)->value, rjp_get_cstring(m_state_key)->length);
} }
raii::rjp_string room_state_event::state_key(void)&&{ raii::rjp_string room_state_event::state_key(void)&&{
return raii::rjp_string(std::exchange(m_state_key, nullptr)); return raii::rjp_string(std::exchange(m_state_key, nullptr));
} }
raii::static_string room_state_event::prev_content(void)const&{ raii::static_string room_state_event::prev_content(void)const&{
return raii::static_string(rjp_value_string(m_prev_content), rjp_value_string_length(m_prev_content)); return raii::static_string(rjp_get_cstring(m_prev_content)->value, rjp_get_cstring(m_prev_content)->length);
} }
raii::rjp_string room_state_event::prev_content(void)&&{ raii::rjp_string room_state_event::prev_content(void)&&{
return raii::rjp_string(std::exchange(m_prev_content, nullptr)); return raii::rjp_string(std::exchange(m_prev_content, nullptr));
@ -126,20 +132,20 @@ namespace matrix::sync{
//Room message event //Room message event
room_message_event::room_message_event(RJP_value* ev, const raii::string_base& roomid): room_message_event::room_message_event(RJP_value* ev, const raii::string_base& roomid):
room_event(ev, roomid), room_event(ev, roomid),
m_body(rjp_search_member(m_content, json::keys::event::contentbody::body(), 0).value), m_body(rjp_search_member(m_content, json::keys::event::contentbody::body())),
m_type(rjp_search_member(m_content, json::keys::event::contentbody::msgtype(), 0).value){} m_type(rjp_search_member(m_content, json::keys::event::contentbody::msgtype())){}
room_message_event::room_message_event(room_event&& ev)noexcept: room_message_event::room_message_event(room_event&& ev)noexcept:
room_event(std::move(ev)), room_event(std::move(ev)),
m_body(rjp_search_member(m_content, json::keys::event::contentbody::body(), 0).value), m_body(rjp_search_member(m_content, json::keys::event::contentbody::body())),
m_type(rjp_search_member(m_content, json::keys::event::contentbody::msgtype(), 0).value){} m_type(rjp_search_member(m_content, json::keys::event::contentbody::msgtype())){}
raii::static_string room_message_event::body(void)const&{ raii::static_string room_message_event::body(void)const&{
return raii::static_string(rjp_value_string(m_body), rjp_value_string_length(m_body)); return raii::static_string(rjp_get_cstring(m_body)->value, rjp_get_cstring(m_body)->length);
} }
raii::rjp_string room_message_event::body(void)&&{ raii::rjp_string room_message_event::body(void)&&{
return raii::rjp_string(m_body); return raii::rjp_string(m_body);
} }
raii::static_string room_message_event::msgtype(void)const&{ raii::static_string room_message_event::msgtype(void)const&{
return raii::static_string(rjp_value_string(m_type), rjp_value_string_length(m_type)); return raii::static_string(rjp_get_cstring(m_type)->value, rjp_get_cstring(m_type)->length);
} }
raii::rjp_string room_message_event::msgtype(void)&&{ raii::rjp_string room_message_event::msgtype(void)&&{
return raii::rjp_string(m_type); return raii::rjp_string(m_type);

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -25,21 +25,21 @@ namespace matrix::sync{
m_root(root.release()){} m_root(root.release()){}
template<class T> template<class T>
raii::static_string roomcxn_event_list_base<T>::start_token(void)const&{ raii::static_string roomcxn_event_list_base<T>::start_token(void)const&{
RJP_value* st = rjp_search_member(m_root.get(), "start", 0).value; RJP_value* st = rjp_search_member(m_root.get(), "start");
return raii::static_string(rjp_value_string(st), rjp_value_string_length(st)); return raii::static_string(rjp_get_cstring(st)->value, rjp_get_cstring(st)->length);
} }
template<class T> template<class T>
raii::rjp_string roomcxn_event_list_base<T>::start_token(void)&&{ raii::rjp_string roomcxn_event_list_base<T>::start_token(void)&&{
return raii::rjp_string(rjp_search_member(m_root.get(), "start", 0).value); return raii::rjp_string(rjp_search_member(m_root.get(), "start"));
} }
template<class T> template<class T>
raii::static_string roomcxn_event_list_base<T>::end_token(void)const&{ raii::static_string roomcxn_event_list_base<T>::end_token(void)const&{
RJP_value* st = rjp_search_member(m_root.get(), "end", 0).value; RJP_value* st = rjp_search_member(m_root.get(), "end");
return raii::static_string(rjp_value_string(st), rjp_value_string_length(st)); return raii::static_string(rjp_get_cstring(st)->value, rjp_get_cstring(st)->length);
} }
template<class T> template<class T>
raii::rjp_string roomcxn_event_list_base<T>::end_token(void)&&{ raii::rjp_string roomcxn_event_list_base<T>::end_token(void)&&{
return raii::rjp_string(rjp_search_member(m_root.get(), "end", 0).value); return raii::rjp_string(rjp_search_member(m_root.get(), "end"));
} }
template class roomcxn_event_list_base<room_state_event>; template class roomcxn_event_list_base<room_state_event>;

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -59,11 +59,12 @@ namespace matrix{
if(!resp) return _create_netreturn(resp, http_status()); if(!resp) return _create_netreturn(resp, http_status());
raii::rjp_ptr root(rjp_parse(resp.get())); raii::rjp_ptr root(rjp_parse(resp.get()));
if(!root) return _create_netreturn(root, http_status()); if(!root) return _create_netreturn(root, http_status());
RJP_search_res res = rjp_search_member(root.get(), json::keys::joined(), 0); RJP_value* res = rjp_search_member(root.get(), json::keys::joined());
if(!res.value) return _create_netreturn(root, http_status()); if(!res) return _create_netreturn(root, http_status());
for(RJP_value* mem = rjp_get_member(res.value);mem;mem = rjp_next_member(mem)){ raii::rjp_object_iterator it(res);
raii::rjp_string tmp = raii::rjp_string_from_key(mem); for(raii::rjp_object_iterator it = res;*it;++it){
raii::rjp_string tmp(rjp_member_key(*it)->value, rjp_member_key(*it)->length);
retval.value().emplace_back(std::move(tmp)); retval.value().emplace_back(std::move(tmp));
} }
return retval; return retval;
@ -130,7 +131,7 @@ namespace matrix{
return _send_message(json::_audio_body(audio)); return _send_message(json::_audio_body(audio));
} }
netreturn<raii::rjp_string> roomcxn::forward_event(const sync::room_event& event)const{ netreturn<raii::rjp_string> roomcxn::forward_event(const sync::room_event& event)const{
raii::rjp_string content = rjp_to_json(event.content()); raii::rjp_string content = rjp_to_json(event.content(), RJP_FORMAT_NONE);
return send_custom_event(content, event.type()); return send_custom_event(content, event.type());
} }
netreturn<void> roomcxn::send_typing(bool active, int timeout)const{ netreturn<void> roomcxn::send_typing(bool active, int timeout)const{
@ -163,9 +164,10 @@ namespace matrix{
if(!root.get()) _create_netreturn(root, http_status()); if(!root.get()) _create_netreturn(root, http_status());
netreturn<sync::roomcxn_message_event_list> retval = _create_netreturn(root, http_status()); netreturn<sync::roomcxn_message_event_list> retval = _create_netreturn(root, http_status());
RJP_value* chunk = rjp_search_member(root.get(), json::keys::chunk(), 0).value; RJP_value* chunk = rjp_search_member(root.get(), json::keys::chunk());
if(!chunk) return retval; if(!chunk) return retval;
retval.value() = sync::roomcxn_message_event_list(root, rjp_get_element(chunk), m_roomid); raii::rjp_array_iterator it(chunk);
retval.value() = sync::roomcxn_message_event_list(root, *it, m_roomid);
return retval; return retval;
} }
netreturn<sync::roomcxn_message_event_list> roomcxn::get_events_forward(int amount){ netreturn<sync::roomcxn_message_event_list> roomcxn::get_events_forward(int amount){

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -88,8 +88,8 @@ namespace matrix{
return retval; return retval;
//attempt to change password via username/password login //attempt to change password via username/password login
RJP_search_res res = rjp_search_member(root.get(), json::keys::session(), 0); RJP_value* res = rjp_search_member(root.get(), json::keys::session());
raii::string request = json::_change_psk_password(m_ses->userid, newpass, oldpass, raii::rjp_string(res.value)); raii::string request = json::_change_psk_password(m_ses->userid, newpass, oldpass, raii::rjp_string(res));
reply = _post_curl(request, rest::session_urls::password(*m_ses), raii::curl_llist()); reply = _post_curl(request, rest::session_urls::password(*m_ses), raii::curl_llist());
return _create_netreturn(reply, http_status()); return _create_netreturn(reply, http_status());
} }
@ -159,10 +159,10 @@ namespace matrix{
netreturn<std::pair<raii::rjp_string,raii::rjp_string>> retval = _create_netreturn(root, http_status()); netreturn<std::pair<raii::rjp_string,raii::rjp_string>> retval = _create_netreturn(root, http_status());
if(!root) if(!root)
return retval; return retval;
RJP_search_res token = rjp_search_member(root.get(), json::keys::auth::accesstoken(), 0); RJP_value* token = rjp_search_member(root.get(), json::keys::auth::accesstoken());
retval.value().first = raii::rjp_string{token.value}; retval.value().first = raii::rjp_string{token};
token = rjp_search_member(root.get(), json::keys::auth::userid(), 0); token = rjp_search_member(root.get(), json::keys::auth::userid());
retval.value().second = raii::rjp_string{token.value}; retval.value().second = raii::rjp_string{token};
return retval; return retval;
} }
} }

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -22,16 +22,13 @@ namespace matrix::sync{
//Room event response //Room event response
raii::static_string room_event_response::roomid(void)const{ raii::static_string room_event_response::roomid(void)const{
return raii::static_string(rjp_member_name(m_room), rjp_member_name_length(m_room)); return raii::static_string(rjp_member_key(m_room)->value, rjp_member_key(m_room)->length);
} }
RJP_value* room_event_response::_find_event_list(const char* mname)const{ RJP_value* room_event_response::_find_event_list(const char* mname)const{
RJP_search_res res = rjp_search_member(m_room, mname, 0); RJP_value* res = rjp_search_member(m_room, mname);
if(!res.value) if(!res)
return nullptr; return nullptr;
res = rjp_search_member(res.value, "events", 0); return rjp_search_member(res, "events");
if(!res.value)
return nullptr;
return rjp_get_element(res.value);
} }
room_event_list room_event_response::account_events(void){ room_event_list room_event_response::account_events(void){
return room_event_list(_find_event_list("account_data"), roomid()); return room_event_list(_find_event_list("account_data"), roomid());
@ -46,10 +43,10 @@ namespace matrix::sync{
return room_state_event_list(_find_event_list("timeline"), roomid()); return room_state_event_list(_find_event_list("timeline"), roomid());
} }
RJP_value* room_event_response::notifications(void){ RJP_value* room_event_response::notifications(void){
return rjp_search_member(m_room, "unread_notifications", 0).value; return rjp_search_member(m_room, "unread_notifications");
} }
RJP_value* room_event_response::summary(void){ RJP_value* room_event_response::summary(void){
return rjp_search_member(m_room, "summary", 0).value; return rjp_search_member(m_room, "summary");
} }
//Sync response //Sync response
@ -67,24 +64,24 @@ namespace matrix::sync{
return _find_room_list("leave"); return _find_room_list("leave");
} }
device_list client_response::device_lists(void)const{ device_list client_response::device_lists(void)const{
return rjp_search_member(m_root.get(), "device_lists", 0).value; return rjp_search_member(m_root.get(), "device_lists");
} }
raii::static_string client_response::next_batch(void)const&{ raii::static_string client_response::next_batch(void)const&{
RJP_value* nb = rjp_search_member(m_root.get(), "next_batch", 0).value; RJP_value* nb = rjp_search_member(m_root.get(), "next_batch");
if(!nb) return {}; if(!nb) return {};
return raii::static_string(rjp_value_string(nb), rjp_value_string_length(nb)); return raii::static_string(rjp_get_cstring(nb)->value, rjp_get_cstring(nb)->length);
} }
raii::rjp_string client_response::next_batch(void)&&{ raii::rjp_string client_response::next_batch(void)&&{
return raii::rjp_string(rjp_search_member(m_root.get(), "next_batch", 0).value); return raii::rjp_string(rjp_search_member(m_root.get(), "next_batch"));
} }
event_list client_response::to_device_events(void)const{ event_list client_response::to_device_events(void)const{
return rjp_search_member(rjp_search_member(m_root.get(), "to_device", 0).value, "events", 0).value; return rjp_search_member(rjp_search_member(m_root.get(), "to_device"), "events");
} }
event_list client_response::presence_events(void)const{ event_list client_response::presence_events(void)const{
return rjp_search_member(rjp_search_member(m_root.get(), "presence", 0).value, "events", 0).value; return rjp_search_member(rjp_search_member(m_root.get(), "presence"), "events");
} }
RJP_value* client_response::device_one_time_keys_count(void)const{ RJP_value* client_response::device_one_time_keys_count(void)const{
return rjp_search_member(m_root.get(), "device_one_time_keys_count", 0).value; return rjp_search_member(m_root.get(), "device_one_time_keys_count");
} }
RJP_value* client_response::raw_handle(void){ RJP_value* client_response::raw_handle(void){
@ -94,19 +91,12 @@ namespace matrix::sync{
return m_root.get(); return m_root.get();
} }
raii::rjp_string client_response::raw_str(void)const{ raii::rjp_string client_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)); return raii::rjp_string(rjp_to_json(m_root.get(), RJP_FORMAT_NONE));
#endif
} }
RJP_value* client_response::_find_room_list(const char* segment)const{ RJP_value* client_response::_find_room_list(const char* segment)const{
RJP_search_res res = rjp_search_member(m_root.get(), "rooms", 0); RJP_value* res = rjp_search_member(m_root.get(), "rooms");
if(!res.value) if(!res)
return nullptr; return nullptr;
res = rjp_search_member(res.value, segment, 0); return rjp_search_member(res, segment);
if(!res.value)
return nullptr;
return rjp_get_member(res.value);
} }
} }

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -21,11 +21,11 @@
namespace raii{ namespace raii{
rjp_object_iterator& rjp_object_iterator::operator++(void){ rjp_object_iterator& rjp_object_iterator::operator++(void){
m_value = rjp_next_member(m_value); rjp_object_iterator_next(&m_obj);
return *this; return *this;
} }
rjp_array_iterator& rjp_array_iterator::operator++(void){ rjp_array_iterator& rjp_array_iterator::operator++(void){
m_value = rjp_next_element(m_value); rjp_array_iterator_next(&m_arr);
return *this; return *this;
} }

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -32,10 +32,4 @@ namespace raii{
return tmp; return tmp;
} }
} }
rjp_string rjp_string_from_key(RJP_value* val){
char* key = rjp_member_name(val);
size_t len = rjp_member_name_length(val);
rjp_set_key(val, nullptr, 0);
return rjp_string(key, len);
}
} }

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -23,11 +23,13 @@
#include "matrix/sync_response.hpp" #include "matrix/sync_response.hpp"
#include "raii/util.hpp" #include "raii/util.hpp"
#include "matrix/json_targets.hpp" #include "matrix/json_targets.hpp"
#include "matrix/events.hpp"
#include <atomic> #include <atomic>
#include <thread> #include <thread>
#include <future> #include <future>
#include <chrono> #include <chrono>
#include <cctype>
matrix::auth_data read_auth_file(const char* filename){ matrix::auth_data read_auth_file(const char* filename){
raii::filerd fp(filename, "r"); raii::filerd fp(filename, "r");
@ -38,26 +40,26 @@ matrix::auth_data read_auth_file(const char* filename){
if(!root.get()) if(!root.get())
return {}; return {};
raii::rjp_string user = rjp_search_member(root.get(), "user", 0).value; raii::rjp_string user = rjp_search_member(root.get(), "user");
raii::rjp_string pass = rjp_search_member(root.get(), "pass", 0).value; raii::rjp_string pass = rjp_search_member(root.get(), "pass");
raii::rjp_string server = rjp_search_member(root.get(), "server", 0).value; raii::rjp_string server = rjp_search_member(root.get(), "server");
raii::rjp_string token = rjp_search_member(root.get(), "token", 0).value; raii::rjp_string token = rjp_search_member(root.get(), "token");
raii::rjp_string agent = rjp_search_member(root.get(), "useragent", 0).value; raii::rjp_string agent = rjp_search_member(root.get(), "useragent");
return matrix::auth_data{user, pass, server, agent, token}; return matrix::auth_data{user, pass, server, agent, token};
} }
void write_to_auth_file(const char* filename, const matrix::auth_data& auth){ void write_to_auth_file(const char* filename, const matrix::auth_data& auth){
raii::rjp_ptr root(rjp_init_json_as(rjp_object())); raii::rjp_ptr root(rjp_new_object());
if(!root.get()) if(!root.get())
return; return;
rjp_add_member(root.get(), "user", 4, rjp_string_copy(auth.name)); rjp_move_value(rjp_add_member_key_copy(root.get(), "user", 4), rjp_new_string_copy(auth.name, 0));
rjp_add_member(root.get(), "pass", 4, rjp_string_copy(auth.pass)); rjp_move_value(rjp_add_member_key_copy(root.get(), "pass", 4), rjp_new_string_copy(auth.pass, 0));
rjp_add_member(root.get(), "server", 6, rjp_string_copy(auth.homeserver)); rjp_move_value(rjp_add_member_key_copy(root.get(), "server", 6), rjp_new_string_copy(auth.homeserver, 0));
rjp_add_member(root.get(), "token", 5, rjp_string_copy(auth.access_token)); rjp_move_value(rjp_add_member_key_copy(root.get(), "token", 5), rjp_new_string_copy(auth.access_token, 0));
rjp_add_member(root.get(), "useragent", 9, rjp_string_copy(auth.useragent)); rjp_move_value(rjp_add_member_key_copy(root.get(), "useragent", 9), rjp_new_string_copy(auth.useragent, 0));
raii::rjp_string output; raii::rjp_string output;
output.reset(rjp_to_json(root.get())); output.reset(rjp_to_json(root.get(), RJP_FORMAT_NONE));
raii::filerd fp(filename, "w"); raii::filerd fp(filename, "w");
if(!fp) if(!fp)
@ -82,28 +84,289 @@ matrix::session login_to_homeserver(void){
return ses; return ses;
} }
struct command{
raii::static_string command_name;
void(*command_exec)(const matrix::client&, const matrix::roomcxn&, const matrix::sync::room_message_event&);
};
void do_help(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event&){
room.send_notice("this is a wip bot\n\n"
"!gaben put gabe newell in his place\n"
"!alexa play despacito\n"
"!shrug ¯\\_(ツ)_/¯\n"
"!emacs !! not implementable with current matrix spec\n"//r0nks opinion on emacs\n"
"!nipple dp nip\n"
"!ddate discordian calendar\n"
"!roll roll the dice\n"
"!test check if bot is working\n"
"!statshow TODO\n"//print last watched episode of show\n"
"!incshow TODO\n"//increment show episode we're on\n"
"!rollshow TODO\n"//pick a random show to watch\n"
"!listshow TODO\n"//show a list of shows\n"
"!excludeshow TODO\n"//add show to exclude list\n"
"!unexcludeshow TODO\n"//remove show from exclude list\n"
"!reqaddshow TODO\n"//add show to request queue\n"
"!reqlistshow TODO\n"//print request queue\n"
"!reqrmshow TODO\n"//remove show from request queue\n"
"!weebrules rules for anime night as decided by the council\n"
"!pusharg TODO\n"//add argument to stack\n"
"!poparg TODO\n"//pop top argument of stack\n"
"!resarg TODO\n"//happily resolve argument\n"
"!dedarg TODO\n"//no winner\n"
"!lsarg TODO\n"//show argument stack\n"
"!trifagsmoment TODO\n"//save a special moment ;)\n"
"!trifagslist TODO\n"//show our special moments\n"
"!sauce print source code\n"
"!help show list of commands"_ss);
}
void do_gaben(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event& event){
if(event.body().length() > 6){
raii::static_string args = event.body().get() + 7;
room.send_message(raii::string(args + " can suck my ass"_ss));
}else{
room.send_notice("can suck my ass"_ss);
}
}
void do_shrug(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event&){
room.send_notice("¯\\_(ツ)_/¯"_ss);
}
void do_alexa(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event&){
room.send_notice("https://www.youtube.com/watch?v=kJQP7kiw5Fk"_ss);
}
void do_emacs(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event&){
room.send_notice("Unfortunately, the matrix spec has no official way to forward messages. As such, riot will not interpret me sending custom forwarded events correctly. Will update when it is in the spec."_ss);
}
void do_nipple(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event&){
raii::static_string str = R"({"msgtype":"m.image","body":"image.jpeg","url":"mxc://rexy712.chickenkiller.com/tmvjkYZYuhfjtMpfQcurSakd","info":{"mimetype":"image/jpeg","size":40202,"h":402,"w":512,"orientation":0}})"_ss;
room.send_custom_event(str, "m.room.message"_ss);
}
raii::string get_command_output(const raii::string_base& command){
raii::string result;
FILE* pipe = popen(command.get(), "r");
if(!pipe)
return {};
char buffer[256];
while(fgets(buffer, sizeof(buffer), pipe)){
result += buffer;
}
pclose(pipe);
return result;
}
void do_ddate(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event& event){
raii::string str = get_command_output(raii::static_string(event.body().get()+1));
room.send_notice(str);
}
int roll_func(const raii::string_base& args, int& returnval){
int state = 0;
int diecount = 1;
int sidecount = 100;
bool finished = false;
const char* symstart = args.get();
returnval = 0;
for(const char* c = symstart;!finished;++c){
switch(state){
case 0:
if(isdigit(*c)){
state = 1;
}else if(isspace(*c)){
++symstart;
}else if(*c == 0){
state = -2;
}else{
return 1;
}
break;
case 1:
if(isdigit(*c)){
continue;
}else if(*c == 'd'){
diecount = atoi(symstart);
symstart = c+1;
state = 2;
}else if(isspace(*c) || *c == 0){
sidecount = atoi(symstart);
state = -2;
}else{
return 1;
}
break;
case 2:
if(*c == '0'){
state = 3;
}else if(isdigit(*c)){
state = 4;
}else if(isspace(*c) || *c == 0){
sidecount = atoi(symstart);
state = -2;
}else{
return 1;
}
break;
case 3:
if(isdigit(*c)){
state = 4;
}else{
return 1;
}
break;
case 4:
if(isdigit(*c)){
continue;
}else if(isspace(*c) || *c == 0){
sidecount = atoi(symstart);
state = -2;
}else{
return 1;
}
break;
case -2:
//accept
finished = 1;
break;
};
}
for(int i = 0;i < diecount;++i){
returnval += (rand() % sidecount) + 1;
}
return 0;
}
void do_roll(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event& event){
int rollval;
if(event.body().length() > 5){
if(roll_func(raii::static_string(event.body().get()+6), rollval))
return;
}else{
if(roll_func(""_ss, rollval))
return;
}
room.send_notice(raii::itostr(rollval));
}
void do_test(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event&){
room.send_notice("working"_ss);
}
void do_weebrules(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event&){
raii::static_string rules =
"1.) Can't exclude without watching >=2 episodes\n"
"2.) Have to respect the role unless\n a.) it's not anime night\n b.) already seen >=3 episodes\n"
"3.) r0nk complaining shall be redirected into /dev/null\n"
"4.) Friday or Saturday is anime night\n"
"5.) Majority vote for exclusions\n"
"6.) Ruburt shall not get drunk without majority vote\n"
"7.) rule ammendments shall be unanimously voted\n"
"8.) Make sure to mince the ginger thoroughly\n"
"9.) rexy712 shall not be silently apathetic\n"
"10.) maximum nigger drive\n"
"11.) must actually watch the show\n"_ss;
room.send_notice(rules);
}
void do_cyberpunk(const matrix::client&, const matrix::roomcxn& room, const matrix::sync::room_message_event&){
room.send_notice("https://is.gd/FJ2FAJ\nnow stop spamming me"_ss);
}
void do_sauce(const matrix::client& client, const matrix::roomcxn& room, const matrix::sync::room_message_event&){
matrix::file_details fd;
fd.name = "src/test.cpp"_ss;
raii::filerd fp(fd.name);
if(!fp){
printf("fopen error\n");
return;
}
fd.data = fp.read_bin(fp.length());
auto ret = client.upload_file(fd);
if(!ret.ok()){
printf("net error\n");
return;
}
room.send_file(ret.value());
}
void do_admin_exit(const matrix::client&, const matrix::roomcxn&, const matrix::sync::room_message_event& event){
if(event.sender() != "@rexy712:rexy712.chickenkiller.com"_ss)
return;
exit(0);
}
static const command commands[] = {
{"!help"_ss, do_help},
{"!gaben"_ss, do_gaben},
{"!shrug"_ss, do_shrug},
{"!alexa"_ss, do_alexa},
{"!emacs"_ss, do_emacs},
{"!nipple"_ss, do_nipple},
{"!ddate"_ss, do_ddate},
{"!roll"_ss, do_roll},
{"!test"_ss, do_test},
{"!weebrules"_ss, do_weebrules},
{"!cyberpunk"_ss, do_cyberpunk},
{"!sauce"_ss, do_sauce},
{"!exit"_ss, do_admin_exit},
};
void handle_command(const matrix::client& client, const matrix::roomcxn& room, const matrix::sync::room_message_event& event){
if(event.msgtype() != matrix::json::events::msgtype::text())
return;
constexpr int commands_size = sizeof(commands) / sizeof(commands[0]);
for(int i = 0;i < commands_size;++i){
int commandlen = commands[i].command_name.length();
if(!strncmp(commands[i].command_name, event.body(), commandlen) && (event.body()[commandlen] == 0 || std::isspace(event.body()[commandlen]))){
commands[i].command_exec(client, room, event);
break;
}
}
}
raii::string get_showinfo(const raii::string_base& file, const raii::string_base& show){
raii::filerd fp(file);
if(!fp)
return {};
raii::string line;
for(line = fp.readln();line.length();line = fp.readln()){
if(!strncmp(line, show, show.length()) && line[show.length()] == ':')
return line;
}
return {};
}
int main(){ int main(){
srand(time(0));
matrix::session ses = login_to_homeserver(); matrix::session ses = login_to_homeserver();
auto client = ses.spawn_client(); auto client = ses.spawn_client();
auto sync_res = client.sync(0).value(); auto sync_res = client.sync(0).value();
while(true){ while(true){
sync_res = client.sync(30000, sync_res).value(); auto sync_status = client.sync(30000, sync_res);
sync_res = std::move(sync_status.value());
printf("here 1\n");
if(!sync_status.ok()){
printf("here 2\n");
fprintf(stderr, "Sync error, attempting to continue\n");
continue;
}
printf("here 3\n");
for(auto room : sync_res.room_join_events()){ for(auto room : sync_res.room_join_events()){
printf("here 4\n");
auto r = client.spawn_room(room.roomid()); auto r = client.spawn_room(room.roomid());
for(auto event : room.timeline_events()){ auto&& range = room.timeline_events();
auto begin = range.begin();
auto end = range.end();
for(;begin != end;++begin){
auto event = *begin;
printf("here 5\n");
if(event.sender() == client.userid()) if(event.sender() == client.userid())
continue; continue;
printf("%s : %s : %s\n", room.roomid().get(), event.type().get(), event.sender().get()); printf("%s : %s : %s\n", room.roomid().get(), event.type().get(), event.sender().get());
r.send_read_receipt(event.eventid()); r.send_read_receipt(event.eventid());
if(event.type() == matrix::json::events::room::message()){ if(event.type() == matrix::json::events::room::message()){
auto message = matrix::event_cast<matrix::sync::room_message_event>(std::move(event)); auto message = matrix::event_cast<matrix::sync::room_message_event>(std::move(event));
if(message.msgtype() == matrix::json::events::msgtype::text()) handle_command(client, r, message);
r.send_message(raii::string("\"" + message.body() + "\" what a faggot thing to say"_ss));
printf("message body: %s\n", message.body().get());
}else if(event.type() == matrix::json::events::room::sticker()){
auto displayname = client.get_display_name(event.sender());
r.send_message(raii::string("only jews send stickers, "_ss + displayname.value()));
} }
} }
} }

View File

@ -1,6 +1,6 @@
/** /**
This file is a part of rexy's matrix client This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712 Copyright (C) 2019-2020 rexy712
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Affero General Public License as published by
@ -58,11 +58,11 @@ matrix::auth_data read_auth_file(const char* filename){
if(!root.get()) if(!root.get())
return {}; return {};
raii::rjp_string user = rjp_search_member(root.get(), "user", 0).value; raii::rjp_string user = rjp_search_member(root.get(), "user");
raii::rjp_string pass = rjp_search_member(root.get(), "pass", 0).value; raii::rjp_string pass = rjp_search_member(root.get(), "pass");
raii::rjp_string server = rjp_search_member(root.get(), "server", 0).value; raii::rjp_string server = rjp_search_member(root.get(), "server");
raii::rjp_string token = rjp_search_member(root.get(), "token", 0).value; raii::rjp_string token = rjp_search_member(root.get(), "token");
raii::rjp_string agent = rjp_search_member(root.get(), "useragent", 0).value; raii::rjp_string agent = rjp_search_member(root.get(), "useragent");
return matrix::auth_data{user, pass, server, agent, token}; return matrix::auth_data{user, pass, server, agent, token};
} }