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 meta information about the joining member
101 * @param message the message text
102 * @param options options for the message
103 * @return GNUNET_OK to accept the message now, GNUNET_NO to
104 * accept (but user is away), GNUNET_SYSERR to signal denied delivery
107 receive_cb (void *cls,
108 struct GNUNET_CHAT_Room *room,
109 const GNUNET_HashCode *sender,
110 const struct GNUNET_CONTAINER_MetaData *member_info,
112 struct GNUNET_TIME_Absolute timestamp,
113 enum GNUNET_CHAT_MsgOptions options)
120 nick = GNUNET_PSEUDONYM_id_to_name (cfg, sender);
122 nick = GNUNET_strdup (_("anonymous"));
126 case GNUNET_CHAT_MSG_OPTION_NONE:
127 case GNUNET_CHAT_MSG_ANONYMOUS:
128 fmt = _("(%s) `%s' said: %s\n");
130 case GNUNET_CHAT_MSG_PRIVATE:
131 fmt = _("(%s) `%s' said to you: %s\n");
133 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ANONYMOUS:
134 fmt = _("(%s) `%s' said to you: %s\n");
136 case GNUNET_CHAT_MSG_AUTHENTICATED:
137 fmt = _("(%s) `%s' said for sure: %s\n");
139 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_AUTHENTICATED:
140 fmt = _("(%s) `%s' said to you for sure: %s\n");
142 case GNUNET_CHAT_MSG_ACKNOWLEDGED:
143 fmt = _("(%s) `%s' was confirmed that you received: %s\n");
145 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
146 fmt = _("(%s) `%s' was confirmed that you and only you received: %s\n");
148 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_ACKNOWLEDGED:
149 fmt = _("(%s) `%s' was confirmed that you received from him or her: %s\n");
151 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
152 fmt = _("(%s) `%s' was confirmed that you and only you received from him or her: %s\n");
154 case GNUNET_CHAT_MSG_OFF_THE_RECORD:
155 fmt = _("(%s) `%s' said off the record: %s\n");
158 fmt = _("(%s) <%s> said using an unknown message type: %s\n");
161 time = GNUNET_STRINGS_absolute_time_to_string (timestamp);
162 fprintf (stdout, fmt, time, nick, message);
170 * Callback used for message delivery confirmations.
172 * @param cls closure, NULL
173 * @param room in which room was the message received?
174 * @param orig_seq_number sequence number of the original message
175 * @param timestamp when was the message received?
176 * @param receiver who is confirming the receipt?
177 * @param msg_hash hash of the original message
178 * @param receipt signature confirming delivery
179 * @return GNUNET_OK to continue, GNUNET_SYSERR to refuse processing further
180 * confirmations from anyone for this message
183 confirmation_cb (void *cls,
184 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,
209 const struct GNUNET_CONTAINER_MetaData *member_info,
210 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *member_id,
211 enum GNUNET_CHAT_MsgOptions options)
215 struct UserList *pos;
216 struct UserList *prev;
218 GNUNET_CRYPTO_hash (member_id,
219 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
221 nick = GNUNET_PSEUDONYM_id_to_name (cfg, &id);
222 fprintf (stdout, member_info != NULL
223 ? _("`%s' entered the room\n") : _("`%s' left the room\n"), nick);
225 if (NULL != member_info)
228 pos = GNUNET_malloc (sizeof (struct UserList));
230 pos->pkey = *member_id;
231 pos->ignored = GNUNET_NO;
239 while ((NULL != pos) &&
240 (0 != memcmp (&pos->pkey,
242 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))))
256 prev->next = pos->next;
265 do_join (const char *arg, const void *xtra)
271 arg++; /* ignore first hash */
272 GNUNET_CHAT_leave_room (room);
274 GNUNET_free (room_name);
275 room_name = GNUNET_strdup (arg);
276 room = GNUNET_CHAT_join_room (cfg,
283 &member_list_cb, NULL,
284 &confirmation_cb, NULL, &me);
287 fprintf (stdout, _("Could not change username\n"));
288 return GNUNET_SYSERR;
290 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
291 fprintf (stdout, _("Joining room `%s' as user `%s'...\n"), room_name, my_name);
292 GNUNET_free (my_name);
298 do_nick (const char *msg, const void *xtra)
303 GNUNET_CHAT_leave_room (room);
305 GNUNET_free (nickname);
306 GNUNET_CONTAINER_meta_data_destroy (meta);
307 nickname = GNUNET_strdup (msg);
308 meta = GNUNET_CONTAINER_meta_data_create ();
309 GNUNET_CONTAINER_meta_data_insert (meta,
311 EXTRACTOR_METATYPE_TITLE,
312 EXTRACTOR_METAFORMAT_UTF8,
316 room = GNUNET_CHAT_join_room (cfg,
323 &member_list_cb, NULL,
324 &confirmation_cb, NULL, &me);
327 fprintf (stdout, _("Could not change username\n"));
328 return GNUNET_SYSERR;
330 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
331 fprintf (stdout, _("Changed username to `%s'\n"), my_name);
332 GNUNET_free (my_name);
338 do_names (const char *msg, const void *xtra)
341 struct UserList *pos;
344 fprintf (stdout, _("Users in room `%s': "), room_name);
348 GNUNET_CRYPTO_hash (&pos->pkey,
349 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
351 name = GNUNET_PSEUDONYM_id_to_name (cfg, &pid);
352 fprintf (stdout, "`%s' ", name);
356 fprintf (stdout, "\n");
362 do_send (const char *msg, const void *xtra)
365 GNUNET_CHAT_send_message (room,
367 GNUNET_CHAT_MSG_OPTION_NONE,
374 do_send_pm (const char *msg, const void *xtra)
380 struct UserList *pos;
382 if (NULL == strstr (msg, " "))
384 fprintf (stderr, _("Syntax: /msg USERNAME MESSAGE"));
387 user = GNUNET_strdup (msg);
388 strstr (user, " ")[0] = '\0';
389 msg += strlen (user) + 1;
390 if (GNUNET_OK != GNUNET_PSEUDONYM_name_to_id (cfg, user, &uid))
392 fprintf (stderr, _("Unknown user `%s'\n"), user);
399 GNUNET_CRYPTO_hash (&pos->pkey,
400 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
402 if (0 == memcmp (&pid, &uid, sizeof (GNUNET_HashCode)))
408 fprintf (stderr, _("User `%s' is currently not in the room!\n"), user);
412 GNUNET_CHAT_send_message (room,
414 GNUNET_CHAT_MSG_PRIVATE,
423 do_send_sig (const char *msg, const void *xtra)
426 GNUNET_CHAT_send_message (room,
428 GNUNET_CHAT_MSG_AUTHENTICATED,
435 do_send_ack (const char *msg, const void *xtra)
438 GNUNET_CHAT_send_message (room,
440 GNUNET_CHAT_MSG_ACKNOWLEDGED,
447 do_send_anonymous (const char *msg, const void *xtra)
450 GNUNET_CHAT_send_message (room,
452 GNUNET_CHAT_MSG_ANONYMOUS,
459 do_quit (const char *args, const void *xtra)
461 return GNUNET_SYSERR;
466 do_unknown (const char *msg, const void *xtra)
468 fprintf (stderr, _("Unknown command `%s'\n"), msg);
474 * List of supported IRC commands. The order matters!
476 static struct ChatCommand commands[] = {
479 ("Use `/join #roomname' to join a chat room. Joining a room will cause you"
480 " to leave the current room")},
483 ("Use `/nick nickname' to change your nickname. This will cause you to"
484 " leave the current room and immediately rejoin it with the new name.")},
485 {"/msg ", &do_send_pm,
487 ("Use `/msg nickname message' to send a private message to the specified"
489 {"/notice ", &do_send_pm,
490 gettext_noop ("The `/notice' command is an alias for `/msg'")},
491 {"/query ", &do_send_pm,
492 gettext_noop ("The `/query' command is an alias for `/msg'")},
493 {"/sig ", &do_send_sig,
494 gettext_noop ("Use `/sig message' to send a signed public message")},
495 {"/ack ", &do_send_ack,
497 ("Use `/ack message' to require signed acknowledgment of the message")},
498 {"/anonymous ", &do_send_anonymous,
500 ("Use `/anonymous message' to send a public anonymous message")},
501 {"/anon ", &do_send_anonymous,
502 gettext_noop ("The `/anon' command is an alias for `/anonymous'")},
504 gettext_noop ("Use `/quit' to terminate gnunet-chat")},
506 gettext_noop ("The `/leave' command is an alias for `/quit'")},
507 {"/names", &do_names,
509 ("Use `/names' to list all of the current members in the chat room")},
511 gettext_noop ("Use `/help command' to get help for a specific command")},
512 /* Add standard commands:
513 /whois (print metadata),
514 /ignore (set flag, check on receive!) */
515 /* the following three commands must be last! */
516 {"/", &do_unknown, NULL},
517 {"", &do_send, NULL},
523 do_help (const char *args, const void *xtra)
527 while ((NULL != args) &&
528 (0 != strlen (args)) && (commands[i].Action != &do_help))
531 strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
533 fprintf (stdout, "%s\n", gettext (commands[i].helptext));
539 fprintf (stdout, "Available commands:");
540 while (commands[i].Action != &do_help)
542 fprintf (stdout, " %s", gettext (commands[i].command));
545 fprintf (stdout, "\n");
546 fprintf (stdout, "%s\n", gettext (commands[i].helptext));
552 do_stop_task (void *cls,
553 const struct GNUNET_SCHEDULER_TaskContext *tc)
555 GNUNET_CHAT_leave_room (room);
556 if (handle_cmd_task != GNUNET_SCHEDULER_NO_TASK)
558 GNUNET_SCHEDULER_cancel (handle_cmd_task);
559 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
562 GNUNET_CONTAINER_meta_data_destroy (meta);
563 GNUNET_free (room_name);
564 GNUNET_free (nickname);
569 handle_command (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
571 char message[MAX_MESSAGE_LENGTH + 1];
574 /* read message from command line and handle it */
575 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
576 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
578 if (strlen (message) == 0)
580 if (message[strlen (message) - 1] == '\n')
581 message[strlen (message) - 1] = '\0';
582 if (strlen (message) == 0)
585 while ((NULL != commands[i].command) &&
586 (0 != strncasecmp (commands[i].command,
587 message, strlen (commands[i].command))))
590 commands[i].Action (&message[strlen (commands[i].command)], NULL))
595 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
602 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
603 GNUNET_SCHEDULER_shutdown ();
608 * Main function that will be run by the scheduler.
610 * @param cls closure, NULL
611 * @param args remaining command-line arguments
612 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
613 * @param c configuration
619 const struct GNUNET_CONFIGURATION_Handle *c)
625 /* check arguments */
626 if (NULL == nickname)
628 fprintf (stderr, _("You must specify a nickname\n"));
632 if (NULL == room_name)
633 room_name = GNUNET_strdup ("gnunet");
634 meta = GNUNET_CONTAINER_meta_data_create ();
635 GNUNET_CONTAINER_meta_data_insert (meta,
637 EXTRACTOR_METATYPE_TITLE,
638 EXTRACTOR_METAFORMAT_UTF8,
642 room = GNUNET_CHAT_join_room (cfg,
649 &member_list_cb, NULL,
650 &confirmation_cb, NULL, &me);
653 fprintf (stderr, _("Failed to join room `%s'\n"), room_name);
654 GNUNET_free (room_name);
655 GNUNET_free (nickname);
656 GNUNET_CONTAINER_meta_data_destroy (meta);
660 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
661 fprintf (stdout, _("Joining room `%s' as user `%s'...\n"), room_name, my_name);
662 GNUNET_free (my_name);
664 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
667 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
674 * The main function to chat via GNUnet.
676 * @param argc number of arguments from the command line
677 * @param argv command line arguments
678 * @return 0 ok, 1 on error
681 main (int argc, char *const *argv)
684 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
685 {'n', "nick", "NAME",
686 gettext_noop ("set the nickname to use (required)"),
687 1, &GNUNET_GETOPT_set_string, &nickname},
688 {'r', "room", "NAME",
689 gettext_noop ("set the chat room to join"),
690 1, &GNUNET_GETOPT_set_string, &room_name},
691 GNUNET_GETOPT_OPTION_END
695 flags = fcntl (0, F_GETFL, 0);
697 fcntl (0, F_SETFL, flags);
700 GNUNET_PROGRAM_run (argc,
703 gettext_noop ("Join a chat on GNUnet."),
704 options, &run, NULL)) ? ret : 1;
707 /* end of gnunet-chat.c */