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