-gads to gnu
[oweals/gnunet.git] / src / include / gnunet_social_service.h
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /** 
22  * @file include/gnunet_social_service.h
23  * @brief Social service; implements social functionality using the PSYC service.
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  */
27 #ifndef GNUNET_SOCIAL_SERVICE_H
28 #define GNUNET_SOCIAL_SERVICE_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 #include "gnunet_util_lib.h"
39 #include "gnunet_psyc_lib.h"
40 #include "gnunet_psyc_service.h"
41 #include "gnunet_multicast_service.h"
42
43
44 /** 
45  * Version number of GNUnet Social API.
46  */
47 #define GNUNET_SOCIAL_VERSION 0x00000000
48
49
50 /** 
51  * Handle for another user (who is likely pseudonymous) in the network.
52  */
53 struct GNUNET_SOCIAL_Nym;
54
55 /** 
56  * Handle for a place where social interactions happen.
57  */
58 struct GNUNET_SOCIAL_Place;
59
60 /** 
61  * Handle for a place that one of our egos hosts.
62  */
63 struct GNUNET_SOCIAL_Home;
64
65 /** 
66  * Handle to an implementation of try-and-slice.
67  */
68 struct GNUNET_SOCIAL_Slicer;
69
70
71 /** 
72  * Method called from SOCIAL upon receiving a message indicating a call
73  * to a @e method.
74  *
75  * @param cls Closure.
76  * @param full_method_name Original method name from PSYC (may be more
77  *        specific than the registered method name due to try-and-slice matching).
78
79  * @param message_id Unique message counter for this message
80  *                   (unique only in combination with the given sender for
81  *                    this channel).
82  * @param modifier_count Number of elements in the @a modifiers array.
83  * @param modifiers Modifiers present in the message. FIXME: use environment instead?
84  * @param data_offset Byte offset of @a data in the overall data of the method.
85  * @param data_size Number of bytes in @a data.
86  * @param data Data stream given to the method (might not be zero-terminated
87  *             if data is binary).
88  * @param flags Message flags indicating fragmentation status.
89  */
90 typedef int (*GNUNET_SOCIAL_Method)(void *cls,
91                                     const char *full_method_name,
92                                     uint64_t message_id,
93                                     size_t modifier_count,
94                                     GNUNET_PSYC_Modifier *modifiers,
95                                     uint64_t data_offset,
96                                     size_t data_size,
97                                     const void *data,
98                                     enum GNUNET_PSYC_MessageFlags flags);
99
100
101 /** 
102  * Create a try-and-slice instance.
103  *
104  * @return A new try-and-slice construct.
105  */
106 struct GNUNET_SOCIAL_Slicer *
107 GNUNET_SOCIAL_slicer_create (void);
108
109
110 /** 
111  * Add a method to the try-and-slice instance.
112  *
113  * A slicer processes messages and calls methods that match a message. A match
114  * happens whenever the method name of a message starts with the method_name
115  * parameter given here.
116  *
117  * @param slicer The try-and-slice instance to extend.
118  * @param method_name Name of the given method, use empty string for default.
119  * @param method Method to invoke.
120  * @param method_cls Closure for method.
121  */
122 void
123 GNUNET_SOCIAL_slicer_add (struct GNUNET_SOCIAL_Slicer *slicer,
124                           const char *method_name,
125                           GNUNET_SOCIAL_Method method,
126                           void *method_cls);
127
128
129 /** 
130  * Remove a registered method from the try-and-slice instance.
131  *
132  * @param slicer The try-and-slice instance.
133  * @param method_name Name of the method to remove.
134  * @param method Method handler.
135  */
136 void
137 GNUNET_SOCIAL_slicer_remove (struct GNUNET_SOCIAL_Slicer *slicer,
138                              const char *method_name,
139                              GNUNET_SOCIAL_Method method);
140
141 /** 
142  * Destroy a given try-and-slice instance.
143  *
144  * @param slicer slicer to destroy
145  */
146 void
147 GNUNET_SOCIAL_slicer_destroy (struct GNUNET_SOCIAL_Slicer *slicer);
148
149
150 /** 
151  * Function called asking for nym to be admitted to the place.
152  *
153  * Should call either GNUNET_SOCIAL_home_admit() or
154  * GNUNET_SOCIAL_home_reject_entry() (possibly asynchronously).  If this owner
155  * cannot decide, it is fine to call neither function, in which case hopefully
156  * some other owner of the home exists that will make the decision. The @a nym
157  * reference remains valid until the #GNUNET_SOCIAL_FarewellCallback is invoked
158  * for it.
159  *
160  * @param cls Closure.
161  * @param nym Handle for the user who wants to enter.
162  * @param variable_count Number of elements in the @a variables array.
163  * @param variables Variables present in the message.
164  * @param data_size Number of bytes in @a data.
165  * @param data Payload given on enter (e.g. a password).
166  */
167 typedef void (*GNUNET_SOCIAL_AnswerDoorCallback)(void *cls,
168                                                  struct GNUNET_SOCIAL_Nym *nym,
169                                                  size_t variable_count,
170                                                  GNUNET_PSYC_Modifier *variables,
171                                                  size_t data_size,
172                                                  const void *data);
173
174
175 /** 
176  * Function called when a @a nym leaves the place.
177  * 
178  * This is also called if the @a nym was never given permission to enter
179  * (i.e. the @a nym stopped asking to get in).
180  *
181  * @param cls Closure.
182  * @param nym Handle for the user who left.
183  * @param variable_count Number of elements in the @a variables array.
184  * @param variables Variables present in the message.
185  */
186 typedef void (*GNUNET_SOCIAL_FarewellCallback)(void *cls,
187                                                struct GNUNET_SOCIAL_Nym *nym,
188                                                size_t variable_count,
189                                                GNUNET_PSYC_Modifier *variables);
190
191
192 /** 
193  * Enter a home where guests (nyms) can be hosted.
194  *
195  * A home is created upon first entering, and exists until
196  * GNUNET_SOCIAL_home_destroy() is called. It can also be left temporarily using
197  * GNUNET_SOCIAL_home_leave().
198  *
199  * @param cfg Configuration to contact the social service.
200  * @param home_keyfile File with the private-public key pair of the home,
201  *        created if the file does not exist; pass NULL for ephemeral homes.
202  * @param policy Policy specifying entry and history restrictions of the home.
203  * @param ego Owner of the home (host).
204  * @param slicer Slicer to handle guests talking.
205  * @param listener_cb Function to handle new nyms that want to enter.
206  * @param farewell_cb Function to handle departing nyms.
207  * @param cls Closure for @a listener_cb and @a farewell_cb.
208  * @return Handle for a new home.
209  */
210 struct GNUNET_SOCIAL_Home *
211 GNUNET_SOCIAL_home_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
212                           const char *home_keyfile,
213                           enum GNUNET_PSYC_Policy policy,
214                           struct GNUNET_IDENTITY_Ego *ego,
215                           struct GNUNET_SOCIAL_Slicer *slicer,
216                           GNUNET_SOCIAL_AnswerDoorCallback listener_cb,
217                           GNUNET_SOCIAL_FarewellCallback farewell_cb,
218                           void *cls);
219
220
221 /** 
222  * Admit @a nym to the @a home.
223  *
224  * The @a nym reference will remain valid until either the home is destroyed or
225  * @a nym leaves.
226  *
227  * @param home Home to allow @a nym to enter.
228  * @param nym Handle for the entity that wants to enter.
229  */
230 void
231 GNUNET_SOCIAL_home_admit (struct GNUNET_SOCIAL_Home *home,
232                           struct GNUNET_SOCIAL_Nym *nym);
233
234
235 /** 
236  * Throw @a nym out of the @a home.
237  *
238  * The @a nym reference will remain valid until the
239  * #GNUNET_SOCIAL_FarewellCallback is invoked,
240  * which should be very soon after this call.
241  *
242  * @param home Home to eject @a nym from.
243  * @param nym Handle for the entity to be ejected.
244  */
245 void
246 GNUNET_SOCIAL_home_eject (struct GNUNET_SOCIAL_Home *home,
247                           struct GNUNET_SOCIAL_Nym *nym);
248
249
250 /** 
251  * Refuse @a nym entry into the @a home.
252  *
253  * @param home Home to disallow @a nym to enter.
254  * @param nym Handle for the entity that wanted to enter.
255  * @param method_name Method name for the rejection message.
256  * @param env Environment containing variables for the message, or NULL.
257  * @param data_size Number of bytes in @a data for method.
258  * @param data Data for the rejection message to send back.
259  */
260 void
261 GNUNET_SOCIAL_home_reject_entry (struct GNUNET_SOCIAL_Home *home,
262                                  struct GNUNET_SOCIAL_Nym *nym,
263                                  const char *method_name,
264                                  const struct GNUNET_ENV_Environment *env,
265                                  size_t data_size,
266                                  const void *data);
267
268
269 /** 
270  * Get the public key of a nym.
271  *
272  * Suitable, for example, to be used with GNUNET_NAMESTORE_zone_to_name().
273  *
274  * @param nym Pseudonym to map to a cryptographic identifier.
275  * @param[out] nym_key Set to the public key of the nym.
276  */
277 void
278 GNUNET_SOCIAL_nym_get_key (struct GNUNET_SOCIAL_Nym *nym,
279                            struct GNUNET_CRYPTO_EccPublicKey *nym_key);
280
281
282 /** 
283  * Obtain the private-public key pair of the home.
284  * 
285  * @param home Home to get the key of.
286  * @param[out] home_key Set to the private-public key pair of the home.  The public part is suitable for storing in GADS within a "PLACE" record, along with peer IDs to join at.
287  */
288 void
289 GNUNET_SOCIAL_home_get_key (struct GNUNET_SOCIAL_Home *home,
290                             struct GNUNET_CRYPTO_EccPrivateKey *home_key);
291
292
293 /** 
294  * Advertise @a home under @a name in the GADS zone of the @e ego.
295  *
296  * @param home The home to advertise.
297  * @param name The name for the PLACE record to put in the zone.
298  * @param peer_count Number of elements in the @a peers array.
299  * @param peers List of peers in the PLACE record that can be used to send join
300  *        requests to.
301  * @param expiration_time Expiration time of the record, use 0 to remove the record.
302  */
303 void
304 GNUNET_SOCIAL_home_advertise (struct GNUNET_SOCIAL_Home *home,
305                               const char *name,
306                               size_t peer_count,
307                               const struct GNUNET_PeerIdentity *peers,
308                               GNUNET_TIME_Relative expiration_time);
309
310
311 /**
312  * Flags for announcements in a home.
313  */
314 enum GNUNET_PSYC_AnnouncementFlags
315 {
316   /** 
317    * Whether this announcement removes all objects from the home.
318    * 
319    * New objects can be still added to the now empty home using the @e env
320    * parameter of the same announcement.
321    */
322   GNUNET_SOCIAL_ANNOUNCEMENT_CLEAR_OBJECTS = 1 << 0
323 };
324
325
326 /** 
327  * Handle for an announcement request.
328  */
329 struct GNUNET_SOCIAL_Announcement;
330
331
332 /** 
333  * Send a message to all nyms that are present in the home.
334  *
335  * This function is restricted to the home owner.  Nyms can only send requests
336  * to the home owner who can decide to relay it to other guests.
337  *
338  * @param home Home to address the announcement to.
339  * @param method_name Method to use for the announcement.
340  * @param env Environment containing variables for the message and operations on
341  *        objects of the home, or NULL.
342  * @param notify Function to call to get the payload of the announcement.
343  * @param notify_cls Closure for @a notify.
344  * @param flags Flags for this announcement.
345  * @return NULL on error (announcement already in progress?).
346  */
347 struct GNUNET_SOCIAL_Announcement *
348 GNUNET_SOCIAL_home_announce (struct GNUNET_SOCIAL_Home *home,
349                              const char *method_name,
350                              const struct GNUNET_ENV_Environment *env,
351                              GNUNET_CONNECTION_TransmitReadyNotify notify,
352                              void *notify_cls,
353                              GNUNET_SOCIAL_AnnouncementFlags flags);
354
355
356 /** 
357  * Cancel announcement.
358  *
359  * @param a The announcement to cancel.
360  */
361 void
362 GNUNET_SOCIAL_home_announce_cancel (struct GNUNET_SOCIAL_Announcement *a);
363
364
365 /** 
366  * Convert our home to a place so we can access it via the place API.
367  *
368  * @param home Handle for the home.
369  * @return Place handle for the same home, valid as long as @a home is valid;
370  *         do NOT try to GNUNET_SOCIAL_place_leave() this place, it's your home!
371  */
372 struct GNUNET_SOCIAL_Place *
373 GNUNET_SOCIAL_home_get_place (struct GNUNET_SOCIAL_Home *home);
374
375
376 /** 
377  * Leave a home temporarily, visitors can stay.
378  *
379  * After leaving, handling of incoming messages are left to other clients of the
380  * social service, and stops after the last client exits.
381  *
382  * @param home Home to leave temporarily (handle becomes invalid).
383  */
384 void
385 GNUNET_SOCIAL_home_away (struct GNUNET_SOCIAL_Home *home);
386
387
388 /** 
389  * Leave a home.
390
391  * Invalidates home handle.
392  * Guests will be disconnected until the home is restarted.
393  *
394  * @param home Home to leave.
395  */
396 void
397 GNUNET_SOCIAL_home_leave (struct GNUNET_SOCIAL_Home *home);
398
399 /** 
400  * Request entry to a place (home hosted by someone else).
401  *
402  * @param cfg Configuration to contact the social service.
403  * @param ego Owner of the home (host).
404  * @param address GADS name of the place to enter.  Either in the form of
405  *        'room.friend.gnu', or 'NYMPUBKEY.zkey'.  This latter case refers to
406  *        the 'PLACE' record of the empty label ("+") in the GADS zone with the
407  *        nym's public key 'NYMPUBKEY', and can be used to request entry to a
408  *        pseudonym's place directly.
409  * @param env Environment containing variables for the message, or NULL.
410  * @param data_size Number of bytes in @a data.
411  * @param data Payload for the message to give to the enter callback.
412  * @param slicer Slicer to use for processing incoming requests from guests.
413  * @return NULL on errors, otherwise handle to the place.
414  */
415 struct GNUNET_SOCIAL_Place *
416 GNUNET_SOCIAL_place_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
417                            struct GNUNET_IDENTITY_Ego *ego,
418                            char *address,
419                            const struct GNUNET_ENV_Environment *env,
420                            size_t data_size,
421                            const void *data,
422                            struct GNUNET_SOCIAL_Slicer *slicer);
423
424 /** 
425  * Request entry to a place (home hosted by someone else).
426  *
427  * @param cfg Configuration to contact the social service.
428  * @param ego Owner of the home (host).
429  * @param crypto_address Public key of the place to enter.
430  * @param peer Peer to send request to.
431  * @param slicer Slicer to use for processing incoming requests from guests.
432  * @param env Environment containing variables for the message, or NULL.
433  * @param data_size Number of bytes in @a data.
434  * @param data Payload for the message to give to the enter callback.
435  * @return NULL on errors, otherwise handle to the place.
436  */
437 struct GNUNET_SOCIAL_Place *
438 GNUNET_SOCIAL_place_enter2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
439                            struct GNUNET_IDENTITY_Ego *ego,
440                            struct GNUNET_CRYPTO_EccPublicKey *crypto_address,
441                            struct GNUNET_PeerIdentity *peer,
442                            struct GNUNET_SOCIAL_Slicer *slicer,
443                            const struct GNUNET_ENV_Environment *env,
444                            size_t data_size,
445                            const void *data);
446
447
448 struct GNUNET_SOCIAL_WatchHandle;
449
450 /** 
451  * Watch a place for changed objects.
452  *
453  * @param place Place to watch.
454  * @param object_filter Object prefix to match.
455  * @param state_cb Function to call when an object/state changes.
456  * @param state_cb_cls Closure for callback.
457  * 
458  * @return Handle that can be used to cancel watching.
459  */
460 struct GNUNET_SOCIAL_WatchHandle *
461 GNUNET_SOCIAL_place_watch (struct GNUNET_SOCIAL_Place *place,
462                            const char *object_filter,
463                            GNUNET_PSYC_StateCallback state_cb,
464                            void *state_cb_cls);
465
466
467 /** 
468  * Cancel watching a place for changed objects.
469  *
470  * @param wh Watch handle to cancel.
471  */
472 void
473 GNUNET_SOCIAL_place_watch_cancel (struct GNUNET_SOCIAL_WatchHandle *wh);
474
475
476 struct GNUNET_SOCIAL_LookHandle;
477
478 /** 
479  * Look at all objects in the place.
480  *
481  * @param place The place to look its objects at.
482  * @param state_cb Function to call for each object found.
483  * @param state_cb_cls Closure for callback function.
484  * 
485  * @return Handle that can be used to stop looking at objects.
486  */
487 struct GNUNET_SOCIAL_LookHandle *
488 GNUNET_SOCIAL_place_look (struct GNUNET_SOCIAL_Place *place,
489                           GNUNET_PSYC_StateCallback state_cb,
490                           void *state_cb_cls);
491
492
493 /** 
494  * Look at matching objects in the place.
495  *
496  * @param place The place to look its objects at.
497  * @param object_filter Only look at objects with names beginning with this filter value.
498  * @param state_cb Function to call for each object found.
499  * @param state_cb_cls Closure for callback function.
500  * 
501  * @return Handle that can be used to stop looking at objects.
502  */
503 struct GNUNET_SOCIAL_LookHandle *
504 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *place,
505                               const char *object_filter,
506                               GNUNET_PSYC_StateCallback state_cb,
507                               void *state_cb_cls);
508
509
510 /** 
511  * Stop looking at objects.
512  *
513  * @param lh Look handle to stop.
514  */
515 void
516 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *lh);
517
518
519
520 /** 
521  * Look at a particular object in the place.
522  *
523  * @param place The place to look the object at.
524  * @param object_name Full name of the object.
525  * @param value_size Set to the size of the returned value.
526  * @return NULL if there is no such object at this place.
527  */
528 const void *
529 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *place,
530                              const char *object_name,
531                              size_t *value_size);
532
533
534 /**
535  * Flags for talking to the host of a place.
536  */
537 enum GNUNET_SOCIAL_TalkFlags;
538
539
540 /** 
541  * A talk request.
542  */
543 struct GNUNET_SOCIAL_TalkRequest;
544
545 /** 
546  * Talk to the host of the place.
547  *
548  * @param place Place where we want to talk to the host.
549  * @param method_name Method to invoke on the host.
550  * @param env Environment containing variables for the message, or NULL.
551  * @param notify Function to use to get the payload for the method.
552  * @param notify_cls Closure for @a notify.
553  * @param flags Flags for the message being sent.
554  * @return NULL if we are already trying to talk to the host,
555  *         otherwise handle to cancel the request.
556  */
557 struct GNUNET_SOCIAL_TalkRequest *
558 GNUNET_SOCIAL_place_talk (struct GNUNET_SOCIAL_Place *place,
559                           const char *method_name,
560                           const struct GNUNET_ENV_Environment *env,
561                           GNUNET_CONNECTION_TransmitReadyNotify notify,
562                           void *notify_cls,
563                           GNUNET_SOCIAL_TalkFlags flags);
564
565
566 /** 
567  * Cancel talking to the host of the place.
568  *
569  * @param tr Talk request to cancel.
570  */
571 void
572 GNUNET_SOCIAL_place_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr);
573
574
575 /** 
576  * A history lesson.
577  */
578 struct GNUNET_SOCIAL_HistoryLesson;
579
580 /** 
581  * Learn about the history of a place.
582  *
583  * Sends messages through the slicer function of the place where
584  * start_message_id <= message_id <= end_message_id.
585  * The messages will have the #GNUNET_PSYC_MESSAGE_HISTORIC flag set.
586  *
587  * To get the latest message, use 0 for both the start and end message ID.
588  * 
589  * @param place Place we want to learn more about.
590  * @param start_message_id First historic message we are interested in.
591  * @param end_message_id Last historic message we are interested in (inclusive).
592  * @param slicer Slicer to use to process history.  Can be the same as the
593  *               slicer of the place, as the HISTORIC flag allows distinguishing
594  *               old messages from fresh ones.
595  * @param finish_cb Function called after the last message if the history lesson
596  *              is passed through the @a slicer. NULL if not needed.
597  * @param finish_cb_cls Closure for @a finish_cb.
598  * @return Handle to abort history lesson, never NULL (multiple lessons
599  *         at the same time are allowed).
600  */
601 struct GNUNET_SOCIAL_HistoryLesson *
602 GNUNET_SOCIAL_place_get_history (struct GNUNET_SOCIAL_Place *place,
603                                  uint64_t start_message_id,
604                                  uint64_t end_message_id,
605                                  const struct GNUNET_SOCIAL_Slicer *slicer,
606                                  void (*finish_cb)(void *),
607                                  void *finish_cb_cls);
608
609
610 /** 
611  * Stop processing messages from the history lesson.
612  *
613  * Must not be called after the finish callback of the history lesson is called.
614  *
615  * @param hist History lesson to cancel.
616  */
617 void
618 GNUNET_SOCIAL_place_get_history_cancel (struct GNUNET_SOCIAL_HistoryLesson *hist);
619
620
621 /** 
622  * Leave a place permanently.
623  *
624  * Notifies the owner of the place about leaving, and destroys the place handle.
625  * 
626  * @param place Place to leave permanently.
627  */
628 void
629 GNUNET_SOCIAL_place_leave (struct GNUNET_SOCIAL_Place *place);
630
631
632 /** 
633  * Leave a place temporarily.
634  *
635  * Stop following the conversation for the @a place and destroy the @a place
636  * handle.  Only affects the application calling this function, other clients of
637  * the service continue receiving the messages.
638  *
639  * @param place Place to leave temporarily.
640  */
641 void
642 GNUNET_SOCIAL_place_away (struct GNUNET_SOCIAL_Place *place);
643
644
645 #if 0                           /* keep Emacsens' auto-indent happy */
646 {
647 #endif
648 #ifdef __cplusplus
649 }
650 #endif
651
652 /* ifndef GNUNET_SOCIAL_SERVICE_H */
653 #endif
654 /* end of gnunet_social_service.h */