diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-03-05 20:08:55 +0100 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-03-07 13:01:21 +0100 |
commit | bcab8106d55c4d1e8c66e9000bad8de394bd708e (patch) | |
tree | 9c427a0629092f96265819cbe78bd9e0855fd058 | |
parent | bfa0029f3e734ce5fda6e96dc74ed4f4a96556f6 (diff) |
Implemented callbacks to use chat messages and curses input
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | Makefile | 4 | ||||
-rw-r--r-- | src/application.c | 82 | ||||
-rw-r--r-- | src/application.h | 56 | ||||
-rw-r--r-- | src/chat.c | 136 | ||||
-rw-r--r-- | src/chat.h | 50 | ||||
-rw-r--r-- | src/messenger_cli.c | 37 | ||||
-rw-r--r-- | src/ui/accounts.h | 38 | ||||
-rw-r--r-- | src/util.h | 30 |
8 files changed, 405 insertions, 28 deletions
@@ -3,7 +3,9 @@ SOURCE_DIR = src/ INSTALL_DIR ?= /usr/local/ BINARY = messenger-cli -SOURCES = messenger_cli.c +SOURCES = messenger_cli.c\ + application.c\ + chat.c HEADERS = LIBRARIES = gnunetchat gnunetutil ncurses diff --git a/src/application.c b/src/application.c new file mode 100644 index 0000000..78d5a77 --- /dev/null +++ b/src/application.c @@ -0,0 +1,82 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet 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 + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file application.c + */ + +#include "application.h" + +void +application_init(MESSENGER_Application *app, + int argc, + char **argv) +{ + memset(app, 0, sizeof(*app)); + + app->argc = argc; + app->argv = argv; + + initscr(); + noecho(); + + keypad(stdscr, TRUE); + timeout(100); +} + +static void +run (void *cls, + UNUSED char* const* args, + UNUSED const char *cfgfile, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + MESSENGER_Application *app = cls; + + chat_start(&(app->chat), app, cfg); +} + +void +application_run(MESSENGER_Application *app) +{ + struct GNUNET_GETOPT_CommandLineOption options[] = { + GNUNET_GETOPT_OPTION_END + }; + + app->status = GNUNET_PROGRAM_run( + app->argc, + app->argv, + "messenger_cli", + gettext_noop("A CLI for the Messenger service of GNUnet."), + options, + &run, + app + ); + + endwin(); +} + +int +application_status(MESSENGER_Application *app) +{ + if (app->status != GNUNET_OK) + return EXIT_FAILURE; + else + return EXIT_SUCCESS; +} diff --git a/src/application.h b/src/application.h new file mode 100644 index 0000000..16909c9 --- /dev/null +++ b/src/application.h @@ -0,0 +1,56 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet 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 + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file application.h + */ + +#ifndef APPLICATION_H_ +#define APPLICATION_H_ + +#include <stdlib.h> +#include <curses.h> + +#include "chat.h" +#include "util.h" +//#include "ui/accounts.h" + +typedef struct MESSENGER_Application +{ + char **argv; + int argc; + + int status; + + MESSENGER_Chat chat; +} MESSENGER_Application; + +void +application_init(MESSENGER_Application *app, + int argc, + char **argv); + +void +application_run(MESSENGER_Application *app); + +int +application_status(MESSENGER_Application *app); + +#endif /* APPLICATION_H_ */ diff --git a/src/chat.c b/src/chat.c new file mode 100644 index 0000000..1b4bd48 --- /dev/null +++ b/src/chat.c @@ -0,0 +1,136 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet 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 + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file chat.c + */ + +#include "chat.h" + +#include "application.h" + +int lc = 0; + +static int +_chat_message(UNUSED void *cls, + UNUSED struct GNUNET_CHAT_Context *context, + UNUSED const struct GNUNET_CHAT_Message *message) +{ + //MESSENGER_Application *app = cls; + + enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind( + message + ); + + move(lc++, 50); + printw("TEST %d", (int) kind); + + return GNUNET_YES; +} + +static void +_chat_refresh(UNUSED MESSENGER_Application *app) +{ + // TODO +} + +static int +_chat_event(MESSENGER_Application *app, + int key) +{ + if (key < 0) + goto refresh; + + if ('q' == key) + return 1; + + move(lc++, 0); + printw("KEY %d", key); + +refresh: + _chat_refresh(app); + return 0; +} + +static void +_chat_idle(void *cls) +{ + MESSENGER_Application *app = cls; + app->chat.idle = NULL; + + if (0 != _chat_event(app, getch())) + { + chat_stop(&(app->chat)); + return; + } + + app->chat.idle = GNUNET_SCHEDULER_add_delayed_with_priority( + GNUNET_TIME_relative_multiply( + GNUNET_TIME_relative_get_millisecond_(), + wgetdelay(stdscr) + ), + GNUNET_SCHEDULER_PRIORITY_IDLE, + &_chat_idle, + app + ); +} + +void +chat_start(MESSENGER_Chat *chat, + struct MESSENGER_Application *app, + const struct GNUNET_CONFIGURATION_Handle *cfg) +{ + chat->handle = GNUNET_CHAT_start( + cfg, + "appdir", + &_chat_message, + app + ); + + chat->idle = GNUNET_SCHEDULER_add_now( + &_chat_idle, + app + ); +} + +void +chat_stop(MESSENGER_Chat *chat) +{ + if (chat->idle) + { + GNUNET_SCHEDULER_cancel(chat->idle); + chat->idle = NULL; + } + + if (chat->key) + { + GNUNET_SCHEDULER_cancel(chat->key); + chat->key = NULL; + } + + if (chat->fdset) + { + GNUNET_NETWORK_fdset_destroy(chat->fdset); + chat->fdset = NULL; + } + + GNUNET_CHAT_stop(chat->handle); + chat->handle = NULL; +} diff --git a/src/chat.h b/src/chat.h new file mode 100644 index 0000000..8248da3 --- /dev/null +++ b/src/chat.h @@ -0,0 +1,50 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet 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 + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file chat.h + */ + +#ifndef CHAT_H_ +#define CHAT_H_ + +#include <gnunet/platform.h> +#include <gnunet/gnunet_chat_lib.h> +#include <gnunet/gnunet_util_lib.h> + +struct MESSENGER_Application; + +typedef struct MESSENGER_Chat +{ + struct GNUNET_CHAT_Handle *handle; + struct GNUNET_NETWORK_FDSet *fdset; + struct GNUNET_SCHEDULER_Task *key; + struct GNUNET_SCHEDULER_Task *idle; +} MESSENGER_Chat; + +void +chat_start(MESSENGER_Chat *chat, + struct MESSENGER_Application *app, + const struct GNUNET_CONFIGURATION_Handle *cfg); + +void +chat_stop(MESSENGER_Chat *chat); + +#endif /* CHAT_H_ */ diff --git a/src/messenger_cli.c b/src/messenger_cli.c index 558170f..0c9fda2 100644 --- a/src/messenger_cli.c +++ b/src/messenger_cli.c @@ -22,18 +22,12 @@ * @file messenger_cli.c */ -#include <stdlib.h> -#include <curses.h> +#include "application.h" -#include <gnunet/platform.h> -#include <gnunet/gnunet_chat_lib.h> -#include <gnunet/gnunet_util_lib.h> - - -static void +/*static void run (void *cls, char* const* args, const char *cfgfile, - const struct GNUNET_CONFIGURATION_Handle *cfg) + const struct GNUNET_CONFIGURATION_Handle *cfg) { struct GNUNET_CHAT_Handle *handle = GNUNET_CHAT_start( cfg, @@ -112,26 +106,15 @@ run (void *cls, char* const* args, endwin(); GNUNET_CHAT_stop(handle); -} +}*/ int -main (int argc, char* const* argv) +main (int argc, char** argv) { - struct GNUNET_GETOPT_CommandLineOption options[] = { - GNUNET_GETOPT_OPTION_END - }; - - int result = GNUNET_PROGRAM_run( - argc, argv, - "messenger_cli", - gettext_noop("A CLI for the Messenger service of GNUnet."), - options, - &run, - NULL - ); + MESSENGER_Application app; + + application_init(&app, argc, argv); + application_run(&app); - if (result != GNUNET_OK) - return EXIT_FAILURE; - else - return EXIT_SUCCESS; + return application_status(&app); } diff --git a/src/ui/accounts.h b/src/ui/accounts.h new file mode 100644 index 0000000..1373502 --- /dev/null +++ b/src/ui/accounts.h @@ -0,0 +1,38 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet 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 + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file ui/accounts.h + */ + +#ifndef UI_ACCOUNTS_H_ +#define UI_ACCOUNTS_H_ + +#include <stdlib.h> +#include <curses.h> + +struct UI_ACCOUNTS_Handle +{ + WINDOW *window; +} UI_ACCOUNTS_Handle; + + + +#endif /* UI_ACCOUNTS_H_ */ diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..ba2fa9b --- /dev/null +++ b/src/util.h @@ -0,0 +1,30 @@ +/* + This file is part of GNUnet. + Copyright (C) 2022 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet 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 + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + SPDX-License-Identifier: AGPL3.0-or-later + */ +/* + * @author Tobias Frisch + * @file util.h + */ + +#ifndef UTIL_H_ +#define UTIL_H_ + +#define UNUSED __attribute__((unused)) + +#endif /* UTIL_H_ */ |