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;
49 typedef int (*ActionFunction)(const char *argumetns, const void *xtra);
54 ActionFunction Action;
60 struct UserList *next;
61 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pkey;
65 static struct UserList *users;
70 struct UserList *next;
81 do_help (const char *args, const void *xtra);
85 * Callback used for notification that we have joined the room.
93 FPRINTF (stdout, "%s", _("Joined\n"));
99 * Callback used for notification about incoming messages.
101 * @param cls closure, NULL
102 * @param room in which room was the message received?
103 * @param sender what is the ID of the sender? (maybe NULL)
104 * @param member_info information about the joining member
105 * @param message the message text
106 * @param timestamp time when the member joined
107 * @param options options for the message
108 * @return GNUNET_OK to accept the message now, GNUNET_NO to
109 * accept (but user is away), GNUNET_SYSERR to signal denied delivery
112 receive_cb (void *cls, struct GNUNET_CHAT_Room *room,
113 const struct GNUNET_HashCode * sender,
114 const struct GNUNET_CONTAINER_MetaData *member_info,
115 const char *message, struct GNUNET_TIME_Absolute timestamp,
116 enum GNUNET_CHAT_MsgOptions options)
118 char *non_unique_nick;
125 nick = GNUNET_strdup (_("anonymous"));
128 if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
129 sender, NULL, NULL, &non_unique_nick, &nick_is_a_dup)
130 || (nick_is_a_dup == GNUNET_YES))
132 GNUNET_free (non_unique_nick);
133 non_unique_nick = GNUNET_strdup (_("anonymous"));
135 nick = GNUNET_PSEUDONYM_name_uniquify (cfg, sender, non_unique_nick, NULL);
136 GNUNET_free (non_unique_nick);
140 switch ((int) options)
142 case GNUNET_CHAT_MSG_OPTION_NONE:
143 case GNUNET_CHAT_MSG_ANONYMOUS:
144 fmt = _("(%s) `%s' said: %s\n");
146 case GNUNET_CHAT_MSG_PRIVATE:
147 fmt = _("(%s) `%s' said to you: %s\n");
149 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ANONYMOUS:
150 fmt = _("(%s) `%s' said to you: %s\n");
152 case GNUNET_CHAT_MSG_AUTHENTICATED:
153 fmt = _("(%s) `%s' said for sure: %s\n");
155 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_AUTHENTICATED:
156 fmt = _("(%s) `%s' said to you for sure: %s\n");
158 case GNUNET_CHAT_MSG_ACKNOWLEDGED:
159 fmt = _("(%s) `%s' was confirmed that you received: %s\n");
161 case GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
162 fmt = _("(%s) `%s' was confirmed that you and only you received: %s\n");
164 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_ACKNOWLEDGED:
165 fmt = _("(%s) `%s' was confirmed that you received from him or her: %s\n");
167 case GNUNET_CHAT_MSG_AUTHENTICATED | GNUNET_CHAT_MSG_PRIVATE | GNUNET_CHAT_MSG_ACKNOWLEDGED:
170 ("(%s) `%s' was confirmed that you and only you received from him or her: %s\n");
172 case GNUNET_CHAT_MSG_OFF_THE_RECORD:
173 fmt = _("(%s) `%s' said off the record: %s\n");
176 fmt = _("(%s) <%s> said using an unknown message type: %s\n");
179 time = GNUNET_STRINGS_absolute_time_to_string (timestamp);
180 FPRINTF (stdout, fmt, time, nick, message);
188 * Callback used for message delivery confirmations.
190 * @param cls closure, NULL
191 * @param room in which room was the message received?
192 * @param orig_seq_number sequence number of the original message
193 * @param timestamp when was the message received?
194 * @param receiver who is confirming the receipt?
195 * @return GNUNET_OK to continue, GNUNET_SYSERR to refuse processing further
196 * confirmations from anyone for this message
199 confirmation_cb (void *cls, struct GNUNET_CHAT_Room *room,
200 uint32_t orig_seq_number,
201 struct GNUNET_TIME_Absolute timestamp,
202 const struct GNUNET_HashCode * receiver)
208 if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
209 receiver, NULL, NULL, &nick, &nick_is_a_dup)
210 || (nick_is_a_dup == GNUNET_YES))
213 nick = GNUNET_strdup (_("anonymous"));
215 unique_nick = GNUNET_PSEUDONYM_name_uniquify (cfg, receiver, nick, NULL);
217 FPRINTF (stdout, _("'%s' acknowledged message #%d\n"), unique_nick, orig_seq_number);
218 GNUNET_free (unique_nick);
224 * Callback used for notification that another room member has joined or left.
226 * @param cls closure (not used)
227 * @param member_info will be non-null if the member is joining, NULL if he is
229 * @param member_id hash of public key of the user (for unique identification)
230 * @param options what types of messages is this member willing to receive?
234 member_list_cb (void *cls, const struct GNUNET_CONTAINER_MetaData *member_info,
235 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *member_id,
236 enum GNUNET_CHAT_MsgOptions options)
239 char *non_unique_nick;
241 struct GNUNET_HashCode id;
242 struct UserList *pos;
243 struct UserList *prev;
245 GNUNET_CRYPTO_hash (member_id,
246 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
248 if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
249 &id, NULL, NULL, &non_unique_nick, &nick_is_a_dup)
250 || (nick_is_a_dup == GNUNET_YES))
252 GNUNET_free (non_unique_nick);
253 non_unique_nick = GNUNET_strdup (_("anonymous"));
255 nick = GNUNET_PSEUDONYM_name_uniquify (cfg, &id, non_unique_nick, NULL);
256 GNUNET_free (non_unique_nick);
260 NULL ? _("`%s' entered the room\n") : _("`%s' left the room\n"),
263 if (NULL != member_info)
266 pos = GNUNET_malloc (sizeof (struct UserList));
268 pos->pkey = *member_id;
269 pos->ignored = GNUNET_NO;
277 while ((NULL != pos) &&
279 memcmp (&pos->pkey, member_id,
280 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded))))
294 prev->next = pos->next;
303 do_join (const char *arg, const void *xtra)
306 int my_name_is_a_dup;
307 struct GNUNET_HashCode me;
310 arg++; /* ignore first hash */
311 GNUNET_CHAT_leave_room (room);
313 GNUNET_free (room_name);
314 room_name = GNUNET_strdup (arg);
316 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
317 &receive_cb, NULL, &member_list_cb, NULL,
318 &confirmation_cb, NULL, &me);
321 FPRINTF (stdout, "%s", _("Could not change username\n"));
322 return GNUNET_SYSERR;
324 if ((GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
325 &me, NULL, NULL, &my_name, &my_name_is_a_dup)) ||
326 (my_name_is_a_dup == GNUNET_YES))
328 GNUNET_free (my_name);
329 my_name = GNUNET_strdup (_("anonymous"));
331 /* Don't uniquify our own name - other people will have a different
332 * suffix for our own name anyway.
334 FPRINTF (stdout, _("Joining room `%s' as user `%s'...\n"), room_name,
336 GNUNET_free (my_name);
342 do_nick (const char *msg, const void *xtra)
345 int my_name_is_a_dup;
346 struct GNUNET_HashCode me;
348 GNUNET_CHAT_leave_room (room);
350 GNUNET_free (nickname);
351 GNUNET_CONTAINER_meta_data_destroy (meta);
352 nickname = GNUNET_strdup (msg);
353 meta = GNUNET_CONTAINER_meta_data_create ();
354 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_TITLE,
355 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
356 nickname, strlen (nickname) + 1);
358 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
359 &receive_cb, NULL, &member_list_cb, NULL,
360 &confirmation_cb, NULL, &me);
363 FPRINTF (stdout, "%s", _("Could not change username\n"));
364 return GNUNET_SYSERR;
366 if ((GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
367 &me, NULL, NULL, &my_name, &my_name_is_a_dup)) ||
368 (my_name_is_a_dup == GNUNET_YES))
370 GNUNET_free (my_name);
371 my_name = GNUNET_strdup (_("anonymous"));
373 FPRINTF (stdout, _("Changed username to `%s'\n"), my_name);
374 GNUNET_free (my_name);
380 do_names (const char *msg, const void *xtra)
385 struct UserList *pos;
386 struct GNUNET_HashCode pid;
388 FPRINTF (stdout, _("Users in room `%s': "), room_name);
392 GNUNET_CRYPTO_hash (&pos->pkey,
393 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
395 if (GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
396 &pid, NULL, NULL, &name, &name_is_a_dup)
397 || (name_is_a_dup == GNUNET_YES))
400 name = GNUNET_strdup (_("anonymous"));
402 unique_name = GNUNET_PSEUDONYM_name_uniquify (cfg, &pid, name, NULL);
404 FPRINTF (stdout, "`%s' ", unique_name);
405 GNUNET_free (unique_name);
408 FPRINTF (stdout, "%s", "\n");
414 do_send (const char *msg, const void *xtra)
418 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_OPTION_NONE, NULL, &seq);
424 do_send_pm (const char *msg, const void *xtra)
427 struct GNUNET_HashCode uid;
428 struct GNUNET_HashCode pid;
430 struct UserList *pos;
432 if (NULL == strstr (msg, " "))
434 FPRINTF (stderr, "%s", _("Syntax: /msg USERNAME MESSAGE"));
437 user = GNUNET_strdup (msg);
438 strstr (user, " ")[0] = '\0';
439 msg += strlen (user) + 1;
440 if (GNUNET_OK != GNUNET_PSEUDONYM_name_to_id (cfg, user, &uid))
443 _("Unknown user `%s'. Make sure you specify its numeric suffix, if any.\n"),
451 GNUNET_CRYPTO_hash (&pos->pkey,
452 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
454 if (0 == memcmp (&pid, &uid, sizeof (struct GNUNET_HashCode)))
460 FPRINTF (stderr, _("User `%s' is currently not in the room!\n"), user);
464 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_PRIVATE, &pos->pkey,
472 do_send_sig (const char *msg, const void *xtra)
476 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_AUTHENTICATED, NULL,
483 do_send_ack (const char *msg, const void *xtra)
487 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_ACKNOWLEDGED, NULL,
494 do_send_anonymous (const char *msg, const void *xtra)
498 GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_ANONYMOUS, NULL, &seq);
504 do_quit (const char *args, const void *xtra)
506 return GNUNET_SYSERR;
511 do_unknown (const char *msg, const void *xtra)
513 FPRINTF (stderr, _("Unknown command `%s'\n"), msg);
519 * List of supported IRC commands. The order matters!
521 static struct ChatCommand commands[] = {
524 ("Use `/join #roomname' to join a chat room. Joining a room will cause you"
525 " to leave the current room")},
528 ("Use `/nick nickname' to change your nickname. This will cause you to"
529 " leave the current room and immediately rejoin it with the new name.")},
530 {"/msg ", &do_send_pm,
532 ("Use `/msg nickname message' to send a private message to the specified"
534 {"/notice ", &do_send_pm,
535 gettext_noop ("The `/notice' command is an alias for `/msg'")},
536 {"/query ", &do_send_pm,
537 gettext_noop ("The `/query' command is an alias for `/msg'")},
538 {"/sig ", &do_send_sig,
539 gettext_noop ("Use `/sig message' to send a signed public message")},
540 {"/ack ", &do_send_ack,
542 ("Use `/ack message' to require signed acknowledgment of the message")},
543 {"/anonymous ", &do_send_anonymous,
545 ("Use `/anonymous message' to send a public anonymous message")},
546 {"/anon ", &do_send_anonymous,
547 gettext_noop ("The `/anon' command is an alias for `/anonymous'")},
549 gettext_noop ("Use `/quit' to terminate gnunet-chat")},
551 gettext_noop ("The `/leave' command is an alias for `/quit'")},
552 {"/names", &do_names,
554 ("Use `/names' to list all of the current members in the chat room")},
556 gettext_noop ("Use `/help command' to get help for a specific command")},
557 /* Add standard commands:
558 * /whois (print metadata),
559 * /ignore (set flag, check on receive!) */
560 /* the following three commands must be last! */
561 {"/", &do_unknown, NULL},
562 {"", &do_send, NULL},
568 do_help (const char *args, const void *xtra)
573 while ((NULL != args) && (0 != strlen (args)) &&
574 (commands[i].Action != &do_help))
576 if (0 == strncasecmp (&args[1], &commands[i].command[1], strlen (args) - 1))
578 FPRINTF (stdout, "%s\n", gettext (commands[i].helptext));
584 FPRINTF (stdout, "%s", "Available commands:");
585 while (commands[i].Action != &do_help)
587 FPRINTF (stdout, " %s", gettext (commands[i].command));
590 FPRINTF (stdout, "%s", "\n");
591 FPRINTF (stdout, "%s\n", gettext (commands[i].helptext));
597 do_stop_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
599 GNUNET_CHAT_leave_room (room);
600 if (handle_cmd_task != GNUNET_SCHEDULER_NO_TASK)
602 GNUNET_SCHEDULER_cancel (handle_cmd_task);
603 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
606 GNUNET_CONTAINER_meta_data_destroy (meta);
607 GNUNET_free (room_name);
608 GNUNET_free (nickname);
613 handle_command (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
615 char message[MAX_MESSAGE_LENGTH + 1];
618 /* read message from command line and handle it */
619 memset (message, 0, MAX_MESSAGE_LENGTH + 1);
620 if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin))
622 if (strlen (message) == 0)
624 if (message[strlen (message) - 1] == '\n')
625 message[strlen (message) - 1] = '\0';
626 if (strlen (message) == 0)
629 while ((NULL != commands[i].command) &&
631 strncasecmp (commands[i].command, message,
632 strlen (commands[i].command))))
635 commands[i].Action (&message[strlen (commands[i].command)], NULL))
640 GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_relative_multiply
641 (GNUNET_TIME_UNIT_MILLISECONDS, 100),
642 GNUNET_SCHEDULER_PRIORITY_UI,
643 &handle_command, NULL);
647 handle_cmd_task = GNUNET_SCHEDULER_NO_TASK;
648 GNUNET_SCHEDULER_shutdown ();
653 * Main function that will be run by the scheduler.
655 * @param cls closure, NULL
656 * @param args remaining command-line arguments
657 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
658 * @param c configuration
661 run (void *cls, char *const *args, const char *cfgfile,
662 const struct GNUNET_CONFIGURATION_Handle *c)
664 struct GNUNET_HashCode me;
666 int my_name_is_a_dup;
669 /* check arguments */
670 if (NULL == nickname)
672 FPRINTF (stderr, "%s", _("You must specify a nickname\n"));
676 if (NULL == room_name)
677 room_name = GNUNET_strdup ("gnunet");
678 meta = GNUNET_CONTAINER_meta_data_create ();
679 GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet>", EXTRACTOR_METATYPE_TITLE,
680 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
681 nickname, strlen (nickname) + 1);
683 GNUNET_CHAT_join_room (cfg, nickname, meta, room_name, -1, &join_cb, NULL,
684 &receive_cb, NULL, &member_list_cb, NULL,
685 &confirmation_cb, NULL, &me);
688 FPRINTF (stderr, _("Failed to join room `%s'\n"), room_name);
689 GNUNET_free (room_name);
690 GNUNET_free (nickname);
691 GNUNET_CONTAINER_meta_data_destroy (meta);
695 if ((GNUNET_OK != GNUNET_PSEUDONYM_get_info (cfg,
696 &me, NULL, NULL, &my_name, &my_name_is_a_dup)) ||
697 (my_name_is_a_dup == GNUNET_YES))
699 GNUNET_free (my_name);
700 my_name = GNUNET_strdup (_("anonymous"));
702 FPRINTF (stdout, _("Joining room `%s' as user `%s'...\n"), room_name,
704 GNUNET_free (my_name);
706 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_UI,
707 &handle_command, NULL);
708 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &do_stop_task,
714 * The main function to chat via GNUnet.
716 * @param argc number of arguments from the command line
717 * @param argv command line arguments
718 * @return 0 ok, 1 on error
721 main (int argc, char *const *argv)
725 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
726 {'n', "nick", "NAME",
727 gettext_noop ("set the nickname to use (required)"),
728 1, &GNUNET_GETOPT_set_string, &nickname},
729 {'r', "room", "NAME",
730 gettext_noop ("set the chat room to join"),
731 1, &GNUNET_GETOPT_set_string, &room_name},
732 GNUNET_GETOPT_OPTION_END
736 flags = fcntl (0, F_GETFL, 0);
738 fcntl (0, F_SETFL, flags);
741 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
745 GNUNET_PROGRAM_run (argc, argv, "gnunet-chat",
746 gettext_noop ("Join a chat on GNUnet."), options,
747 &run, NULL)) ? ret : 1;
750 /* end of gnunet-chat.c */