diff options
Diffstat (limited to 'src/messenger/gnunet-messenger.c')
-rw-r--r-- | src/messenger/gnunet-messenger.c | 92 |
1 files changed, 61 insertions, 31 deletions
diff --git a/src/messenger/gnunet-messenger.c b/src/messenger/gnunet-messenger.c index 0fa706319..79c0d0b1e 100644 --- a/src/messenger/gnunet-messenger.c +++ b/src/messenger/gnunet-messenger.c | |||
@@ -52,12 +52,12 @@ on_message (void *cls, | |||
52 | { | 52 | { |
53 | const char *sender_name = GNUNET_MESSENGER_contact_get_name (sender); | 53 | const char *sender_name = GNUNET_MESSENGER_contact_get_name (sender); |
54 | 54 | ||
55 | if (!sender_name) | 55 | if (! sender_name) |
56 | sender_name = "anonymous"; | 56 | sender_name = "anonymous"; |
57 | 57 | ||
58 | printf ("[%s ->", GNUNET_h2s(&(message->header.previous))); | 58 | printf ("[%s ->", GNUNET_h2s (&(message->header.previous))); |
59 | printf (" %s]", GNUNET_h2s(hash)); | 59 | printf (" %s]", GNUNET_h2s (hash)); |
60 | printf ("[%s] ", GNUNET_sh2s(&(message->header.sender_id))); | 60 | printf ("[%s] ", GNUNET_sh2s (&(message->header.sender_id))); |
61 | 61 | ||
62 | if (flags & GNUNET_MESSENGER_FLAG_PRIVATE) | 62 | if (flags & GNUNET_MESSENGER_FLAG_PRIVATE) |
63 | printf ("*"); | 63 | printf ("*"); |
@@ -71,7 +71,8 @@ on_message (void *cls, | |||
71 | } | 71 | } |
72 | case GNUNET_MESSENGER_KIND_NAME: | 72 | case GNUNET_MESSENGER_KIND_NAME: |
73 | { | 73 | { |
74 | printf ("* '%s' gets renamed to '%s'\n", sender_name, message->body.name.name); | 74 | printf ("* '%s' gets renamed to '%s'\n", sender_name, |
75 | message->body.name.name); | ||
75 | break; | 76 | break; |
76 | } | 77 | } |
77 | case GNUNET_MESSENGER_KIND_LEAVE: | 78 | case GNUNET_MESSENGER_KIND_LEAVE: |
@@ -81,7 +82,8 @@ on_message (void *cls, | |||
81 | } | 82 | } |
82 | case GNUNET_MESSENGER_KIND_PEER: | 83 | case GNUNET_MESSENGER_KIND_PEER: |
83 | { | 84 | { |
84 | printf ("* '%s' opened the room on: %s\n", sender_name, GNUNET_i2s_full (&(message->body.peer.peer))); | 85 | printf ("* '%s' opened the room on: %s\n", sender_name, |
86 | GNUNET_i2s_full (&(message->body.peer.peer))); | ||
85 | break; | 87 | break; |
86 | } | 88 | } |
87 | case GNUNET_MESSENGER_KIND_TEXT: | 89 | case GNUNET_MESSENGER_KIND_TEXT: |
@@ -96,16 +98,18 @@ on_message (void *cls, | |||
96 | } | 98 | } |
97 | default: | 99 | default: |
98 | { | 100 | { |
99 | printf ("~ message: %s\n", GNUNET_MESSENGER_name_of_kind(message->header.kind)); | 101 | printf ("~ message: %s\n", |
102 | GNUNET_MESSENGER_name_of_kind (message->header.kind)); | ||
100 | break; | 103 | break; |
101 | } | 104 | } |
102 | } | 105 | } |
103 | 106 | ||
104 | if ((GNUNET_MESSENGER_KIND_JOIN == message->header.kind) && (flags & GNUNET_MESSENGER_FLAG_SENT)) | 107 | if ((GNUNET_MESSENGER_KIND_JOIN == message->header.kind) && |
108 | (flags & GNUNET_MESSENGER_FLAG_SENT)) | ||
105 | { | 109 | { |
106 | const char* name = GNUNET_MESSENGER_get_name (messenger); | 110 | const char *name = GNUNET_MESSENGER_get_name (messenger); |
107 | 111 | ||
108 | if (!name) | 112 | if (! name) |
109 | return; | 113 | return; |
110 | 114 | ||
111 | struct GNUNET_MESSENGER_Message response; | 115 | struct GNUNET_MESSENGER_Message response; |
@@ -118,6 +122,7 @@ on_message (void *cls, | |||
118 | } | 122 | } |
119 | } | 123 | } |
120 | 124 | ||
125 | |||
121 | struct GNUNET_SCHEDULER_Task *read_task; | 126 | struct GNUNET_SCHEDULER_Task *read_task; |
122 | struct GNUNET_IDENTITY_EgoLookup *ego_lookup; | 127 | struct GNUNET_IDENTITY_EgoLookup *ego_lookup; |
123 | 128 | ||
@@ -139,11 +144,12 @@ shutdown_hook (void *cls) | |||
139 | 144 | ||
140 | if (messenger) | 145 | if (messenger) |
141 | GNUNET_MESSENGER_disconnect (messenger); | 146 | GNUNET_MESSENGER_disconnect (messenger); |
142 | 147 | ||
143 | if (ego_lookup) | 148 | if (ego_lookup) |
144 | GNUNET_IDENTITY_ego_lookup_cancel (ego_lookup); | 149 | GNUNET_IDENTITY_ego_lookup_cancel (ego_lookup); |
145 | } | 150 | } |
146 | 151 | ||
152 | |||
147 | static void | 153 | static void |
148 | listen_stdio (void *cls); | 154 | listen_stdio (void *cls); |
149 | 155 | ||
@@ -156,12 +162,13 @@ iterate_send_private_message (void *cls, | |||
156 | { | 162 | { |
157 | struct GNUNET_MESSENGER_Message *message = cls; | 163 | struct GNUNET_MESSENGER_Message *message = cls; |
158 | 164 | ||
159 | if (GNUNET_MESSENGER_contact_get_key(contact)) | 165 | if (GNUNET_MESSENGER_contact_get_key (contact)) |
160 | GNUNET_MESSENGER_send_message (room, message, contact); | 166 | GNUNET_MESSENGER_send_message (room, message, contact); |
161 | 167 | ||
162 | return GNUNET_YES; | 168 | return GNUNET_YES; |
163 | } | 169 | } |
164 | 170 | ||
171 | |||
165 | int private_mode; | 172 | int private_mode; |
166 | 173 | ||
167 | /** | 174 | /** |
@@ -197,13 +204,15 @@ read_stdio (void *cls) | |||
197 | message.body.text.text = buffer; | 204 | message.body.text.text = buffer; |
198 | 205 | ||
199 | if (GNUNET_YES == private_mode) | 206 | if (GNUNET_YES == private_mode) |
200 | GNUNET_MESSENGER_iterate_members(room, iterate_send_private_message, &message); | 207 | GNUNET_MESSENGER_iterate_members (room, iterate_send_private_message, |
208 | &message); | ||
201 | else | 209 | else |
202 | GNUNET_MESSENGER_send_message (room, &message, NULL); | 210 | GNUNET_MESSENGER_send_message (room, &message, NULL); |
203 | 211 | ||
204 | read_task = GNUNET_SCHEDULER_add_now (listen_stdio, cls); | 212 | read_task = GNUNET_SCHEDULER_add_now (listen_stdio, cls); |
205 | } | 213 | } |
206 | 214 | ||
215 | |||
207 | /** | 216 | /** |
208 | * Wait for input on STDIO and send it out over the #ch. | 217 | * Wait for input on STDIO and send it out over the #ch. |
209 | * | 218 | * |
@@ -225,6 +234,7 @@ listen_stdio (void *cls) | |||
225 | GNUNET_NETWORK_fdset_destroy (rs); | 234 | GNUNET_NETWORK_fdset_destroy (rs); |
226 | } | 235 | } |
227 | 236 | ||
237 | |||
228 | /** | 238 | /** |
229 | * Initial task to startup application. | 239 | * Initial task to startup application. |
230 | * | 240 | * |
@@ -240,6 +250,7 @@ idle (void *cls) | |||
240 | read_task = GNUNET_SCHEDULER_add_now (listen_stdio, room); | 250 | read_task = GNUNET_SCHEDULER_add_now (listen_stdio, room); |
241 | } | 251 | } |
242 | 252 | ||
253 | |||
243 | char *door_id; | 254 | char *door_id; |
244 | char *ego_name; | 255 | char *ego_name; |
245 | char *room_key; | 256 | char *room_key; |
@@ -266,12 +277,16 @@ on_identity (void *cls, | |||
266 | struct GNUNET_PeerIdentity *door = NULL; | 277 | struct GNUNET_PeerIdentity *door = NULL; |
267 | 278 | ||
268 | if ((door_id) && | 279 | if ((door_id) && |
269 | (GNUNET_OK == GNUNET_CRYPTO_eddsa_public_key_from_string (door_id, strlen (door_id), &(door_peer.public_key)))) | 280 | (GNUNET_OK == GNUNET_CRYPTO_eddsa_public_key_from_string (door_id, |
281 | strlen ( | ||
282 | door_id), | ||
283 | &(door_peer. | ||
284 | public_key)))) | ||
270 | door = &door_peer; | 285 | door = &door_peer; |
271 | 286 | ||
272 | const char *name = GNUNET_MESSENGER_get_name (handle); | 287 | const char *name = GNUNET_MESSENGER_get_name (handle); |
273 | 288 | ||
274 | if (!name) | 289 | if (! name) |
275 | name = "anonymous"; | 290 | name = "anonymous"; |
276 | 291 | ||
277 | printf ("* Welcome to the messenger, '%s'!\n", name); | 292 | printf ("* Welcome to the messenger, '%s'!\n", name); |
@@ -295,15 +310,18 @@ on_identity (void *cls, | |||
295 | 310 | ||
296 | shutdown_task = GNUNET_SCHEDULER_add_shutdown (shutdown_hook, room); | 311 | shutdown_task = GNUNET_SCHEDULER_add_shutdown (shutdown_hook, room); |
297 | 312 | ||
298 | if (!room) | 313 | if (! room) |
299 | GNUNET_SCHEDULER_shutdown (); | 314 | GNUNET_SCHEDULER_shutdown (); |
300 | else | 315 | else |
301 | { | 316 | { |
302 | GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_relative_get_zero_ (), GNUNET_SCHEDULER_PRIORITY_IDLE, idle, | 317 | GNUNET_SCHEDULER_add_delayed_with_priority ( |
303 | room); | 318 | GNUNET_TIME_relative_get_zero_ (), |
319 | GNUNET_SCHEDULER_PRIORITY_IDLE, | ||
320 | idle, room); | ||
304 | } | 321 | } |
305 | } | 322 | } |
306 | 323 | ||
324 | |||
307 | static void | 325 | static void |
308 | on_ego_lookup (void *cls, | 326 | on_ego_lookup (void *cls, |
309 | struct GNUNET_IDENTITY_Ego *ego) | 327 | struct GNUNET_IDENTITY_Ego *ego) |
@@ -313,7 +331,8 @@ on_ego_lookup (void *cls, | |||
313 | const struct GNUNET_IDENTITY_PrivateKey *key; | 331 | const struct GNUNET_IDENTITY_PrivateKey *key; |
314 | key = ego ? GNUNET_IDENTITY_ego_get_private_key (ego) : NULL; | 332 | key = ego ? GNUNET_IDENTITY_ego_get_private_key (ego) : NULL; |
315 | 333 | ||
316 | messenger = GNUNET_MESSENGER_connect (config, ego_name, key, &on_message, NULL); | 334 | messenger = GNUNET_MESSENGER_connect (config, ego_name, key, &on_message, |
335 | NULL); | ||
317 | 336 | ||
318 | on_identity (NULL, messenger); | 337 | on_identity (NULL, messenger); |
319 | } | 338 | } |
@@ -329,7 +348,7 @@ on_ego_lookup (void *cls, | |||
329 | */ | 348 | */ |
330 | static void | 349 | static void |
331 | run (void *cls, | 350 | run (void *cls, |
332 | char *const*args, | 351 | char *const *args, |
333 | const char *cfgfile, | 352 | const char *cfgfile, |
334 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 353 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
335 | { | 354 | { |
@@ -337,7 +356,8 @@ run (void *cls, | |||
337 | 356 | ||
338 | if (ego_name) | 357 | if (ego_name) |
339 | { | 358 | { |
340 | ego_lookup = GNUNET_IDENTITY_ego_lookup (cfg, ego_name, &on_ego_lookup, NULL); | 359 | ego_lookup = GNUNET_IDENTITY_ego_lookup (cfg, ego_name, &on_ego_lookup, |
360 | NULL); | ||
341 | messenger = NULL; | 361 | messenger = NULL; |
342 | } | 362 | } |
343 | else | 363 | else |
@@ -352,6 +372,7 @@ run (void *cls, | |||
352 | on_identity (NULL, messenger); | 372 | on_identity (NULL, messenger); |
353 | } | 373 | } |
354 | 374 | ||
375 | |||
355 | /** | 376 | /** |
356 | * The main function to obtain messenger information. | 377 | * The main function to obtain messenger information. |
357 | * | 378 | * |
@@ -363,17 +384,26 @@ int | |||
363 | main (int argc, | 384 | main (int argc, |
364 | char **argv) | 385 | char **argv) |
365 | { | 386 | { |
366 | const char *description = "Open and connect to rooms using the MESSENGER to chat."; | 387 | const char *description = |
367 | 388 | "Open and connect to rooms using the MESSENGER to chat."; | |
368 | struct GNUNET_GETOPT_CommandLineOption options[] = | 389 | |
369 | { | 390 | struct GNUNET_GETOPT_CommandLineOption options[] = { |
370 | GNUNET_GETOPT_option_string ('d', "door", "PEERIDENTITY", "peer identity to entry into the room", &door_id), | 391 | GNUNET_GETOPT_option_string ('d', "door", "PEERIDENTITY", |
371 | GNUNET_GETOPT_option_string ('e', "ego", "IDENTITY", "identity to use for messaging", &ego_name), | 392 | "peer identity to entry into the room", |
372 | GNUNET_GETOPT_option_string ('r', "room", "ROOMKEY", "key of the room to connect to", &room_key), | 393 | &door_id), |
373 | GNUNET_GETOPT_option_flag ('p', "private", "flag to enable private mode", &private_mode), | 394 | GNUNET_GETOPT_option_string ('e', "ego", "IDENTITY", |
395 | "identity to use for messaging", | ||
396 | &ego_name), | ||
397 | GNUNET_GETOPT_option_string ('r', "room", "ROOMKEY", | ||
398 | "key of the room to connect to", | ||
399 | &room_key), | ||
400 | GNUNET_GETOPT_option_flag ('p', "private", "flag to enable private mode", | ||
401 | &private_mode), | ||
374 | GNUNET_GETOPT_OPTION_END | 402 | GNUNET_GETOPT_OPTION_END |
375 | }; | 403 | }; |
376 | 404 | ||
377 | return (GNUNET_OK == GNUNET_PROGRAM_run (argc, argv, "gnunet-messenger\0", gettext_noop(description), options, &run, | 405 | return (GNUNET_OK == GNUNET_PROGRAM_run (argc, argv, "gnunet-messenger\0", |
378 | NULL) ? EXIT_SUCCESS : EXIT_FAILURE); | 406 | gettext_noop (description), options, |
407 | &run, | ||
408 | NULL) ? EXIT_SUCCESS : EXIT_FAILURE); | ||
379 | } | 409 | } |