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;
79 static int do_help (const char *args, const void *xtra);
83 * Callback used for notification that we have joined the room.
91 fprintf (stdout, _("Joined\n"));
97 * Callback used for notification about incoming messages.
99 * @param cls closure, NULL
100 * @param room in which room was the message received?
101 * @param sender what is the ID of the sender? (maybe NULL)
102 * @param member_info information about the joining member
103 * @param message the message text
104 * @param timestamp time when the member joined
105 * @param options options for the message
106 * @return GNUNET_OK to accept the message now, GNUNET_NO to
107 * accept (but user is away), GNUNET_SYSERR to signal denied delivery
110 receive_cb (void *cls, struct GNUNET_CHAT_Room *room,
111 const GNUNET_HashCode * sender,
112 const struct GNUNET_CONTAINER_MetaData *member_info,
113 const char *message, 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:
155 ("(%s) `%s' was confirmed that you and only you received from him or her: %s\n");
157 case GNUNET_CHAT_MSG_OFF_THE_RECORD:
158 fmt = _("(%s) `%s' said off the record: %s\n");
161 fmt = _("(%s) <%s> said using an unknown message type: %s\n");
164 time = GNUNET_STRINGS_absolute_time_to_string (timestamp);
165 fprintf (stdout, fmt, time, nick, message);
173 * Callback used for message delivery confirmations.
175 * @param cls closure, NULL
176 * @param room in which room was the message received?
177 * @param orig_seq_number sequence number of the original message
178 * @param timestamp when was the message received?
179 * @param receiver who is confirming the receipt?
180 * @return GNUNET_OK to continue, GNUNET_SYSERR to refuse processing further
181 * confirmations from anyone for this message
184 confirmation_cb (void *cls, struct GNUNET_CHAT_Room *room,
185 uint32_t orig_seq_number,
186 struct GNUNET_TIME_Absolute timestamp,
187 const GNUNET_HashCode * receiver)
191 nick = GNUNET_PSEUDONYM_id_to_name (cfg, receiver);
192 fprintf (stdout, _("'%s' acknowledged message #%d\n"), nick, orig_seq_number);
198 * Callback used for notification that another room member has joined or left.
200 * @param cls closure (not used)
201 * @param member_info will be non-null if the member is joining, NULL if he is
203 * @param member_id hash of public key of the user (for unique identification)
204 * @param options what types of messages is this member willing to receive?
208 member_list_cb (void *cls, 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);
223 NULL ? _("`%s' entered the room\n") : _("`%s' left the room\n"),
226 if (NULL != member_info)
229 pos = GNUNET_malloc (sizeof (struct UserList));
231 pos->pkey = *member_id;
232 pos->ignored = GNUNET_NO;
240 while ((NULL != pos) &&
242 memcmp (&pos->pkey, member_id,
243 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))))
257 prev->next = pos->next;
266 do_join (const char *arg, const void *xtra)
272 arg++; /* ignore first hash */
273 GNUNET_CHAT_leave_room (room);
275 GNUNET_free (room_name);
276 room_name = GNUNET_strdup (arg);
278 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
279 &receive_cb, NULL, &member_list_cb, NULL,
280 &confirmation_cb, NULL, &me);
283 fprintf (stdout, _("Could not change username\n"));
284 return GNUNET_SYSERR;
286 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
287 fprintf (stdout, _("Joining room `%s' as user `%s'...\n"), room_name,
289 GNUNET_free (my_name);
295 do_nick (const char *msg, const void *xtra)
300 GNUNET_CHAT_leave_room (room);
302 GNUNET_free (nickname);
303 GNUNET_CONTAINER_meta_data_destroy (meta);
304 nickname = GNUNET_strdup (msg);
305 meta = GNUNET_CONTAINER_meta_data_create ();
306 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_TITLE,
307 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
308 nickname, strlen (nickname) + 1);
310 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
311 &receive_cb, NULL, &member_list_cb, NULL,
312 &confirmation_cb, NULL, &me);
315 fprintf (stdout, _("Could not change username\n"));
316 return GNUNET_SYSERR;
318 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
319 fprintf (stdout, _("Changed username to `%s'\n"), my_name);
320 GNUNET_free (my_name);
326 do_names (const char *msg, const void *xtra)
329 struct UserList *pos;
332 fprintf (stdout, _("Users in room `%s': "), room_name);
336 GNUNET_CRYPTO_hash (&pos->pkey,
337 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
339 name = GNUNET_PSEUDONYM_id_to_name (cfg, &pid);
340 fprintf (stdout, "`%s' ", name);
344 fprintf (stdout, "\n");
350 do_send (const char *msg, const void *xtra)
354 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_OPTION_NONE, NULL, &seq);
360 do_send_pm (const char *msg, const void *xtra)
366 struct UserList *pos;
368 if (NULL == strstr (msg, " "))
370 fprintf (stderr, _("Syntax: /msg USERNAME MESSAGE"));
373 user = GNUNET_strdup (msg);
374 strstr (user, " ")[0] = '\0';
375 msg += strlen (user) + 1;
376 if (GNUNET_OK != GNUNET_PSEUDONYM_name_to_id (cfg, user, &uid))
378 fprintf (stderr, _("Unknown user `%s'\n"), user);
385 GNUNET_CRYPTO_hash (&pos->pkey,
386 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
388 if (0 == memcmp (&pid, &uid, sizeof (GNUNET_HashCode)))
394 fprintf (stderr, _("User `%s' is currently not in the room!\n"), user);
398 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_PRIVATE, &pos->pkey,
406 do_send_sig (const char *msg, const void *xtra)
410 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_AUTHENTICATED, NULL,
417 do_send_ack (const char *msg, const void *xtra)
421 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_ACKNOWLEDGED, NULL,
428 do_send_anonymous (const char *msg, const void *xtra)
432 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_ANONYMOUS, NULL, &seq);
438 do_quit (const char *args, const void *xtra)
440 return GNUNET_SYSERR;
445 do_unknown (const char *msg, const void *xtra)
447 fprintf (stderr, _("Unknown command `%s'\n"), msg);
453 * List of supported IRC commands. The order matters!
455 static struct ChatCommand commands[] = {
458 ("Use `/join #roomname' to join a chat room. Joining a room will cause you"
459 " to leave the current room")},
462 ("Use `/nick nickname' to change your nickname. This will cause you to"
463 " leave the current room and immediately rejoin it with the new name.")},
464 {"/msg ", &do_send_pm,
466 ("Use `/msg nickname message' to send a private message to the specified"
468 {"/notice ", &do_send_pm,
469 gettext_noop ("The `/notice' command is an alias for `/msg'")},
470 {"/query ", &do_send_pm,
471 gettext_noop ("The `/query' command is an alias for `/msg'")},
472 {"/sig ", &do_send_sig,
473 gettext_noop ("Use `/sig message' to send a signed public message")},
474 {"/ack ", &do_send_ack,
476 ("Use `/ack message' to require signed acknowledgment of the message")},
477 {"/anonymous ", &do_send_anonymous,
479 ("Use `/anonymous message' to send a public anonymous message")},
480 {"/anon ", &do_send_anonymous,
481 gettext_noop ("The `/anon' command is an alias for `/anonymous'")},
483 gettext_noop ("Use `/quit' to terminate gnunet-chat")},
485 gettext_noop ("The `/leave' command is an alias for `/quit'")},
486 {"/names", &do_names,
488 ("Use `/names' to list all of the current members in the chat room")},
490 gettext_noop ("Use `/help command' to get help for a specific command")},
491 /* Add standard commands:
492 * /whois (print metadata),
493 * /ignore (set flag, check on receive!) */
494 /* the following three commands must be last! */
495 {"/", &do_unknown, NULL},
496 {"", &do_send, NULL},
502 do_help (const char *args, const void *xtra)
507 while ((NULL != args) && (0 != strlen (args)) &&
508 (commands[i].Action != &do_help))
510 if (0 == strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
512 fprintf (stdout, "%s\n", gettext (commands[i].helptext));
518 fprintf (stdout, "Available commands:");
519 while (commands[i].Action != &do_help)
521 fprintf (stdout, " %s", gettext (commands[i].command));
524 fprintf (stdout, "\n");
525 fprintf (stdout, "%s\n", gettext (commands[i].helptext));
531 do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
533 GNUNET_CHAT_leave_room (room);
534 if (handle_cmd_task != GNUNET_SCHEDULER_NO_TASK)
536 GNUNET_SCHEDULER_cancel (handle_cmd_task);
537 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
540 GNUNET_CONTAINER_meta_data_destroy (meta);
541 GNUNET_free (room_name);
542 GNUNET_free (nickname);
547 handle_command (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
549 char message[MAX_MESSAGE_LENGTH + 1];
552 /* read message from command line and handle it */
553 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
554 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
556 if (strlen (message) == 0)
558 if (message[strlen (message) - 1] == '\n')
559 message[strlen (message) - 1] = '\0';
560 if (strlen (message) == 0)
563 while ((NULL != commands[i].command) &&
565 strncasecmp (commands[i].command, message,
566 strlen (commands[i].command))))
569 commands[i].Action (&message[strlen (commands[i].command)], NULL))
574 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
575 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
576 &handle_command, NULL);
580 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
581 GNUNET_SCHEDULER_shutdown ();
586 * Main function that will be run by the scheduler.
588 * @param cls closure, NULL
589 * @param args remaining command-line arguments
590 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
591 * @param c configuration
594 run (void *cls, char *const *args, const char *cfgfile,
595 const struct GNUNET_CONFIGURATION_Handle *c)
601 /* check arguments */
602 if (NULL == nickname)
604 fprintf (stderr, _("You must specify a nickname\n"));
608 if (NULL == room_name)
609 room_name = GNUNET_strdup ("gnunet");
610 meta = GNUNET_CONTAINER_meta_data_create ();
611 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_TITLE,
612 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
613 nickname, strlen (nickname) + 1);
615 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
616 &receive_cb, NULL, &member_list_cb, NULL,
617 &confirmation_cb, NULL, &me);
620 fprintf (stderr, _("Failed to join room `%s'\n"), room_name);
621 GNUNET_free (room_name);
622 GNUNET_free (nickname);
623 GNUNET_CONTAINER_meta_data_destroy (meta);
627 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
628 fprintf (stdout, _("Joining room `%s' as user `%s'...\n"), room_name,
630 GNUNET_free (my_name);
632 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
633 &handle_command, NULL);
634 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
640 * The main function to chat via GNUnet.
642 * @param argc number of arguments from the command line
643 * @param argv command line arguments
644 * @return 0 ok, 1 on error
647 main (int argc, char *const *argv)
651 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
652 {'n', "nick", "NAME",
653 gettext_noop ("set the nickname to use (required)"),
654 1, &GNUNET_GETOPT_set_string, &nickname},
655 {'r', "room", "NAME",
656 gettext_noop ("set the chat room to join"),
657 1, &GNUNET_GETOPT_set_string, &room_name},
658 GNUNET_GETOPT_OPTION_END
662 flags = fcntl (0, F_GETFL, 0);
664 fcntl (0, F_SETFL, flags);
667 GNUNET_PROGRAM_run (argc, argv, "gnunet-chat",
668 gettext_noop ("Join a chat on GNUnet."), options,
669 &run, NULL)) ? ret : 1;
672 /* end of gnunet-chat.c */