diff options
Diffstat (limited to 'src/chat.c')
-rw-r--r-- | src/chat.c | 136 |
1 files changed, 136 insertions, 0 deletions
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 | } | ||