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);
33 static meshlink::node **nodes;
36 static void parse_command(meshlink::mesh *mesh, char *buf) {
37 char *arg = strchr(buf, ' ');
41 if(!strcasecmp(buf, "invite")) {
45 fprintf(stderr, "/invite requires an argument!\n");
49 invitation = mesh->invite(arg);
51 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
55 printf("Invitation for %s: %s\n", arg, invitation);
57 } else if(!strcasecmp(buf, "join")) {
59 fprintf(stderr, "/join requires an argument!\n");
66 fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
68 fprintf(stderr, "Invitation accepted!\n");
71 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink::strerror());
74 } else if(!strcasecmp(buf, "kick")) {
76 fprintf(stderr, "/kick requires an argument!\n");
80 meshlink::node *node = mesh->get_node(arg);
82 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
86 mesh->blacklist(node);
88 printf("Node '%s' blacklisted.\n", arg);
89 } else if(!strcasecmp(buf, "who")) {
91 nodes = mesh->get_all_nodes(nodes, &nnodes);
93 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
95 printf("%zu known nodes:", nnodes);
96 for(size_t i = 0; i < nnodes; i++)
97 printf(" %s", nodes[i]->name);
101 meshlink::node *node = mesh->get_node(arg);
103 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
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::mesh *mesh, char *buf) {
125 static meshlink::node *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 = mesh->get_node(buf);
164 fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
170 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
174 if(!mesh->send(destination, msg, strlen(msg) + 1)) {
175 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror());
179 printf("Message sent to '%s'.\n", destination->name);
183 int main(int argc, char *argv[]) {
184 const char *confbase = ".chat";
185 const char *nick = NULL;
195 mesh.open(confbase, nick, "chatpp", DEV_CLASS_STATIONARY);
198 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
203 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
207 printf("Chat started.\nType /help for a list of commands.\n");
209 while(fgets(buf, sizeof(buf), stdin))
210 parse_input(&mesh, buf);
212 printf("Chat stopping.\n");