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");
65 fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
67 fprintf(stderr, "Invitation accepted!\n");
68 } else if(!strcasecmp(buf, "kick")) {
70 fprintf(stderr, "/kick requires an argument!\n");
74 meshlink::node *node = mesh->get_node(arg);
76 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
80 mesh->blacklist(node);
82 printf("Node '%s' blacklisted.\n", arg);
83 } else if(!strcasecmp(buf, "who")) {
85 nodes = mesh->get_all_nodes(nodes, &nnodes);
87 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
89 printf("%zu known nodes:", nnodes);
90 for(size_t i = 0; i < nnodes; i++)
91 printf(" %s", nodes[i]->name);
95 meshlink::node *node = mesh->get_node(arg);
97 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
99 printf("Node %s found\n", arg);
102 } else if(!strcasecmp(buf, "quit")) {
105 } else if(!strcasecmp(buf, "help")) {
107 "<name>: <message> Send a message to the given node.\n"
108 " Subsequent messages don't need the <name>: prefix.\n"
109 "/invite <name> Create an invitation for a new node.\n"
110 "/join <invitation> Join an existing mesh using an invitation.\n"
111 "/kick <name> Blacklist the given node.\n"
112 "/who [<name>] List all nodes or show information about the given node.\n"
113 "/quit Exit this program.\n"
116 fprintf(stderr, "Unknown command '/%s'\n", buf);
120 static void parse_input(meshlink::mesh *mesh, char *buf) {
121 static meshlink::node *destination;
131 if(len && buf[len - 1] == '\n')
134 if(len && buf[len - 1] == '\r')
137 // Ignore empty lines.
142 // Commands start with '/'
145 return parse_command(mesh, buf + 1);
147 // Lines in the form "name: message..." set the destination node.
150 char *colon = strchr(buf, ':');
158 destination = mesh->get_node(buf);
160 fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
166 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
170 if(!mesh->send(destination, msg, strlen(msg) + 1)) {
171 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror());
175 printf("Message sent to '%s'.\n", destination->name);
179 int main(int argc, char *argv[]) {
180 const char *confbase = ".chat";
181 const char *nick = NULL;
190 ChatMesh* mesh = meshlink::open<ChatMesh>(confbase, nick, "chatpp");
193 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
198 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
202 printf("Chat started.\nType /help for a list of commands.\n");
204 while(fgets(buf, sizeof buf, stdin))
205 parse_input(mesh, buf);
207 printf("Chat stopping.\n");
210 meshlink::close(mesh);