X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=examples%2Fchat.c;h=1a3343288a6c7699ada71e27da5cb4260462828c;hb=f45b2b5dbcd1fb7af7a92c54834b9cc3dd5326d4;hp=a735cb1a0b2f6a05cfe9f4aab85c5fb03d9167bf;hpb=fcbee2208944dcd0ab93d5469ea5eeb490977c45;p=meshlink diff --git a/examples/chat.c b/examples/chat.c index a735cb1a..1a334328 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) { @@ -26,6 +27,9 @@ static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool rea printf("%s left.\n", node->name); } +static meshlink_node_t **nodes; +static size_t nnodes; + static void parse_command(meshlink_handle_t *mesh, char *buf) { char *arg = strchr(buf, ' '); if(arg) @@ -41,7 +45,7 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { invitation = meshlink_invite(mesh, arg); if(!invitation) { - fprintf(stderr, "Could not invite '%s': %s\n", arg, mesh->errstr); + fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; } @@ -52,11 +56,16 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { fprintf(stderr, "/join requires an argument!\n"); return; } - + meshlink_stop(mesh); if(!meshlink_join(mesh, arg)) - fprintf(stderr, "Could not join using invitation: %s\n", mesh->errstr); - else + fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno)); + else { fprintf(stderr, "Invitation accepted!\n"); + if(!meshlink_start(mesh)) { + fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno)); + return 1; + } + } } else if(!strcasecmp(buf, "kick")) { if(!arg) { fprintf(stderr, "/kick requires an argument!\n"); @@ -65,16 +74,45 @@ static void parse_command(meshlink_handle_t *mesh, char *buf) { meshlink_node_t *node = meshlink_get_node(mesh, arg); if(!node) { - fprintf(stderr, "Unknown node '%s'\n", arg); + fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); return; } meshlink_blacklist(mesh, node); printf("Node '%s' blacklisted.\n", arg); + } else if(!strcasecmp(buf, "who")) { + if(!arg) { + nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes); + if(!nnodes) { + fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno)); + } else { + printf("%zu known nodes:", nnodes); + for(int i = 0; i < nnodes; i++) + printf(" %s", nodes[i]->name); + printf("\n"); + } + } else { + meshlink_node_t *node = meshlink_get_node(mesh, arg); + if(!node) { + fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno)); + } 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); } @@ -120,7 +158,7 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { destination = meshlink_get_node(mesh, buf); if(!destination) { - fprintf(stderr, "Unknown node '%s'\n", buf); + fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno)); return; } } @@ -131,7 +169,7 @@ static void parse_input(meshlink_handle_t *mesh, char *buf) { } if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) { - fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, mesh->errstr); + fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno)); return; } @@ -151,7 +189,7 @@ int main(int argc, char *argv[]) { meshlink_handle_t *mesh = meshlink_open(confbase, nick); if(!mesh) { - fprintf(stderr, "Could not open MeshLink!\n"); + fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno)); return 1; } @@ -160,11 +198,11 @@ int main(int argc, char *argv[]) { meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message); if(!meshlink_start(mesh)) { - fprintf(stderr, "Could not start MeshLink: %s\n", mesh->errstr); + fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno)); 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);