-rps service: prevent division by zero
[oweals/gnunet.git] / src / social / gnunet-social.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2016 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * CLI tool to interact with the social service.
23  *
24  * @author Gabor X Toth
25  */
26
27 #include <inttypes.h>
28
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_social_service.h"
32 #include "gnunet_core_service.h"
33
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
35
36 #define DATA2ARG(data) data, sizeof (data)
37
38 /* operations corresponding to API calls */
39
40 /** --status */
41 static int op_status;
42
43 /** --host-enter */
44 static int op_host_enter;
45
46 /** --host-reconnect */
47 static int op_host_reconnect;
48
49 /** --host-leave */
50 static int op_host_leave;
51
52 /** --host-announce */
53 static int op_host_announce;
54
55 /** --host-assign */
56 static int op_host_assign;
57
58 /** --guest-enter */
59 static int op_guest_enter;
60
61 /** --guest-reconnect */
62 static int op_guest_reconnect;
63
64 /** --guest-leave */
65 static int op_guest_leave;
66
67 /** --guest-talk */
68 static int op_guest_talk;
69
70 /** --replay */
71 static char *op_replay;
72
73 /** --replay-latest */
74 static char *op_replay_latest;
75
76 /** --look-at */
77 static int op_look_at;
78
79 /** --look-for */
80 static int op_look_for;
81
82
83 /* options */
84
85 /** --app */
86 static char *opt_app = "cli";
87
88 /** --place */
89 static char *opt_place;
90
91 /** --ego */
92 static char *opt_ego;
93
94 /** --gns */
95 static char *opt_gns;
96
97 /** --peer */
98 static char *opt_peer;
99
100 /** --follow */
101 static int opt_follow;
102
103 /** --welcome */
104 static int opt_welcome;
105
106 /** --deny */
107 static int opt_deny;
108
109 /** --method */
110 static char *opt_method;
111
112 /** --data */
113 // FIXME: should come from STDIN
114 static char *opt_data;
115
116 /** --name */
117 static char *opt_name;
118
119 /** --start */
120 static uint64_t opt_start;
121
122 /** --until */
123 static uint64_t opt_until;
124
125 /** --limit */
126 static int opt_limit;
127
128
129 /* global vars */
130
131 /** exit code */
132 static int ret = 1;
133
134 /** are we waiting for service to close our connection */
135 static char is_disconnecting = 0;
136
137 /** Task handle for timeout termination. */
138 struct GNUNET_SCHEDULER_Task *timeout_task;
139
140 const struct GNUNET_CONFIGURATION_Handle *cfg;
141
142 struct GNUNET_CORE_Handle *core;
143 struct GNUNET_PeerIdentity peer, this_peer;
144
145 struct GNUNET_SOCIAL_App *app;
146
147 /** public key of connected place */
148 struct GNUNET_CRYPTO_EddsaPublicKey place_pub_key;
149
150 struct GNUNET_PSYC_Slicer *slicer;
151
152 struct GNUNET_SOCIAL_Ego *ego;
153 struct GNUNET_CRYPTO_EcdsaPublicKey ego_pub_key;
154
155 struct GNUNET_SOCIAL_Host *hst;
156 struct GNUNET_SOCIAL_Guest *gst;
157 struct GNUNET_SOCIAL_Place *plc;
158
159
160 /* DISCONNECT */
161
162
163 /**
164  * Callback called after the host or guest place disconnected.
165  */
166 static void
167 disconnected (void *cls)
168 {
169   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnected()\n");
170   GNUNET_SCHEDULER_shutdown ();
171 }
172
173
174 /**
175  * Callback called after the application disconnected.
176  */
177 static void
178 app_disconnected (void *cls)
179 {
180   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "app_disconnected()\n");
181   if (hst || gst)
182   {
183     if (hst)
184     {
185       GNUNET_SOCIAL_host_disconnect (hst, disconnected, NULL);
186     }
187     if (gst)
188     {
189       GNUNET_SOCIAL_guest_disconnect (gst, disconnected, NULL);
190     }
191   }
192   else
193   {
194     GNUNET_SCHEDULER_shutdown ();
195   }
196 }
197
198
199 /**
200  * Disconnect from connected GNUnet services.
201  */
202 static void
203 disconnect ()
204 {
205   // handle that we get called several times from several places, but should we?
206   if (!is_disconnecting++) {
207     GNUNET_SOCIAL_app_disconnect (app, app_disconnected, NULL);
208   }
209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "disconnect() called for the #%d time\n", is_disconnecting);
210 }
211
212
213 static void
214 scheduler_shutdown (void *cls)
215 {
216   disconnect ();
217 }
218
219
220 /**
221  * Callback called when the program failed to finish the requested operation in time.
222  */
223 static void
224 timeout (void *cls)
225 {
226   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "timeout()\n");
227   disconnect ();
228 }
229
230 static void
231 schedule_success (void *cls)
232 {
233   ret = 0;
234   disconnect ();
235 }
236
237
238 static void
239 schedule_fail (void *cls)
240 {
241   disconnect ();
242 }
243
244
245 /**
246  * Schedule exit with success result.
247  */
248 static void
249 exit_success ()
250 {
251   if (timeout_task != NULL)
252   {
253     GNUNET_SCHEDULER_cancel (timeout_task);
254     timeout_task = NULL;
255   }
256   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, schedule_success, NULL);
257 }
258
259
260 /**
261  * Schedule exit with failure result.
262  */
263 static void
264 exit_fail ()
265 {
266   if (timeout_task != NULL)
267   {
268     GNUNET_SCHEDULER_cancel (timeout_task);
269     timeout_task = NULL;
270   }
271   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, schedule_fail, NULL);
272 }
273
274
275 /* LEAVE */
276
277
278 /**
279  * Callback notifying about the host has left and stopped hosting the place.
280  *
281  * This also indicates the end of the connection to the service.
282  */
283 static void
284 host_left ()
285 {
286   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287               "The host has left the place.\n");
288   exit_success ();
289 }
290
291
292 /**
293  * Leave a place permanently and stop hosting a place.
294  */
295 static void
296 host_leave ()
297 {
298   GNUNET_SOCIAL_host_leave (hst, NULL, host_left, NULL);
299   hst = NULL;
300   plc = NULL;
301 }
302
303
304 /**
305  * Callback notifying about the guest has left the place.
306  *
307  * This also indicates the end of the connection to the service.
308  */
309 static void
310 guest_left (void *cls)
311 {
312   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
313               "The guest has left the place.\n");
314 }
315
316
317 /**
318  * Leave a place permanently as guest.
319  */
320 static void
321 guest_leave ()
322 {
323   struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
324   // FIXME: wrong use of vars
325   GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
326                        "_message", DATA2ARG ("Leaving."));
327   GNUNET_SOCIAL_guest_leave (gst, env, guest_left, NULL);
328   GNUNET_PSYC_env_destroy (env);
329   gst = NULL;
330   plc = NULL;
331 }
332
333
334 /* ANNOUNCE / ASSIGN / TALK */
335
336
337 struct TransmitClosure
338 {
339   const char *data;
340   size_t size;
341 } tmit;
342
343
344 /**
345  * Callback notifying about available buffer space to write message data
346  * when transmitting messages using host_announce() or guest_talk()
347  */
348 static int
349 notify_data (void *cls, uint16_t *data_size, void *data)
350 {
351   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
352               "Transmit notify data: %u bytes available\n",
353               *data_size);
354
355   struct TransmitClosure *tmit = cls;
356   uint16_t size = tmit->size < *data_size ? tmit->size : *data_size;
357   *data_size = size;
358   GNUNET_memcpy (data, tmit->data, size);
359
360   tmit->size -= size;
361   tmit->data += size;
362
363   if (0 == tmit->size)
364   {
365     if (op_host_announce || op_host_assign || op_guest_talk)
366     {
367       exit_success ();
368     }
369     return GNUNET_YES;
370   }
371   else
372   {
373     return GNUNET_NO;
374   }
375 }
376
377
378 /**
379  * Host announcement - send a message to the place.
380  */
381 static void
382 host_announce (const char *method, const char *data, size_t data_size)
383 {
384   struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
385   GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
386                        "_foo", DATA2ARG ("bar baz"));
387
388   tmit = (struct TransmitClosure) {};
389   tmit.data = data;
390   tmit.size = data_size;
391
392   GNUNET_SOCIAL_host_announce (hst, method, env,
393                                notify_data, &tmit,
394                                GNUNET_SOCIAL_ANNOUNCE_NONE);
395   GNUNET_PSYC_env_destroy (env);
396 }
397
398
399 /**
400  * Assign a state var of @a name to the value of @a data.
401  */
402 static void
403 host_assign (const char *name, const char *data, size_t data_size)
404 {
405   struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
406   GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_ASSIGN,
407                        name, data, data_size);
408
409   tmit = (struct TransmitClosure) {};
410   GNUNET_SOCIAL_host_announce (hst, "_assign", env,
411                                notify_data, &tmit,
412                                GNUNET_SOCIAL_ANNOUNCE_NONE);
413   GNUNET_PSYC_env_destroy (env);
414 }
415
416
417 /**
418  * Guest talk request to host.
419  */
420 static void
421 guest_talk (const char *method,
422             const char *data, size_t data_size)
423 {
424   struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
425   GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
426                        "_foo", DATA2ARG ("bar baz"));
427
428   tmit = (struct TransmitClosure) {};
429   tmit.data = data;
430   tmit.size = data_size;
431
432   GNUNET_SOCIAL_guest_talk (gst, method, env,
433                             notify_data, &tmit,
434                             GNUNET_SOCIAL_TALK_NONE);
435   GNUNET_PSYC_env_destroy (env);
436 }
437
438
439 /* HISTORY REPLAY */
440
441
442 /**
443  * Callback notifying about the end of history replay results.
444  */
445 static void
446 recv_history_replay_result (void *cls, int64_t result,
447                             const void *data, uint16_t data_size)
448 {
449   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
450               "Received history replay result: %" PRId64 "\n"
451               "%.*s\n",
452               result, data_size, (const char *) data);
453
454   if (op_replay || op_replay_latest)
455   {
456     exit_success ();
457   }
458 }
459
460
461 /**
462  * Replay history between a given @a start and @a end message IDs,
463  * optionally filtered by a method @a prefix.
464  */
465 static void
466 history_replay (uint64_t start, uint64_t end, const char *prefix)
467 {
468   GNUNET_SOCIAL_place_history_replay (plc, start, end, prefix,
469                                       GNUNET_PSYC_HISTORY_REPLAY_LOCAL,
470                                       slicer,
471                                       recv_history_replay_result,
472                                       NULL);
473 }
474
475
476 /**
477  * Replay latest @a limit messages.
478  */
479 static void
480 history_replay_latest (uint64_t limit, const char *prefix)
481 {
482   GNUNET_SOCIAL_place_history_replay_latest (plc, limit, prefix,
483                                              GNUNET_PSYC_HISTORY_REPLAY_LOCAL,
484                                              slicer,
485                                              recv_history_replay_result,
486                                              NULL);
487 }
488
489
490 /* LOOK AT/FOR */
491
492
493 /**
494  * Callback notifying about the end of state var results.
495  */
496 static void
497 look_result (void *cls, int64_t result_code,
498              const void *data, uint16_t data_size)
499 {
500   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501               "Received look result: %" PRId64 "\n", result_code);
502
503   if (op_look_at || op_look_for)
504   {
505     exit_success ();
506   }
507 }
508
509
510 /**
511  * Callback notifying about a state var result.
512  */
513 static void
514 look_var (void *cls,
515           const struct GNUNET_MessageHeader *mod,
516           const char *name,
517           const void *value,
518           uint32_t value_size,
519           uint32_t full_value_size)
520 {
521   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
522               "Received var: %s\n%.*s\n",
523               name, value_size, (const char *) value);
524 }
525
526
527 /**
528  * Look for a state var using exact match of the name.
529  */
530 static void
531 look_at (const char *full_name)
532 {
533   GNUNET_SOCIAL_place_look_at (plc, full_name, look_var, look_result, NULL);
534 }
535
536
537 /**
538  * Look for state vars by name prefix.
539  */
540 static void
541 look_for (const char *name_prefix)
542 {
543   GNUNET_SOCIAL_place_look_for (plc, name_prefix, look_var, look_result, NULL);
544 }
545
546
547 /* SLICER */
548
549
550 /**
551  * Callback notifying about the start of a new incoming message.
552  */
553 static void
554 slicer_recv_method (void *cls,
555                     const struct GNUNET_PSYC_MessageHeader *msg,
556                     const struct GNUNET_PSYC_MessageMethod *meth,
557                     uint64_t message_id,
558                     const char *method_name)
559 {
560   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
561               "Received method for message ID %" PRIu64 ":\n"
562               "%s (flags: %x)\n",
563               message_id, method_name, ntohl (meth->flags));
564 }
565
566
567 /**
568  * Callback notifying about an incoming modifier.
569  */
570 static void
571 slicer_recv_modifier (void *cls,
572                       const struct GNUNET_PSYC_MessageHeader *msg,
573                       const struct GNUNET_MessageHeader *pmsg,
574                       uint64_t message_id,
575                       enum GNUNET_PSYC_Operator oper,
576                       const char *name,
577                       const void *value,
578                       uint16_t value_size,
579                       uint16_t full_value_size)
580 {
581   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
582               "Received modifier for message ID %" PRIu64 ":\n"
583               "%c%s: %.*s (size: %u)\n",
584               message_id, oper, name, value_size, (const char *) value, value_size);
585 }
586
587
588 /**
589  * Callback notifying about an incoming data fragment.
590  */
591 static void
592 slicer_recv_data (void *cls,
593                   const struct GNUNET_PSYC_MessageHeader *msg,
594                   const struct GNUNET_MessageHeader *pmsg,
595                   uint64_t message_id,
596                   const void *data,
597                   uint16_t data_size)
598 {
599   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
600               "Received data for message ID %" PRIu64 ":\n"
601               "%.*s\n",
602               message_id, data_size, (const char *) data);
603 }
604
605
606 /**
607  * Callback notifying about the end of a message.
608  */
609 static void
610 slicer_recv_eom (void *cls,
611                 const struct GNUNET_PSYC_MessageHeader *msg,
612                 const struct GNUNET_MessageHeader *pmsg,
613                 uint64_t message_id,
614                 uint8_t is_cancelled)
615 {
616   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
617               "Received end of message ID %" PRIu64
618               ", cancelled: %u\n",
619               message_id, is_cancelled);
620 }
621
622
623 /**
624  * Create a slicer for receiving message parts.
625  */
626 static struct GNUNET_PSYC_Slicer *
627 slicer_create ()
628 {
629   slicer = GNUNET_PSYC_slicer_create ();
630
631   /* register slicer to receive incoming messages with any method name */
632   GNUNET_PSYC_slicer_method_add (slicer, "", NULL,
633                                  slicer_recv_method, slicer_recv_modifier,
634                                  slicer_recv_data, slicer_recv_eom, NULL);
635   return slicer;
636 }
637
638
639 /* GUEST ENTER */
640
641
642 /**
643  * Callback called when the guest receives an entry decision from the host.
644  *
645  * It is called once after using guest_enter() or guest_enter_by_name(),
646  * in case of a reconnection only the local enter callback is called.
647  */
648 static void
649 guest_recv_entry_decision (void *cls,
650                            int is_admitted,
651                            const struct GNUNET_PSYC_Message *entry_msg)
652 {
653   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
654               "Guest received entry decision %d\n",
655               is_admitted);
656
657   if (NULL != entry_msg)
658   {
659     struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
660     const char *method_name = NULL;
661     const void *data = NULL;
662     uint16_t data_size = 0;
663     struct GNUNET_PSYC_MessageHeader *
664       pmsg = GNUNET_PSYC_message_header_create_from_psyc (entry_msg);
665     GNUNET_PSYC_message_parse (pmsg, &method_name, env, &data, &data_size);
666     GNUNET_free (pmsg);
667
668     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
669                 "%s\n%.*s\n",
670                 method_name, data_size, (const char *) data);
671   }
672
673   if (op_guest_enter && !opt_follow)
674   {
675     exit_success ();
676   }
677 }
678
679
680 /**
681  * Callback called after a guest connection is established to the local service.
682  */
683 static void
684 guest_recv_local_enter (void *cls, int result,
685                         const struct GNUNET_CRYPTO_EddsaPublicKey *pub_key,
686                         uint64_t max_message_id)
687 {
688   char *pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (pub_key);
689   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
690               "Guest entered to local place: %s, max_message_id: %" PRIu64 "\n",
691               pub_str, max_message_id);
692   GNUNET_free (pub_str);
693   GNUNET_assert (0 <= result);
694
695   if (op_guest_enter && !opt_follow)
696   {
697     exit_success ();
698   }
699 }
700
701
702 /**
703  * Create entry requset message.
704  */
705 static struct GNUNET_PSYC_Message *
706 guest_enter_msg_create ()
707 {
708   const char *method_name = "_request_enter";
709   struct GNUNET_PSYC_Environment *env = GNUNET_PSYC_env_create ();
710   GNUNET_PSYC_env_add (env, GNUNET_PSYC_OP_SET,
711                        "_foo", DATA2ARG ("bar"));
712   void *data = "let me in";
713   uint16_t data_size = strlen (data) + 1;
714
715   return GNUNET_PSYC_message_create (method_name, env, data, data_size);
716 }
717
718
719 /**
720  * Enter a place as guest, using its public key and peer ID.
721  */
722 static void
723 guest_enter (const struct GNUNET_CRYPTO_EddsaPublicKey *pub_key,
724              const struct GNUNET_PeerIdentity *peer)
725 {
726   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727               "Entering to place as guest.\n");
728
729   if (NULL == ego)
730   {
731     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "--ego missing or invalid\n");
732     exit_fail ();
733     return;
734   }
735
736   struct GNUNET_PSYC_Message *join_msg = guest_enter_msg_create ();
737   gst = GNUNET_SOCIAL_guest_enter (app, ego, pub_key,
738                                    GNUNET_PSYC_SLAVE_JOIN_NONE,
739                                    peer, 0, NULL, join_msg, slicer_create (),
740                                    guest_recv_local_enter,
741                                    guest_recv_entry_decision, NULL);
742   GNUNET_free (join_msg);
743   plc = GNUNET_SOCIAL_guest_get_place (gst);
744 }
745
746
747 /**
748  * Enter a place as guest using its GNS address.
749  */
750 static void
751 guest_enter_by_name (const char *gns_name)
752 {
753   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
754               "Entering to place by name as guest.\n");
755
756   struct GNUNET_PSYC_Message *join_msg = guest_enter_msg_create ();
757   gst = GNUNET_SOCIAL_guest_enter_by_name (app, ego, gns_name, NULL,
758                                            join_msg, slicer,
759                                            guest_recv_local_enter,
760                                            guest_recv_entry_decision, NULL);
761   GNUNET_free (join_msg);
762   plc = GNUNET_SOCIAL_guest_get_place (gst);
763 }
764
765
766 /* HOST ENTER */
767
768
769 /**
770  * Callback called when a @a nym wants to enter the place.
771  *
772  * The request needs to be replied with an entry decision.
773  */
774 static void
775 host_answer_door (void *cls,
776                   struct GNUNET_SOCIAL_Nym *nym,
777                   const char *method_name,
778                   struct GNUNET_PSYC_Environment *env,
779                   const void *data,
780                   size_t data_size)
781 {
782   const struct GNUNET_CRYPTO_EcdsaPublicKey *
783     nym_key = GNUNET_SOCIAL_nym_get_pub_key (nym);
784   char *
785     nym_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (nym_key);
786
787   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
788               "Entry request: %s\n", nym_str);
789   GNUNET_free (nym_str);
790
791   if (opt_welcome)
792   {
793     struct GNUNET_PSYC_Message *
794       resp = GNUNET_PSYC_message_create ("_notice_place_admit", env,
795                                          DATA2ARG ("Welcome, nym!"));
796     GNUNET_SOCIAL_host_entry_decision (hst, nym, GNUNET_YES, resp);
797     GNUNET_free (resp);
798   }
799   else if (opt_deny)
800   {
801     struct GNUNET_PSYC_Message *
802       resp = GNUNET_PSYC_message_create ("_notice_place_refuse", NULL,
803                                          DATA2ARG ("Go away!"));
804     GNUNET_SOCIAL_host_entry_decision (hst, nym, GNUNET_NO, resp);
805     GNUNET_free (resp);
806   }
807
808
809 }
810
811
812 /**
813  * Callback called when a @a nym has left the place.
814  */
815 static void
816 host_farewell (void *cls,
817                const struct GNUNET_SOCIAL_Nym *nym,
818                struct GNUNET_PSYC_Environment *env)
819 {
820   const struct GNUNET_CRYPTO_EcdsaPublicKey *
821     nym_key = GNUNET_SOCIAL_nym_get_pub_key (nym);
822   char *
823     nym_str = GNUNET_CRYPTO_ecdsa_public_key_to_string (nym_key);
824
825   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
826               "Farewell: %s\n", nym_str);
827   GNUNET_free (nym_str);
828 }
829
830
831 /**
832  * Callback called after the host entered the place.
833  */
834 static void
835 host_entered (void *cls, int result,
836               const struct GNUNET_CRYPTO_EddsaPublicKey *pub_key,
837               uint64_t max_message_id)
838 {
839   place_pub_key = *pub_key;
840   char *pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (pub_key);
841   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
842               "Host entered: %s, max_message_id: %" PRIu64 "\n",
843               pub_str, max_message_id);
844   GNUNET_free (pub_str);
845
846   if (op_host_enter && !opt_follow)
847   {
848     exit_success ();
849   }
850 }
851
852
853 /**
854  * Enter and start hosting a place.
855  */
856 static void
857 host_enter ()
858 {
859   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "host_enter()\n");
860
861   if (NULL == ego)
862   {
863     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "--ego missing or invalid\n");
864     exit_fail ();
865     return;
866   }
867
868   hst = GNUNET_SOCIAL_host_enter (app, ego,
869                                   GNUNET_PSYC_CHANNEL_PRIVATE,
870                                   slicer_create (), host_entered,
871                                   host_answer_door, host_farewell, NULL);
872   plc = GNUNET_SOCIAL_host_get_place (hst);
873 }
874
875
876 /* PLACE RECONNECT */
877
878
879 /**
880  * Perform operations common to both host & guest places.
881  */
882 static void
883 place_reconnected ()
884 {
885   static int first_run = GNUNET_YES;
886   if (GNUNET_NO == first_run)
887     return;
888   first_run = GNUNET_NO;
889
890   if (op_replay) {
891     history_replay (opt_start, opt_until, opt_method);
892   }
893   else if (op_replay_latest) {
894     history_replay_latest (opt_limit, opt_method);
895   }
896   else if (op_look_at) {
897     look_at (opt_name);
898   }
899   else if (op_look_for) {
900     look_for (opt_name);
901   }
902 }
903
904
905 /**
906  * Callback called after reconnecting to a host place.
907  */
908 static void
909 host_reconnected (void *cls, int result,
910                   const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
911                   uint64_t max_message_id)
912 {
913   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
914               "Host reconnected.\n");
915
916   if (op_host_leave) {
917     host_leave ();
918   }
919   else if (op_host_announce) {
920     host_announce (opt_method, opt_data, strlen (opt_data));
921   }
922   else if (op_host_assign) {
923     host_assign (opt_name, opt_data, strlen (opt_data) + 1);
924   }
925   else {
926     place_reconnected ();
927   }
928 }
929
930
931 /**
932  * Callback called after reconnecting to a guest place.
933  */
934 static void
935 guest_reconnected (void *cls, int result,
936                    const struct GNUNET_CRYPTO_EddsaPublicKey *place_pub_key,
937                    uint64_t max_message_id)
938 {
939   char *place_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (place_pub_key);
940   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
941               "Guest reconnected to place %s.\n", place_pub_str);
942   GNUNET_free (place_pub_str);
943
944   if (op_guest_leave) {
945     guest_leave ();
946   }
947   else if (op_guest_talk) {
948     guest_talk (opt_method, opt_data, strlen (opt_data));
949   }
950   else {
951     place_reconnected ();
952   }
953 }
954
955
956 /* APP */
957
958
959 /**
960  * Callback called after the ego and place callbacks.
961  */
962 static void
963 app_connected (void *cls)
964 {
965   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966               "App connected: %p\n", cls);
967
968   if (op_status)
969   {
970     exit_success ();
971   }
972   else if (op_host_enter)
973   {
974     host_enter ();
975   }
976   else if (op_guest_enter)
977   {
978     if (opt_gns)
979     {
980       guest_enter_by_name (opt_gns);
981     }
982     else
983     {
984       if (opt_peer)
985       {
986         if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (opt_peer,
987                                                                      strlen (opt_peer),
988                                                                      &peer.public_key))
989         {
990           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
991                       "--peer invalid");
992           exit_fail ();
993           return;
994         }
995       }
996       else
997       {
998         peer = this_peer;
999       }
1000       guest_enter (&place_pub_key, &peer);
1001     }
1002   }
1003 }
1004
1005
1006 /**
1007  * Callback notifying about a host place available for reconnection.
1008  */
1009 static void
1010 app_recv_host (void *cls,
1011                struct GNUNET_SOCIAL_HostConnection *hconn,
1012                struct GNUNET_SOCIAL_Ego *ego,
1013                const struct GNUNET_CRYPTO_EddsaPublicKey *host_pub_key,
1014                enum GNUNET_SOCIAL_AppPlaceState place_state)
1015 {
1016   char *host_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (host_pub_key);
1017   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1018               "Host:  %s\n", host_pub_str);
1019   GNUNET_free (host_pub_str);
1020
1021   if ((op_host_reconnect || op_host_leave || op_host_announce || op_host_assign
1022        || op_replay || op_replay_latest
1023        || op_look_at || op_look_for)
1024       && 0 == memcmp (&place_pub_key, host_pub_key, sizeof (*host_pub_key)))
1025   {
1026     hst = GNUNET_SOCIAL_host_enter_reconnect (hconn, slicer_create (), host_reconnected,
1027                                               host_answer_door, host_farewell, NULL);
1028     plc = GNUNET_SOCIAL_host_get_place (hst);
1029   }
1030 }
1031
1032
1033 /**
1034  * Callback notifying about a guest place available for reconnection.
1035  */
1036 static void
1037 app_recv_guest (void *cls,
1038                 struct GNUNET_SOCIAL_GuestConnection *gconn,
1039                 struct GNUNET_SOCIAL_Ego *ego,
1040                 const struct GNUNET_CRYPTO_EddsaPublicKey *guest_pub_key,
1041                 enum GNUNET_SOCIAL_AppPlaceState place_state)
1042 {
1043   char *guest_pub_str = GNUNET_CRYPTO_eddsa_public_key_to_string (guest_pub_key);
1044   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1045               "Guest: %s\n", guest_pub_str);
1046   GNUNET_free (guest_pub_str);
1047
1048   if ((op_guest_reconnect || op_guest_leave || op_guest_talk
1049        || op_replay || op_replay_latest
1050        || op_look_at || op_look_for)
1051       && 0 == memcmp (&place_pub_key, guest_pub_key, sizeof (*guest_pub_key)))
1052   {
1053     gst = GNUNET_SOCIAL_guest_enter_reconnect (gconn, GNUNET_PSYC_SLAVE_JOIN_NONE,
1054                                                slicer_create (), guest_reconnected, NULL);
1055     plc = GNUNET_SOCIAL_guest_get_place (gst);
1056   }
1057 }
1058
1059
1060 /**
1061  * Callback notifying about an available ego.
1062  */
1063 static void
1064 app_recv_ego (void *cls,
1065               struct GNUNET_SOCIAL_Ego *e,
1066               const struct GNUNET_CRYPTO_EcdsaPublicKey *pub_key,
1067               const char *name)
1068 {
1069   char *s = GNUNET_CRYPTO_ecdsa_public_key_to_string (pub_key);
1070   GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Ego:   %s\t%s\n", s, name);
1071   GNUNET_free (s);
1072
1073   if (0 == memcmp (&ego_pub_key, pub_key, sizeof (*pub_key))
1074       || (NULL != opt_ego && 0 == strcmp (opt_ego, name)))
1075   {
1076     ego = e;
1077   }
1078
1079 }
1080
1081
1082
1083 /**
1084  * Establish application connection to receive available egos and places.
1085  */
1086 static void
1087 app_connect (void *cls)
1088 {
1089   GNUNET_CORE_disconnecT (core);
1090   core = NULL;
1091
1092   app = GNUNET_SOCIAL_app_connect (cfg, opt_app,
1093                                    app_recv_ego,
1094                                    app_recv_host,
1095                                    app_recv_guest,
1096                                    app_connected,
1097                                    NULL);
1098 }
1099
1100
1101 /* CORE */
1102
1103
1104 static void
1105 core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
1106 {
1107   this_peer = *my_identity;
1108   GNUNET_SCHEDULER_add_now (app_connect, NULL);
1109 }
1110
1111
1112 /* RUN */
1113
1114
1115 /**
1116  * Main function run by the scheduler.
1117  *
1118  * @param cls closure
1119  * @param args remaining command-line arguments
1120  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1121  * @param c configuration
1122  */
1123 static void
1124 run (void *cls, char *const *args, const char *cfgfile,
1125      const struct GNUNET_CONFIGURATION_Handle *c)
1126 {
1127   cfg = c;
1128
1129   if (!opt_method)
1130     opt_method = "message";
1131   if (!opt_data)
1132     opt_data = "";
1133   if (!opt_name)
1134     opt_name = "";
1135
1136   if (! (op_status
1137          || op_host_enter || op_host_reconnect || op_host_leave
1138          || op_host_announce || op_host_assign
1139          || op_guest_enter || op_guest_reconnect
1140          || op_guest_leave || op_guest_talk
1141          || op_replay || op_replay_latest
1142          || op_look_at || op_look_for))
1143   {
1144     op_status = 1;
1145   }
1146
1147   GNUNET_SCHEDULER_add_shutdown (scheduler_shutdown, NULL);
1148   if (!opt_follow)
1149   {
1150     timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, timeout, NULL);
1151   }
1152
1153   if ((op_host_reconnect || op_host_leave || op_host_announce || op_host_assign
1154        || op_guest_reconnect || (op_guest_enter && !opt_gns)
1155        || op_guest_leave || op_guest_talk
1156        || op_replay || op_replay_latest
1157        || op_look_at || op_look_for)
1158       && (!opt_place
1159           || GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (opt_place,
1160                                                                       strlen (opt_place),
1161                                                                       &place_pub_key)))
1162   {
1163     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1164                 _("--place missing or invalid.\n"));
1165     exit_fail ();
1166     return;
1167   }
1168
1169   if (opt_ego)
1170   {
1171     if (GNUNET_OK !=
1172         GNUNET_CRYPTO_ecdsa_public_key_from_string (opt_ego,
1173                                                 strlen (opt_ego),
1174                                                 &ego_pub_key))
1175     {
1176       FPRINTF (stderr,
1177                _("Public key `%s' malformed\n"),
1178                opt_ego);
1179       exit_fail ();
1180       return;
1181     }
1182   }
1183
1184   core = GNUNET_CORE_connecT (cfg, NULL, &core_connected, NULL, NULL, NULL);
1185 }
1186
1187
1188 /**
1189  * The main function to obtain peer information.
1190  *
1191  * @param argc number of arguments from the command line
1192  * @param argv command line arguments
1193  * @return 0 ok, 1 on error
1194  */
1195 int
1196 main (int argc, char *const *argv)
1197 {
1198   int res;
1199   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1200     /*
1201      * gnunet program options in addition to the ones below:
1202      *
1203      * -c, --config=FILENAME
1204      * -l, --logfile=LOGFILE
1205      * -L, --log=LOGLEVEL
1206      * -h, --help
1207      * -v, --version
1208      */
1209
1210     /* operations */
1211
1212     { 'A', "host-assign", NULL,
1213       gettext_noop ("assign --name in state to --data"),
1214       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_host_assign },
1215
1216     { 'B', "guest-leave", NULL,
1217       gettext_noop ("say good-bye and leave somebody else's place"),
1218       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_guest_leave },
1219
1220     { 'C', "host-enter", NULL,
1221       gettext_noop ("create a place"),
1222       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_host_enter },
1223
1224     { 'D', "host-leave", NULL,
1225       gettext_noop ("destroy a place we were hosting"),
1226       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_host_leave },
1227
1228     { 'E', "guest-enter", NULL,
1229       gettext_noop ("enter somebody else's place"),
1230       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_guest_enter },
1231
1232     { 'F', "look-for", NULL,
1233       gettext_noop ("find state matching name prefix"),
1234       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_look_for },
1235
1236     { 'H', "replay-latest", NULL,
1237       gettext_noop ("replay history of messages up to the given --limit"),
1238       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_replay_latest },
1239
1240     { 'N', "host-reconnect", NULL,
1241       gettext_noop ("reconnect to a previously created place"),
1242       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_host_reconnect },
1243
1244     { 'P', "host-announce", NULL,
1245       gettext_noop ("publish something to a place we are hosting"),
1246       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_host_announce },
1247
1248     { 'R', "guest-reconnect", NULL,
1249       gettext_noop ("reconnect to a previously entered place"),
1250       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_guest_reconnect },
1251
1252     { 'S', "look-at", NULL,
1253       gettext_noop ("search for state matching exact name"),
1254       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_look_at },
1255
1256     { 'T', "guest-talk", NULL,
1257       gettext_noop ("submit something to somebody's place"),
1258       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_guest_talk },
1259
1260     { 'U', "status", NULL,
1261       gettext_noop ("list of egos and subscribed places"),
1262       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_status },
1263
1264     { 'X', "replay", NULL,
1265       gettext_noop ("extract and replay history between message IDs --start and --until"),
1266       GNUNET_NO, &GNUNET_GETOPT_set_one, &op_replay },
1267
1268
1269     /* options */
1270
1271     { 'a', "app", "APPLICATION_ID",
1272       gettext_noop ("application ID to use when connecting"),
1273       GNUNET_YES, &GNUNET_GETOPT_set_string, &opt_app },
1274
1275     { 'd', "data", "DATA",
1276       gettext_noop ("message body or state value"),
1277       GNUNET_YES, &GNUNET_GETOPT_set_string, &opt_data },
1278
1279     { 'e', "ego", "NAME|PUBKEY",
1280       gettext_noop ("name or public key of ego"),
1281       GNUNET_YES, &GNUNET_GETOPT_set_string, &opt_ego },
1282
1283     { 'f', "follow", NULL,
1284       gettext_noop ("wait for incoming messages"),
1285       GNUNET_NO, &GNUNET_GETOPT_set_one, &opt_follow },
1286
1287     { 'g', "gns", "GNS_NAME",
1288       gettext_noop ("GNS name"),
1289       GNUNET_YES, &GNUNET_GETOPT_set_string, &opt_gns },
1290
1291     { 'i', "peer", "PEER_ID",
1292       gettext_noop ("peer ID for --guest-enter"),
1293       GNUNET_YES, &GNUNET_GETOPT_set_string, &opt_peer },
1294
1295     { 'k', "name", "VAR_NAME",
1296       gettext_noop ("name (key) to query from state"),
1297       GNUNET_YES, &GNUNET_GETOPT_set_string, &opt_name },
1298
1299     { 'm', "method", "METHOD_NAME",
1300       gettext_noop ("method name"),
1301       GNUNET_YES, &GNUNET_GETOPT_set_string, &opt_method },
1302
1303     { 'n', "limit", NULL,
1304       gettext_noop ("number of messages to replay from history"),
1305       GNUNET_YES, &GNUNET_GETOPT_set_ulong, &opt_limit },
1306
1307     { 'p', "place", "PUBKEY",
1308       gettext_noop ("key address of place"),
1309       GNUNET_YES, &GNUNET_GETOPT_set_string, &opt_place },
1310
1311     { 's', "start", NULL,
1312       gettext_noop ("start message ID for history replay"),
1313       GNUNET_YES, &GNUNET_GETOPT_set_ulong, &opt_start },
1314
1315     { 'w', "welcome", NULL,
1316       gettext_noop ("respond to entry requests by admitting all guests"),
1317       GNUNET_NO, &GNUNET_GETOPT_set_one, &opt_welcome },
1318
1319     { 'u', "until", NULL,
1320       gettext_noop ("end message ID for history replay"),
1321       GNUNET_YES, &GNUNET_GETOPT_set_ulong, &opt_until },
1322
1323     { 'y', "deny", NULL,
1324       gettext_noop ("respond to entry requests by refusing all guests"),
1325       GNUNET_NO, &GNUNET_GETOPT_set_one, &opt_deny },
1326
1327     GNUNET_GETOPT_OPTION_END
1328   };
1329
1330   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1331     return 2;
1332
1333   const char *help =
1334     _ ("gnunet-social - Interact with the social service: enter/leave, send/receive messages, access history and state.\n");
1335   const char *usage =
1336     "gnunet-social [--status]\n"
1337     "\n"
1338     "gnunet-social --host-enter --ego <NAME or PUBKEY> [--follow] [--welcome | --deny]\n"
1339     "gnunet-social --host-reconnect --place <PUBKEY> [--follow] [--welcome | --deny]\n"
1340     "gnunet-social --host-leave --place <PUBKEY>\n"
1341     "gnunet-social --host-assign --place <PUBKEY> --name <NAME> --data <VALUE>\n"
1342 // FIXME: some state ops not implemented yet (no hurry)
1343 //  "gnunet-social --host-augment --place <PUBKEY> --name <NAME> --data <VALUE>\n"
1344 //  "gnunet-social --host-diminish --place <PUBKEY> --name <NAME> --data <VALUE>\n"
1345 //  "gnunet-social --host-set --place <PUBKEY> --name <NAME> --data <VALUE>\n"
1346     "gnunet-social --host-announce --place <PUBKEY> --method <METHOD_NAME> --data <MESSAGE_BODY>\n"
1347     "\n"
1348     "gnunet-social --guest-enter --place <PUBKEY> --peer <PEERID> --ego <NAME or PUBKEY> [--follow]\n"
1349     "gnunet-social --guest-enter --gns <GNS_NAME> --ego <NAME or PUBKEY> [--follow]\n"
1350     "gnunet-social --guest-reconnect --place <PUBKEY> [--follow]\n"
1351     "gnunet-social --guest-leave --place <PUBKEY>\n"
1352     "gnunet-social --guest-talk --place <PUBKEY> --method <METHOD_NAME> --data <MESSAGE_BODY>\n"
1353     "\n"
1354     "gnunet-social --history-replay --place <PUBKEY> --start <MSGID> --until <MSGID>  [--method <METHOD_PREFIX>]\n"
1355     "gnunet-social --history-replay-latest --place <PUBKEY> --limit <MSG_LIMIT> [--method <METHOD_PREFIX>]\n"
1356     "\n"
1357     "gnunet-social --look-at --place <PUBKEY> --name <FULL_NAME>\n"
1358     "gnunet-social --look-for --place <PUBKEY> --name <NAME_PREFIX>\n";
1359
1360   res = GNUNET_PROGRAM_run (argc, argv, help, usage, options, &run, NULL);
1361
1362   GNUNET_free ((void *) argv);
1363
1364   if (GNUNET_OK == res)
1365     return ret;
1366   else
1367     return 1;
1368 }