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 =
48 GNUNET_SCHEDULER_NO_TASK;
53 int (*Action) (const char *arguments, const void *xtra);
59 struct UserList *next;
60 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
64 static struct UserList *users;
69 struct UserList *next;
80 do_help (const char *args, const void *xtra);
84 * Callback used for notification that we have joined the room.
92 FPRINTF (stdout, "%s", _("Joined\n"));
98 * Callback used for notification about incoming messages.
100 * @param cls closure, NULL
101 * @param room in which room was the message received?
102 * @param sender what is the ID of the sender? (maybe NULL)
103 * @param member_info information about the joining member
104 * @param message the message text
105 * @param timestamp time when the member joined
106 * @param options options for the message
107 * @return GNUNET_OK to accept the message now, GNUNET_NO to
108 * accept (but user is away), GNUNET_SYSERR to signal denied delivery
111 receive_cb (void *cls, struct GNUNET_CHAT_Room *room,
112 const GNUNET_HashCode * sender,
113 const struct GNUNET_CONTAINER_MetaData *member_info,
114 const char *message, struct GNUNET_TIME_Absolute timestamp,
115 enum GNUNET_CHAT_MsgOptions options)
117 char *non_unique_nick;
124 nick = GNUNET_strdup (_("anonymous"));
127 if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
128 sender, NULL, NULL, &non_unique_nick, &nick_is_a_dup)
129 || (nick_is_a_dup == GNUNET_YES))
131 GNUNET_free (non_unique_nick);
132 non_unique_nick = GNUNET_strdup (_("anonymous"));
134 nick = GNUNET_PSEUDONYM_name_uniquify (cfg, sender, non_unique_nick, NULL);
135 GNUNET_free (non_unique_nick);
139 switch ((int) options)
141 case GNUNET_CHAT_MSG_OPTION_NONE:
142 case GNUNET_CHAT_MSG_ANONYMOUS:
143 fmt = _("(%s) `%s' said: %s\n");
145 case GNUNET_CHAT_MSG_PRIVATE:
146 fmt = _("(%s) `%s' said to you: %s\n");
148 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ANONYMOUS:
149 fmt = _("(%s) `%s' said to you: %s\n");
151 case GNUNET_CHAT_MSG_AUTHENTICATED:
152 fmt = _("(%s) `%s' said for sure: %s\n");
154 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_AUTHENTICATED:
155 fmt = _("(%s) `%s' said to you for sure: %s\n");
157 case GNUNET_CHAT_MSG_ACKNOWLEDGED:
158 fmt = _("(%s) `%s' was confirmed that you received: %s\n");
160 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
161 fmt = _("(%s) `%s' was confirmed that you and only you received: %s\n");
163 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_ACKNOWLEDGED:
164 fmt = _("(%s) `%s' was confirmed that you received from him or her: %s\n");
166 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
169 ("(%s) `%s' was confirmed that you and only you received from him or her: %s\n");
171 case GNUNET_CHAT_MSG_OFF_THE_RECORD:
172 fmt = _("(%s) `%s' said off the record: %s\n");
175 fmt = _("(%s) <%s> said using an unknown message type: %s\n");
178 time = GNUNET_STRINGS_absolute_time_to_string (timestamp);
179 FPRINTF (stdout, fmt, time, nick, message);
187 * Callback used for message delivery confirmations.
189 * @param cls closure, NULL
190 * @param room in which room was the message received?
191 * @param orig_seq_number sequence number of the original message
192 * @param timestamp when was the message received?
193 * @param receiver who is confirming the receipt?
194 * @return GNUNET_OK to continue, GNUNET_SYSERR to refuse processing further
195 * confirmations from anyone for this message
198 confirmation_cb (void *cls, struct GNUNET_CHAT_Room *room,
199 uint32_t orig_seq_number,
200 struct GNUNET_TIME_Absolute timestamp,
201 const GNUNET_HashCode * receiver)
207 if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
208 receiver, NULL, NULL, &nick, &nick_is_a_dup)
209 || (nick_is_a_dup == GNUNET_YES))
212 nick = GNUNET_strdup (_("anonymous"));
214 unique_nick = GNUNET_PSEUDONYM_name_uniquify (cfg, receiver, nick, NULL);
216 FPRINTF (stdout, _("'%s' acknowledged message #%d\n"), unique_nick, orig_seq_number);
217 GNUNET_free (unique_nick);
223 * Callback used for notification that another room member has joined or left.
225 * @param cls closure (not used)
226 * @param member_info will be non-null if the member is joining, NULL if he is
228 * @param member_id hash of public key of the user (for unique identification)
229 * @param options what types of messages is this member willing to receive?
233 member_list_cb (void *cls, const struct GNUNET_CONTAINER_MetaData *member_info,
234 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *member_id,
235 enum GNUNET_CHAT_MsgOptions options)
237 char *nick, *non_unique_nick;
240 struct UserList *pos;
241 struct UserList *prev;
243 GNUNET_CRYPTO_hash (member_id,
244 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
246 if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
247 &id, NULL, NULL, &non_unique_nick, &nick_is_a_dup)
248 || (nick_is_a_dup == GNUNET_YES))
250 GNUNET_free (non_unique_nick);
251 non_unique_nick = GNUNET_strdup (_("anonymous"));
253 nick = GNUNET_PSEUDONYM_name_uniquify (cfg, &id, non_unique_nick, NULL);
254 GNUNET_free (non_unique_nick);
258 NULL ? _("`%s' entered the room\n") : _("`%s' left the room\n"),
261 if (NULL != member_info)
264 pos = GNUNET_malloc (sizeof (struct UserList));
266 pos->pkey = *member_id;
267 pos->ignored = GNUNET_NO;
275 while ((NULL != pos) &&
277 memcmp (&pos->pkey, member_id,
278 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))))
292 prev->next = pos->next;
301 do_join (const char *arg, const void *xtra)
304 int my_name_is_a_dup;
308 arg++; /* ignore first hash */
309 GNUNET_CHAT_leave_room (room);
311 GNUNET_free (room_name);
312 room_name = GNUNET_strdup (arg);
314 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
315 &receive_cb, NULL, &member_list_cb, NULL,
316 &confirmation_cb, NULL, &me);
319 FPRINTF (stdout, "%s", _("Could not change username\n"));
320 return GNUNET_SYSERR;
322 if ((GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
323 &me, NULL, NULL, &my_name, &my_name_is_a_dup)) ||
324 (my_name_is_a_dup == GNUNET_YES))
326 GNUNET_free (my_name);
327 my_name = GNUNET_strdup (_("anonymous"));
329 /* Don't uniquify our own name - other people will have a different
330 * suffix for our own name anyway.
332 FPRINTF (stdout, _("Joining room `%s' as user `%s'...\n"), room_name,
334 GNUNET_free (my_name);
340 do_nick (const char *msg, const void *xtra)
343 int my_name_is_a_dup;
346 GNUNET_CHAT_leave_room (room);
348 GNUNET_free (nickname);
349 GNUNET_CONTAINER_meta_data_destroy (meta);
350 nickname = GNUNET_strdup (msg);
351 meta = GNUNET_CONTAINER_meta_data_create ();
352 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_TITLE,
353 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
354 nickname, strlen (nickname) + 1);
356 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
357 &receive_cb, NULL, &member_list_cb, NULL,
358 &confirmation_cb, NULL, &me);
361 FPRINTF (stdout, "%s", _("Could not change username\n"));
362 return GNUNET_SYSERR;
364 if ((GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
365 &me, NULL, NULL, &my_name, &my_name_is_a_dup)) ||
366 (my_name_is_a_dup == GNUNET_YES))
368 GNUNET_free (my_name);
369 my_name = GNUNET_strdup (_("anonymous"));
371 FPRINTF (stdout, _("Changed username to `%s'\n"), my_name);
372 GNUNET_free (my_name);
378 do_names (const char *msg, const void *xtra)
383 struct UserList *pos;
386 FPRINTF (stdout, _("Users in room `%s': "), room_name);
390 GNUNET_CRYPTO_hash (&pos->pkey,
391 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
393 if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
394 &pid, NULL, NULL, &name, &name_is_a_dup)
395 || (name_is_a_dup == GNUNET_YES))
398 name = GNUNET_strdup (_("anonymous"));
400 unique_name = GNUNET_PSEUDONYM_name_uniquify (cfg, &pid, name, NULL);
402 FPRINTF (stdout, "`%s' ", unique_name);
403 GNUNET_free (unique_name);
406 FPRINTF (stdout, "%s", "\n");
412 do_send (const char *msg, const void *xtra)
416 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_OPTION_NONE, NULL, &seq);
422 do_send_pm (const char *msg, const void *xtra)
428 struct UserList *pos;
430 if (NULL == strstr (msg, " "))
432 FPRINTF (stderr, "%s", _("Syntax: /msg USERNAME MESSAGE"));
435 user = GNUNET_strdup (msg);
436 strstr (user, " ")[0] = '\0';
437 msg += strlen (user) + 1;
438 if (GNUNET_OK != GNUNET_PSEUDONYM_name_to_id (cfg, user, &uid))
441 _("Unknown user `%s'. Make sure you specify its numeric suffix, if any.\n"),
449 GNUNET_CRYPTO_hash (&pos->pkey,
450 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
452 if (0 == memcmp (&pid, &uid, sizeof (GNUNET_HashCode)))
458 FPRINTF (stderr, _("User `%s' is currently not in the room!\n"), user);
462 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_PRIVATE, &pos->pkey,
470 do_send_sig (const char *msg, const void *xtra)
474 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_AUTHENTICATED, NULL,
481 do_send_ack (const char *msg, const void *xtra)
485 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_ACKNOWLEDGED, NULL,
492 do_send_anonymous (const char *msg, const void *xtra)
496 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_ANONYMOUS, NULL, &seq);
502 do_quit (const char *args, const void *xtra)
504 return GNUNET_SYSERR;
509 do_unknown (const char *msg, const void *xtra)
511 FPRINTF (stderr, _("Unknown command `%s'\n"), msg);
517 * List of supported IRC commands. The order matters!
519 static struct ChatCommand commands[] = {
522 ("Use `/join #roomname' to join a chat room. Joining a room will cause you"
523 " to leave the current room")},
526 ("Use `/nick nickname' to change your nickname. This will cause you to"
527 " leave the current room and immediately rejoin it with the new name.")},
528 {"/msg ", &do_send_pm,
530 ("Use `/msg nickname message' to send a private message to the specified"
532 {"/notice ", &do_send_pm,
533 gettext_noop ("The `/notice' command is an alias for `/msg'")},
534 {"/query ", &do_send_pm,
535 gettext_noop ("The `/query' command is an alias for `/msg'")},
536 {"/sig ", &do_send_sig,
537 gettext_noop ("Use `/sig message' to send a signed public message")},
538 {"/ack ", &do_send_ack,
540 ("Use `/ack message' to require signed acknowledgment of the message")},
541 {"/anonymous ", &do_send_anonymous,
543 ("Use `/anonymous message' to send a public anonymous message")},
544 {"/anon ", &do_send_anonymous,
545 gettext_noop ("The `/anon' command is an alias for `/anonymous'")},
547 gettext_noop ("Use `/quit' to terminate gnunet-chat")},
549 gettext_noop ("The `/leave' command is an alias for `/quit'")},
550 {"/names", &do_names,
552 ("Use `/names' to list all of the current members in the chat room")},
554 gettext_noop ("Use `/help command' to get help for a specific command")},
555 /* Add standard commands:
556 * /whois (print metadata),
557 * /ignore (set flag, check on receive!) */
558 /* the following three commands must be last! */
559 {"/", &do_unknown, NULL},
560 {"", &do_send, NULL},
566 do_help (const char *args, const void *xtra)
571 while ((NULL != args) && (0 != strlen (args)) &&
572 (commands[i].Action != &do_help))
574 if (0 == strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
576 FPRINTF (stdout, "%s\n", gettext (commands[i].helptext));
582 FPRINTF (stdout, "%s", "Available commands:");
583 while (commands[i].Action != &do_help)
585 FPRINTF (stdout, " %s", gettext (commands[i].command));
588 FPRINTF (stdout, "%s", "\n");
589 FPRINTF (stdout, "%s\n", gettext (commands[i].helptext));
595 do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
597 GNUNET_CHAT_leave_room (room);
598 if (handle_cmd_task != GNUNET_SCHEDULER_NO_TASK)
600 GNUNET_SCHEDULER_cancel (handle_cmd_task);
601 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
604 GNUNET_CONTAINER_meta_data_destroy (meta);
605 GNUNET_free (room_name);
606 GNUNET_free (nickname);
611 handle_command (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
613 char message[MAX_MESSAGE_LENGTH + 1];
616 /* read message from command line and handle it */
617 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
618 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
620 if (strlen (message) == 0)
622 if (message[strlen (message) - 1] == '\n')
623 message[strlen (message) - 1] = '\0';
624 if (strlen (message) == 0)
627 while ((NULL != commands[i].command) &&
629 strncasecmp (commands[i].command, message,
630 strlen (commands[i].command))))
633 commands[i].Action (&message[strlen (commands[i].command)], NULL))
638 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_relative_multiply
639 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
640 GNUNET_SCHEDULER_PRIORITY_UI,
641 &handle_command, NULL);
645 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
646 GNUNET_SCHEDULER_shutdown ();
651 * Main function that will be run by the scheduler.
653 * @param cls closure, NULL
654 * @param args remaining command-line arguments
655 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
656 * @param c configuration
659 run (void *cls, char *const *args, const char *cfgfile,
660 const struct GNUNET_CONFIGURATION_Handle *c)
664 int my_name_is_a_dup;
667 /* check arguments */
668 if (NULL == nickname)
670 FPRINTF (stderr, "%s", _("You must specify a nickname\n"));
674 if (NULL == room_name)
675 room_name = GNUNET_strdup ("gnunet");
676 meta = GNUNET_CONTAINER_meta_data_create ();
677 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_TITLE,
678 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
679 nickname, strlen (nickname) + 1);
681 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
682 &receive_cb, NULL, &member_list_cb, NULL,
683 &confirmation_cb, NULL, &me);
686 FPRINTF (stderr, _("Failed to join room `%s'\n"), room_name);
687 GNUNET_free (room_name);
688 GNUNET_free (nickname);
689 GNUNET_CONTAINER_meta_data_destroy (meta);
693 if ((GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
694 &me, NULL, NULL, &my_name, &my_name_is_a_dup)) ||
695 (my_name_is_a_dup == GNUNET_YES))
697 GNUNET_free (my_name);
698 my_name = GNUNET_strdup (_("anonymous"));
700 FPRINTF (stdout, _("Joining room `%s' as user `%s'...\n"), room_name,
702 GNUNET_free (my_name);
704 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
705 &handle_command, NULL);
706 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
712 * The main function to chat via GNUnet.
714 * @param argc number of arguments from the command line
715 * @param argv command line arguments
716 * @return 0 ok, 1 on error
719 main (int argc, char *const *argv)
723 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
724 {'n', "nick", "NAME",
725 gettext_noop ("set the nickname to use (required)"),
726 1, &GNUNET_GETOPT_set_string, &nickname},
727 {'r', "room", "NAME",
728 gettext_noop ("set the chat room to join"),
729 1, &GNUNET_GETOPT_set_string, &room_name},
730 GNUNET_GETOPT_OPTION_END
734 flags = fcntl (0, F_GETFL, 0);
736 fcntl (0, F_SETFL, flags);
739 GNUNET_PROGRAM_run (argc, argv, "gnunet-chat",
740 gettext_noop ("Join a chat on GNUnet."), options,
741 &run, NULL)) ? ret : 1;
744 /* end of gnunet-chat.c */