d4d9c437c06e80cc9c4968bc6f72b1e604f8bae6
[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.
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 /* FIXME: No slicer_remove for now, is it needed? */
134
135 /** 
136  * Destroy a given try-and-slice instance.
137  *
138  * @param slicer slicer to destroy
139  */
140 void
141 GNUNET_SOCIAL_slicer_destroy (struct GNUNET_SOCIAL_Slicer *slicer);
142
143
144 /** 
145  * Create an ego.
146  *
147  * Create an ego using the private key from the given file.  If the file does
148  * not exist, a fresh key is created.
149  *
150  * @param keyfile Name of the file with the private key for the ego,
151  *                NULL for ephemeral egos.
152  * @return Handle to the ego, NULL on error.
153  */
154 struct GNUNET_SOCIAL_Ego *
155 GNUNET_SOCIAL_ego_create (const char *keyfile);
156
157
158 /** 
159  * Destroy a handle to an ego.
160  *
161  * @param ego Ego to destroy.
162  */
163 void
164 GNUNET_SOCIAL_ego_destroy (struct GNUNET_SOCIAL_Ego *ego);
165
166
167 /** 
168  * Function called asking for nym to be admitted to the place.
169  *
170  * Should call either GNUNET_SOCIAL_home_admit() or
171  * GNUNET_SOCIAL_home_reject_entry() (possibly asynchronously).  If this owner
172  * cannot decide, it is fine to call neither function, in which case hopefully
173  * some other owner of the home exists that will make the decision. The @a nym
174  * reference remains valid until the #GNUNET_SOCIAL_FarewellCallback is invoked
175  * for it.
176  *
177  * @param cls Closure.
178  * @param nym Handle for the user who wants to join.
179  * @param header_length Number of modifiers in header.
180  * @param header Modifiers present in the message.
181  * @param msg_size Number of bytes in @a msg.
182  * @param msg Payload given on enter (e.g. a password).
183  */
184 typedef void (*GNUNET_SOCIAL_AnswerDoorCallback)(void *cls,
185                                                  struct GNUNET_SOCIAL_Nym *nym,
186                                                  size_t header_length,
187                                                  GNUNET_PSYC_Modifier *header,
188                                                  size_t msg_size,
189                                                  const void *msg);
190
191
192 /** 
193  * Function called when a @a nym leaves the place.
194  * 
195  * This is also called if the @a nym was never given permission to enter
196  * (i.e. the @a nym stopped asking to get in).
197  *
198  * @param cls Closure.
199  * @param nym Handle for the user who left.
200  * @param header_length Number of modifiers in header.
201  * @param header Modifiers present in the message.
202  */
203 typedef void (*GNUNET_SOCIAL_FarewellCallback)(void *cls,
204                                                struct GNUNET_SOCIAL_Nym *nym,
205                                                size_t header_length,
206                                                GNUNET_PSYC_Modifier *header);
207
208
209 /** 
210  * Enter a home where guests (nyms) can be hosted.
211  *
212  * A home is created upon first entering, and exists until
213  * GNUNET_SOCIAL_home_destroy() is called. It can also be left temporarily using
214  * GNUNET_SOCIAL_home_leave().
215  *
216  * @param cfg Configuration to contact the social service.
217  * @param home_keyfile File with the private key for the home,
218  *              created if the file does not exist;
219  *              pass NULL for ephemeral homes.
220  * @param join_policy What is our policy for allowing people in?
221  * @param ego Owner of the home (host).
222  * @param slicer Slicer to handle guests talking.
223  * @param listener_cb Function to handle new nyms that want to join.
224  * @param farewell_cb Function to handle departing nyms.
225  * @param cls Closure for @a listener_cb and @a farewell_cb.
226  * @return Handle for a new home.
227  */
228 struct GNUNET_SOCIAL_Home *
229 GNUNET_SOCIAL_home_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
230                           const char *home_keyfile,
231                           enum GNUNET_MULTICAST_JoinPolicy join_policy,
232                           struct GNUNET_SOCIAL_Ego *ego,
233                           struct GNUNET_SOCIAL_Slicer *slicer,
234                           GNUNET_SOCIAL_AnswerDoorCallback listener_cb,
235                           GNUNET_SOCIAL_FarewellCallback farewell_cb,
236                           void *cls);
237
238
239 /** 
240  * Admit @a nym to the @a home.
241  *
242  * The @a nym reference will remain valid until either the home is destroyed or
243  * @a nym leaves.
244  *
245  * @param home Home to allow @a nym to join.
246  * @param nym Handle for the entity that wants to join.
247  */
248 void
249 GNUNET_SOCIAL_home_admit (struct GNUNET_SOCIAL_Home *home,
250                           struct GNUNET_SOCIAL_Nym *nym);
251
252
253 /** 
254  * Throw @a nym out of the @a home.
255  *
256  * The @a nym reference will remain valid until the
257  * #GNUNET_SOCIAL_FarewellCallback is invoked,
258  * which should be very soon after this call.
259  *
260  * @param home Home to eject @a nym from.
261  * @param nym Handle for the entity to be ejected.
262  */
263 void
264 GNUNET_SOCIAL_home_eject (struct GNUNET_SOCIAL_Home *home,
265                           struct GNUNET_SOCIAL_Nym *nym);
266
267
268 /** 
269  * Refuse @a nym entry into the @a home.
270  *
271  * @param home Home to disallow @a nym to join.
272  * @param nym Handle for the entity that wanted to join.
273  * @param method Method name to invoke on caller.
274  * @param message_size Number of bytes in @a message for method.
275  * @param message Rejection message to send back.
276  *
277  * FIXME: allow setting variables as well for the message
278  */
279 void
280 GNUNET_SOCIAL_home_reject_entry (struct GNUNET_SOCIAL_Home *home,
281                                  struct GNUNET_SOCIAL_Nym *nym,
282                                  const char *method,
283                                  size_t message_size,
284                                  const void *message);
285
286
287 /** 
288  * Get the identity of a user.
289  *
290  * Suitable, for example, to be used with GNUNET_NAMESTORE_zone_to_name().
291  *
292  * @param nym Pseudonym to map to a cryptographic identifier.
293  * @param[out] identity Set to the identity of the nym (short hash of the public key).
294  */
295 void
296 GNUNET_SOCIAL_nym_get_identity (struct GNUNET_SOCIAL_Nym *nym,
297                                 struct GNUNET_CRYPTO_ShortHashCode *identity);
298
299
300 /** 
301  * Obtain the (cryptographic, binary) address for the home.
302  * 
303  * @param home Home to get the (public) address from.
304  * @param[out] crypto_address Address suitable for storing in GADS, i.e. in
305  *        'HEX.place' or within the respective GADS record type ("PLACE")
306  */
307 void
308 GNUNET_SOCIAL_home_get_address (struct GNUNET_SOCIAL_Home *home,
309                                 struct GNUNET_HashCode *crypto_address);
310
311
312
313 /** 
314  * Advertise @a home under @a name in the GADS zone of the @e ego.
315  *
316  * @param home The home to advertise.
317  * @param name The name to put in the zone.
318  * @param expiration_time Expiration time of the record, use 0 to remove the record.
319  */
320 void
321 GNUNET_SOCIAL_home_advertise (struct GNUNET_SOCIAL_Home *home,
322                               const char *name,
323                               GNUNET_TIME_Relative expiration_time);
324
325
326
327 /** 
328  * (Re)decorate the home by changing objects in it.
329  *
330  * If the operation is #GNUNET_PSYC_OP_SET then the decoration only
331  * applies to the next announcement by the home owner.
332  *
333  * @param home The home to decorate.
334  * @param operation Operation to perform on the object.
335  * @param object_name Name of the object to modify.
336  * @param object_value_size Size of the given @a object_value.
337  * @param object_value Value to use for the modification.
338  */
339 void
340 GNUNET_SOCIAL_home_decorate (struct GNUNET_SOCIAL_Home *home,
341                              enum GNUNET_PSYC_Operator operation,
342                              const char *object_name,
343                              size_t object_value_size,
344                              const void *object_value);
345
346
347 /** 
348  * Handle for an announcement request.
349  */
350 struct GNUNET_SOCIAL_Announcement;
351
352
353 /** 
354  * Send a message to all nyms that are present in the home.
355  *
356  * This function is restricted to the home owner.  Nyms can only send requests
357  * to the home owner who can decide to relay it to other guests.
358  *
359  * @param home Home to address the announcement to.
360  * @param method_name Method to use for the announcement.
361  * @param notify Function to call to get the payload of the announcement.
362  * @param notify_cls Closure for @a notify.
363  * @return NULL on error (announcement already in progress?).
364  */
365 struct GNUNET_SOCIAL_Announcement *
366 GNUNET_SOCIAL_home_announce (struct GNUNET_SOCIAL_Home *home,
367                              const char *method_name,
368                              GNUNET_PSYC_OriginReadyNotify notify,
369                              void *notify_cls);
370
371
372 /** 
373  * Cancel announcement.
374  *
375  * @param a The announcement to cancel.
376  */
377 void
378 GNUNET_SOCIAL_home_announce_cancel (struct GNUNET_SOCIAL_Announcement *a);
379
380
381 /** 
382  * Convert our home to a place so we can access it via the place API.
383  *
384  * @param home Handle for the home.
385  * @return Place handle for the same home, valid as long as @a home is valid;
386  *         do NOT try to GNUNET_SOCIAL_place_leave() this place, it's your home!
387  */
388 struct GNUNET_SOCIAL_Place *
389 GNUNET_SOCIAL_home_to_place (struct GNUNET_SOCIAL_Home *home);
390
391
392 /** 
393  * Leave a home temporarily, visitors can stay.
394  *
395  * After leaving, handling of incoming messages are left to other clients of the
396  * social service, and stops after the last client exits.
397  *
398  * @param home Home to leave temporarily (handle becomes invalid).
399  */
400 void
401 GNUNET_SOCIAL_home_away (struct GNUNET_SOCIAL_Home *home);
402
403
404 /** 
405  * Destroy a home, all guests will be ejected.
406  *
407  * @param home Home to destroy.
408  */
409 void
410 GNUNET_SOCIAL_home_destroy (struct GNUNET_SOCIAL_Home *home);
411
412 /** 
413  * Request entry to a place (home hosted by someone else).
414  *
415  * @param cfg Configuration to contact the social service.
416  * @param ego Owner of the home (host).
417  * @param address Address of the place to enter (GADS name, i.e. 'room.friend.gads'),
418  *        if the name has the form 'HEX.place', GADS is not
419  *        used and HEX is assumed to be the hash of the public
420  *        key already; 'HEX.zkey' however would refer to
421  *        the 'PLACE' record in the GADS zone with the public key
422  *        'HEX'.
423  * @param msg_size Number of bytes in @a msg.
424  * @param msg Message to give to the enter callback.
425  * @param slicer Slicer to use for processing incoming requests from guests.
426  * @return NULL on errors, otherwise handle to the place.
427  */
428 struct GNUNET_SOCIAL_Place *
429 GNUNET_SOCIAL_place_enter (const struct GNUNET_CONFIGURATION_Handle *cfg,
430                            struct GNUNET_SOCIAL_Ego *ego,
431                            char *address,
432                            size_t msg_size,
433                            const void *msg,
434                            struct GNUNET_SOCIAL_Slicer *slicer);
435
436 /** 
437  * Request entry to a place (home hosted by someone else).
438  *
439  * @param cfg Configuration to contact the social service.
440  * @param ego Owner of the home (host).
441  * @param crypto_address Public key of the place to enter.
442  * @param peer Peer to send request to.
443  * @param slicer Slicer to use for processing incoming requests from guests.
444  * @param msg_size Number of bytes in @a msg.
445  * @param msg Message to give to the enter callback.
446  * @return NULL on errors, otherwise handle to the place.
447  */
448 struct GNUNET_SOCIAL_Place *
449 GNUNET_SOCIAL_place_enter2 (const struct GNUNET_CONFIGURATION_Handle *cfg,
450                            struct GNUNET_SOCIAL_Ego *ego,
451                            struct GNUNET_HashCode *crypto_address,
452                            struct GNUNET_PeerIdentity *peer,
453                            struct GNUNET_SOCIAL_Slicer *slicer,
454                            size_t msg_size,
455                            const void *msg);
456
457
458 struct GNUNET_SOCIAL_WatchHandle;
459
460 /** 
461  * Watch a place for changed objects.
462  *
463  * @param place Place to watch.
464  * @param object_filter Object prefix to match.
465  * @param state_cb Function to call when an object/state changes.
466  * @param state_cb_cls Closure for callback.
467  * 
468  * @return Handle that can be used to cancel watching.
469  */
470 struct GNUNET_SOCIAL_WatchHandle *
471 GNUNET_SOCIAL_place_watch (struct GNUNET_SOCIAL_Place *place,
472                            const char *object_filter,
473                            GNUNET_PSYC_StateCallback state_cb,
474                            void *state_cb_cls);
475
476
477 /** 
478  * Cancel watching a place for changed objects.
479  *
480  * @param wh Watch handle to cancel.
481  */
482 void
483 GNUNET_SOCIAL_place_watch_cancel (struct GNUNET_SOCIAL_WatchHandle *wh);
484
485
486 struct GNUNET_SOCIAL_LookHandle;
487
488 /** 
489  * Look at all objects in the place.
490  *
491  * @param place The place to look its objects at.
492  * @param state_cb Function to call for each object found.
493  * @param state_cb_cls Closure for callback function.
494  * 
495  * @return Handle that can be used to stop looking at objects.
496  */
497 struct GNUNET_SOCIAL_LookHandle *
498 GNUNET_SOCIAL_place_look (struct GNUNET_SOCIAL_Place *place,
499                           GNUNET_PSYC_StateCallback state_cb,
500                           void *state_cb_cls);
501
502
503 /** 
504  * Look at matching objects in the place.
505  *
506  * @param place The place to look its objects at.
507  * @param object_filter Only look at objects with names beginning with this filter value. 
508  * @param state_cb Function to call for each object found.
509  * @param state_cb_cls Closure for callback function.
510  * 
511  * @return Handle that can be used to stop looking at objects.
512  */
513 struct GNUNET_SOCIAL_LookHandle *
514 GNUNET_SOCIAL_place_look_for (struct GNUNET_SOCIAL_Place *place,
515                               const char *object_filter,
516                               GNUNET_PSYC_StateCallback state_cb,
517                               void *state_cb_cls);
518
519
520 /** 
521  * Stop looking at objects.
522  *
523  * @param lh Look handle to stop.
524  */
525 void
526 GNUNET_SOCIAL_place_look_cancel (struct GNUNET_SOCIAL_LookHandle *lh);
527
528
529
530 /** 
531  * Look at a particular object in the place.
532  *
533  * @param place The place to look the object at.
534  * @param object_name Full name of the object.
535  * @param value_size Set to the size of the returned value.
536  * @return NULL if there is no such object at this place.
537  */
538 const void *
539 GNUNET_SOCIAL_place_look_at (struct GNUNET_SOCIAL_Place *place,
540                              const char *object_name,
541                              size_t *value_size);
542
543
544 /** 
545  * Frame the (next) talk by setting some variables for it.
546  *
547  * FIXME: use a TalkFrame struct instead that can be passed to
548  * place_talk, nym_talk and home_reject_entry.
549  *
550  * @param place Place to talk in.
551  * @param variable_name Name of variable to set.
552  * @param value_size Number of bytes in @a value.
553  * @param value Value of variable.
554  */
555 void
556 GNUNET_SOCIAL_place_frame_talk (struct GNUNET_SOCIAL_Place *place,
557                                 const char *variable_name,
558                                 size_t value_size,
559                                 const void *value);
560
561
562 /** 
563  * A talk request.
564  */
565 struct GNUNET_SOCIAL_TalkRequest;
566
567 /** 
568  * Talk to the host of the place.
569  *
570  * @param place Place where we want to talk to the host.
571  * @param method_name Method to invoke on the host.
572  * @param cb Function to use to get the payload for the method.
573  * @param cb_cls Closure for @a cb.
574  * @return NULL if we are already trying to talk to the host,
575  *         otherwise handle to cancel the request.
576  */
577 struct GNUNET_SOCIAL_TalkRequest *
578 GNUNET_SOCIAL_place_talk (struct GNUNET_SOCIAL_Place *place,
579                           const char *method_name,
580                           GNUNET_PSYC_OriginReadyNotify cb,
581                           void *cb_cls);
582
583
584 /** 
585  * Cancel talking to the host of the place.
586  *
587  * @param tr Talk request to cancel.
588  */
589 void
590 GNUNET_SOCIAL_place_talk_cancel (struct GNUNET_SOCIAL_TalkRequest *tr);
591
592
593 /** 
594  * A history lesson.
595  */
596 struct GNUNET_SOCIAL_HistoryLesson;
597
598 /** 
599  * Learn about the history of a place.
600  *
601  * Sends messages through the given slicer function where
602  * start_message_id <= message_id <= end_message_id.
603  *
604  * To get the latest message, use 0 for both the start and end message ID.
605  * 
606  * @param place Place we want to learn more about.
607  * @param start_message_id First historic message we are interested in.
608  * @param end_message_id Last historic message we are interested in (inclusive).
609  * @param slicer Slicer to use to process history.
610  * @return Handle to abort history lesson, never NULL (multiple lessons
611  *        at the same time are allowed).
612  */
613 struct GNUNET_SOCIAL_HistoryLesson *
614 GNUNET_SOCIAL_place_get_history (struct GNUNET_SOCIAL_Place *place,
615                                  uint64_t start_message_id,
616                                  uint64_t end_message_id,
617                                  struct GNUNET_SOCIAL_Slicer *slicer);
618
619
620 /** 
621  * Stop processing messages from the history lesson.
622  *
623  * Must also be called explicitly after all of the requested messages have been
624  * received.
625  *
626  * @param hist History lesson to cancel.
627  */
628 void
629 GNUNET_SOCIAL_place_get_history_cancel (struct GNUNET_SOCIAL_HistoryLesson *hist);
630
631           
632 /** 
633  * Leave a place permanently.
634  *
635  * Notifies the owner of the place about leaving, and destroys the place handle.
636  * 
637  * @param place Place to leave permanently.
638  */
639 void
640 GNUNET_SOCIAL_place_leave (struct GNUNET_SOCIAL_Place *place);
641
642
643 /** 
644  * Leave a place temporarily.
645  *
646  * Stop following the conversation for the @a place and destroy the @a place
647  * handle.  Only affects the application calling this function, other clients of
648  * the service continue receiving the messages.
649  *
650  * @param place Place to leave temporarily.
651  */
652 void
653 GNUNET_SOCIAL_place_away (struct GNUNET_SOCIAL_Place *place);
654
655
656 #if 0                           /* keep Emacsens' auto-indent happy */
657 {
658 #endif
659 #ifdef __cplusplus
660 }
661 #endif
662
663 /* ifndef GNUNET_SOCIAL_SERVICE_H */
664 #endif
665 /* end of gnunet_social_service.h */