4 #include "../src/meshlink.h"
6 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
7 const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"};
8 fprintf(stderr, "%s: %s\n", levelstr[level], text);
11 static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
12 const char *msg = data;
14 if(!len || msg[len - 1]) {
15 fprintf(stderr, "Received invalid data from %s\n", source->name);
19 printf("%s says: %s\n", source->name, msg);
22 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
24 printf("%s joined.\n", node->name);
26 printf("%s left.\n", node->name);
29 static void parse_command(meshlink_handle_t *mesh, char *buf) {
30 char *arg = strchr(buf, ' ');
34 if(!strcasecmp(buf, "invite")) {
38 fprintf(stderr, "/invite requires an argument!\n");
42 invitation = meshlink_invite(mesh, arg);
44 fprintf(stderr, "Could not invite '%s': %s\n", arg, mesh->errstr);
48 printf("Invitation for %s: %s\n", arg, invitation);
50 } else if(!strcasecmp(buf, "join")) {
52 fprintf(stderr, "/join requires an argument!\n");
56 if(!meshlink_join(mesh, arg))
57 fprintf(stderr, "Could not join using invitation: %s\n", mesh->errstr);
59 fprintf(stderr, "Invitation accepted!\n");
60 } else if(!strcasecmp(buf, "kick")) {
62 fprintf(stderr, "/kick requires an argument!\n");
66 meshlink_node_t *node = meshlink_get_node(mesh, arg);
68 fprintf(stderr, "Unknown node '%s'\n", arg);
72 meshlink_blacklist(mesh, node);
74 printf("Node '%s' blacklisted.\n", arg);
75 } else if(!strcasecmp(buf, "who")) {
77 meshlink_node_t *nodes[100];
78 size_t n = meshlink_get_all_nodes(mesh, nodes, 100);
80 fprintf(stderr, "No nodes known!\n");
82 printf("Known nodes:");
83 for(int i = 0; i < n && i < 100; i++)
84 printf(" %s", nodes[i]->name);
86 printf(" (and %zu more)", n - 100);
90 meshlink_node_t *node = meshlink_get_node(mesh, arg);
92 fprintf(stderr, "Unknown node '%s'\n", arg);
94 printf("Node %s found\n", arg);
97 } else if(!strcasecmp(buf, "quit")) {
100 } else if(!strcasecmp(buf, "help")) {
102 "<name>: <message> Send a message to the given node.\n"
103 " Subsequent messages don't need the <name>: prefix.\n"
104 "/invite <name> Create an invitation for a new node.\n"
105 "/join <invitation> Join an existing mesh using an invitation.\n"
106 "/kick <name> Blacklist the given node.\n"
107 "/who [<name>] List all nodes or show information about the given node.\n"
108 "/quit Exit this program.\n"
111 fprintf(stderr, "Unknown command '/%s'\n", buf);
115 static void parse_input(meshlink_handle_t *mesh, char *buf) {
116 static meshlink_node_t *destination;
126 if(len && buf[len - 1] == '\n')
129 if(len && buf[len - 1] == '\r')
132 // Ignore empty lines.
137 // Commands start with '/'
140 return parse_command(mesh, buf + 1);
142 // Lines in the form "name: message..." set the destination node.
145 char *colon = strchr(buf, ':');
153 destination = meshlink_get_node(mesh, buf);
155 fprintf(stderr, "Unknown node '%s'\n", buf);
161 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
165 if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) {
166 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, mesh->errstr);
170 printf("Message sent to '%s'.\n", destination->name);
173 int main(int argc, char *argv[]) {
174 const char *confbase = ".chat";
175 const char *nick = NULL;
184 meshlink_handle_t *mesh = meshlink_open(confbase, nick);
186 fprintf(stderr, "Could not open MeshLink!\n");
190 meshlink_set_receive_cb(mesh, receive);
191 meshlink_set_node_status_cb(mesh, node_status);
192 meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
194 if(!meshlink_start(mesh)) {
195 fprintf(stderr, "Could not start MeshLink: %s\n", mesh->errstr);
199 printf("Chat started.\nType /help for a list of commands.\n");
201 while(fgets(buf, sizeof buf, stdin))
202 parse_input(mesh, buf);
204 printf("Chat stopping.\n");
207 meshlink_close(mesh);