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 about incoming messages.
83 * @param cls closure, NULL
84 * @param room in which room was the message received?
85 * @param sender what is the ID of the sender? (maybe NULL)
86 * @param member_info information about the joining member
87 * @param message the message text
88 * @param options options for the message
89 * @return GNUNET_OK to accept the message now, GNUNET_NO to
90 * accept (but user is away), GNUNET_SYSERR to signal denied delivery
93 receive_cb (void *cls,
94 struct GNUNET_CHAT_Room *room,
95 const GNUNET_HashCode *sender,
96 const struct GNUNET_CONTAINER_MetaData *meta,
98 enum GNUNET_CHAT_MsgOptions options)
104 nick = GNUNET_PSEUDONYM_id_to_name (cfg, sender);
106 nick = GNUNET_strdup (_("anonymous"));
110 case GNUNET_CHAT_MSG_OPTION_NONE:
111 case GNUNET_CHAT_MSG_ANONYMOUS:
112 fmt = _("`%s' said: %s\n");
114 case GNUNET_CHAT_MSG_PRIVATE:
115 fmt = _("`%s' said to you: %s\n");
117 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ANONYMOUS:
118 fmt = _("`%s' said to you: %s\n");
120 case GNUNET_CHAT_MSG_AUTHENTICATED:
121 fmt = _("`%s' said for sure: %s\n");
123 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_AUTHENTICATED:
124 fmt = _("`%s' said to you for sure: %s\n");
126 case GNUNET_CHAT_MSG_ACKNOWLEDGED:
127 fmt = _("`%s' was confirmed that you received: %s\n");
129 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
130 fmt = _("`%s' was confirmed that you and only you received: %s\n");
132 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_ACKNOWLEDGED:
133 fmt = _("`%s' was confirmed that you received from him or her: %s\n");
135 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
138 ("`%s' was confirmed that you and only you received from him or her: %s\n");
140 case GNUNET_CHAT_MSG_OFF_THE_RECORD:
141 fmt = _("`%s' said off the record: %s\n");
144 fmt = _("<%s> said using an unknown message type: %s\n");
147 fprintf (stdout, fmt, nick, message);
154 * Callback used for message delivery confirmations.
156 * @param cls closure, NULL
157 * @param room in which room was the message received?
158 * @param orig_seq_number sequence number of the original message
159 * @param timestamp when was the message received?
160 * @param receiver who is confirming the receipt?
161 * @param msg_hash hash of the original message
162 * @param receipt signature confirming delivery
163 * @return GNUNET_OK to continue, GNUNET_SYSERR to refuse processing further
164 * confirmations from anyone for this message
167 confirmation_cb (void *cls,
168 struct GNUNET_CHAT_Room *room,
169 uint32_t orig_seq_number,
170 struct GNUNET_TIME_Absolute timestamp,
171 const GNUNET_HashCode *receiver,
172 const GNUNET_HashCode *msg_hash,
173 const struct GNUNET_CRYPTO_RsaSignature *receipt)
177 nick = GNUNET_PSEUDONYM_id_to_name (cfg, receiver);
178 fprintf (stdout, _("'%s' acknowledged message #%d\n"), nick, orig_seq_number);
184 * Callback used for notification that another room member has joined or left.
186 * @param member_info will be non-null if the member is joining, NULL if he is
188 * @param member_id hash of public key of the user (for unique identification)
189 * @param options what types of messages is this member willing to receive?
193 member_list_cb (void *cls,
194 const struct GNUNET_CONTAINER_MetaData *member_info,
195 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *member_id,
196 enum GNUNET_CHAT_MsgOptions options)
200 struct UserList *pos;
201 struct UserList *prev;
203 GNUNET_CRYPTO_hash (member_id,
204 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
206 nick = GNUNET_PSEUDONYM_id_to_name (cfg, &id);
207 fprintf (stdout, member_info != NULL
208 ? _("`%s' entered the room\n") : _("`%s' left the room\n"), nick);
210 if (NULL != member_info)
213 pos = GNUNET_malloc (sizeof (struct UserList));
215 pos->pkey = *member_id;
216 pos->ignored = GNUNET_NO;
224 while ((NULL != pos) &&
225 (0 != memcmp (&pos->pkey,
227 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))))
241 prev->next = pos->next;
250 do_transmit (const char *msg, const void *xtra)
253 GNUNET_CHAT_send_message (room,
255 GNUNET_CHAT_MSG_OPTION_NONE,
262 do_join (const char *arg, const void *xtra)
268 arg++; /* ignore first hash */
269 GNUNET_CHAT_leave_room (room);
271 GNUNET_free (room_name);
272 room_name = GNUNET_strdup (arg);
273 room = GNUNET_CHAT_join_room (cfg,
279 &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, _("Joined room `%s' as user `%s'\n"), room_name, my_name);
288 GNUNET_free (my_name);
294 do_nick (const char *msg, const void *xtra)
299 GNUNET_CHAT_leave_room (room);
301 GNUNET_free (nickname);
302 GNUNET_CONTAINER_meta_data_destroy (meta);
303 nickname = GNUNET_strdup (msg);
304 meta = GNUNET_CONTAINER_meta_data_create ();
305 GNUNET_CONTAINER_meta_data_insert (meta,
307 EXTRACTOR_METATYPE_TITLE,
308 EXTRACTOR_METAFORMAT_UTF8,
312 room = GNUNET_CHAT_join_room (cfg,
318 &member_list_cb, NULL,
319 &confirmation_cb, NULL, &me);
322 fprintf (stdout, _("Could not change username\n"));
323 return GNUNET_SYSERR;
325 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
326 fprintf (stdout, _("Changed username to `%s'\n"), my_name);
327 GNUNET_free (my_name);
333 do_names (const char *msg, const void *xtra)
336 struct UserList *pos;
339 fprintf (stdout, _("Users in room `%s': "), room_name);
343 GNUNET_CRYPTO_hash (&pos->pkey,
344 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
346 name = GNUNET_PSEUDONYM_id_to_name (cfg, &pid);
347 fprintf (stdout, "`%s' ", name);
351 fprintf (stdout, "\n");
357 do_pm (const char *msg, const void *xtra)
363 struct UserList *pos;
365 if (NULL == strstr (msg, " "))
367 fprintf (stderr, _("Syntax: /msg USERNAME MESSAGE"));
370 user = GNUNET_strdup (msg);
371 strstr (user, " ")[0] = '\0';
372 msg += strlen (user) + 1;
373 if (GNUNET_OK != GNUNET_PSEUDONYM_name_to_id (cfg, user, &uid))
375 fprintf (stderr, _("Unknown user `%s'\n"), user);
382 GNUNET_CRYPTO_hash (&pos->pkey,
383 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
385 if (0 == memcmp (&pid, &uid, sizeof (GNUNET_HashCode)))
391 fprintf (stderr, _("User `%s' is currently not in the room!\n"), user);
395 GNUNET_CHAT_send_message (room,
397 GNUNET_CHAT_MSG_PRIVATE,
406 do_transmit_sig (const char *msg, const void *xtra)
409 GNUNET_CHAT_send_message (room,
411 GNUNET_CHAT_MSG_AUTHENTICATED,
418 do_transmit_ack (const char *msg, const void *xtra)
421 GNUNET_CHAT_send_message (room,
423 GNUNET_CHAT_MSG_ACKNOWLEDGED,
430 do_quit (const char *args, const void *xtra)
432 return GNUNET_SYSERR;
437 do_unknown (const char *msg, const void *xtra)
439 fprintf (stderr, _("Unknown command `%s'\n"), msg);
445 * List of supported IRC commands. The order matters!
447 static struct ChatCommand commands[] = {
450 ("Use `/join #roomname' to join a chat room. Joining a room will cause you"
451 " to leave the current room")},
454 ("Use `/nick nickname' to change your nickname. This will cause you to"
455 " leave the current room and immediately rejoin it with the new name.")},
458 ("Use `/msg nickname message' to send a private message to the specified"
461 gettext_noop ("The `/notice' command is an alias for `/msg'")},
463 gettext_noop ("The `/query' command is an alias for `/msg'")},
464 {"/sig ", &do_transmit_sig,
465 gettext_noop ("Use `/sig message' to send a signed public message")},
466 {"/ack ", &do_transmit_ack,
468 ("Use `/ack message' to require signed acknowledgment of the message")},
470 gettext_noop ("Use `/quit' to terminate gnunet-chat")},
472 gettext_noop ("The `/leave' command is an alias for `/quit'")},
473 {"/names", &do_names,
475 ("Use `/names' to list all of the current members in the chat room")},
477 gettext_noop ("Use `/help command' to get help for a specific command")},
478 /* Add standard commands:
479 /whois (print metadata),
480 /ignore (set flag, check on receive!) */
481 /* Add special commands (currently supported):
485 /* the following three commands must be last! */
486 {"/", &do_unknown, NULL},
487 {"", &do_transmit, NULL},
493 do_help (const char *args, const void *xtra)
497 while ((NULL != args) &&
498 (0 != strlen (args)) && (commands[i].Action != &do_help))
501 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
503 fprintf (stdout, "%s\n", gettext (commands[i].helptext));
509 fprintf (stdout, "Available commands:");
510 while (commands[i].Action != &do_help)
512 fprintf (stdout, " %s", gettext (commands[i].command));
515 fprintf (stdout, "\n");
516 fprintf (stdout, "%s\n", gettext (commands[i].helptext));
522 do_stop_task (void *cls,
523 const struct GNUNET_SCHEDULER_TaskContext *tc)
525 GNUNET_CHAT_leave_room (room);
526 if (handle_cmd_task != GNUNET_SCHEDULER_NO_TASK)
528 GNUNET_SCHEDULER_cancel (handle_cmd_task);
529 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
532 GNUNET_CONTAINER_meta_data_destroy (meta);
533 GNUNET_free (room_name);
534 GNUNET_free (nickname);
539 handle_command (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
541 char message[MAX_MESSAGE_LENGTH + 1];
544 /* read message from command line and handle it */
545 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
546 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
548 if (strlen (message) == 0)
550 if (message[strlen (message) - 1] == '\n')
551 message[strlen (message) - 1] = '\0';
552 if (strlen (message) == 0)
555 while ((NULL != commands[i].command) &&
556 (0 != strncasecmp (commands[i].command,
557 message, strlen (commands[i].command))))
560 commands[i].Action (&message[strlen (commands[i].command)], NULL))
565 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
572 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
573 GNUNET_SCHEDULER_shutdown ();
578 * Main function that will be run by the scheduler.
580 * @param cls closure, NULL
581 * @param args remaining command-line arguments
582 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
583 * @param cfg configuration
589 const struct GNUNET_CONFIGURATION_Handle *c)
595 /* check arguments */
596 if (NULL == nickname)
598 fprintf (stderr, _("You must specify a nickname\n"));
602 if (NULL == room_name)
603 room_name = GNUNET_strdup ("gnunet");
604 meta = GNUNET_CONTAINER_meta_data_create ();
605 GNUNET_CONTAINER_meta_data_insert (meta,
607 EXTRACTOR_METATYPE_TITLE,
608 EXTRACTOR_METAFORMAT_UTF8,
612 room = GNUNET_CHAT_join_room (cfg,
618 &member_list_cb, NULL,
619 &confirmation_cb, NULL, &me);
622 fprintf (stderr, _("Failed to join room `%s'\n"), room_name);
623 GNUNET_free (room_name);
624 GNUNET_free (nickname);
625 GNUNET_CONTAINER_meta_data_destroy (meta);
629 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
630 fprintf (stdout, _("Joined room `%s' as user `%s'\n"), room_name, my_name);
631 GNUNET_free (my_name);
633 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
636 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
643 * The main function to chat via GNUnet.
645 * @param argc number of arguments from the command line
646 * @param argv command line arguments
647 * @return 0 ok, 1 on error
650 main (int argc, char *const *argv)
653 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
654 {'n', "nick", "NAME",
655 gettext_noop ("set the nickname to use (required)"),
656 1, &GNUNET_GETOPT_set_string, &nickname},
657 {'r', "room", "NAME",
658 gettext_noop ("set the chat room to join"),
659 1, &GNUNET_GETOPT_set_string, &room_name},
660 GNUNET_GETOPT_OPTION_END
663 flags = fcntl (0, F_GETFL, 0);
665 fcntl (0, F_SETFL, flags);
667 GNUNET_PROGRAM_run (argc,
670 gettext_noop ("Join a chat on GNUnet."),
671 options, &run, NULL)) ? ret : 1;
674 /* end of gnunet-chat.c */