X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fchat.c;h=70d17d7d917bf6515aaf98d1fb97bea2f969df22;hb=be457ad7e4857ac3b0cbb7e64c0ccd0e44ba21e3;hp=21ced7b092ad0ee17d1021581e3bbddf506e7bc0;hpb=6e182846ce82233e900d919afaab216181e627c2;p=meshlink diff --git a/examples/chat.c b/examples/chat.c index 21ced7b0..70d17d7d 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "../src/meshlink.h" static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { @@ -72,9 +73,41 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { meshlink_blacklist(mesh, node); printf("Node '%s' blacklisted.\n", arg); + } else if(!strcasecmp(buf, "who")) { + if(!arg) { + meshlink_node_t *nodes[100]; + size_t n = meshlink_get_all_nodes(mesh, nodes, 100); + if(!n) { + fprintf(stderr, "No nodes known!\n"); + } else { + printf("Known nodes:"); + for(int i = 0; i < n && i < 100; i++) + printf(" %s", nodes[i]->name); + if(n > 100) + printf(" (and %zu more)", n - 100); + printf("\n"); + } + } else { + meshlink_node_t *node = meshlink_get_node(mesh, arg); + if(!node) { + fprintf(stderr, "Unknown node '%s'\n", arg); + } else { + printf("Node %s found\n", arg); + } + } } else if(!strcasecmp(buf, "quit")) { printf("Bye!\n"); fclose(stdin); + } else if(!strcasecmp(buf, "help")) { + printf( + ": Send a message to the given node.\n" + " Subsequent messages don't need the : prefix.\n" + "/invite Create an invitation for a new node.\n" + "/join Join an existing mesh using an invitation.\n" + "/kick Blacklist the given node.\n" + "/who [] List all nodes or show information about the given node.\n" + "/quit Exit this program.\n" + ); } else { fprintf(stderr, "Unknown command '/%s'\n", buf); } @@ -151,7 +184,7 @@ int main(int argc, char *argv[]) { meshlink_handle_t *mesh = meshlink_open(confbase, nick); if(!mesh) { - fprintf(stderr, "Could not open MeshLink: %s\n", mesh->errstr); + fprintf(stderr, "Could not open MeshLink!\n"); return 1; } @@ -164,7 +197,7 @@ int main(int argc, char *argv[]) { return 1; } - printf("Chat started.\n"); + printf("Chat started.\nType /help for a list of commands.\n"); while(fgets(buf, sizeof buf, stdin)) parse_input(mesh, buf);