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