diff --git a/include/common.hpp b/include/common.hpp index 4013b60..b59b564 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/libav/frame.hpp b/include/libav/frame.hpp index dcfda86..5c0aba1 100644 --- a/include/libav/frame.hpp +++ b/include/libav/frame.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/libav/libav.hpp b/include/libav/libav.hpp index f1a68ed..ad9b3f0 100644 --- a/include/libav/libav.hpp +++ b/include/libav/libav.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/libav/packet.hpp b/include/libav/packet.hpp index 8c8c7eb..43f2dd5 100644 --- a/include/libav/packet.hpp +++ b/include/libav/packet.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/auth.hpp b/include/matrix/auth.hpp index 5bc46f5..67a1b17 100644 --- a/include/matrix/auth.hpp +++ b/include/matrix/auth.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/client.hpp b/include/matrix/client.hpp index 04feb48..9954664 100644 --- a/include/matrix/client.hpp +++ b/include/matrix/client.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/connection.hpp b/include/matrix/connection.hpp index 6936452..214a630 100644 --- a/include/matrix/connection.hpp +++ b/include/matrix/connection.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/events.hpp b/include/matrix/events.hpp index c9e758e..bd7b37e 100644 --- a/include/matrix/events.hpp +++ b/include/matrix/events.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/fat_strings.hpp b/include/matrix/fat_strings.hpp index 7f1c968..554ec6f 100644 --- a/include/matrix/fat_strings.hpp +++ b/include/matrix/fat_strings.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/iterable.hpp b/include/matrix/iterable.hpp index 658d54f..7f7aa23 100644 --- a/include/matrix/iterable.hpp +++ b/include/matrix/iterable.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/matrix.hpp b/include/matrix/matrix.hpp index 0ba51dd..492d1d4 100644 --- a/include/matrix/matrix.hpp +++ b/include/matrix/matrix.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/netreturn.hpp b/include/matrix/netreturn.hpp index eadc098..9b6b23e 100644 --- a/include/matrix/netreturn.hpp +++ b/include/matrix/netreturn.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/rest/client_url_list.hpp b/include/matrix/rest/client_url_list.hpp index faa8501..01a9928 100644 --- a/include/matrix/rest/client_url_list.hpp +++ b/include/matrix/rest/client_url_list.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/rest/room_url_list.hpp b/include/matrix/rest/room_url_list.hpp index 7a5df40..62e1de3 100644 --- a/include/matrix/rest/room_url_list.hpp +++ b/include/matrix/rest/room_url_list.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/rest/session_urls.hpp b/include/matrix/rest/session_urls.hpp index 14da73a..ec84c35 100644 --- a/include/matrix/rest/session_urls.hpp +++ b/include/matrix/rest/session_urls.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/rest/urls_common.hpp b/include/matrix/rest/urls_common.hpp index ddf43af..d13e425 100644 --- a/include/matrix/rest/urls_common.hpp +++ b/include/matrix/rest/urls_common.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/roomcxn.hpp b/include/matrix/roomcxn.hpp index 5437ded..9ea05f4 100644 --- a/include/matrix/roomcxn.hpp +++ b/include/matrix/roomcxn.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/session.hpp b/include/matrix/session.hpp index e9affa2..2d7f6a9 100644 --- a/include/matrix/session.hpp +++ b/include/matrix/session.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/session_info.hpp b/include/matrix/session_info.hpp index 017123e..e4a2295 100644 --- a/include/matrix/session_info.hpp +++ b/include/matrix/session_info.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/matrix/sync_response.hpp b/include/matrix/sync_response.hpp index 696ee4a..8f5e3aa 100644 --- a/include/matrix/sync_response.hpp +++ b/include/matrix/sync_response.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by @@ -57,7 +57,7 @@ namespace matrix::sync{ private: raii::rjp_object_iterator m_room; 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){ return r.m_room == m_room; } @@ -68,7 +68,7 @@ namespace matrix::sync{ ++m_room; return *this; } - constexpr room_event_response operator*(void){ + room_event_response operator*(void){ return room_event_response(*m_room); } }; @@ -84,8 +84,8 @@ namespace matrix::sync{ private: iterator m_room; public: - constexpr room_list(RJP_value* room):m_room(room){} - constexpr room_list(const room_list& r):m_room(r.m_room){} + room_list(RJP_value* room):m_room(room){} + room_list(const room_list& r):m_room(r.m_room){} constexpr iterator& begin(void){ return m_room; @@ -93,7 +93,7 @@ namespace matrix::sync{ constexpr const_iterator& begin(void)const{ return m_room; } - constexpr const_iterator end(void)const{ + const_iterator end(void)const{ return const_iterator(nullptr); } }; @@ -106,10 +106,16 @@ namespace matrix::sync{ device_list(RJP_value* root): m_root(root){} 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{ - 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); } }; diff --git a/include/matrix/upload_info.hpp b/include/matrix/upload_info.hpp index afa76f3..57a8082 100644 --- a/include/matrix/upload_info.hpp +++ b/include/matrix/upload_info.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/atomic_shared.hpp b/include/raii/atomic_shared.hpp index d73059d..710da20 100644 --- a/include/raii/atomic_shared.hpp +++ b/include/raii/atomic_shared.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/binary.hpp b/include/raii/binary.hpp index c574da8..e9138f6 100644 --- a/include/raii/binary.hpp +++ b/include/raii/binary.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/binary_string_conv.hpp b/include/raii/binary_string_conv.hpp index f53a5fe..db75d33 100644 --- a/include/raii/binary_string_conv.hpp +++ b/include/raii/binary_string_conv.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/curl_llist.hpp b/include/raii/curl_llist.hpp index db1b127..94c9075 100644 --- a/include/raii/curl_llist.hpp +++ b/include/raii/curl_llist.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/curl_string.hpp b/include/raii/curl_string.hpp index 7fd8b04..ef1cb05 100644 --- a/include/raii/curl_string.hpp +++ b/include/raii/curl_string.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/curler.hpp b/include/raii/curler.hpp index 7cdedf3..0d05a1d 100644 --- a/include/raii/curler.hpp +++ b/include/raii/curler.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/default_allocator.hpp b/include/raii/default_allocator.hpp index d6e66ed..6a7c6d2 100644 --- a/include/raii/default_allocator.hpp +++ b/include/raii/default_allocator.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/filerd.hpp b/include/raii/filerd.hpp index 8a4e780..1a6e55c 100644 --- a/include/raii/filerd.hpp +++ b/include/raii/filerd.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/rjp_iterator.hpp b/include/raii/rjp_iterator.hpp index 4e4ae36..a2cc165 100644 --- a/include/raii/rjp_iterator.hpp +++ b/include/raii/rjp_iterator.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by @@ -20,75 +20,117 @@ #define RJP_ITERATOR_HPP #include +#include //exchange namespace raii{ class rjp_object_iterator { public: - using iterator = RJP_value*; + using iterator = RJP_object_iterator; using const_iterator = const iterator; private: - RJP_value* m_value; + RJP_value* m_root; + RJP_object_iterator m_obj = {}; public: - constexpr rjp_object_iterator(RJP_value* v):m_value(v){} - constexpr rjp_object_iterator(const rjp_object_iterator& e):m_value(e.m_value){} - constexpr rjp_object_iterator& operator=(const rjp_object_iterator& e){ - m_value = e.m_value; + rjp_object_iterator(RJP_value* v):m_root(v){ + if(m_root) + rjp_init_object_iterator(&m_obj, m_root); + } + 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; } + 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{ - 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{ - return m_value != e.m_value; + return !(*this == e); } rjp_object_iterator& operator++(void); - constexpr RJP_value* operator*(void){ - return m_value; + RJP_value* operator*(void){ + return rjp_object_iterator_current(&m_obj); } - constexpr const RJP_value* operator*(void)const{ - return m_value; - } - constexpr RJP_value& operator->(void){ - return *m_value; - } - constexpr const RJP_value& operator->(void)const{ - return *m_value; + const RJP_value* operator*(void)const{ + return rjp_object_iterator_current(&m_obj); } }; class rjp_array_iterator { public: - using iterator = RJP_value*; + using iterator = RJP_array_iterator*; using const_iterator = const iterator; private: - RJP_value* m_value; + RJP_value* m_root; + RJP_array_iterator m_arr; public: - constexpr rjp_array_iterator(RJP_value* v):m_value(v){} - constexpr rjp_array_iterator(const rjp_array_iterator& e):m_value(e.m_value){} - constexpr rjp_array_iterator& operator=(const rjp_array_iterator& e){ - m_value = e.m_value; + rjp_array_iterator(RJP_value* v):m_root(v){ + if(m_root) + rjp_init_array_iterator(&m_arr, m_root); + } + 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; } + 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{ - 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{ - return m_value != e.m_value; + return !(*this == e); } rjp_array_iterator& operator++(void); - constexpr RJP_value* operator*(void){ - return m_value; + RJP_value* operator*(void){ + return rjp_array_iterator_current(&m_arr); } - constexpr const RJP_value* operator*(void)const{ - return m_value; - } - constexpr RJP_value& operator->(void){ - return *m_value; - } - constexpr const RJP_value& operator->(void)const{ - return *m_value; + const RJP_value* operator*(void)const{ + return rjp_array_iterator_current(&m_arr); } }; diff --git a/include/raii/rjp_ptr.hpp b/include/raii/rjp_ptr.hpp index f453041..3de73a2 100644 --- a/include/raii/rjp_ptr.hpp +++ b/include/raii/rjp_ptr.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/rjp_string.hpp b/include/raii/rjp_string.hpp index 72bec3a..8bee1ab 100644 --- a/include/raii/rjp_string.hpp +++ b/include/raii/rjp_string.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by @@ -48,19 +48,30 @@ namespace raii{ using string_intermediary::string_intermediary; rjp_string(RJP_value* r): - string_intermediary(r ? std::exchange(r->string.value, nullptr) : nullptr, r ? r->string.length : 0, r ? r->string.length : 0){} + string_intermediary(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::operator=; rjp_string& operator=(RJP_value* r){ if(!r) return *this; reset(); - m_data = std::exchange(r->string.value, nullptr); - m_length = r->string.length; - m_cap = r->string.length; + RJP_string* str = rjp_get_string(r); + m_data = std::exchange(str->value, nullptr); + m_length = str->length; + m_cap = str->length; + str->length = 0; return *this; } }; - rjp_string rjp_string_from_key(RJP_value* val); } #endif diff --git a/include/raii/string.hpp b/include/raii/string.hpp index 054f305..e1cf707 100644 --- a/include/raii/string.hpp +++ b/include/raii/string.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/string_base.hpp b/include/raii/string_base.hpp index 0373060..06122bf 100644 --- a/include/raii/string_base.hpp +++ b/include/raii/string_base.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/include/raii/util.hpp b/include/raii/util.hpp index c1113a0..727c388 100644 --- a/include/raii/util.hpp +++ b/include/raii/util.hpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/makefile b/makefile index fddec7c..cc45f59 100644 --- a/makefile +++ b/makefile @@ -11,7 +11,7 @@ #You should have received a copy of the GNU General Public License #along with this program. If not, see . -#Copyright 2018-2019 rexy712 +#Copyright 2018-2020 rexy712 ifeq ($(OS),Windows_NT) diff --git a/src/matrix/client.cpp b/src/matrix/client.cpp index 89c6df6..ebdc424 100644 --- a/src/matrix/client.cpp +++ b/src/matrix/client.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by @@ -72,13 +72,16 @@ namespace matrix{ if(!root) return retval; - RJP_search_res res = rjp_search_member(root.get(), json::keys::joinedrooms(), 0); - if(!res.value) + RJP_value* res = rjp_search_member(root.get(), json::keys::joinedrooms()); + if(!res) 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); } + rjp_delete_array_iterator(&it); return retval; } @@ -182,8 +185,8 @@ namespace matrix{ if(!root) return retval; - RJP_search_res res = rjp_search_member(root.get(), "next_batch", 0); - if(!res.value) + RJP_value* res = rjp_search_member(root.get(), "next_batch"); + if(!res) return retval; retval.value() = sync::client_response(root.release()); return retval; @@ -228,10 +231,10 @@ namespace matrix{ netreturn retval = _create_netreturn(root, http_status()); if(!root) return retval; - RJP_search_res res = rjp_search_member(root.get(), json::keys::contenturi(), 0); - if(!res.value) + RJP_value* res = rjp_search_member(root.get(), json::keys::contenturi()); + if(!res) return retval; - retval.value().m_fileurl = res.value; + retval.value().m_fileurl = res; retval.value().m_filename = file.name; retval.value().m_filesize = file.data.size(); return retval; diff --git a/src/matrix/connection.cpp b/src/matrix/connection.cpp index dfe1fc9..d76a8b3 100644 --- a/src/matrix/connection.cpp +++ b/src/matrix/connection.cpp @@ -1,6 +1,6 @@ /** 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 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{ if(!root) return {}; - RJP_search_res res = rjp_search_member(root.get(), target.get(), 0); - if(!res.value) + RJP_value* res = rjp_search_member(root.get(), target.get()); + if(!res) return {}; - if(rjp_value_type(res.value) != json_string) + if(rjp_value_type(res) != rjp_json_string) return {}; - return raii::rjp_string(res.value); + return raii::rjp_string(res); } 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){ 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(), "errcode", 0).value, httpstatus); + return netreturn_base(rjp_search_member(root.get(), "error"), rjp_search_member(root.get(), "errcode"), httpstatus); } } diff --git a/src/matrix/events.cpp b/src/matrix/events.cpp index 61162eb..45b7b40 100644 --- a/src/matrix/events.cpp +++ b/src/matrix/events.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by @@ -24,14 +24,14 @@ namespace matrix::sync{ //Event base event::event(RJP_value* ev): m_event(ev), - m_type(rjp_search_member(ev, json::keys::event::type(), 0).value), - m_content(rjp_search_member(ev, json::keys::event::content(), 0).value){} + m_type(ev ? rjp_search_member(ev, json::keys::event::type()) : nullptr), + m_content(ev ? rjp_search_member(ev, json::keys::event::content()) : nullptr){} event::event(event&& ev)noexcept: m_event(std::exchange(ev.m_event, nullptr)), m_type(std::exchange(ev.m_type, nullptr)), m_content(std::exchange(ev.m_content, nullptr)){} 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)&&{ return raii::rjp_string(std::exchange(m_type, nullptr)); @@ -51,12 +51,16 @@ namespace matrix::sync{ //Room event room_event::room_event(RJP_value* ev, const raii::string_base& roomid): - 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), - m_unsigned(rjp_search_member(ev, json::keys::event::extra(), 0).value), - m_redacts(rjp_search_member(ev, json::keys::event::redacts(), 0).value), - m_origin_server_ts(rjp_value_integer(rjp_search_member(ev, json::keys::event::origin_server_ts(), 0).value)){} + room_event_base(roomid), event(ev) + { + if(!ev) + return; + m_id = rjp_search_member(ev, json::keys::event::eventid()); + 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_base(std::move(ev)), event(std::move(ev)), m_id(std::exchange(ev.m_id, nullptr)), @@ -65,14 +69,14 @@ namespace matrix::sync{ m_redacts(std::exchange(ev.m_redacts, nullptr)), m_origin_server_ts(ev.m_origin_server_ts){} 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)&&{ return raii::rjp_string(std::exchange(m_id, nullptr)); } 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)&&{ return raii::rjp_string(std::exchange(m_sender, nullptr)); @@ -89,7 +93,7 @@ namespace matrix::sync{ raii::static_string room_event::redacts(void)const&{ if(!m_redacts) 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)&&{ if(!m_redacts) @@ -99,22 +103,24 @@ namespace matrix::sync{ //Room state event room_state_event::room_state_event(RJP_value* ev, const raii::string_base& 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) + room_event(ev, roomid) { + 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){ - 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&{ - 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)&&{ return raii::rjp_string(std::exchange(m_state_key, nullptr)); } 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)&&{ 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(RJP_value* ev, const raii::string_base& roomid): room_event(ev, roomid), - m_body(rjp_search_member(m_content, json::keys::event::contentbody::body(), 0).value), - m_type(rjp_search_member(m_content, json::keys::event::contentbody::msgtype(), 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())){} room_message_event::room_message_event(room_event&& ev)noexcept: room_event(std::move(ev)), - m_body(rjp_search_member(m_content, json::keys::event::contentbody::body(), 0).value), - m_type(rjp_search_member(m_content, json::keys::event::contentbody::msgtype(), 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())){} 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)&&{ return raii::rjp_string(m_body); } 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)&&{ return raii::rjp_string(m_type); diff --git a/src/matrix/fat_strings.cpp b/src/matrix/fat_strings.cpp index 8133c6b..0013125 100644 --- a/src/matrix/fat_strings.cpp +++ b/src/matrix/fat_strings.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/matrix/iterable.cpp b/src/matrix/iterable.cpp index 666dc54..5103eac 100644 --- a/src/matrix/iterable.cpp +++ b/src/matrix/iterable.cpp @@ -1,6 +1,6 @@ /** 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 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()){} template raii::static_string roomcxn_event_list_base::start_token(void)const&{ - RJP_value* st = rjp_search_member(m_root.get(), "start", 0).value; - return raii::static_string(rjp_value_string(st), rjp_value_string_length(st)); + RJP_value* st = rjp_search_member(m_root.get(), "start"); + return raii::static_string(rjp_get_cstring(st)->value, rjp_get_cstring(st)->length); } template raii::rjp_string roomcxn_event_list_base::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 raii::static_string roomcxn_event_list_base::end_token(void)const&{ - RJP_value* st = rjp_search_member(m_root.get(), "end", 0).value; - return raii::static_string(rjp_value_string(st), rjp_value_string_length(st)); + RJP_value* st = rjp_search_member(m_root.get(), "end"); + return raii::static_string(rjp_get_cstring(st)->value, rjp_get_cstring(st)->length); } template raii::rjp_string roomcxn_event_list_base::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; diff --git a/src/matrix/netreturn.cpp b/src/matrix/netreturn.cpp index 7636df7..aae5803 100644 --- a/src/matrix/netreturn.cpp +++ b/src/matrix/netreturn.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/matrix/rest/client_url_list.cpp b/src/matrix/rest/client_url_list.cpp index 7c56871..af7f28f 100644 --- a/src/matrix/rest/client_url_list.cpp +++ b/src/matrix/rest/client_url_list.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/matrix/rest/room_url_list.cpp b/src/matrix/rest/room_url_list.cpp index 277c4c3..50427f0 100644 --- a/src/matrix/rest/room_url_list.cpp +++ b/src/matrix/rest/room_url_list.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/matrix/rest/session_urls.cpp b/src/matrix/rest/session_urls.cpp index e51b10a..8ffa470 100644 --- a/src/matrix/rest/session_urls.cpp +++ b/src/matrix/rest/session_urls.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/matrix/roomcxn.cpp b/src/matrix/roomcxn.cpp index cf5c6ca..55c61cf 100644 --- a/src/matrix/roomcxn.cpp +++ b/src/matrix/roomcxn.cpp @@ -1,6 +1,6 @@ /** 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 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()); raii::rjp_ptr root(rjp_parse(resp.get())); if(!root) return _create_netreturn(root, http_status()); - RJP_search_res res = rjp_search_member(root.get(), json::keys::joined(), 0); - if(!res.value) return _create_netreturn(root, http_status()); + RJP_value* res = rjp_search_member(root.get(), json::keys::joined()); + 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_string tmp = raii::rjp_string_from_key(mem); + raii::rjp_object_iterator it(res); + 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)); } return retval; @@ -130,7 +131,7 @@ namespace matrix{ return _send_message(json::_audio_body(audio)); } netreturn 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()); } netreturn roomcxn::send_typing(bool active, int timeout)const{ @@ -163,9 +164,10 @@ namespace matrix{ if(!root.get()) _create_netreturn(root, http_status()); netreturn 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; - 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; } netreturn roomcxn::get_events_forward(int amount){ diff --git a/src/matrix/session.cpp b/src/matrix/session.cpp index df91e09..a172a3b 100644 --- a/src/matrix/session.cpp +++ b/src/matrix/session.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by @@ -88,8 +88,8 @@ namespace matrix{ return retval; //attempt to change password via username/password login - RJP_search_res res = rjp_search_member(root.get(), json::keys::session(), 0); - raii::string request = json::_change_psk_password(m_ses->userid, newpass, oldpass, raii::rjp_string(res.value)); + 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)); reply = _post_curl(request, rest::session_urls::password(*m_ses), raii::curl_llist()); return _create_netreturn(reply, http_status()); } @@ -159,10 +159,10 @@ namespace matrix{ netreturn> retval = _create_netreturn(root, http_status()); if(!root) return retval; - RJP_search_res token = rjp_search_member(root.get(), json::keys::auth::accesstoken(), 0); - retval.value().first = raii::rjp_string{token.value}; - token = rjp_search_member(root.get(), json::keys::auth::userid(), 0); - retval.value().second = raii::rjp_string{token.value}; + RJP_value* token = rjp_search_member(root.get(), json::keys::auth::accesstoken()); + retval.value().first = raii::rjp_string{token}; + token = rjp_search_member(root.get(), json::keys::auth::userid()); + retval.value().second = raii::rjp_string{token}; return retval; } } diff --git a/src/matrix/sync_response.cpp b/src/matrix/sync_response.cpp index b2ecc92..36930a2 100644 --- a/src/matrix/sync_response.cpp +++ b/src/matrix/sync_response.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by @@ -22,16 +22,13 @@ namespace matrix::sync{ //Room event response 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_search_res res = rjp_search_member(m_room, mname, 0); - if(!res.value) + RJP_value* res = rjp_search_member(m_room, mname); + if(!res) return nullptr; - res = rjp_search_member(res.value, "events", 0); - if(!res.value) - return nullptr; - return rjp_get_element(res.value); + return rjp_search_member(res, "events"); } room_event_list room_event_response::account_events(void){ 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()); } 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){ - return rjp_search_member(m_room, "summary", 0).value; + return rjp_search_member(m_room, "summary"); } //Sync response @@ -67,24 +64,24 @@ namespace matrix::sync{ return _find_room_list("leave"); } 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&{ - 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 {}; - 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)&&{ - 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{ - 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{ - 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{ - 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){ @@ -94,19 +91,12 @@ namespace matrix::sync{ return m_root.get(); } 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)); -#endif } RJP_value* client_response::_find_room_list(const char* segment)const{ - RJP_search_res res = rjp_search_member(m_root.get(), "rooms", 0); - if(!res.value) + RJP_value* res = rjp_search_member(m_root.get(), "rooms"); + if(!res) return nullptr; - res = rjp_search_member(res.value, segment, 0); - if(!res.value) - return nullptr; - return rjp_get_member(res.value); + return rjp_search_member(res, segment); } } diff --git a/src/raii/binary.cpp b/src/raii/binary.cpp index aa0aac5..7a3b1af 100644 --- a/src/raii/binary.cpp +++ b/src/raii/binary.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/raii/curl_llist.cpp b/src/raii/curl_llist.cpp index 9409142..09269a9 100644 --- a/src/raii/curl_llist.cpp +++ b/src/raii/curl_llist.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/raii/curler.cpp b/src/raii/curler.cpp index 5144ead..caa0d7e 100644 --- a/src/raii/curler.cpp +++ b/src/raii/curler.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/raii/filerd.cpp b/src/raii/filerd.cpp index 0b57058..315e285 100644 --- a/src/raii/filerd.cpp +++ b/src/raii/filerd.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/raii/rjp_iterator.cpp b/src/raii/rjp_iterator.cpp index 99cf203..da6ddd9 100644 --- a/src/raii/rjp_iterator.cpp +++ b/src/raii/rjp_iterator.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by @@ -21,11 +21,11 @@ namespace raii{ rjp_object_iterator& rjp_object_iterator::operator++(void){ - m_value = rjp_next_member(m_value); + rjp_object_iterator_next(&m_obj); return *this; } rjp_array_iterator& rjp_array_iterator::operator++(void){ - m_value = rjp_next_element(m_value); + rjp_array_iterator_next(&m_arr); return *this; } diff --git a/src/raii/rjp_ptr.cpp b/src/raii/rjp_ptr.cpp index 7bfdb6e..5a1f659 100644 --- a/src/raii/rjp_ptr.cpp +++ b/src/raii/rjp_ptr.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/raii/rjp_string.cpp b/src/raii/rjp_string.cpp index 963b136..8120ff9 100644 --- a/src/raii/rjp_string.cpp +++ b/src/raii/rjp_string.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by @@ -32,10 +32,4 @@ namespace raii{ 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); - } } diff --git a/src/raii/string_base.cpp b/src/raii/string_base.cpp index 1ff38a3..69aa27a 100644 --- a/src/raii/string_base.cpp +++ b/src/raii/string_base.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/raii/util.cpp b/src/raii/util.cpp index 7c359a2..cc3e8b1 100644 --- a/src/raii/util.cpp +++ b/src/raii/util.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by diff --git a/src/test.cpp b/src/test.cpp index 25bb18a..e68d29a 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -1,6 +1,6 @@ /** 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 it under the terms of the GNU Affero General Public License as published by @@ -23,11 +23,13 @@ #include "matrix/sync_response.hpp" #include "raii/util.hpp" #include "matrix/json_targets.hpp" +#include "matrix/events.hpp" #include #include #include #include +#include matrix::auth_data read_auth_file(const char* filename){ raii::filerd fp(filename, "r"); @@ -38,26 +40,26 @@ matrix::auth_data read_auth_file(const char* filename){ 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; + 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}; } 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()) return; - rjp_add_member(root.get(), "user", 4, rjp_string_copy(auth.name)); - rjp_add_member(root.get(), "pass", 4, rjp_string_copy(auth.pass)); - rjp_add_member(root.get(), "server", 6, rjp_string_copy(auth.homeserver)); - rjp_add_member(root.get(), "token", 5, rjp_string_copy(auth.access_token)); - rjp_add_member(root.get(), "useragent", 9, rjp_string_copy(auth.useragent)); + rjp_move_value(rjp_add_member_key_copy(root.get(), "user", 4), rjp_new_string_copy(auth.name, 0)); + rjp_move_value(rjp_add_member_key_copy(root.get(), "pass", 4), rjp_new_string_copy(auth.pass, 0)); + rjp_move_value(rjp_add_member_key_copy(root.get(), "server", 6), rjp_new_string_copy(auth.homeserver, 0)); + rjp_move_value(rjp_add_member_key_copy(root.get(), "token", 5), rjp_new_string_copy(auth.access_token, 0)); + rjp_move_value(rjp_add_member_key_copy(root.get(), "useragent", 9), rjp_new_string_copy(auth.useragent, 0)); 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"); if(!fp) @@ -82,28 +84,289 @@ matrix::session login_to_homeserver(void){ 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(){ + srand(time(0)); matrix::session ses = login_to_homeserver(); auto client = ses.spawn_client(); auto sync_res = client.sync(0).value(); 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()){ + printf("here 4\n"); 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()) continue; printf("%s : %s : %s\n", room.roomid().get(), event.type().get(), event.sender().get()); r.send_read_receipt(event.eventid()); if(event.type() == matrix::json::events::room::message()){ auto message = matrix::event_cast(std::move(event)); - if(message.msgtype() == matrix::json::events::msgtype::text()) - 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())); + handle_command(client, r, message); } } } diff --git a/util/matrix-send.cpp b/util/matrix-send.cpp index d8ff1ec..4487d7a 100644 --- a/util/matrix-send.cpp +++ b/util/matrix-send.cpp @@ -1,6 +1,6 @@ /** 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 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()) 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; + 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}; }