diff options
Diffstat (limited to 'libchatprivacy/src')
-rw-r--r-- | libchatprivacy/src/.gitignore | 5 | ||||
-rw-r--r-- | libchatprivacy/src/Makefile | 15 | ||||
-rw-r--r-- | libchatprivacy/src/libchatprivacy.c | 133 | ||||
-rw-r--r-- | libchatprivacy/src/libchatprivacy.h | 120 |
4 files changed, 273 insertions, 0 deletions
diff --git a/libchatprivacy/src/.gitignore b/libchatprivacy/src/.gitignore new file mode 100644 index 0000000..5f3eafd --- /dev/null +++ b/libchatprivacy/src/.gitignore @@ -0,0 +1,5 @@ +*.jar +*.kt +*.wasm +*.o +*.wat diff --git a/libchatprivacy/src/Makefile b/libchatprivacy/src/Makefile new file mode 100644 index 0000000..ca3e70e --- /dev/null +++ b/libchatprivacy/src/Makefile @@ -0,0 +1,15 @@ +EMCC=emcc +KOTLINC=kotlinc +WASM2KOTLIN=wasm2kotlin + +libchatprivacy.jar: libchatprivacy.kt wasm_rt_impl.kt + JAVA_OPTS=-Xmx2G $(KOTLINC) -jvm-target 1.8 -nowarn -d libchatprivacy.jar libchatprivacy.kt wasm_rt_impl.kt + +libchatprivacy.kt: libchatprivacy.wasm + $(WASM2KOTLIN) -p libchatprivacy libchatprivacy.wasm -o libchatprivacy.kt + +libchatprivacy.wasm: libchatprivacy.o + $(EMCC) -flto=full -Oz -s ALLOW_TABLE_GROWTH=1 -s ALLOW_MEMORY_GROWTH=1 -s PURE_WASI=1 -L$$(cd ../../libotr/buildenv; pwd)/lib -lotr -lgcrypt -lgpg-error --no-entry -o libchatprivacy.wasm libchatprivacy.o + +libchatprivacy.o: libchatprivacy.c libchatprivacy.h + $(EMCC) -flto=full -Oz -s PURE_WASI=1 -std=c99 -Wall -Wextra -Weverything -pedantic -isystem$$(cd ../../libotr/buildenv; pwd)/include -c -o libchatprivacy.o libchatprivacy.c diff --git a/libchatprivacy/src/libchatprivacy.c b/libchatprivacy/src/libchatprivacy.c new file mode 100644 index 0000000..e3cd59f --- /dev/null +++ b/libchatprivacy/src/libchatprivacy.c @@ -0,0 +1,133 @@ +// LibChatPrivacy - C component of ChatPrivacy +// Copyright (C) 2021 Soni L. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include <stdlib.h> +#include <assert.h> +#include <string.h> + +/* libotr headers */ +#include <libotr/proto.h> +#include <libotr/message.h> +#include <libotr/privkey.h> + +#include "libchatprivacy.h" + +/* The protocol ID */ +static char const *const PROTOCOL = "minecraft"; + +static OtrlUserState userstate = NULL; +static int initialized = 0; +static char *userat = NULL; + +static OtrlMessageAppOps msgappops = {0}; + +#define LCP_CB_IMPL(name, nullable) \ + static lcp_cb_##name *cb_##name = NULL; \ + lcp_cb_##name *lcp_setup_cb_##name(lcp_cb_##name *const func) { \ + nullable \ + lcp_cb_##name *const oldfunc = cb_##name; \ + cb_##name = func; \ + return oldfunc; \ + } + +#define LCP_CB_IMPL_OPT(name) LCP_CB_IMPL(name, ) +#define LCP_CB_IMPL_REQ(name) LCP_CB_IMPL(name, assert(func != NULL);) + +LCP_CB_IMPL_REQ(printmsg) +LCP_CB_IMPL_REQ(sendmsg) +LCP_CB_IMPL_REQ(dialogmsg) +LCP_CB_IMPL_OPT(diagmsg) + +int lcp_init(char const *const uuid) { + if (!initialized) { + initialized = 1; + OTRL_INIT; + } + + /* callback checks */ + assert(cb_printmsg != NULL); + assert(cb_sendmsg != NULL); + assert(cb_dialogmsg != NULL); + + /* library state checks */ + if (userstate != NULL || userat != NULL) { + return 0; + } + + /* arg checks */ + if (uuid == NULL) { + return 0; + } + + userstate = otrl_userstate_create(); + assert(userstate != NULL); + + /* read stuff */ + otrl_privkey_read(userstate, "privkey"); + otrl_instag_read(userstate, "instag"); + otrl_privkey_read_fingerprints(userstate, "fingerprints", NULL, NULL); + + /* copy uuid/userat */ + size_t const uuidlen = strlen(uuid); + size_t const bufsize = uuidlen + 1; + char *const userat_buf = calloc(bufsize, 1); + if (userat_buf == NULL) { + return 0; + } + memcpy(userat_buf, uuid, uuidlen); + userat = userat_buf; + + if (cb_diagmsg != NULL) { + cb_diagmsg(LCP_DIAG_INFO, "lcp initialized"); + } + + return 1; +} + +int lcp_deinit(void) { + assert(userstate != NULL); + assert(userat != NULL); + otrl_userstate_free(userstate); + userstate = NULL; + free((void *)userat); + userat = NULL; + return 1; +} + +void lcp_user_join(char const *const uuid, char const *const name) { + assert(0); +} + +void lcp_user_part(char const *const uuid, char const *const name) { + assert(0); +} + +void lcp_send(char const *const name, char const *const message) { + assert(0); +} + +void lcp_recv(char const *const uuid, char const *const message) { + assert(0); +} + +void lcp_cmd(char const *const cmd) { + assert(0); +} + +void lcp_tick(void) { + assert(0); +} diff --git a/libchatprivacy/src/libchatprivacy.h b/libchatprivacy/src/libchatprivacy.h new file mode 100644 index 0000000..539d8b5 --- /dev/null +++ b/libchatprivacy/src/libchatprivacy.h @@ -0,0 +1,120 @@ +// LibChatPrivacy - C component of ChatPrivacy +// Copyright (C) 2021 Soni L. +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#include <emscripten.h> + + +/// Diagnostic message kind. +// NOTE: We use semver here. Keep these as-is and only add new ones at the end. +// Unless you're making a new major version, in which case do whatever. +enum lcp_diag_kind { + LCP_DIAG_DEBUG = 0, + LCP_DIAG_INFO = 1, + LCP_DIAG_WARN = 2, + LCP_DIAG_ERROR = 3 +}; + +/// Callback: Prints a message from the given user name. +typedef void lcp_cb_printmsg(char const *const name, char const *const message); + +/// Callback: Sends a message to the given user name. +typedef void lcp_cb_sendmsg(char const *const name, char const *const message); + +/// Callback: Prints a dialog message. +typedef void lcp_cb_dialogmsg(char const *const message); + +/// Callback: Prints a diagnostic message of the given diagnostic kind. +typedef void lcp_cb_diagmsg(enum lcp_diag_kind const kind, char const *const message); + +// helper for declaring callback setters. +// these are done as separate functions for convenience on the wasm/kotlin +// side. +#define LCP_CB(name) \ + lcp_cb_##name *lcp_setup_cb_##name(lcp_cb_##name *const func) + +/// Sets up print message callback. Must be called before lcp_init! +/// +/// Returns the previously registered callback function, or NULL if none had +/// been registered. +EMSCRIPTEN_KEEPALIVE +LCP_CB(printmsg); + +/// Sets up send message callback. Must be called before lcp_init! +/// +/// Returns the previously registered callback function, or NULL if none had +/// been registered. +EMSCRIPTEN_KEEPALIVE +LCP_CB(sendmsg); + +/// Sets up dialog message callback. Must be called before lcp_init! +/// +/// Returns the previously registered callback function, or NULL if none had +/// been registered. +EMSCRIPTEN_KEEPALIVE +LCP_CB(dialogmsg); + +/// Sets up diagnostic message callback. Optional, may be NULL! +/// +/// Returns the previously registered callback function, or NULL if none had +/// been registered. +EMSCRIPTEN_KEEPALIVE +LCP_CB(diagmsg); + +#undef LCP_CB + + +/// Initializes this library for the given unique user identifier and server. +/// +/// This should be called exactly once for each wasm instance. +/// +/// Returns 1 on success, 0 on failure. +EMSCRIPTEN_KEEPALIVE +int lcp_init(char const *const uuid); + +/// Indicate an user has joined. This should be called when an user joins. +/// +/// This should also be called for every online user at the time of connecting. +EMSCRIPTEN_KEEPALIVE +void lcp_user_join(char const *const uuid, char const *const name); + +/// Indicate an user has left. This should be called when an user leaves. +EMSCRIPTEN_KEEPALIVE +void lcp_user_part(char const *const uuid, char const *const name); + +/// Sends a message. +EMSCRIPTEN_KEEPALIVE +void lcp_send(char const *const name, char const *const message); + +/// Receives a message. +EMSCRIPTEN_KEEPALIVE +void lcp_recv(char const *const uuid, char const *const message); + +/// Executes an OTR/LCP command. +/// +/// Example: lcp_cmd("help") +EMSCRIPTEN_KEEPALIVE +void lcp_cmd(char const *const cmd); + +/// Updates internal state. Should be called every game tick. +EMSCRIPTEN_KEEPALIVE +void lcp_tick(void); + +/// Deinitializes this library. +/// +/// This should be called at most once for each wasm instance. +EMSCRIPTEN_KEEPALIVE +int lcp_deinit(void); |