2 This file is part of GNUnet.
3 (C) 2007, 2008, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file chat/gnunet-chat.c
23 * @brief Minimal chat command line tool
24 * @author Christian Grothoff
25 * @author Nathan Evans
26 * @author Vitaly Minko
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_chat_service.h"
37 static const struct GNUNET_CONFIGURATION_Handle *cfg;
39 static char *nickname;
41 static char *room_name;
43 static struct GNUNET_CONTAINER_MetaData *meta;
45 static struct GNUNET_CHAT_Room *room;
47 static GNUNET_SCHEDULER_TaskIdentifier handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
52 int (*Action) (const char *arguments, const void *xtra);
58 struct UserList *next;
59 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
63 static struct UserList *users;
68 struct UserList *next;
77 static int do_help (const char *args, const void *xtra);
81 * Callback used for notification that we have joined the room.
89 fprintf (stdout, _("Joined\n"));
95 * Callback used for notification about incoming messages.
97 * @param cls closure, NULL
98 * @param room in which room was the message received?
99 * @param sender what is the ID of the sender? (maybe NULL)
100 * @param member_info information about the joining member
101 * @param message the message text
102 * @param timestamp time when the member joined
103 * @param options options for the message
104 * @return GNUNET_OK to accept the message now, GNUNET_NO to
105 * accept (but user is away), GNUNET_SYSERR to signal denied delivery
108 receive_cb (void *cls,
109 struct GNUNET_CHAT_Room *room,
110 const GNUNET_HashCode *sender,
111 const struct GNUNET_CONTAINER_MetaData *member_info,
113 struct GNUNET_TIME_Absolute timestamp,
114 enum GNUNET_CHAT_MsgOptions options)
121 nick = GNUNET_PSEUDONYM_id_to_name (cfg, sender);
123 nick = GNUNET_strdup (_("anonymous"));
125 switch ( (int) options)
127 case GNUNET_CHAT_MSG_OPTION_NONE:
128 case GNUNET_CHAT_MSG_ANONYMOUS:
129 fmt = _("(%s) `%s' said: %s\n");
131 case GNUNET_CHAT_MSG_PRIVATE:
132 fmt = _("(%s) `%s' said to you: %s\n");
134 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ANONYMOUS:
135 fmt = _("(%s) `%s' said to you: %s\n");
137 case GNUNET_CHAT_MSG_AUTHENTICATED:
138 fmt = _("(%s) `%s' said for sure: %s\n");
140 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_AUTHENTICATED:
141 fmt = _("(%s) `%s' said to you for sure: %s\n");
143 case GNUNET_CHAT_MSG_ACKNOWLEDGED:
144 fmt = _("(%s) `%s' was confirmed that you received: %s\n");
146 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
147 fmt = _("(%s) `%s' was confirmed that you and only you received: %s\n");
149 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_ACKNOWLEDGED:
150 fmt = _("(%s) `%s' was confirmed that you received from him or her: %s\n");
152 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
153 fmt = _("(%s) `%s' was confirmed that you and only you received from him or her: %s\n");
155 case GNUNET_CHAT_MSG_OFF_THE_RECORD:
156 fmt = _("(%s) `%s' said off the record: %s\n");
159 fmt = _("(%s) <%s> said using an unknown message type: %s\n");
162 time = GNUNET_STRINGS_absolute_time_to_string (timestamp);
163 fprintf (stdout, fmt, time, nick, message);
171 * Callback used for message delivery confirmations.
173 * @param cls closure, NULL
174 * @param room in which room was the message received?
175 * @param orig_seq_number sequence number of the original message
176 * @param timestamp when was the message received?
177 * @param receiver who is confirming the receipt?
178 * @return GNUNET_OK to continue, GNUNET_SYSERR to refuse processing further
179 * confirmations from anyone for this message
182 confirmation_cb (void *cls,
183 struct GNUNET_CHAT_Room *room,
184 uint32_t orig_seq_number,
185 struct GNUNET_TIME_Absolute timestamp,
186 const GNUNET_HashCode *receiver)
190 nick = GNUNET_PSEUDONYM_id_to_name (cfg, receiver);
191 fprintf (stdout, _("'%s' acknowledged message #%d\n"), nick, orig_seq_number);
197 * Callback used for notification that another room member has joined or left.
199 * @param cls closure (not used)
200 * @param member_info will be non-null if the member is joining, NULL if he is
202 * @param member_id hash of public key of the user (for unique identification)
203 * @param options what types of messages is this member willing to receive?
207 member_list_cb (void *cls,
208 const struct GNUNET_CONTAINER_MetaData *member_info,
209 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *member_id,
210 enum GNUNET_CHAT_MsgOptions options)
214 struct UserList *pos;
215 struct UserList *prev;
217 GNUNET_CRYPTO_hash (member_id,
218 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
220 nick = GNUNET_PSEUDONYM_id_to_name (cfg, &id);
221 fprintf (stdout, member_info != NULL
222 ? _("`%s' entered the room\n") : _("`%s' left the room\n"), nick);
224 if (NULL != member_info)
227 pos = GNUNET_malloc (sizeof (struct UserList));
229 pos->pkey = *member_id;
230 pos->ignored = GNUNET_NO;
238 while ((NULL != pos) &&
239 (0 != memcmp (&pos->pkey,
241 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))))
255 prev->next = pos->next;
264 do_join (const char *arg, const void *xtra)
270 arg++; /* ignore first hash */
271 GNUNET_CHAT_leave_room (room);
273 GNUNET_free (room_name);
274 room_name = GNUNET_strdup (arg);
275 room = GNUNET_CHAT_join_room (cfg,
282 &member_list_cb, NULL,
283 &confirmation_cb, NULL, &me);
286 fprintf (stdout, _("Could not change username\n"));
287 return GNUNET_SYSERR;
289 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
290 fprintf (stdout, _("Joining room `%s' as user `%s'...\n"), room_name, my_name);
291 GNUNET_free (my_name);
297 do_nick (const char *msg, const void *xtra)
302 GNUNET_CHAT_leave_room (room);
304 GNUNET_free (nickname);
305 GNUNET_CONTAINER_meta_data_destroy (meta);
306 nickname = GNUNET_strdup (msg);
307 meta = GNUNET_CONTAINER_meta_data_create ();
308 GNUNET_CONTAINER_meta_data_insert (meta,
310 EXTRACTOR_METATYPE_TITLE,
311 EXTRACTOR_METAFORMAT_UTF8,
315 room = GNUNET_CHAT_join_room (cfg,
322 &member_list_cb, NULL,
323 &confirmation_cb, NULL, &me);
326 fprintf (stdout, _("Could not change username\n"));
327 return GNUNET_SYSERR;
329 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
330 fprintf (stdout, _("Changed username to `%s'\n"), my_name);
331 GNUNET_free (my_name);
337 do_names (const char *msg, const void *xtra)
340 struct UserList *pos;
343 fprintf (stdout, _("Users in room `%s': "), room_name);
347 GNUNET_CRYPTO_hash (&pos->pkey,
348 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
350 name = GNUNET_PSEUDONYM_id_to_name (cfg, &pid);
351 fprintf (stdout, "`%s' ", name);
355 fprintf (stdout, "\n");
361 do_send (const char *msg, const void *xtra)
364 GNUNET_CHAT_send_message (room,
366 GNUNET_CHAT_MSG_OPTION_NONE,
373 do_send_pm (const char *msg, const void *xtra)
379 struct UserList *pos;
381 if (NULL == strstr (msg, " "))
383 fprintf (stderr, _("Syntax: /msg USERNAME MESSAGE"));
386 user = GNUNET_strdup (msg);
387 strstr (user, " ")[0] = '\0';
388 msg += strlen (user) + 1;
389 if (GNUNET_OK != GNUNET_PSEUDONYM_name_to_id (cfg, user, &uid))
391 fprintf (stderr, _("Unknown user `%s'\n"), user);
398 GNUNET_CRYPTO_hash (&pos->pkey,
399 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
401 if (0 == memcmp (&pid, &uid, sizeof (GNUNET_HashCode)))
407 fprintf (stderr, _("User `%s' is currently not in the room!\n"), user);
411 GNUNET_CHAT_send_message (room,
413 GNUNET_CHAT_MSG_PRIVATE,
422 do_send_sig (const char *msg, const void *xtra)
425 GNUNET_CHAT_send_message (room,
427 GNUNET_CHAT_MSG_AUTHENTICATED,
434 do_send_ack (const char *msg, const void *xtra)
437 GNUNET_CHAT_send_message (room,
439 GNUNET_CHAT_MSG_ACKNOWLEDGED,
446 do_send_anonymous (const char *msg, const void *xtra)
449 GNUNET_CHAT_send_message (room,
451 GNUNET_CHAT_MSG_ANONYMOUS,
458 do_quit (const char *args, const void *xtra)
460 return GNUNET_SYSERR;
465 do_unknown (const char *msg, const void *xtra)
467 fprintf (stderr, _("Unknown command `%s'\n"), msg);
473 * List of supported IRC commands. The order matters!
475 static struct ChatCommand commands[] = {
478 ("Use `/join #roomname' to join a chat room. Joining a room will cause you"
479 " to leave the current room")},
482 ("Use `/nick nickname' to change your nickname. This will cause you to"
483 " leave the current room and immediately rejoin it with the new name.")},
484 {"/msg ", &do_send_pm,
486 ("Use `/msg nickname message' to send a private message to the specified"
488 {"/notice ", &do_send_pm,
489 gettext_noop ("The `/notice' command is an alias for `/msg'")},
490 {"/query ", &do_send_pm,
491 gettext_noop ("The `/query' command is an alias for `/msg'")},
492 {"/sig ", &do_send_sig,
493 gettext_noop ("Use `/sig message' to send a signed public message")},
494 {"/ack ", &do_send_ack,
496 ("Use `/ack message' to require signed acknowledgment of the message")},
497 {"/anonymous ", &do_send_anonymous,
499 ("Use `/anonymous message' to send a public anonymous message")},
500 {"/anon ", &do_send_anonymous,
501 gettext_noop ("The `/anon' command is an alias for `/anonymous'")},
503 gettext_noop ("Use `/quit' to terminate gnunet-chat")},
505 gettext_noop ("The `/leave' command is an alias for `/quit'")},
506 {"/names", &do_names,
508 ("Use `/names' to list all of the current members in the chat room")},
510 gettext_noop ("Use `/help command' to get help for a specific command")},
511 /* Add standard commands:
512 /whois (print metadata),
513 /ignore (set flag, check on receive!) */
514 /* the following three commands must be last! */
515 {"/", &do_unknown, NULL},
516 {"", &do_send, NULL},
522 do_help (const char *args, const void *xtra)
526 while ((NULL != args) &&
527 (0 != strlen (args)) && (commands[i].Action != &do_help))
530 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
532 fprintf (stdout, "%s\n", gettext (commands[i].helptext));
538 fprintf (stdout, "Available commands:");
539 while (commands[i].Action != &do_help)
541 fprintf (stdout, " %s", gettext (commands[i].command));
544 fprintf (stdout, "\n");
545 fprintf (stdout, "%s\n", gettext (commands[i].helptext));
551 do_stop_task (void *cls,
552 const struct GNUNET_SCHEDULER_TaskContext *tc)
554 GNUNET_CHAT_leave_room (room);
555 if (handle_cmd_task != GNUNET_SCHEDULER_NO_TASK)
557 GNUNET_SCHEDULER_cancel (handle_cmd_task);
558 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
561 GNUNET_CONTAINER_meta_data_destroy (meta);
562 GNUNET_free (room_name);
563 GNUNET_free (nickname);
568 handle_command (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
570 char message[MAX_MESSAGE_LENGTH + 1];
573 /* read message from command line and handle it */
574 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
575 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
577 if (strlen (message) == 0)
579 if (message[strlen (message) - 1] == '\n')
580 message[strlen (message) - 1] = '\0';
581 if (strlen (message) == 0)
584 while ((NULL != commands[i].command) &&
585 (0 != strncasecmp (commands[i].command,
586 message, strlen (commands[i].command))))
589 commands[i].Action (&message[strlen (commands[i].command)], NULL))
594 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
601 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
602 GNUNET_SCHEDULER_shutdown ();
607 * Main function that will be run by the scheduler.
609 * @param cls closure, NULL
610 * @param args remaining command-line arguments
611 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
612 * @param c configuration
618 const struct GNUNET_CONFIGURATION_Handle *c)
624 /* check arguments */
625 if (NULL == nickname)
627 fprintf (stderr, _("You must specify a nickname\n"));
631 if (NULL == room_name)
632 room_name = GNUNET_strdup ("gnunet");
633 meta = GNUNET_CONTAINER_meta_data_create ();
634 GNUNET_CONTAINER_meta_data_insert (meta,
636 EXTRACTOR_METATYPE_TITLE,
637 EXTRACTOR_METAFORMAT_UTF8,
641 room = GNUNET_CHAT_join_room (cfg,
648 &member_list_cb, NULL,
649 &confirmation_cb, NULL, &me);
652 fprintf (stderr, _("Failed to join room `%s'\n"), room_name);
653 GNUNET_free (room_name);
654 GNUNET_free (nickname);
655 GNUNET_CONTAINER_meta_data_destroy (meta);
659 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
660 fprintf (stdout, _("Joining room `%s' as user `%s'...\n"), room_name, my_name);
661 GNUNET_free (my_name);
663 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
666 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
673 * The main function to chat via GNUnet.
675 * @param argc number of arguments from the command line
676 * @param argv command line arguments
677 * @return 0 ok, 1 on error
680 main (int argc, char *const *argv)
683 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
684 {'n', "nick", "NAME",
685 gettext_noop ("set the nickname to use (required)"),
686 1, &GNUNET_GETOPT_set_string, &nickname},
687 {'r', "room", "NAME",
688 gettext_noop ("set the chat room to join"),
689 1, &GNUNET_GETOPT_set_string, &room_name},
690 GNUNET_GETOPT_OPTION_END
694 flags = fcntl (0, F_GETFL, 0);
696 fcntl (0, F_SETFL, flags);
699 GNUNET_PROGRAM_run (argc,
702 gettext_noop ("Join a chat on GNUnet."),
703 options, &run, NULL)) ? ret : 1;
706 /* end of gnunet-chat.c */