5 #include "../src/meshlink++.h"
7 class ChatMesh : public meshlink::mesh
10 void log(meshlink::log_level_t level, const char *text) {
11 const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"};
12 fprintf(stderr, "%s: %s\n", levelstr[level], text);
15 void receive(meshlink::node *source, const void *data, size_t len) {
16 const char *msg = (const char *)data;
18 if(!len || msg[len - 1]) {
19 fprintf(stderr, "Received invalid data from %s\n", source->name);
23 printf("%s says: %s\n", source->name, msg);
26 void node_status(meshlink::node *node, bool reachable) {
28 printf("%s joined.\n", node->name);
30 printf("%s left.\n", node->name);
34 static meshlink::node **nodes;
37 static void parse_command(meshlink::mesh *mesh, char *buf) {
38 char *arg = strchr(buf, ' ');
42 if(!strcasecmp(buf, "invite")) {
46 fprintf(stderr, "/invite requires an argument!\n");
50 invitation = mesh->invite(arg);
52 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
56 printf("Invitation for %s: %s\n", arg, invitation);
58 } else if(!strcasecmp(buf, "join")) {
60 fprintf(stderr, "/join requires an argument!\n");
67 fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
69 fprintf(stderr, "Invitation accepted!\n");
72 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink::strerror());
75 } else if(!strcasecmp(buf, "kick")) {
77 fprintf(stderr, "/kick requires an argument!\n");
81 meshlink::node *node = mesh->get_node(arg);
83 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
87 mesh->blacklist(node);
89 printf("Node '%s' blacklisted.\n", arg);
90 } else if(!strcasecmp(buf, "who")) {
92 nodes = mesh->get_all_nodes(nodes, &nnodes);
94 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
96 printf("%zu known nodes:", nnodes);
97 for(size_t i = 0; i < nnodes; i++)
98 printf(" %s", nodes[i]->name);
102 meshlink::node *node = mesh->get_node(arg);
104 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
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::mesh *mesh, char *buf) {
128 static meshlink::node *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 = mesh->get_node(buf);
167 fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
173 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
177 if(!mesh->send(destination, msg, strlen(msg) + 1)) {
178 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror());
182 printf("Message sent to '%s'.\n", destination->name);
186 int main(int argc, char *argv[]) {
187 const char *confbase = ".chat";
188 const char *nick = NULL;
198 mesh.open(confbase, nick, "chatpp", DEV_CLASS_STATIONARY);
201 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
206 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
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");