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 restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
74 } else if(!strcasecmp(buf, "kick")) {
76 fprintf(stderr, "/kick requires an argument!\n");
80 meshlink_node_t *node = meshlink_get_node(mesh, arg);
82 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
86 meshlink_blacklist(mesh, node);
88 printf("Node '%s' blacklisted.\n", arg);
89 } else if(!strcasecmp(buf, "who")) {
91 nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
93 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
95 printf("%zu known nodes:", nnodes);
96 for(int i = 0; i < nnodes; i++)
97 printf(" %s", nodes[i]->name);
101 meshlink_node_t *node = meshlink_get_node(mesh, arg);
103 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
105 printf("Node %s found\n", arg);
107 } else if(!strcasecmp(buf, "quit")) {
110 } else if(!strcasecmp(buf, "help")) {
112 "<name>: <message> Send a message to the given node.\n"
113 " Subsequent messages don't need the <name>: prefix.\n"
114 "/invite <name> Create an invitation for a new node.\n"
115 "/join <invitation> Join an existing mesh using an invitation.\n"
116 "/kick <name> Blacklist the given node.\n"
117 "/who [<name>] List all nodes or show information about the given node.\n"
118 "/quit Exit this program.\n"
121 fprintf(stderr, "Unknown command '/%s'\n", buf);
124 static void parse_input(meshlink_handle_t *mesh, char *buf) {
125 static meshlink_node_t *destination;
135 if(len && buf[len - 1] == '\n')
138 if(len && buf[len - 1] == '\r')
141 // Ignore empty lines.
146 // Commands start with '/'
149 return parse_command(mesh, buf + 1);
151 // Lines in the form "name: message..." set the destination node.
154 char *colon = strchr(buf, ':');
162 destination = meshlink_get_node(mesh, buf);
164 fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
170 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
174 if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) {
175 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
179 printf("Message sent to '%s'.\n", destination->name);
182 int main(int argc, char *argv[]) {
183 const char *confbase = ".chat";
184 const char *nick = NULL;
193 meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
195 meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
197 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
201 meshlink_set_receive_cb(mesh, receive);
202 meshlink_set_node_status_cb(mesh, node_status);
203 meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
205 if(!meshlink_start(mesh)) {
206 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
210 printf("Chat started.\nType /help for a list of commands.\n");
212 while(fgets(buf, sizeof(buf), stdin))
213 parse_input(mesh, buf);
215 printf("Chat stopping.\n");
218 meshlink_close(mesh);