📄 gnunet-chat.c
字号:
nickname = GNUNET_strdup (msg); meta = GNUNET_meta_data_create (); GNUNET_meta_data_insert (meta, EXTRACTOR_TITLE, nickname); room = GNUNET_CHAT_join_room (ectx, cfg, nickname, meta, room_name, -1, &receive_callback, NULL, &member_list_callback, NULL, &confirmation_callback, NULL, &me); my_name = GNUNET_pseudonym_id_to_name (ectx, cfg, &me); fprintf (stdout, _("Changed username to `%s'.\n"), my_name); GNUNET_free (my_name); return GNUNET_OK;}static intdo_unknown (const char *msg, const void *xtra){ fprintf (stderr, _("Unknown command `%s'.\n"), msg); return GNUNET_OK;}static intdo_pm (const char *msg, const void *xtra){ char *user; GNUNET_HashCode uid; GNUNET_HashCode pid; unsigned int seq; struct UserList *pos; if (NULL == strstr (msg, " ")) { fprintf (stderr, _("Syntax: /msg USERNAME MESSAGE")); return GNUNET_OK; } user = GNUNET_strdup (msg); strstr (user, " ")[0] = '\0'; msg += strlen (user) + 1; if (GNUNET_OK != GNUNET_pseudonym_name_to_id (ectx, cfg, user, &uid)) { fprintf (stderr, _("Unknown user `%s'\n"), user); GNUNET_free (user); return GNUNET_OK; } GNUNET_mutex_lock (lock); pos = users; while (pos != NULL) { GNUNET_hash (&pos->pkey, sizeof (GNUNET_RSA_PublicKey), &pid); if (0 == memcmp (&pid, &uid, sizeof (GNUNET_HashCode))) break; pos = pos->next; } if (pos == NULL) { fprintf (stderr, _("User `%s' is currently not in the room!\n"), user); GNUNET_free (user); GNUNET_mutex_unlock (lock); return GNUNET_OK; } if (GNUNET_OK != GNUNET_CHAT_send_message (room, msg, GNUNET_CHAT_MSG_PRIVATE, &pos->pkey, &seq)) fprintf (stderr, _("Failed to send message.\n")); GNUNET_mutex_unlock (lock); return GNUNET_OK;}static intdo_names (const char *msg, const void *xtra){ char *name; struct UserList *pos; GNUNET_HashCode pid; GNUNET_mutex_lock (lock); fprintf (stdout, _("Users in room `%s': "), room_name); pos = users; while (pos != NULL) { GNUNET_hash (&pos->pkey, sizeof (GNUNET_RSA_PublicKey), &pid); name = GNUNET_pseudonym_id_to_name (ectx, cfg, &pid); fprintf (stdout, "`%s' ", name); GNUNET_free (name); pos = pos->next; } fprintf (stdout, "\n"); GNUNET_mutex_unlock (lock); return GNUNET_OK;}static intdo_quit (const char *args, const void *xtra){ return GNUNET_SYSERR;}static int do_help (const char *args, const void *xtra);/** * List of supported IRC commands. The order matters! */static struct ChatCommand commands[] = { {"/join ", &do_join, gettext_noop ("Use `/join #roomname' to join a chat room. Joining a room will cause you to leave the current room")}, {"/nick ", &do_nick, gettext_noop ("Use `/nick nickname' to change your nickname. This will cause you to leave the current room and immediately rejoin it with the new name.")}, {"/msg ", &do_pm, gettext_noop ("Use `/msg nickname message' to send a private message to the specified user")}, {"/notice ", &do_pm, gettext_noop ("The `/notice' command is an alias for `/msg'")}, {"/query ", &do_pm, gettext_noop ("The `/query' command is an alias for `/msg'")}, {"/quit", &do_quit, gettext_noop ("Use `/quit' to terminate gnunet-chat")}, {"/leave", &do_quit, gettext_noop ("The `/leave' command is an alias for `/quit'")}, {"/names", &do_names, gettext_noop ("Use `/names' to list all of the current members in the chat room")}, {"/help", &do_help, gettext_noop ("Use `/help command' to get help for a specific command")}, /* Add standard commands: /help (print help texts), /whois (print metadata), /ignore (set flag, check on receive!) */ /* Add special commands (currently supported): + anonymous msgs + authenticated msgs */ /* the following three commands must be last! */ {"/", &do_unknown, NULL}, {"", &do_transmit, NULL}, {NULL, NULL, NULL},};static intdo_help (const char *args, const void *xtra){ int i; i = 0; while ((args != NULL) && (0 != strlen(args)) && (commands[i].Action != &do_help)) { if (0 == strncasecmp (&args[1], &commands[i].command[1], strlen(args)-1)) { fprintf (stdout, "%s\n", gettext (commands[i].helptext)); return GNUNET_OK; } i++; } i = 0; fprintf (stdout, "Available commands:"); while (commands[i].Action != &do_help) { fprintf (stdout, " %s", gettext (commands[i].command)); i++; } fprintf (stdout, "\n"); fprintf (stdout, "%s\n", gettext (commands[i].helptext)); return GNUNET_OK;}/** * All gnunet-chat command line options */static struct GNUNET_CommandLineOption gnunetchatOptions[] = { GNUNET_COMMAND_LINE_OPTION_HELP (gettext_noop ("Join a chat on GNUnet.")), /* -h */ GNUNET_COMMAND_LINE_OPTION_HOSTNAME, /* -H */ GNUNET_COMMAND_LINE_OPTION_LOGGING, /* -L */ {'n', "nick", "NAME", gettext_noop ("set the nickname to use (required)"), 1, &GNUNET_getopt_configure_set_string, &nickname}, {'r', "room", "NAME", gettext_noop ("set the chat room to join"), 1, &GNUNET_getopt_configure_set_string, &room_name}, GNUNET_COMMAND_LINE_OPTION_VERSION (PACKAGE_VERSION), /* -v */ GNUNET_COMMAND_LINE_OPTION_VERBOSE, GNUNET_COMMAND_LINE_OPTION_END,};/** * GNUnet-chat main. * * @param argc number of arguments from the command line * @param argv command line arguments * @return 0: ok, otherwise error */intmain (int argc, char **argv){ char message[MAX_MESSAGE_LENGTH + 1]; char *my_name; GNUNET_HashCode me; int i; if (GNUNET_SYSERR == GNUNET_init (argc, argv, "gnunet-chat [OPTIONS]", &cfgFilename, gnunetchatOptions, &ectx, &cfg)) return -1; if (nickname == NULL) { fprintf (stderr, _("You must specify a nickname\n")); GNUNET_fini (ectx, cfg); return -1; } lock = GNUNET_mutex_create (GNUNET_NO); if (room_name == NULL) room_name = GNUNET_strdup ("gnunet"); meta = GNUNET_meta_data_create (); GNUNET_meta_data_insert (meta, EXTRACTOR_TITLE, nickname); room = GNUNET_CHAT_join_room (ectx, cfg, nickname, meta, room_name, -1, &receive_callback, NULL, &member_list_callback, NULL, &confirmation_callback, NULL, &me); if (room == NULL) { fprintf (stderr, _("Failed to join room `%s'\n"), room_name); GNUNET_free (room_name); GNUNET_free (nickname); GNUNET_meta_data_destroy (meta); GNUNET_mutex_destroy (lock); GNUNET_fini (ectx, cfg); return -1; } my_name = GNUNET_pseudonym_id_to_name (ectx, cfg, &me); fprintf (stdout, _("Joined room `%s' as user `%s'.\n"), room_name, my_name); GNUNET_free (my_name); /* read messages from command line and send */ while (GNUNET_shutdown_test () == GNUNET_NO) { memset (message, 0, MAX_MESSAGE_LENGTH + 1); if (NULL == fgets (message, MAX_MESSAGE_LENGTH, stdin)) break; if (strlen (message) == 0) continue; if (message[strlen (message) - 1] == '\n') message[strlen (message) - 1] = '\0'; if (strlen (message) == 0) continue; i = 0; while ((commands[i].command != NULL) && (0 != strncasecmp (commands[i].command, message, strlen (commands[i].command)))) i++; if (GNUNET_OK != commands[i].Action (&message[strlen (commands[i].command)], NULL)) break; } GNUNET_CHAT_leave_room (room); free_user_list (); GNUNET_meta_data_destroy (meta); GNUNET_free (room_name); GNUNET_free (nickname); GNUNET_fini (ectx, cfg); GNUNET_mutex_destroy (lock); return 0;}/* end of gnunet-chat.c */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -