aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheJackiMonster <thejackimonster@gmail.com>2022-03-05 20:08:55 +0100
committerTheJackiMonster <thejackimonster@gmail.com>2022-03-07 13:01:21 +0100
commitbcab8106d55c4d1e8c66e9000bad8de394bd708e (patch)
tree9c427a0629092f96265819cbe78bd9e0855fd058
parentbfa0029f3e734ce5fda6e96dc74ed4f4a96556f6 (diff)
downloadmessenger-cli-bcab8106d55c4d1e8c66e9000bad8de394bd708e.tar.gz
messenger-cli-bcab8106d55c4d1e8c66e9000bad8de394bd708e.zip
Implemented callbacks to use chat messages and curses input
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r--Makefile4
-rw-r--r--src/application.c82
-rw-r--r--src/application.h56
-rw-r--r--src/chat.c136
-rw-r--r--src/chat.h50
-rw-r--r--src/messenger_cli.c37
-rw-r--r--src/ui/accounts.h38
-rw-r--r--src/util.h30
8 files changed, 405 insertions, 28 deletions
diff --git a/Makefile b/Makefile
index 41fc57e..000c792 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,9 @@ SOURCE_DIR = src/
3INSTALL_DIR ?= /usr/local/ 3INSTALL_DIR ?= /usr/local/
4 4
5BINARY = messenger-cli 5BINARY = messenger-cli
6SOURCES = messenger_cli.c 6SOURCES = messenger_cli.c\
7 application.c\
8 chat.c
7HEADERS = 9HEADERS =
8 10
9LIBRARIES = gnunetchat gnunetutil ncurses 11LIBRARIES = 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 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file application.c
23 */
24
25#include "application.h"
26
27void
28application_init(MESSENGER_Application *app,
29 int argc,
30 char **argv)
31{
32 memset(app, 0, sizeof(*app));
33
34 app->argc = argc;
35 app->argv = argv;
36
37 initscr();
38 noecho();
39
40 keypad(stdscr, TRUE);
41 timeout(100);
42}
43
44static void
45run (void *cls,
46 UNUSED char* const* args,
47 UNUSED const char *cfgfile,
48 const struct GNUNET_CONFIGURATION_Handle *cfg)
49{
50 MESSENGER_Application *app = cls;
51
52 chat_start(&(app->chat), app, cfg);
53}
54
55void
56application_run(MESSENGER_Application *app)
57{
58 struct GNUNET_GETOPT_CommandLineOption options[] = {
59 GNUNET_GETOPT_OPTION_END
60 };
61
62 app->status = GNUNET_PROGRAM_run(
63 app->argc,
64 app->argv,
65 "messenger_cli",
66 gettext_noop("A CLI for the Messenger service of GNUnet."),
67 options,
68 &run,
69 app
70 );
71
72 endwin();
73}
74
75int
76application_status(MESSENGER_Application *app)
77{
78 if (app->status != GNUNET_OK)
79 return EXIT_FAILURE;
80 else
81 return EXIT_SUCCESS;
82}
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 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file application.h
23 */
24
25#ifndef APPLICATION_H_
26#define APPLICATION_H_
27
28#include <stdlib.h>
29#include <curses.h>
30
31#include "chat.h"
32#include "util.h"
33//#include "ui/accounts.h"
34
35typedef struct MESSENGER_Application
36{
37 char **argv;
38 int argc;
39
40 int status;
41
42 MESSENGER_Chat chat;
43} MESSENGER_Application;
44
45void
46application_init(MESSENGER_Application *app,
47 int argc,
48 char **argv);
49
50void
51application_run(MESSENGER_Application *app);
52
53int
54application_status(MESSENGER_Application *app);
55
56#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 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file chat.c
23 */
24
25#include "chat.h"
26
27#include "application.h"
28
29int lc = 0;
30
31static int
32_chat_message(UNUSED void *cls,
33 UNUSED struct GNUNET_CHAT_Context *context,
34 UNUSED const struct GNUNET_CHAT_Message *message)
35{
36 //MESSENGER_Application *app = cls;
37
38 enum GNUNET_CHAT_MessageKind kind = GNUNET_CHAT_message_get_kind(
39 message
40 );
41
42 move(lc++, 50);
43 printw("TEST %d", (int) kind);
44
45 return GNUNET_YES;
46}
47
48static void
49_chat_refresh(UNUSED MESSENGER_Application *app)
50{
51 // TODO
52}
53
54static int
55_chat_event(MESSENGER_Application *app,
56 int key)
57{
58 if (key < 0)
59 goto refresh;
60
61 if ('q' == key)
62 return 1;
63
64 move(lc++, 0);
65 printw("KEY %d", key);
66
67refresh:
68 _chat_refresh(app);
69 return 0;
70}
71
72static void
73_chat_idle(void *cls)
74{
75 MESSENGER_Application *app = cls;
76 app->chat.idle = NULL;
77
78 if (0 != _chat_event(app, getch()))
79 {
80 chat_stop(&(app->chat));
81 return;
82 }
83
84 app->chat.idle = GNUNET_SCHEDULER_add_delayed_with_priority(
85 GNUNET_TIME_relative_multiply(
86 GNUNET_TIME_relative_get_millisecond_(),
87 wgetdelay(stdscr)
88 ),
89 GNUNET_SCHEDULER_PRIORITY_IDLE,
90 &_chat_idle,
91 app
92 );
93}
94
95void
96chat_start(MESSENGER_Chat *chat,
97 struct MESSENGER_Application *app,
98 const struct GNUNET_CONFIGURATION_Handle *cfg)
99{
100 chat->handle = GNUNET_CHAT_start(
101 cfg,
102 "appdir",
103 &_chat_message,
104 app
105 );
106
107 chat->idle = GNUNET_SCHEDULER_add_now(
108 &_chat_idle,
109 app
110 );
111}
112
113void
114chat_stop(MESSENGER_Chat *chat)
115{
116 if (chat->idle)
117 {
118 GNUNET_SCHEDULER_cancel(chat->idle);
119 chat->idle = NULL;
120 }
121
122 if (chat->key)
123 {
124 GNUNET_SCHEDULER_cancel(chat->key);
125 chat->key = NULL;
126 }
127
128 if (chat->fdset)
129 {
130 GNUNET_NETWORK_fdset_destroy(chat->fdset);
131 chat->fdset = NULL;
132 }
133
134 GNUNET_CHAT_stop(chat->handle);
135 chat->handle = NULL;
136}
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 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file chat.h
23 */
24
25#ifndef CHAT_H_
26#define CHAT_H_
27
28#include <gnunet/platform.h>
29#include <gnunet/gnunet_chat_lib.h>
30#include <gnunet/gnunet_util_lib.h>
31
32struct MESSENGER_Application;
33
34typedef struct MESSENGER_Chat
35{
36 struct GNUNET_CHAT_Handle *handle;
37 struct GNUNET_NETWORK_FDSet *fdset;
38 struct GNUNET_SCHEDULER_Task *key;
39 struct GNUNET_SCHEDULER_Task *idle;
40} MESSENGER_Chat;
41
42void
43chat_start(MESSENGER_Chat *chat,
44 struct MESSENGER_Application *app,
45 const struct GNUNET_CONFIGURATION_Handle *cfg);
46
47void
48chat_stop(MESSENGER_Chat *chat);
49
50#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 @@
22 * @file messenger_cli.c 22 * @file messenger_cli.c
23 */ 23 */
24 24
25#include <stdlib.h> 25#include "application.h"
26#include <curses.h>
27 26
28#include <gnunet/platform.h> 27/*static void
29#include <gnunet/gnunet_chat_lib.h>
30#include <gnunet/gnunet_util_lib.h>
31
32
33static void
34run (void *cls, char* const* args, 28run (void *cls, char* const* args,
35 const char *cfgfile, 29 const char *cfgfile,
36 const struct GNUNET_CONFIGURATION_Handle *cfg) 30 const struct GNUNET_CONFIGURATION_Handle *cfg)
37{ 31{
38 struct GNUNET_CHAT_Handle *handle = GNUNET_CHAT_start( 32 struct GNUNET_CHAT_Handle *handle = GNUNET_CHAT_start(
39 cfg, 33 cfg,
@@ -112,26 +106,15 @@ run (void *cls, char* const* args,
112 endwin(); 106 endwin();
113 107
114 GNUNET_CHAT_stop(handle); 108 GNUNET_CHAT_stop(handle);
115} 109}*/
116 110
117int 111int
118main (int argc, char* const* argv) 112main (int argc, char** argv)
119{ 113{
120 struct GNUNET_GETOPT_CommandLineOption options[] = { 114 MESSENGER_Application app;
121 GNUNET_GETOPT_OPTION_END 115
122 }; 116 application_init(&app, argc, argv);
123 117 application_run(&app);
124 int result = GNUNET_PROGRAM_run(
125 argc, argv,
126 "messenger_cli",
127 gettext_noop("A CLI for the Messenger service of GNUnet."),
128 options,
129 &run,
130 NULL
131 );
132 118
133 if (result != GNUNET_OK) 119 return application_status(&app);
134 return EXIT_FAILURE;
135 else
136 return EXIT_SUCCESS;
137} 120}
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 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file ui/accounts.h
23 */
24
25#ifndef UI_ACCOUNTS_H_
26#define UI_ACCOUNTS_H_
27
28#include <stdlib.h>
29#include <curses.h>
30
31struct UI_ACCOUNTS_Handle
32{
33 WINDOW *window;
34} UI_ACCOUNTS_Handle;
35
36
37
38#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 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2022 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/*
21 * @author Tobias Frisch
22 * @file util.h
23 */
24
25#ifndef UTIL_H_
26#define UTIL_H_
27
28#define UNUSED __attribute__((unused))
29
30#endif /* UTIL_H_ */