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)
122 nick = GNUNET_PSEUDONYM_id_to_name (cfg, sender);
124 nick = GNUNET_strdup (_("anonymous"));
126 switch ((int) options)
128 case GNUNET_CHAT_MSG_OPTION_NONE:
129 case GNUNET_CHAT_MSG_ANONYMOUS:
130 fmt = _("(%s) `%s' said: %s\n");
132 case GNUNET_CHAT_MSG_PRIVATE:
133 fmt = _("(%s) `%s' said to you: %s\n");
135 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ANONYMOUS:
136 fmt = _("(%s) `%s' said to you: %s\n");
138 case GNUNET_CHAT_MSG_AUTHENTICATED:
139 fmt = _("(%s) `%s' said for sure: %s\n");
141 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_AUTHENTICATED:
142 fmt = _("(%s) `%s' said to you for sure: %s\n");
144 case GNUNET_CHAT_MSG_ACKNOWLEDGED:
145 fmt = _("(%s) `%s' was confirmed that you received: %s\n");
147 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
148 fmt = _("(%s) `%s' was confirmed that you and only you received: %s\n");
150 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_ACKNOWLEDGED:
151 fmt = _("(%s) `%s' was confirmed that you received from him or her: %s\n");
153 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
156 ("(%s) `%s' was confirmed that you and only you received from him or her: %s\n");
158 case GNUNET_CHAT_MSG_OFF_THE_RECORD:
159 fmt = _("(%s) `%s' said off the record: %s\n");
162 fmt = _("(%s) <%s> said using an unknown message type: %s\n");
165 time = GNUNET_STRINGS_absolute_time_to_string (timestamp);
166 FPRINTF (stdout, fmt, time, nick, message);
174 * Callback used for message delivery confirmations.
176 * @param cls closure, NULL
177 * @param room in which room was the message received?
178 * @param orig_seq_number sequence number of the original message
179 * @param timestamp when was the message received?
180 * @param receiver who is confirming the receipt?
181 * @return GNUNET_OK to continue, GNUNET_SYSERR to refuse processing further
182 * confirmations from anyone for this message
185 confirmation_cb (void *cls, struct GNUNET_CHAT_Room *room,
186 uint32_t orig_seq_number,
187 struct GNUNET_TIME_Absolute timestamp,
188 const GNUNET_HashCode * receiver)
192 nick = GNUNET_PSEUDONYM_id_to_name (cfg, receiver);
193 FPRINTF (stdout, _("'%s' acknowledged message #%d\n"), nick, orig_seq_number);
199 * Callback used for notification that another room member has joined or left.
201 * @param cls closure (not used)
202 * @param member_info will be non-null if the member is joining, NULL if he is
204 * @param member_id hash of public key of the user (for unique identification)
205 * @param options what types of messages is this member willing to receive?
209 member_list_cb (void *cls, 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);
224 NULL ? _("`%s' entered the room\n") : _("`%s' left the room\n"),
227 if (NULL != member_info)
230 pos = GNUNET_malloc (sizeof (struct UserList));
232 pos->pkey = *member_id;
233 pos->ignored = GNUNET_NO;
241 while ((NULL != pos) &&
243 memcmp (&pos->pkey, member_id,
244 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))))
258 prev->next = pos->next;
267 do_join (const char *arg, const void *xtra)
273 arg++; /* ignore first hash */
274 GNUNET_CHAT_leave_room (room);
276 GNUNET_free (room_name);
277 room_name = GNUNET_strdup (arg);
279 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
280 &receive_cb, NULL, &member_list_cb, NULL,
281 &confirmation_cb, NULL, &me);
284 FPRINTF (stdout, "%s", _("Could not change username\n"));
285 return GNUNET_SYSERR;
287 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
288 FPRINTF (stdout, _("Joining room `%s' as user `%s'...\n"), room_name,
290 GNUNET_free (my_name);
296 do_nick (const char *msg, const void *xtra)
301 GNUNET_CHAT_leave_room (room);
303 GNUNET_free (nickname);
304 GNUNET_CONTAINER_meta_data_destroy (meta);
305 nickname = GNUNET_strdup (msg);
306 meta = GNUNET_CONTAINER_meta_data_create ();
307 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_TITLE,
308 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
309 nickname, strlen (nickname) + 1);
311 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
312 &receive_cb, NULL, &member_list_cb, NULL,
313 &confirmation_cb, NULL, &me);
316 FPRINTF (stdout, "%s", _("Could not change username\n"));
317 return GNUNET_SYSERR;
319 my_name = GNUNET_PSEUDONYM_id_to_name (cfg, &me);
320 FPRINTF (stdout, _("Changed username to `%s'\n"), my_name);
321 GNUNET_free (my_name);
327 do_names (const char *msg, const void *xtra)
330 struct UserList *pos;
333 FPRINTF (stdout, _("Users in room `%s': "), room_name);
337 GNUNET_CRYPTO_hash (&pos->pkey,
338 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
340 name = GNUNET_PSEUDONYM_id_to_name (cfg, &pid);
341 FPRINTF (stdout, "`%s' ", name);
345 FPRINTF (stdout, "%s", "\n");
351 do_send (const char *msg, const void *xtra)
355 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_OPTION_NONE, NULL, &seq);
361 do_send_pm (const char *msg, const void *xtra)
367 struct UserList *pos;
369 if (NULL == strstr (msg, " "))
371 FPRINTF (stderr, "%s", _("Syntax: /msg USERNAME MESSAGE"));
374 user = GNUNET_strdup (msg);
375 strstr (user, " ")[0] = '\0';
376 msg += strlen (user) + 1;
377 if (GNUNET_OK != GNUNET_PSEUDONYM_name_to_id (cfg, user, &uid))
379 FPRINTF (stderr, _("Unknown user `%s'\n"), user);
386 GNUNET_CRYPTO_hash (&pos->pkey,
387 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
389 if (0 == memcmp (&pid, &uid, sizeof (GNUNET_HashCode)))
395 FPRINTF (stderr, _("User `%s' is currently not in the room!\n"), user);
399 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_PRIVATE, &pos->pkey,
407 do_send_sig (const char *msg, const void *xtra)
411 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_AUTHENTICATED, NULL,
418 do_send_ack (const char *msg, const void *xtra)
422 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_ACKNOWLEDGED, NULL,
429 do_send_anonymous (const char *msg, const void *xtra)
433 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_ANONYMOUS, NULL, &seq);
439 do_quit (const char *args, const void *xtra)
441 return GNUNET_SYSERR;
446 do_unknown (const char *msg, const void *xtra)
448 FPRINTF (stderr, _("Unknown command `%s'\n"), msg);
454 * List of supported IRC commands. The order matters!
456 static struct ChatCommand commands[] = {
459 ("Use `/join #roomname' to join a chat room. Joining a room will cause you"
460 " to leave the current room")},
463 ("Use `/nick nickname' to change your nickname. This will cause you to"
464 " leave the current room and immediately rejoin it with the new name.")},
465 {"/msg ", &do_send_pm,
467 ("Use `/msg nickname message' to send a private message to the specified"
469 {"/notice ", &do_send_pm,
470 gettext_noop ("The `/notice' command is an alias for `/msg'")},
471 {"/query ", &do_send_pm,
472 gettext_noop ("The `/query' command is an alias for `/msg'")},
473 {"/sig ", &do_send_sig,
474 gettext_noop ("Use `/sig message' to send a signed public message")},
475 {"/ack ", &do_send_ack,
477 ("Use `/ack message' to require signed acknowledgment of the message")},
478 {"/anonymous ", &do_send_anonymous,
480 ("Use `/anonymous message' to send a public anonymous message")},
481 {"/anon ", &do_send_anonymous,
482 gettext_noop ("The `/anon' command is an alias for `/anonymous'")},
484 gettext_noop ("Use `/quit' to terminate gnunet-chat")},
486 gettext_noop ("The `/leave' command is an alias for `/quit'")},
487 {"/names", &do_names,
489 ("Use `/names' to list all of the current members in the chat room")},
491 gettext_noop ("Use `/help command' to get help for a specific command")},
492 /* Add standard commands:
493 * /whois (print metadata),
494 * /ignore (set flag, check on receive!) */
495 /* the following three commands must be last! */
496 {"/", &do_unknown, NULL},
497 {"", &do_send, NULL},
503 do_help (const char *args, const void *xtra)
508 while ((NULL != args) && (0 != strlen (args)) &&
509 (commands[i].Action != &do_help))
511 if (0 == strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
513 FPRINTF (stdout, "%s\n", gettext (commands[i].helptext));
519 FPRINTF (stdout, "%s", "Available commands:");
520 while (commands[i].Action != &do_help)
522 FPRINTF (stdout, " %s", gettext (commands[i].command));
525 FPRINTF (stdout, "%s", "\n");
526 FPRINTF (stdout, "%s\n", gettext (commands[i].helptext));
532 do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
534 GNUNET_CHAT_leave_room (room);
535 if (handle_cmd_task != GNUNET_SCHEDULER_NO_TASK)
537 GNUNET_SCHEDULER_cancel (handle_cmd_task);
538 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
541 GNUNET_CONTAINER_meta_data_destroy (meta);
542 GNUNET_free (room_name);
543 GNUNET_free (nickname);
548 handle_command (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
550 char message[MAX_MESSAGE_LENGTH + 1];
553 /* read message from command line and handle it */
554 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
555 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
557 if (strlen (message) == 0)
559 if (message[strlen (message) - 1] == '\n')
560 message[strlen (message) - 1] = '\0';
561 if (strlen (message) == 0)
564 while ((NULL != commands[i].command) &&
566 strncasecmp (commands[i].command, message,
567 strlen (commands[i].command))))
570 commands[i].Action (&message[strlen (commands[i].command)], NULL))
575 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_relative_multiply
576 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
577 GNUNET_SCHEDULER_PRIORITY_UI,
578 &handle_command, NULL);
582 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
583 GNUNET_SCHEDULER_shutdown ();
588 * Main function that will be run by the scheduler.
590 * @param cls closure, NULL
591 * @param args remaining command-line arguments
592 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
593 * @param c configuration
596 run (void *cls, char *const *args, const char *cfgfile,
597 const struct GNUNET_CONFIGURATION_Handle *c)
603 /* check arguments */
604 if (NULL == nickname)
606 FPRINTF (stderr, "%s", _("You must specify a nickname\n"));
610 if (NULL == room_name)
611 room_name = GNUNET_strdup ("gnunet");
612 meta = GNUNET_CONTAINER_meta_data_create ();
613 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_TITLE,
614 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
615 nickname, strlen (nickname) + 1);
617 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
618 &receive_cb, NULL, &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, _("Joining room `%s' as user `%s'...\n"), room_name,
632 GNUNET_free (my_name);
634 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
635 &handle_command, NULL);
636 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
642 * The main function to chat via GNUnet.
644 * @param argc number of arguments from the command line
645 * @param argv command line arguments
646 * @return 0 ok, 1 on error
649 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
664 flags = fcntl (0, F_GETFL, 0);
666 fcntl (0, F_SETFL, flags);
669 GNUNET_PROGRAM_run (argc, argv, "gnunet-chat",
670 gettext_noop ("Join a chat on GNUnet."), options,
671 &run, NULL)) ? ret : 1;
674 /* end of gnunet-chat.c */