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 meshlink::node **nodes;
33 static void parse_command(meshlink::mesh *mesh, char *buf) {
34 char *arg = strchr(buf, ' ');
38 if(!strcasecmp(buf, "invite")) {
42 fprintf(stderr, "/invite requires an argument!\n");
46 invitation = mesh->invite(arg);
48 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
52 printf("Invitation for %s: %s\n", arg, invitation);
54 } else if(!strcasecmp(buf, "join")) {
56 fprintf(stderr, "/join requires an argument!\n");
61 fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
63 fprintf(stderr, "Invitation accepted!\n");
64 } else if(!strcasecmp(buf, "kick")) {
66 fprintf(stderr, "/kick requires an argument!\n");
70 meshlink::node *node = mesh->get_node(arg);
72 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
76 mesh->blacklist(node);
78 printf("Node '%s' blacklisted.\n", arg);
79 } else if(!strcasecmp(buf, "who")) {
81 nodes = mesh->get_all_nodes(nodes, &nnodes);
83 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
85 printf("%d known nodes:", nnodes);
86 for(size_t i = 0; i < nnodes; i++)
87 printf(" %s", nodes[i]->name);
91 meshlink::node *node = mesh->get_node(arg);
93 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
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, "Error looking up '%s': %s\n", buf, meshlink::strerror());
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: %s\n", meshlink::strerror());
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);