diff options
-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/ | |||
3 | INSTALL_DIR ?= /usr/local/ | 3 | INSTALL_DIR ?= /usr/local/ |
4 | 4 | ||
5 | BINARY = messenger-cli | 5 | BINARY = messenger-cli |
6 | SOURCES = messenger_cli.c | 6 | SOURCES = messenger_cli.c\ |
7 | application.c\ | ||
8 | chat.c | ||
7 | HEADERS = | 9 | HEADERS = |
8 | 10 | ||
9 | LIBRARIES = gnunetchat gnunetutil ncurses | 11 | 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 @@ | |||
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 | |||
27 | void | ||
28 | application_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 | |||
44 | static void | ||
45 | run (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 | |||
55 | void | ||
56 | application_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 | |||
75 | int | ||
76 | application_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 | |||
35 | typedef struct MESSENGER_Application | ||
36 | { | ||
37 | char **argv; | ||
38 | int argc; | ||
39 | |||
40 | int status; | ||
41 | |||
42 | MESSENGER_Chat chat; | ||
43 | } MESSENGER_Application; | ||
44 | |||
45 | void | ||
46 | application_init(MESSENGER_Application *app, | ||
47 | int argc, | ||
48 | char **argv); | ||
49 | |||
50 | void | ||
51 | application_run(MESSENGER_Application *app); | ||
52 | |||
53 | int | ||
54 | application_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 | |||
29 | int lc = 0; | ||
30 | |||
31 | static 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 | |||
48 | static void | ||
49 | _chat_refresh(UNUSED MESSENGER_Application *app) | ||
50 | { | ||
51 | // TODO | ||
52 | } | ||
53 | |||
54 | static 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 | |||
67 | refresh: | ||
68 | _chat_refresh(app); | ||
69 | return 0; | ||
70 | } | ||
71 | |||
72 | static 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 | |||
95 | void | ||
96 | chat_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 | |||
113 | void | ||
114 | chat_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 | |||
32 | struct MESSENGER_Application; | ||
33 | |||
34 | typedef 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 | |||
42 | void | ||
43 | chat_start(MESSENGER_Chat *chat, | ||
44 | struct MESSENGER_Application *app, | ||
45 | const struct GNUNET_CONFIGURATION_Handle *cfg); | ||
46 | |||
47 | void | ||
48 | chat_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 | |||
33 | static void | ||
34 | run (void *cls, char* const* args, | 28 | run (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 | ||
117 | int | 111 | int |
118 | main (int argc, char* const* argv) | 112 | main (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 | |||
31 | struct 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_ */ | ||