diff options
Diffstat (limited to 'src/messenger/gnunet-messenger.c')
-rw-r--r-- | src/messenger/gnunet-messenger.c | 132 |
1 files changed, 76 insertions, 56 deletions
diff --git a/src/messenger/gnunet-messenger.c b/src/messenger/gnunet-messenger.c index 579e5c3ad..737bb83c8 100644 --- a/src/messenger/gnunet-messenger.c +++ b/src/messenger/gnunet-messenger.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2020 GNUnet e.V. | 3 | Copyright (C) 2020--2021 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 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 | 6 | under the terms of the GNU Affero General Public License as published |
@@ -34,29 +34,38 @@ struct GNUNET_MESSENGER_Handle *messenger; | |||
34 | /** | 34 | /** |
35 | * Function called whenever a message is received or sent. | 35 | * Function called whenever a message is received or sent. |
36 | * | 36 | * |
37 | * @param cls Closure | 37 | * @param[in/out] cls Closure |
38 | * @param room Room | 38 | * @param[in] room Room |
39 | * @param message Message | 39 | * @param[in] sender Sender of message |
40 | * @param hash Hash of message | 40 | * @param[in] message Message |
41 | * @param[in] hash Hash of message | ||
42 | * @param[in] flags Flags of message | ||
41 | */ | 43 | */ |
42 | void | 44 | void |
43 | on_message (void *cls, const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message, | 45 | on_message (void *cls, struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Contact *sender, |
44 | const struct GNUNET_HashCode *hash) | 46 | const struct GNUNET_MESSENGER_Message *message, const struct GNUNET_HashCode *hash, |
47 | enum GNUNET_MESSENGER_MessageFlags flags) | ||
45 | { | 48 | { |
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 | const char *sender_name = GNUNET_MESSENGER_contact_get_name (sender); |
49 | 50 | ||
50 | if (!sender_name) | 51 | if (!sender_name) |
51 | sender_name = "anonymous"; | 52 | sender_name = "anonymous"; |
52 | 53 | ||
54 | printf ("[%s] ", GNUNET_sh2s(&(message->header.sender_id))); | ||
55 | |||
56 | if (flags & GNUNET_MESSENGER_FLAG_PRIVATE) | ||
57 | printf ("*"); | ||
58 | |||
53 | switch (message->header.kind) | 59 | switch (message->header.kind) |
54 | { | 60 | { |
55 | case GNUNET_MESSENGER_KIND_JOIN: | 61 | case GNUNET_MESSENGER_KIND_JOIN: |
56 | { | 62 | { |
57 | printf ("* '%s' joined the room! [ %u %u %u %u ]\n", sender_name, message->body.join.key.ecdsa_key.q_y[0], | 63 | printf ("* '%s' joined the room!\n", sender_name); |
58 | message->body.join.key.ecdsa_key.q_y[1], message->body.join.key.ecdsa_key.q_y[2], | 64 | break; |
59 | message->body.join.key.ecdsa_key.q_y[3]); | 65 | } |
66 | case GNUNET_MESSENGER_KIND_NAME: | ||
67 | { | ||
68 | printf ("* '%s' gets renamed to '%s'\n", sender_name, message->body.name.name); | ||
60 | break; | 69 | break; |
61 | } | 70 | } |
62 | case GNUNET_MESSENGER_KIND_LEAVE: | 71 | case GNUNET_MESSENGER_KIND_LEAVE: |
@@ -76,6 +85,7 @@ on_message (void *cls, const struct GNUNET_MESSENGER_Room *room, const struct GN | |||
76 | } | 85 | } |
77 | default: | 86 | default: |
78 | { | 87 | { |
88 | printf ("~ message: %s\n", GNUNET_MESSENGER_name_of_kind(message->header.kind)); | ||
79 | break; | 89 | break; |
80 | } | 90 | } |
81 | } | 91 | } |
@@ -86,7 +96,7 @@ struct GNUNET_SCHEDULER_Task *read_task; | |||
86 | /** | 96 | /** |
87 | * Task to shut down this application. | 97 | * Task to shut down this application. |
88 | * | 98 | * |
89 | * @param cls Closure | 99 | * @param[in/out] cls Closure |
90 | */ | 100 | */ |
91 | static void | 101 | static void |
92 | shutdown_hook (void *cls) | 102 | shutdown_hook (void *cls) |
@@ -108,10 +118,24 @@ listen_stdio (void *cls); | |||
108 | 118 | ||
109 | #define MAX_BUFFER_SIZE 60000 | 119 | #define MAX_BUFFER_SIZE 60000 |
110 | 120 | ||
121 | static int | ||
122 | iterate_send_private_message (void *cls, struct GNUNET_MESSENGER_Room *room, | ||
123 | const struct GNUNET_MESSENGER_Contact *contact) | ||
124 | { | ||
125 | struct GNUNET_MESSENGER_Message *message = cls; | ||
126 | |||
127 | if (GNUNET_MESSENGER_contact_get_key(contact)) | ||
128 | GNUNET_MESSENGER_send_message (room, message, contact); | ||
129 | |||
130 | return GNUNET_YES; | ||
131 | } | ||
132 | |||
133 | int private_mode; | ||
134 | |||
111 | /** | 135 | /** |
112 | * Task run in stdio mode, after some data is available at stdin. | 136 | * Task run in stdio mode, after some data is available at stdin. |
113 | * | 137 | * |
114 | * @param cls Closure | 138 | * @param[in/out] cls Closure |
115 | */ | 139 | */ |
116 | static void | 140 | static void |
117 | read_stdio (void *cls) | 141 | read_stdio (void *cls) |
@@ -140,7 +164,10 @@ read_stdio (void *cls) | |||
140 | message.header.kind = GNUNET_MESSENGER_KIND_TEXT; | 164 | message.header.kind = GNUNET_MESSENGER_KIND_TEXT; |
141 | message.body.text.text = buffer; | 165 | message.body.text.text = buffer; |
142 | 166 | ||
143 | GNUNET_MESSENGER_send_message (room, &message); | 167 | if (GNUNET_YES == private_mode) |
168 | GNUNET_MESSENGER_iterate_members(room, iterate_send_private_message, &message); | ||
169 | else | ||
170 | GNUNET_MESSENGER_send_message (room, &message, NULL); | ||
144 | 171 | ||
145 | read_task = GNUNET_SCHEDULER_add_now (listen_stdio, cls); | 172 | read_task = GNUNET_SCHEDULER_add_now (listen_stdio, cls); |
146 | } | 173 | } |
@@ -148,7 +175,7 @@ read_stdio (void *cls) | |||
148 | /** | 175 | /** |
149 | * Wait for input on STDIO and send it out over the #ch. | 176 | * Wait for input on STDIO and send it out over the #ch. |
150 | * | 177 | * |
151 | * @param cls Closure | 178 | * @param[in/out] cls Closure |
152 | */ | 179 | */ |
153 | static void | 180 | static void |
154 | listen_stdio (void *cls) | 181 | listen_stdio (void *cls) |
@@ -160,10 +187,8 @@ listen_stdio (void *cls) | |||
160 | GNUNET_NETWORK_fdset_set_native (rs, 0); | 187 | GNUNET_NETWORK_fdset_set_native (rs, 0); |
161 | 188 | ||
162 | read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | 189 | read_task = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT, |
163 | GNUNET_TIME_UNIT_FOREVER_REL, | 190 | GNUNET_TIME_UNIT_FOREVER_REL, rs, |
164 | rs, | 191 | NULL, &read_stdio, cls); |
165 | NULL, | ||
166 | &read_stdio, cls); | ||
167 | 192 | ||
168 | GNUNET_NETWORK_fdset_destroy (rs); | 193 | GNUNET_NETWORK_fdset_destroy (rs); |
169 | } | 194 | } |
@@ -171,7 +196,7 @@ listen_stdio (void *cls) | |||
171 | /** | 196 | /** |
172 | * Initial task to startup application. | 197 | * Initial task to startup application. |
173 | * | 198 | * |
174 | * @param cls Closure | 199 | * @param[in/out] cls Closure |
175 | */ | 200 | */ |
176 | static void | 201 | static void |
177 | idle (void *cls) | 202 | idle (void *cls) |
@@ -192,8 +217,8 @@ struct GNUNET_SCHEDULER_Task *shutdown_task; | |||
192 | /** | 217 | /** |
193 | * Function called when an identity is retrieved. | 218 | * Function called when an identity is retrieved. |
194 | * | 219 | * |
195 | * @param cls Closure | 220 | * @param[in/out] cls Closure |
196 | * @param handle Handle of messenger service | 221 | * @param[in/out] handle Handle of messenger service |
197 | */ | 222 | */ |
198 | static void | 223 | static void |
199 | on_identity (void *cls, struct GNUNET_MESSENGER_Handle *handle) | 224 | on_identity (void *cls, struct GNUNET_MESSENGER_Handle *handle) |
@@ -230,7 +255,7 @@ on_identity (void *cls, struct GNUNET_MESSENGER_Handle *handle) | |||
230 | { | 255 | { |
231 | printf ("* You try to entry a room...\n"); | 256 | printf ("* You try to entry a room...\n"); |
232 | 257 | ||
233 | room = GNUNET_MESSENGER_entry_room (messenger, door, &key); | 258 | room = GNUNET_MESSENGER_enter_room (messenger, door, &key); |
234 | } | 259 | } |
235 | else | 260 | else |
236 | { | 261 | { |
@@ -246,17 +271,26 @@ on_identity (void *cls, struct GNUNET_MESSENGER_Handle *handle) | |||
246 | if (!room) | 271 | if (!room) |
247 | GNUNET_SCHEDULER_shutdown (); | 272 | GNUNET_SCHEDULER_shutdown (); |
248 | else | 273 | else |
274 | { | ||
275 | struct GNUNET_MESSENGER_Message message; | ||
276 | message.header.kind = GNUNET_MESSENGER_KIND_NAME; | ||
277 | message.body.name.name = GNUNET_strdup(name); | ||
278 | |||
279 | GNUNET_MESSENGER_send_message (room, &message, NULL); | ||
280 | GNUNET_free(message.body.name.name); | ||
281 | |||
249 | GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_relative_get_zero_ (), GNUNET_SCHEDULER_PRIORITY_IDLE, idle, | 282 | GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_relative_get_zero_ (), GNUNET_SCHEDULER_PRIORITY_IDLE, idle, |
250 | room); | 283 | room); |
284 | } | ||
251 | } | 285 | } |
252 | 286 | ||
253 | /** | 287 | /** |
254 | * Main function that will be run by the scheduler. | 288 | * Main function that will be run by the scheduler. |
255 | * | 289 | * |
256 | * @param cls closure | 290 | * @param[in/out] cls closure |
257 | * @param args remaining command-line arguments | 291 | * @param[in] args remaining command-line arguments |
258 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | 292 | * @param[in] cfgfile name of the configuration file used (for saving, can be NULL!) |
259 | * @param cfg configuration | 293 | * @param[in] cfg configuration |
260 | */ | 294 | */ |
261 | static void | 295 | static void |
262 | run (void *cls, char *const*args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) | 296 | run (void *cls, char *const*args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) |
@@ -269,38 +303,24 @@ run (void *cls, char *const*args, const char *cfgfile, const struct GNUNET_CONFI | |||
269 | /** | 303 | /** |
270 | * The main function to obtain messenger information. | 304 | * The main function to obtain messenger information. |
271 | * | 305 | * |
272 | * @param argc number of arguments from the command line | 306 | * @param[in] argc number of arguments from the command line |
273 | * @param argv command line arguments | 307 | * @param[in] argv command line arguments |
274 | * @return 0 ok, 1 on error | 308 | * @return #EXIT_SUCCESS ok, #EXIT_FAILURE on error |
275 | */ | 309 | */ |
276 | int | 310 | int |
277 | main (int argc, char **argv) | 311 | main (int argc, char **argv) |
278 | { | 312 | { |
279 | const char *description = "Open and connect to rooms using the MESSENGER to chat."; | 313 | const char *description = "Open and connect to rooms using the MESSENGER to chat."; |
280 | 314 | ||
281 | struct GNUNET_GETOPT_CommandLineOption options[] = { | 315 | struct GNUNET_GETOPT_CommandLineOption options[] = |
282 | GNUNET_GETOPT_option_string ('d', | 316 | { |
283 | "door", | 317 | GNUNET_GETOPT_option_string ('d', "door", "PEERIDENTITY", "peer identity to entry into the room", &door_id), |
284 | "PEERIDENTITY", | 318 | GNUNET_GETOPT_option_string ('e', "ego", "IDENTITY", "identity to use for messaging", &ego_name), |
285 | "peer identity to entry into the room", | 319 | GNUNET_GETOPT_option_string ('r', "room", "ROOMKEY", "key of the room to connect to", &room_key), |
286 | &door_id), | 320 | GNUNET_GETOPT_option_flag ('p', "private", "flag to enable private mode", &private_mode), |
287 | GNUNET_GETOPT_option_string ('e', | 321 | GNUNET_GETOPT_OPTION_END |
288 | "ego", | 322 | }; |
289 | "IDENTITY", | 323 | |
290 | "identity to use for messaging", | 324 | return (GNUNET_OK == GNUNET_PROGRAM_run (argc, argv, "gnunet-messenger\0", gettext_noop(description), options, &run, |
291 | &ego_name), | 325 | NULL) ? EXIT_SUCCESS : EXIT_FAILURE); |
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 | } | 326 | } |