5 #include "../src/meshlink.h"
7 static void log_message(meshlink_handle_t *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_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
13 const char *msg = 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_handle_t *mesh, meshlink_node_t *node, bool reachable) {
25 printf("%s joined.\n", node->name);
27 printf("%s left.\n", node->name);
30 static meshlink_node_t **nodes;
33 static void parse_command(meshlink_handle_t *mesh, char *buf) {
34 char *arg = strchr(buf, ' ');
38 if(!strcasecmp(buf, "invite")) {
42 fprintf(stderr, "/invite requires an argument!\n");
46 invitation = meshlink_invite(mesh, arg);
48 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
52 printf("Invitation for %s: %s\n", arg, invitation);
54 } else if(!strcasecmp(buf, "join")) {
56 fprintf(stderr, "/join requires an argument!\n");
60 if(!meshlink_join(mesh, arg))
61 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
63 fprintf(stderr, "Invitation accepted!\n");
64 } else if(!strcasecmp(buf, "kick")) {
66 fprintf(stderr, "/kick requires an argument!\n");
70 meshlink_node_t *node = meshlink_get_node(mesh, arg);
72 fprintf(stderr, "Unknown node '%s'\n", arg);
76 meshlink_blacklist(mesh, node);
78 printf("Node '%s' blacklisted.\n", arg);
79 } else if(!strcasecmp(buf, "who")) {
81 nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
83 fprintf(stderr, "No nodes known!\n");
85 printf("Known nodes:");
86 for(int i = 0; i < nnodes; i++)
87 printf(" %s", nodes[i]->name);
91 meshlink_node_t *node = meshlink_get_node(mesh, arg);
93 fprintf(stderr, "Unknown node '%s'\n", arg);
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_handle_t *mesh, char *buf) {
117 static meshlink_node_t *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 = meshlink_get_node(mesh, buf);
156 fprintf(stderr, "Unknown node '%s'\n", buf);
162 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
166 if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) {
167 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
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_handle_t *mesh = meshlink_open(confbase, nick);
187 fprintf(stderr, "Could not open MeshLink!\n");
191 meshlink_set_receive_cb(mesh, receive);
192 meshlink_set_node_status_cb(mesh, node_status);
193 meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
195 if(!meshlink_start(mesh)) {
196 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
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);