5 #include "../src/meshlink++.h"
7 static void log_message(meshlink::mesh *mesh, meshlink::log_level_t level, const char *text) {
8 const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"};
9 fprintf(stderr, "%s: %s\n", levelstr[level], text);
12 static void receive(meshlink::mesh *mesh, meshlink::node *source, const void *data, size_t len) {
13 const char *msg = (const char *)data;
15 if(!len || msg[len - 1]) {
16 fprintf(stderr, "Received invalid data from %s\n", source->name);
20 printf("%s says: %s\n", source->name, msg);
23 static void node_status(meshlink::mesh *mesh, meshlink::node *node, bool reachable) {
25 printf("%s joined.\n", node->name);
27 printf("%s left.\n", node->name);
30 static void parse_command(meshlink::mesh *mesh, char *buf) {
31 char *arg = strchr(buf, ' ');
35 if(!strcasecmp(buf, "invite")) {
39 fprintf(stderr, "/invite requires an argument!\n");
43 invitation = mesh->invite(arg);
45 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
49 printf("Invitation for %s: %s\n", arg, invitation);
51 } else if(!strcasecmp(buf, "join")) {
53 fprintf(stderr, "/join requires an argument!\n");
58 fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
60 fprintf(stderr, "Invitation accepted!\n");
61 } else if(!strcasecmp(buf, "kick")) {
63 fprintf(stderr, "/kick requires an argument!\n");
67 meshlink::node *node = mesh->get_node(arg);
69 fprintf(stderr, "Unknown node '%s'\n", arg);
73 mesh->blacklist(node);
75 printf("Node '%s' blacklisted.\n", arg);
76 } else if(!strcasecmp(buf, "who")) {
78 meshlink::node *nodes[100];
79 size_t n = mesh->get_all_nodes(nodes, 100);
81 fprintf(stderr, "No nodes known!\n");
83 printf("Known nodes:");
84 for(size_t i = 0; i < n && i < 100; i++)
85 printf(" %s", nodes[i]->name);
87 printf(" (and %zu more)", n - 100);
91 meshlink::node *node = mesh->get_node(arg);
93 fprintf(stderr, "Unknown node '%s'\n", arg);
95 printf("Node %s found\n", arg);
98 } else if(!strcasecmp(buf, "quit")) {
101 } else if(!strcasecmp(buf, "help")) {
103 "<name>: <message> Send a message to the given node.\n"
104 " Subsequent messages don't need the <name>: prefix.\n"
105 "/invite <name> Create an invitation for a new node.\n"
106 "/join <invitation> Join an existing mesh using an invitation.\n"
107 "/kick <name> Blacklist the given node.\n"
108 "/who [<name>] List all nodes or show information about the given node.\n"
109 "/quit Exit this program.\n"
112 fprintf(stderr, "Unknown command '/%s'\n", buf);
116 static void parse_input(meshlink::mesh *mesh, char *buf) {
117 static meshlink::node *destination;
127 if(len && buf[len - 1] == '\n')
130 if(len && buf[len - 1] == '\r')
133 // Ignore empty lines.
138 // Commands start with '/'
141 return parse_command(mesh, buf + 1);
143 // Lines in the form "name: message..." set the destination node.
146 char *colon = strchr(buf, ':');
154 destination = mesh->get_node(buf);
156 fprintf(stderr, "Unknown node '%s'\n", buf);
162 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
166 if(!mesh->send(destination, msg, strlen(msg) + 1)) {
167 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror());
171 printf("Message sent to '%s'.\n", destination->name);
174 int main(int argc, char *argv[]) {
175 const char *confbase = ".chat";
176 const char *nick = NULL;
185 meshlink::mesh *mesh = meshlink::open(confbase, nick);
187 fprintf(stderr, "Could not open MeshLink!\n");
191 mesh->set_receive_cb(receive);
192 mesh->set_node_status_cb(node_status);
193 mesh->set_log_cb(MESHLINK_INFO, log_message);
196 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
200 printf("Chat started.\nType /help for a list of commands.\n");
202 while(fgets(buf, sizeof buf, stdin))
203 parse_input(mesh, buf);
205 printf("Chat stopping.\n");
208 meshlink::close(mesh);