diff options
Diffstat (limited to 'src/messenger/gnunet-messenger.c')
-rw-r--r-- | src/messenger/gnunet-messenger.c | 334 |
1 files changed, 0 insertions, 334 deletions
diff --git a/src/messenger/gnunet-messenger.c b/src/messenger/gnunet-messenger.c deleted file mode 100644 index e2d106be8..000000000 --- a/src/messenger/gnunet-messenger.c +++ /dev/null | |||
@@ -1,334 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2020--2021 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 src/messenger/gnunet-messenger.c | ||
23 | * @brief Print information about messenger groups. | ||
24 | */ | ||
25 | |||
26 | #include <stdio.h> | ||
27 | |||
28 | #include "platform.h" | ||
29 | #include "gnunet_util_lib.h" | ||
30 | #include "gnunet_messenger_service.h" | ||
31 | |||
32 | struct GNUNET_MESSENGER_Handle *messenger; | ||
33 | |||
34 | /** | ||
35 | * Function called whenever a message is received or sent. | ||
36 | * | ||
37 | * @param[in/out] cls Closure | ||
38 | * @param[in] room Room | ||
39 | * @param[in] sender Sender of message | ||
40 | * @param[in] message Message | ||
41 | * @param[in] hash Hash of message | ||
42 | * @param[in] flags Flags of message | ||
43 | */ | ||
44 | void | ||
45 | on_message (void *cls, | ||
46 | struct GNUNET_MESSENGER_Room *room, | ||
47 | const struct GNUNET_MESSENGER_Contact *sender, | ||
48 | const struct GNUNET_MESSENGER_Message *message, | ||
49 | const struct GNUNET_HashCode *hash, | ||
50 | enum GNUNET_MESSENGER_MessageFlags flags) | ||
51 | { | ||
52 | const char *sender_name = GNUNET_MESSENGER_contact_get_name (sender); | ||
53 | |||
54 | if (!sender_name) | ||
55 | sender_name = "anonymous"; | ||
56 | |||
57 | printf ("[%s] ", GNUNET_sh2s(&(message->header.sender_id))); | ||
58 | |||
59 | if (flags & GNUNET_MESSENGER_FLAG_PRIVATE) | ||
60 | printf ("*"); | ||
61 | |||
62 | switch (message->header.kind) | ||
63 | { | ||
64 | case GNUNET_MESSENGER_KIND_JOIN: | ||
65 | { | ||
66 | printf ("* '%s' joined the room!\n", sender_name); | ||
67 | break; | ||
68 | } | ||
69 | case GNUNET_MESSENGER_KIND_NAME: | ||
70 | { | ||
71 | printf ("* '%s' gets renamed to '%s'\n", sender_name, message->body.name.name); | ||
72 | break; | ||
73 | } | ||
74 | case GNUNET_MESSENGER_KIND_LEAVE: | ||
75 | { | ||
76 | printf ("* '%s' leaves the room!\n", sender_name); | ||
77 | break; | ||
78 | } | ||
79 | case GNUNET_MESSENGER_KIND_PEER: | ||
80 | { | ||
81 | printf ("* '%s' opened the room on: %s\n", sender_name, GNUNET_i2s_full (&(message->body.peer.peer))); | ||
82 | break; | ||
83 | } | ||
84 | case GNUNET_MESSENGER_KIND_TEXT: | ||
85 | { | ||
86 | if (flags & GNUNET_MESSENGER_FLAG_SENT) | ||
87 | printf (">"); | ||
88 | else | ||
89 | printf ("<"); | ||
90 | |||
91 | printf (" '%s' says: \"%s\"\n", sender_name, message->body.text.text); | ||
92 | break; | ||
93 | } | ||
94 | default: | ||
95 | { | ||
96 | printf ("~ message: %s\n", GNUNET_MESSENGER_name_of_kind(message->header.kind)); | ||
97 | break; | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | struct GNUNET_SCHEDULER_Task *read_task; | ||
103 | |||
104 | /** | ||
105 | * Task to shut down this application. | ||
106 | * | ||
107 | * @param[in/out] cls Closure | ||
108 | */ | ||
109 | static void | ||
110 | shutdown_hook (void *cls) | ||
111 | { | ||
112 | struct GNUNET_MESSENGER_Room *room = cls; | ||
113 | |||
114 | if (read_task) | ||
115 | GNUNET_SCHEDULER_cancel (read_task); | ||
116 | |||
117 | if (room) | ||
118 | GNUNET_MESSENGER_close_room (room); | ||
119 | |||
120 | if (messenger) | ||
121 | GNUNET_MESSENGER_disconnect (messenger); | ||
122 | } | ||
123 | |||
124 | static void | ||
125 | listen_stdio (void *cls); | ||
126 | |||
127 | #define MAX_BUFFER_SIZE 60000 | ||
128 | |||
129 | static int | ||
130 | iterate_send_private_message (void *cls, | ||
131 | struct GNUNET_MESSENGER_Room *room, | ||
132 | const struct GNUNET_MESSENGER_Contact *contact) | ||
133 | { | ||
134 | struct GNUNET_MESSENGER_Message *message = cls; | ||
135 | |||
136 | if (GNUNET_MESSENGER_contact_get_key(contact)) | ||
137 | GNUNET_MESSENGER_send_message (room, message, contact); | ||
138 | |||
139 | return GNUNET_YES; | ||
140 | } | ||
141 | |||
142 | int private_mode; | ||
143 | |||
144 | /** | ||
145 | * Task run in stdio mode, after some data is available at stdin. | ||
146 | * | ||
147 | * @param[in/out] cls Closure | ||
148 | */ | ||
149 | static void | ||
150 | read_stdio (void *cls) | ||
151 | { | ||
152 | read_task = NULL; | ||
153 | |||
154 | char buffer[MAX_BUFFER_SIZE]; | ||
155 | ssize_t length; | ||
156 | |||
157 | length = read (0, buffer, MAX_BUFFER_SIZE); | ||
158 | |||
159 | if ((length <= 0) || (length >= MAX_BUFFER_SIZE)) | ||
160 | { | ||
161 | GNUNET_SCHEDULER_shutdown (); | ||
162 | return; | ||
163 | } | ||
164 | |||
165 | if (buffer[length - 1] == '\n') | ||
166 | buffer[length - 1] = '\0'; | ||
167 | else | ||
168 | buffer[length] = '\0'; | ||
169 | |||
170 | struct GNUNET_MESSENGER_Room *room = cls; | ||
171 | |||
172 | struct GNUNET_MESSENGER_Message message; | ||
173 | message.header.kind = GNUNET_MESSENGER_KIND_TEXT; | ||
174 | message.body.text.text = buffer; | ||
175 | |||
176 | if (GNUNET_YES == private_mode) | ||
177 | GNUNET_MESSENGER_iterate_members(room, iterate_send_private_message, &message); | ||
178 | else | ||
179 | GNUNET_MESSENGER_send_message (room, &message, NULL); | ||
180 | |||
181 | read_task = GNUNET_SCHEDULER_add_now (listen_stdio, cls); | ||
182 | } | ||
183 | |||
184 | /** | ||
185 | * Wait for input on STDIO and send it out over the #ch. | ||
186 | * | ||
187 | * @param[in/out] cls Closure | ||
188 | */ | ||
189 | static void | ||
190 | listen_stdio (void *cls) | ||
191 | { | ||
192 | read_task = NULL; | ||
193 | |||
194 | struct GNUNET_NETWORK_FDSet *rs = GNUNET_NETWORK_fdset_create (); | ||
195 | |||
196 | GNUNET_NETWORK_fdset_set_native (rs, 0); | ||
197 | |||
198 | read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
199 | GNUNET_TIME_UNIT_FOREVER_REL, rs, | ||
200 | NULL, &read_stdio, cls); | ||
201 | |||
202 | GNUNET_NETWORK_fdset_destroy (rs); | ||
203 | } | ||
204 | |||
205 | /** | ||
206 | * Initial task to startup application. | ||
207 | * | ||
208 | * @param[in/out] cls Closure | ||
209 | */ | ||
210 | static void | ||
211 | idle (void *cls) | ||
212 | { | ||
213 | struct GNUNET_MESSENGER_Room *room = cls; | ||
214 | |||
215 | printf ("* You joined the room.\n"); | ||
216 | |||
217 | read_task = GNUNET_SCHEDULER_add_now (listen_stdio, room); | ||
218 | } | ||
219 | |||
220 | char *door_id; | ||
221 | char *ego_name; | ||
222 | char *room_key; | ||
223 | |||
224 | struct GNUNET_SCHEDULER_Task *shutdown_task; | ||
225 | |||
226 | /** | ||
227 | * Function called when an identity is retrieved. | ||
228 | * | ||
229 | * @param[in/out] cls Closure | ||
230 | * @param[in/out] handle Handle of messenger service | ||
231 | */ | ||
232 | static void | ||
233 | on_identity (void *cls, | ||
234 | struct GNUNET_MESSENGER_Handle *handle) | ||
235 | { | ||
236 | struct GNUNET_HashCode key; | ||
237 | memset (&key, 0, sizeof(key)); | ||
238 | |||
239 | if (room_key) | ||
240 | GNUNET_CRYPTO_hash (room_key, strlen (room_key), &key); | ||
241 | |||
242 | struct GNUNET_PeerIdentity door_peer; | ||
243 | struct GNUNET_PeerIdentity *door = NULL; | ||
244 | |||
245 | if ((door_id) && | ||
246 | (GNUNET_OK == GNUNET_CRYPTO_eddsa_public_key_from_string (door_id, strlen (door_id), &(door_peer.public_key)))) | ||
247 | door = &door_peer; | ||
248 | |||
249 | const char *name = GNUNET_MESSENGER_get_name (handle); | ||
250 | |||
251 | if (!name) | ||
252 | name = "anonymous"; | ||
253 | |||
254 | printf ("* Welcome to the messenger, '%s'!\n", name); | ||
255 | |||
256 | struct GNUNET_MESSENGER_Room *room; | ||
257 | |||
258 | if (door) | ||
259 | { | ||
260 | printf ("* You try to entry a room...\n"); | ||
261 | |||
262 | room = GNUNET_MESSENGER_enter_room (messenger, door, &key); | ||
263 | } | ||
264 | else | ||
265 | { | ||
266 | printf ("* You try to open a room...\n"); | ||
267 | |||
268 | room = GNUNET_MESSENGER_open_room (messenger, &key); | ||
269 | } | ||
270 | |||
271 | GNUNET_SCHEDULER_cancel (shutdown_task); | ||
272 | |||
273 | shutdown_task = GNUNET_SCHEDULER_add_shutdown (shutdown_hook, room); | ||
274 | |||
275 | if (!room) | ||
276 | GNUNET_SCHEDULER_shutdown (); | ||
277 | else | ||
278 | { | ||
279 | struct GNUNET_MESSENGER_Message message; | ||
280 | message.header.kind = GNUNET_MESSENGER_KIND_NAME; | ||
281 | message.body.name.name = GNUNET_strdup(name); | ||
282 | |||
283 | GNUNET_MESSENGER_send_message (room, &message, NULL); | ||
284 | GNUNET_free(message.body.name.name); | ||
285 | |||
286 | GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_relative_get_zero_ (), GNUNET_SCHEDULER_PRIORITY_IDLE, idle, | ||
287 | room); | ||
288 | } | ||
289 | } | ||
290 | |||
291 | /** | ||
292 | * Main function that will be run by the scheduler. | ||
293 | * | ||
294 | * @param[in/out] cls closure | ||
295 | * @param[in] args remaining command-line arguments | ||
296 | * @param[in] cfgfile name of the configuration file used (for saving, can be NULL!) | ||
297 | * @param[in] cfg configuration | ||
298 | */ | ||
299 | static void | ||
300 | run (void *cls, | ||
301 | char *const*args, | ||
302 | const char *cfgfile, | ||
303 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
304 | { | ||
305 | messenger = GNUNET_MESSENGER_connect (cfg, ego_name, &on_identity, NULL, &on_message, NULL); | ||
306 | |||
307 | shutdown_task = GNUNET_SCHEDULER_add_shutdown (shutdown_hook, NULL); | ||
308 | } | ||
309 | |||
310 | /** | ||
311 | * The main function to obtain messenger information. | ||
312 | * | ||
313 | * @param[in] argc number of arguments from the command line | ||
314 | * @param[in] argv command line arguments | ||
315 | * @return #EXIT_SUCCESS ok, #EXIT_FAILURE on error | ||
316 | */ | ||
317 | int | ||
318 | main (int argc, | ||
319 | char **argv) | ||
320 | { | ||
321 | const char *description = "Open and connect to rooms using the MESSENGER to chat."; | ||
322 | |||
323 | struct GNUNET_GETOPT_CommandLineOption options[] = | ||
324 | { | ||
325 | GNUNET_GETOPT_option_string ('d', "door", "PEERIDENTITY", "peer identity to entry into the room", &door_id), | ||
326 | GNUNET_GETOPT_option_string ('e', "ego", "IDENTITY", "identity to use for messaging", &ego_name), | ||
327 | GNUNET_GETOPT_option_string ('r', "room", "ROOMKEY", "key of the room to connect to", &room_key), | ||
328 | GNUNET_GETOPT_option_flag ('p', "private", "flag to enable private mode", &private_mode), | ||
329 | GNUNET_GETOPT_OPTION_END | ||
330 | }; | ||
331 | |||
332 | return (GNUNET_OK == GNUNET_PROGRAM_run (argc, argv, "gnunet-messenger\0", gettext_noop(description), options, &run, | ||
333 | NULL) ? EXIT_SUCCESS : EXIT_FAILURE); | ||
334 | } | ||