#include "../src/meshlink.h"
static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
- const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"};
- fprintf(stderr, "%s: %s\n", levelstr[level], text);
+ (void)mesh;
+
+ static const char *levelstr[] = {
+ [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
+ [MESHLINK_INFO] = "\x1b[32mINFO",
+ [MESHLINK_WARNING] = "\x1b[33mWARNING",
+ [MESHLINK_ERROR] = "\x1b[31mERROR",
+ [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
+ };
+
+ fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
}
static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
+ (void)mesh;
+
const char *msg = data;
if(!len || msg[len - 1]) {
}
static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
- if(reachable)
+ (void)mesh;
+
+ if(reachable) {
printf("%s joined.\n", node->name);
- else
+ } else {
printf("%s left.\n", node->name);
+ }
}
static meshlink_node_t **nodes;
static void parse_command(meshlink_handle_t *mesh, char *buf) {
char *arg = strchr(buf, ' ');
- if(arg)
+
+ if(arg) {
*arg++ = 0;
+ }
if(!strcasecmp(buf, "invite")) {
char *invitation;
return;
}
- invitation = meshlink_invite(mesh, arg);
+ invitation = meshlink_invite(mesh, NULL, arg);
+
if(!invitation) {
fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
return;
fprintf(stderr, "/join requires an argument!\n");
return;
}
+
meshlink_stop(mesh);
- if(!meshlink_join(mesh, arg))
+
+ if(!meshlink_join(mesh, arg)) {
fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
- else {
+ } else {
fprintf(stderr, "Invitation accepted!\n");
- if(!meshlink_start(mesh)) {
- fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
- return;
- }
+ }
+
+ if(!meshlink_start(mesh)) {
+ fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
+ exit(1);
}
} else if(!strcasecmp(buf, "kick")) {
if(!arg) {
}
meshlink_node_t *node = meshlink_get_node(mesh, arg);
+
if(!node) {
fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
return;
} 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++)
+
+ for(size_t 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 {
fclose(stdin);
} else if(!strcasecmp(buf, "help")) {
printf(
- "<name>: <message> Send a message to the given node.\n"
- " Subsequent messages don't need the <name>: prefix.\n"
- "/invite <name> Create an invitation for a new node.\n"
- "/join <invitation> Join an existing mesh using an invitation.\n"
- "/kick <name> Blacklist the given node.\n"
- "/who [<name>] List all nodes or show information about the given node.\n"
- "/quit Exit this program.\n"
- );
+ "<name>: <message> Send a message to the given node.\n"
+ " Subsequent messages don't need the <name>: prefix.\n"
+ "/invite <name> Create an invitation for a new node.\n"
+ "/join <invitation> Join an existing mesh using an invitation.\n"
+ "/kick <name> Blacklist the given node.\n"
+ "/who [<name>] List all nodes or show information about the given node.\n"
+ "/quit Exit this program.\n"
+ );
} else {
fprintf(stderr, "Unknown command '/%s'\n", buf);
}
static meshlink_node_t *destination;
size_t len;
- if(!buf)
+ if(!buf) {
return;
+ }
// Remove newline.
len = strlen(buf);
- if(len && buf[len - 1] == '\n')
+ if(len && buf[len - 1] == '\n') {
buf[--len] = 0;
+ }
- if(len && buf[len - 1] == '\r')
+ if(len && buf[len - 1] == '\r') {
buf[--len] = 0;
+ }
// Ignore empty lines.
- if(!len)
+ if(!len) {
return;
+ }
// Commands start with '/'
- if(*buf == '/')
- return parse_command(mesh, buf + 1);
+ if(*buf == '/') {
+ parse_command(mesh, buf + 1);
+ return;
+ }
// Lines in the form "name: message..." set the destination node.
if(colon) {
*colon = 0;
msg = colon + 1;
- if(*msg == ' ')
+
+ if(*msg == ' ') {
msg++;
+ }
destination = meshlink_get_node(mesh, buf);
+
if(!destination) {
fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
return;
const char *nick = NULL;
char buf[1024];
- if(argc > 1)
+ if(argc > 1) {
confbase = argv[1];
+ }
- if(argc > 2)
+ if(argc > 2) {
nick = argv[2];
+ }
+
+ meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
+
+ meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
- meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat");
if(!mesh) {
fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
return 1;
printf("Chat started.\nType /help for a list of commands.\n");
- while(fgets(buf, sizeof buf, stdin))
+ while(fgets(buf, sizeof(buf), stdin)) {
parse_input(mesh, buf);
+ }
printf("Chat stopping.\n");