5 #include "../src/meshlink.h"
9 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
10 const char *levelstr[] = {
11 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
12 [MESHLINK_INFO] = "\x1b[32mINFO",
13 [MESHLINK_WARNING] = "\x1b[33mWARNING",
14 [MESHLINK_ERROR] = "\x1b[31mERROR",
15 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
17 fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
20 static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
23 fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno));
25 fprintf(stderr, "Chat connection closed by %s\n", channel->node->name);
27 channel->node->priv = NULL;
28 meshlink_channel_close(mesh, channel);
32 // TODO: we now have TCP semantics, don't expect exactly one message per receive call.
34 printf("%s says: ", channel->node->name);
35 fwrite(data, len, 1, stdout);
39 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
40 // Only accept connections to the chat port
41 if(port != CHAT_PORT) {
42 fprintf(stderr, "Rejected incoming channel from '%s' to port %u\n", channel->node->name, port);
46 fprintf(stderr, "Accepted incoming channel from '%s'\n", channel->node->name);
48 // Remember the channel
49 channel->node->priv = channel;
51 // Set the receive callback
52 meshlink_set_channel_receive_cb(mesh, channel, channel_receive);
54 // Accept this channel
58 static void channel_poll(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
59 fprintf(stderr, "Channel to '%s' connected\n", channel->node->name);
60 meshlink_set_channel_poll_cb(mesh, channel, NULL);
63 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
65 printf("%s joined.\n", node->name);
67 printf("%s left.\n", node->name);
70 static meshlink_node_t **nodes;
73 static void parse_command(meshlink_handle_t *mesh, char *buf) {
74 char *arg = strchr(buf, ' ');
78 if(!strcasecmp(buf, "invite")) {
82 fprintf(stderr, "/invite requires an argument!\n");
86 invitation = meshlink_invite(mesh, arg);
88 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
92 printf("Invitation for %s: %s\n", arg, invitation);
94 } else if(!strcasecmp(buf, "join")) {
96 fprintf(stderr, "/join requires an argument!\n");
100 if(!meshlink_join(mesh, arg))
101 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
103 fprintf(stderr, "Invitation accepted!\n");
104 if(!meshlink_start(mesh)) {
105 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
108 } else if(!strcasecmp(buf, "kick")) {
110 fprintf(stderr, "/kick requires an argument!\n");
114 meshlink_node_t *node = meshlink_get_node(mesh, arg);
116 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
120 meshlink_blacklist(mesh, node);
122 printf("Node '%s' blacklisted.\n", arg);
123 } else if(!strcasecmp(buf, "who")) {
125 nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
127 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
129 printf("%zu known nodes:", nnodes);
130 for(int i = 0; i < nnodes; i++)
131 printf(" %s", nodes[i]->name);
135 meshlink_node_t *node = meshlink_get_node(mesh, arg);
137 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
139 printf("Node %s found\n", arg);
141 } else if(!strcasecmp(buf, "quit")) {
144 } else if(!strcasecmp(buf, "help")) {
146 "<name>: <message> Send a message to the given node.\n"
147 " Subsequent messages don't need the <name>: prefix.\n"
148 "/invite <name> Create an invitation for a new node.\n"
149 "/join <invitation> Join an existing mesh using an invitation.\n"
150 "/kick <name> Blacklist the given node.\n"
151 "/who [<name>] List all nodes or show information about the given node.\n"
152 "/quit Exit this program.\n"
155 fprintf(stderr, "Unknown command '/%s'\n", buf);
158 static void parse_input(meshlink_handle_t *mesh, char *buf) {
159 static meshlink_node_t *destination;
169 if(len && buf[len - 1] == '\n')
172 if(len && buf[len - 1] == '\r')
175 // Ignore empty lines.
180 // Commands start with '/'
183 return parse_command(mesh, buf + 1);
185 // Lines in the form "name: message..." set the destination node.
188 char *colon = strchr(buf, ':');
196 destination = meshlink_get_node(mesh, buf);
198 fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
204 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
208 // We want to have one channel per node.
209 // We keep the pointer to the meshlink_channel_t in the priv field of that node.
210 meshlink_channel_t *channel = destination->priv;
213 fprintf(stderr, "Opening chat channel to '%s'\n", destination->name);
214 channel = meshlink_channel_open(mesh, destination, CHAT_PORT, channel_receive, NULL, 0);
216 fprintf(stderr, "Could not create channel to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
219 destination->priv = channel;
220 meshlink_set_channel_poll_cb(mesh, channel, channel_poll);
223 if(!meshlink_channel_send(mesh, channel, msg, strlen(msg))) {
224 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
228 printf("Message sent to '%s'.\n", destination->name);
231 int main(int argc, char *argv[]) {
232 const char *confbase = ".chat";
233 const char *nick = NULL;
242 meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
244 meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
246 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
250 meshlink_set_node_status_cb(mesh, node_status);
251 meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
253 // Set the channel accept callback. This implicitly turns on channels for all nodes.
254 // This replaces the call to meshlink_set_receive_cb().
255 meshlink_set_channel_accept_cb(mesh, channel_accept);
257 if(!meshlink_start(mesh)) {
258 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
262 printf("Chat started.\nType /help for a list of commands.\n");
264 while(fgets(buf, sizeof buf, stdin))
265 parse_input(mesh, buf);
267 printf("Chat stopping.\n");
270 meshlink_close(mesh);