From bf6b5d2e0f79f5cee4795db451259c57500b29e1 Mon Sep 17 00:00:00 2001 From: bqv Date: Thu, 5 Jan 2023 10:10:59 +0000 Subject: [PATCH] move omemo database, to prepare for more DBs --- .depend | 2 + README.org | 3 +- clean.mk | 23 ++++++++ data/capability.cpp | 0 data/capability.hh | 0 data/omemo.cpp | 0 data/omemo.hh | 0 depend.mk | 24 ++++++++ install.mk | 25 +++++++++ makefile | 132 ++++++++++++-------------------------------- omemo.cpp | 6 +- test.mk | 25 +++++++++ tests/plugin.inl | 2 + 13 files changed, 142 insertions(+), 100 deletions(-) create mode 100755 clean.mk create mode 100644 data/capability.cpp create mode 100644 data/capability.hh create mode 100644 data/omemo.cpp create mode 100644 data/omemo.hh create mode 100755 depend.mk create mode 100755 install.mk create mode 100755 test.mk diff --git a/.depend b/.depend index 7305661..7f495aa 100644 --- a/.depend +++ b/.depend @@ -820,6 +820,8 @@ config/../channel.hh: config/../connection.hh: config/../xmpp/ns.hh: config/../user.hh: +data/.omemo.o: data/omemo.cpp +data/.capability.o: data/capability.cpp xmpp/.presence.o: xmpp/presence.cpp xmpp/stanza.hh xmpp/stanza.hh: xmpp/.iq.o: xmpp/iq.cpp xmpp/stanza.hh diff --git a/README.org b/README.org index 9d03b5f..f6b4a53 100644 --- a/README.org +++ b/README.org @@ -133,7 +133,8 @@ * [X] Single media on a line * [ ] [#D] Multiple media inline (protocol?) * [ ] [#C] Encrypted (pgp/omemo) - * [ ] Join and handle events asynchronously + * [X] Join and handle events asynchronously + * [ ] Cache caps * [ ] Handle wide errors gracefully * [ ] [#C] Event-driven MUC entrance * [ ] XMPP Ping (xep-199) diff --git a/clean.mk b/clean.mk new file mode 100755 index 0000000..a6bc380 --- /dev/null +++ b/clean.mk @@ -0,0 +1,23 @@ +#!/usr/bin/env -S gmake clean +# vim: set noexpandtab: + +.PHONY: tidy +tidy: + $(FIND) . -name "*.o" -delete + $(FIND) . -name "*.gcno" -delete + $(FIND) . -name "*.gcda" -delete + +.PHONY: clean +clean: tidy + $(RM) -f $(OBJS) $(COVS) \ + sexp/parser.tab.cc sexp/parser.tab.hh \ + sexp/location.hh sexp/position.hh \ + sexp/stack.hh sexp/parser.output sexp/parser.o \ + sexp/lexer.o sexp/lexer.yy.cc sexp/sexp.a + $(MAKE) -C deps/diff clean || true + git submodule foreach --recursive git clean -xfd || true + git submodule foreach --recursive git reset --hard || true + +.PHONY: distclean +distclean: clean + $(RM) *~ .depend diff --git a/data/capability.cpp b/data/capability.cpp new file mode 100644 index 0000000..e69de29 diff --git a/data/capability.hh b/data/capability.hh new file mode 100644 index 0000000..e69de29 diff --git a/data/omemo.cpp b/data/omemo.cpp new file mode 100644 index 0000000..e69de29 diff --git a/data/omemo.hh b/data/omemo.hh new file mode 100644 index 0000000..e69de29 diff --git a/depend.mk b/depend.mk new file mode 100755 index 0000000..b09432c --- /dev/null +++ b/depend.mk @@ -0,0 +1,24 @@ +#!/usr/bin/env -S gmake depend +# vim: set noexpandtab: + +.PHONY: depend +depend: $(DEPS) $(SRCS) $(HDRS) + echo > ./.depend + for src in $(SRCS) tests/main.cc; do \ + dir="$$(dirname $$src)"; \ + src="$$(basename $$src)"; \ + if [[ $$src == *.cpp ]]; then \ + echo "g++ $(CPPFLAGS) -MM -MMD -MP -MF - \ + -MT $$dir/.$${src/.cpp/.o} $$dir/$$src >> ./.depend"; \ + g++ $(CPPFLAGS) -MM -MMD -MP -MF - \ + -MT $$dir/.$${src/.cpp/.o} $$dir/$$src >> ./.depend || true ; \ + elif [[ $$src == *.c ]]; then \ + echo "gcc $(CFLAGS) -MM -MMD -MP -MF - \ + -MT $$dir/.$${src/.c/.o} $$dir/$$src >> ./.depend"; \ + gcc $(CFLAGS) -MM -MMD -MP -MF - \ + -MT $$dir/.$${src/.c/.o} $$dir/$$src >> ./.depend || true ; \ + else continue; \ + fi; \ + done + +include .depend diff --git a/install.mk b/install.mk new file mode 100755 index 0000000..90bdd35 --- /dev/null +++ b/install.mk @@ -0,0 +1,25 @@ +#!/usr/bin/env -S gmake install +# vim: set noexpandtab: + +HOME ?=~ + +install: xmpp.so +ifeq ($(shell id -u),0) + mkdir -p $(DESTDIR)$(LIBDIR)/weechat/plugins + cp xmpp.so $(DESTDIR)$(LIBDIR)/weechat/plugins/xmpp.so + chmod 644 $(DESTDIR)$(LIBDIR)/weechat/plugins/xmpp.so +else + mkdir -p $(HOME)/.weechat/plugins + cp xmpp.so $(HOME)/.weechat/plugins/xmpp.so + chmod 755 $(HOME)/.weechat/plugins/xmpp.so +endif + +release: xmpp.so + cp xmpp.so .xmpp.so.$(SUFFIX) + ln -sf .xmpp.so.$(SUFFIX) .xmpp.so + +.xmpp.so.%: + mkdir src$@ + objcopy --dump-section .source=/dev/stdout $@ | tar -C src$@ xz + +.PHONY: install release .xmpp.so.% diff --git a/makefile b/makefile index 05b7552..81e8d21 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,8 @@ +#!/usr/bin/env -S gmake all +# vim: set noexpandtab: + ifdef DEBUG - DBGCFLAGS=-fno-omit-frame-pointer -fsanitize=address #-fsanitize=undefined -fsanitize=leak + DBGCFLAGS=-DDEBUG -fno-omit-frame-pointer -fsanitize=address #-fsanitize=undefined -fsanitize=leak DBGLDFLAGS=-lasan -lrt -lasan #-lubsan -llsan endif @@ -23,7 +26,7 @@ CFLAGS+=$(DBGCFLAGS) \ ifeq ($(CC),clang) CFLAGS+= else - CFLAGS+= -fkeep-inline-functions + CFLAGS+= endif CPPFLAGS+=$(DBGCFLAGS) \ -fno-omit-frame-pointer -fPIC \ @@ -36,10 +39,10 @@ CPPFLAGS+=$(DBGCFLAGS) \ ifeq ($(CXX),clang) CPPFLAGS+= else - CPPFLAGS+= -fkeep-inline-functions + CPPFLAGS+= endif LDFLAGS+=$(DBGLDFLAGS) \ - -gdwarf-4 \ + -std=c++23 -gdwarf-4 \ -fuse-ld=mold \ $(DBGCFLAGS) LDLIBS=-lstrophe \ @@ -72,6 +75,8 @@ HDRS=plugin.hh \ config/section.hh \ config/account.hh \ config/option.hh \ + data/omemo.hh \ + data/capability.hh \ xmpp/stanza.hh \ xmpp/ns.hh \ xmpp/node.hh \ @@ -95,6 +100,8 @@ SRCS=plugin.cpp \ config/section.cpp \ config/account.cpp \ config/option.cpp \ + data/omemo.cpp \ + data/capability.cpp \ xmpp/presence.cpp \ xmpp/iq.cpp \ xmpp/node.cpp \ @@ -102,47 +109,49 @@ SRCS=plugin.cpp \ DEPS=deps/diff/libdiff.a \ sexp/sexp.a \ -OBJS=$(patsubst %.cpp,.%.o,$(patsubst %.c,.%.o,$(patsubst config/%.cpp,config/.%.o,$(patsubst xmpp/%.cpp,xmpp/.%.o,$(SRCS))))) -COVS=$(patsubst %.cpp,.%.cov.o,$(patsubst config/%.cpp,config/.%.cov.o,$(patsubst xmpp/%.cpp,xmpp/.%.cov.o,$(SRCS)))) +OBJS=$(patsubst %.cpp,.%.o,$(patsubst %.c,.%.o,$(patsubst config/%.cpp,config/.%.o,$(patsubst data/%.cpp,data/.%.o,$(patsubst xmpp/%.cpp,xmpp/.%.o,$(SRCS)))))) +COVS=$(patsubst %.cpp,.%.cov.o,$(patsubst config/%.cpp,config/.%.cov.o,$(patsubst data/%.cpp,data/.%.cov.o,$(patsubst xmpp/%.cpp,xmpp/.%.cov.o,$(SRCS))))) SUFFIX=$(shell date +%s) +$(eval GIT_REF=$(shell git describe --abbrev=6 --always --dirty 2>/dev/null || true)) + +.DEFAULT_GOAL := all + +include test.mk +include install.mk +include clean.mk +include depend.mk + .PHONY: all -all: - make depend - make weechat-xmpp && make test +all: depend + $(MAKE) weechat-xmpp && $(MAKE) test -.PHONY: weechat-xmpp release +.PHONY: weechat-xmpp weechat-xmpp: $(DEPS) xmpp.so -release: xmpp.so - cp xmpp.so .xmpp.so.$(SUFFIX) - ln -sf .xmpp.so.$(SUFFIX) .xmpp.so xmpp.so: $(DEPS) $(OBJS) $(HDRS) $(CXX) -shared $(LDFLAGS) -o $@ -Wl,--as-needed $(OBJS) $(DEPS) $(LDLIBS) git ls-files | xargs ls -d | xargs tar cz | objcopy --add-section .source=/dev/stdin xmpp.so - #objcopy --dump-section .source=/dev/stdout xmpp.so | tar tz sexp/sexp.a: sexp/parser.o sexp/lexer.o sexp/driver.o ar -r $@ $^ sexp/parser.o: sexp/parser.yy cd sexp && bison -t -d -v parser.yy - $(CXX) $(CPPFLAGS) -c sexp/parser.tab.cc -o $@ + $(CXX) $(CPPFLAGS) -fvisibility=default -c sexp/parser.tab.cc -o $@ sexp/lexer.o: sexp/lexer.l cd sexp && flex -d --outfile=lexer.yy.cc lexer.l - $(CXX) $(CPPFLAGS) -c sexp/lexer.yy.cc -o $@ + $(CXX) $(CPPFLAGS) -fvisibility=default -c sexp/lexer.yy.cc -o $@ sexp/driver.o: sexp/driver.cpp - $(CXX) $(CPPFLAGS) -c $< -o $@ + $(CXX) $(CPPFLAGS) -fvisibility=default -c $< -o $@ .%.o: %.c - $(eval GIT_REF=$(shell git describe --abbrev=6 --always --dirty 2>/dev/null || true)) $(CC) -DGIT_COMMIT=$(GIT_REF) $(CFLAGS) -c $< -o $@ .%.o: %.cpp - $(eval GIT_REF=$(shell git describe --abbrev=6 --always --dirty 2>/dev/null || true)) $(CXX) -DGIT_COMMIT=$(GIT_REF) $(CPPFLAGS) -c $< -o $@ .%.cov.o: %.cpp @@ -154,6 +163,12 @@ config/.%.o: config/%.cpp config/.%.cov.o: config/%.cpp @$(CXX) --coverage $(CPPFLAGS) -c $< -o $@ +data/.%.o: data/%.cpp + $(CXX) $(CPPFLAGS) -c $< -o $@ + +data/.%.cov.o: data/%.cpp + @$(CXX) --coverage $(CPPFLAGS) -c $< -o $@ + xmpp/.%.o: xmpp/%.cpp $(CXX) $(CPPFLAGS) -c $< -o $@ @@ -166,82 +181,3 @@ deps/diff/libdiff.a: cd deps/diff && env -u MAKEFLAGS ./configure $(MAKE) -C deps/diff CFLAGS=-fPIC diff: deps/diff/libdiff.a - -tests/xmpp.cov.so: $(COVS) $(DEPS) $(HDRS) - $(CXX) --coverage -shared $(LDFLAGS) -o tests/xmpp.cov.so -Wl,--as-needed $(DEPS) $(LDLIBS) $(COVS) - -tests/run: $(COVS) tests/main.cc tests/xmpp.cov.so $(wildcard tests/*.inl) - env --chdir tests $(CXX) $(CPPFLAGS) $(LDFLAGS) -o run -Wl,--as-needed ./xmpp.cov.so main.cc $(patsubst %,../%,$(DEPS)) $(LDLIBS) -lstdc++ - -.PHONY: test -test: tests/run - env --chdir tests ./run -sm - -.PHONY: coverage -coverage: tests/run - gcovr --txt -s - -.PHONY: debug -debug: xmpp.so - env LD_PRELOAD=$(DEBUG) gdb -ex "handle SIGPIPE nostop noprint pass" --args \ - weechat -a -P 'alias,buflist,exec,irc,relay' -r '/plugin load ./xmpp.so' - -.PHONY: depend -depend: $(DEPS) $(SRCS) $(HDRS) - $(RM) -f ./.depend - echo > ./.depend - for src in $(SRCS) tests/main.cc; do \ - dir="$$(dirname $$src)"; \ - src="$$(basename $$src)"; \ - if [[ $$src == *.cpp ]]; then \ - echo "g++ $(CPPFLAGS) -MM -MMD -MP -MF - \ - -MT $$dir/.$${src/.cpp/.o} $$dir/$$src >> ./.depend"; \ - g++ $(CPPFLAGS) -MM -MMD -MP -MF - \ - -MT $$dir/.$${src/.cpp/.o} $$dir/$$src >> ./.depend || true ; \ - elif [[ $$src == *.c ]]; then \ - echo "gcc $(CFLAGS) -MM -MMD -MP -MF - \ - -MT $$dir/.$${src/.c/.o} $$dir/$$src >> ./.depend"; \ - gcc $(CFLAGS) -MM -MMD -MP -MF - \ - -MT $$dir/.$${src/.c/.o} $$dir/$$src >> ./.depend || true ; \ - else continue; \ - fi; \ - done - -.PHONY: tidy -tidy: - $(FIND) . -name "*.o" -delete - $(FIND) . -name "*.gcno" -delete - $(FIND) . -name "*.gcda" -delete - -.PHONY: clean -clean: tidy - $(RM) -f $(OBJS) $(COVS) \ - sexp/parser.tab.cc sexp/parser.tab.hh \ - sexp/location.hh sexp/position.hh \ - sexp/stack.hh sexp/parser.output sexp/parser.o \ - sexp/lexer.o sexp/lexer.yy.cc sexp/sexp.a - $(MAKE) -C deps/diff clean || true - git submodule foreach --recursive git clean -xfd || true - git submodule foreach --recursive git reset --hard || true - -.PHONY: distclean -distclean: clean - $(RM) *~ .depend - -.PHONY: install -install: xmpp.so -ifeq ($(shell id -u),0) - mkdir -p $(DESTDIR)$(LIBDIR)/weechat/plugins - cp xmpp.so $(DESTDIR)$(LIBDIR)/weechat/plugins/xmpp.so - chmod 644 $(DESTDIR)$(LIBDIR)/weechat/plugins/xmpp.so -else - mkdir -p ~/.weechat/plugins - cp xmpp.so ~/.weechat/plugins/xmpp.so - chmod 755 ~/.weechat/plugins/xmpp.so -endif - -.PHONY: check -check: - clang-check --analyze *.c *.cc *.cpp - -include .depend diff --git a/omemo.cpp b/omemo.cpp index 9916115..45ff542 100644 --- a/omemo.cpp +++ b/omemo.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -1906,9 +1907,12 @@ void omemo::init(struct t_gui_buffer *buffer, const char *account_name) try { omemo->db_path = std::shared_ptr( - weechat_string_eval_expression("${weechat_data_dir}/xmpp.omemo.db", + weechat_string_eval_expression("${weechat_data_dir}/xmpp/omemo.db", NULL, NULL, NULL), &free).get(); + std::filesystem::path path(omemo->db_path.data()); + std::filesystem::create_directories( + std::filesystem::path(omemo->db_path.data()).parent_path()); omemo->db_env = lmdb::env::create(); omemo->db_env.set_max_dbs(50); diff --git a/test.mk b/test.mk new file mode 100755 index 0000000..2b740a7 --- /dev/null +++ b/test.mk @@ -0,0 +1,25 @@ +#!/usr/bin/env -S gmake test coverage +# vim: set noexpandtab: + +.PHONY: debug +debug: xmpp.so + env LD_PRELOAD=$(DEBUG) gdb -ex "handle SIGPIPE nostop noprint pass" --args \ + weechat -a -P 'alias,buflist,exec,irc,relay' -r '/plugin load ./xmpp.so' + +tests/xmpp.cov.so: $(COVS) $(DEPS) $(HDRS) + $(CXX) --coverage -shared $(LDFLAGS) -o tests/xmpp.cov.so -Wl,--as-needed $(DEPS) $(LDLIBS) $(COVS) + +tests/run: $(COVS) tests/main.cc tests/xmpp.cov.so $(wildcard tests/*.inl) + cd tests && $(CXX) $(CPPFLAGS) $(LDFLAGS) -o run $$PWD/xmpp.cov.so main.cc $(patsubst %,../%,$(DEPS)) $(LDLIBS) + +.PHONY: test +test: tests/run + cd tests && ./run -sm + +.PHONY: coverage +coverage: tests/run + gcovr --txt -s + +.PHONY: check +check: + clang-check --analyze *.c *.cc *.cpp diff --git a/tests/plugin.inl b/tests/plugin.inl index 85a3d91..ca08fcb 100644 --- a/tests/plugin.inl +++ b/tests/plugin.inl @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #include "../deps/fdstream.hpp" #include "../plugin.hh"