5 #include "../src/meshlink++.h"
7 class ChatMesh : public meshlink::mesh {
9 void log(meshlink::log_level_t level, const char *text) {
10 const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"};
11 fprintf(stderr, "%s: %s\n", levelstr[level], text);
14 void receive(meshlink::node *source, const void *data, size_t len) {
15 const char *msg = (const char *)data;
17 if(!len || msg[len - 1]) {
18 fprintf(stderr, "Received invalid data from %s\n", source->name);
22 printf("%s says: %s\n", source->name, msg);
25 void node_status(meshlink::node *node, bool reachable) {
27 printf("%s joined.\n", node->name);
29 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, ' ');
44 if(!strcasecmp(buf, "invite")) {
48 fprintf(stderr, "/invite requires an argument!\n");
52 invitation = mesh->invite(arg);
55 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
59 printf("Invitation for %s: %s\n", arg, invitation);
61 } else if(!strcasecmp(buf, "join")) {
63 fprintf(stderr, "/join requires an argument!\n");
69 if(!mesh->join(arg)) {
70 fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
72 fprintf(stderr, "Invitation accepted!\n");
76 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink::strerror());
79 } else if(!strcasecmp(buf, "kick")) {
81 fprintf(stderr, "/kick requires an argument!\n");
85 meshlink::node *node = mesh->get_node(arg);
88 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
92 mesh->blacklist(node);
94 printf("Node '%s' blacklisted.\n", arg);
95 } else if(!strcasecmp(buf, "who")) {
97 nodes = mesh->get_all_nodes(nodes, &nnodes);
100 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
102 printf("%zu known nodes:", nnodes);
104 for(size_t i = 0; i < nnodes; i++) {
105 printf(" %s", nodes[i]->name);
111 meshlink::node *node = mesh->get_node(arg);
114 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
116 printf("Node %s found\n", arg);
119 } else if(!strcasecmp(buf, "quit")) {
122 } else if(!strcasecmp(buf, "help")) {
124 "<name>: <message> Send a message to the given node.\n"
125 " Subsequent messages don't need the <name>: prefix.\n"
126 "/invite <name> Create an invitation for a new node.\n"
127 "/join <invitation> Join an existing mesh using an invitation.\n"
128 "/kick <name> Blacklist the given node.\n"
129 "/who [<name>] List all nodes or show information about the given node.\n"
130 "/quit Exit this program.\n"
133 fprintf(stderr, "Unknown command '/%s'\n", buf);
137 static void parse_input(meshlink::mesh *mesh, char *buf) {
138 static meshlink::node *destination;
149 if(len && buf[len - 1] == '\n') {
153 if(len && buf[len - 1] == '\r') {
157 // Ignore empty lines.
163 // Commands start with '/'
166 return parse_command(mesh, buf + 1);
169 // Lines in the form "name: message..." set the destination node.
172 char *colon = strchr(buf, ':');
182 destination = mesh->get_node(buf);
185 fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
191 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
195 if(!mesh->send(destination, msg, strlen(msg) + 1)) {
196 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror());
200 printf("Message sent to '%s'.\n", destination->name);
204 int main(int argc, char *argv[]) {
205 const char *confbase = ".chat";
206 const char *nick = NULL;
218 mesh.open(confbase, nick, "chatpp", DEV_CLASS_STATIONARY);
221 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
226 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
230 printf("Chat started.\nType /help for a list of commands.\n");
232 while(fgets(buf, sizeof(buf), stdin)) {
233 parse_input(&mesh, buf);
236 printf("Chat stopping.\n");