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