5 #include "../src/meshlink.h"
7 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
8 const char *levelstr[] = {
9 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
10 [MESHLINK_INFO] = "\x1b[32mINFO",
11 [MESHLINK_WARNING] = "\x1b[33mWARNING",
12 [MESHLINK_ERROR] = "\x1b[31mERROR",
13 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
15 fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
18 static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
19 const char *msg = data;
21 if(!len || msg[len - 1]) {
22 fprintf(stderr, "Received invalid data from %s\n", source->name);
26 printf("%s says: %s\n", source->name, msg);
29 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
31 printf("%s joined.\n", node->name);
33 printf("%s left.\n", node->name);
36 static meshlink_node_t **nodes;
39 static void parse_command(meshlink_handle_t *mesh, char *buf) {
40 char *arg = strchr(buf, ' ');
44 if(!strcasecmp(buf, "invite")) {
48 fprintf(stderr, "/invite requires an argument!\n");
52 invitation = meshlink_invite(mesh, arg);
54 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
58 printf("Invitation for %s: %s\n", arg, invitation);
60 } else if(!strcasecmp(buf, "join")) {
62 fprintf(stderr, "/join requires an argument!\n");
66 if(!meshlink_join(mesh, arg))
67 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
69 fprintf(stderr, "Invitation accepted!\n");
70 if(!meshlink_start(mesh)) {
71 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
75 } else if(!strcasecmp(buf, "kick")) {
77 fprintf(stderr, "/kick requires an argument!\n");
81 meshlink_node_t *node = meshlink_get_node(mesh, arg);
83 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
87 meshlink_blacklist(mesh, node);
89 printf("Node '%s' blacklisted.\n", arg);
90 } else if(!strcasecmp(buf, "who")) {
92 nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
94 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
96 printf("%zu known nodes:", nnodes);
97 for(int i = 0; i < nnodes; i++)
98 printf(" %s", nodes[i]->name);
102 meshlink_node_t *node = meshlink_get_node(mesh, arg);
104 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
106 printf("Node %s found\n", arg);
109 } else if(!strcasecmp(buf, "quit")) {
112 } else if(!strcasecmp(buf, "help")) {
114 "<name>: <message> Send a message to the given node.\n"
115 " Subsequent messages don't need the <name>: prefix.\n"
116 "/invite <name> Create an invitation for a new node.\n"
117 "/join <invitation> Join an existing mesh using an invitation.\n"
118 "/kick <name> Blacklist the given node.\n"
119 "/who [<name>] List all nodes or show information about the given node.\n"
120 "/quit Exit this program.\n"
123 fprintf(stderr, "Unknown command '/%s'\n", buf);
127 static void parse_input(meshlink_handle_t *mesh, char *buf) {
128 static meshlink_node_t *destination;
138 if(len && buf[len - 1] == '\n')
141 if(len && buf[len - 1] == '\r')
144 // Ignore empty lines.
149 // Commands start with '/'
152 return parse_command(mesh, buf + 1);
154 // Lines in the form "name: message..." set the destination node.
157 char *colon = strchr(buf, ':');
165 destination = meshlink_get_node(mesh, buf);
167 fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
173 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
177 if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) {
178 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
182 printf("Message sent to '%s'.\n", destination->name);
185 int main(int argc, char *argv[]) {
186 const char *confbase = ".chat";
187 const char *nick = NULL;
196 meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
198 meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
200 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
204 meshlink_set_receive_cb(mesh, receive);
205 meshlink_set_node_status_cb(mesh, node_status);
206 meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
208 if(!meshlink_start(mesh)) {
209 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
213 printf("Chat started.\nType /help for a list of commands.\n");
215 while(fgets(buf, sizeof buf, stdin))
216 parse_input(mesh, buf);
218 printf("Chat stopping.\n");
221 meshlink_close(mesh);