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