]> git.meshlink.io Git - meshlink/blob - examples/groupchat.c
Add the /whitelist command to the chat examples.
[meshlink] / examples / groupchat.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5
6 #include "../src/meshlink.h"
7 #include "../src/devtools.h"
8
9 #define CHAT_PORT 531
10
11 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
12         (void) mesh;
13
14         static const char *levelstr[] = {
15                 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
16                 [MESHLINK_INFO] = "\x1b[32mINFO",
17                 [MESHLINK_WARNING] = "\x1b[33mWARNING",
18                 [MESHLINK_ERROR] = "\x1b[31mERROR",
19                 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
20         };
21         fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
22 }
23
24 static void channel_receive(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
25         if(!len) {
26                 if(meshlink_errno) {
27                         fprintf(stderr, "Error while reading data from %s: %s\n", channel->node->name, meshlink_strerror(meshlink_errno));
28                 } else {
29                         fprintf(stderr, "Chat connection closed by %s\n", channel->node->name);
30                 }
31
32                 channel->node->priv = NULL;
33                 meshlink_channel_close(mesh, channel);
34                 return;
35         }
36
37         // TODO: we now have TCP semantics, don't expect exactly one message per receive call.
38
39         fprintf(stderr, "%s says: ", channel->node->name);
40         fwrite(data, len, 1, stderr);
41         fputc('\n', stderr);
42 }
43
44 static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
45         (void)data;
46         (void)len;
47
48         // Only accept connections to the chat port
49         if(port != CHAT_PORT) {
50                 fprintf(stderr, "Rejected incoming channel from '%s' to port %u\n", channel->node->name, port);
51                 return false;
52         }
53
54         fprintf(stderr, "Accepted incoming channel from '%s'\n", channel->node->name);
55
56         // Remember the channel
57         channel->node->priv = channel;
58
59         // Set the receive callback
60         meshlink_set_channel_receive_cb(mesh, channel, channel_receive);
61
62         // Accept this channel
63         return true;
64 }
65
66 static void channel_poll(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
67         (void)len;
68
69         fprintf(stderr, "Channel to '%s' connected\n", channel->node->name);
70         meshlink_set_channel_poll_cb(mesh, channel, NULL);
71 }
72
73 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
74         (void)mesh;
75
76         if(reachable) {
77                 fprintf(stderr, "%s joined.\n", node->name);
78         } else {
79                 fprintf(stderr, "%s left.\n", node->name);
80         }
81 }
82
83 static meshlink_node_t **nodes;
84 static size_t nnodes;
85
86 static void parse_command(meshlink_handle_t *mesh, char *buf) {
87         char *arg = strchr(buf, ' ');
88         char *arg1 = NULL;
89         meshlink_submesh_t *s = NULL;
90         static meshlink_submesh_t **submesh = NULL;
91         static size_t nmemb = 0;
92         size_t i;
93
94         if(arg) {
95                 *arg++ = 0;
96
97                 arg1 = strchr(arg, ' ');
98
99                 if(arg1) {
100                         *arg1++ = 0;
101                 }
102
103         }
104
105         if(!strcasecmp(buf, "invite")) {
106                 char *invitation;
107
108                 if(!arg) {
109                         fprintf(stderr, "/invite requires an argument!\n");
110                         return;
111                 }
112
113                 if(arg1) {
114
115                         submesh = devtool_get_all_submeshes(mesh, submesh, &nmemb);
116
117                         if(!submesh || !nmemb) {
118                                 fprintf(stderr, "Group does not exist!\n");
119                                 return;
120                         }
121
122                         for(i = 0; i < nmemb; i++) {
123                                 if(!strcmp(arg1, submesh[i]->name)) {
124                                         s = submesh[i];
125                                         break;
126                                 } else {
127                                         s = NULL;
128                                 }
129                         }
130
131                         if(!s) {
132                                 fprintf(stderr, "Group is not yet creted!\n");
133                                 return;
134                         }
135                 }
136
137                 invitation = meshlink_invite(mesh, s, arg);
138
139                 if(!invitation) {
140                         fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
141                         return;
142                 }
143
144                 fprintf(stderr, "Invitation for %s: %s\n", arg, invitation);
145                 free(invitation);
146         } else if(!strcasecmp(buf, "canonical")) {
147                 bool set;
148                 char *host = NULL, *port = NULL;
149
150                 if(!arg) {
151                         fprintf(stderr, "/canonical requires an argument!\n");
152                         return;
153                 }
154
155                 if((0 == strncasecmp(arg, "-h", 2)) && (strlen(arg) > 2)) {
156                         host = arg + 2;
157                 } else if((0 == strncasecmp(arg, "-p", 2)) && (strlen(arg) > 2)) {
158                         port = arg + 2;
159                 } else {
160                         fprintf(stderr, "Unknown argument: %s!\n", arg);
161                         return;
162                 }
163
164                 if(arg1) {
165                         if((0 == strncasecmp(arg1, "-h", 2)) && (strlen(arg1) > 2)) {
166                                 host = arg1 + 2;
167                         } else if((0 == strncasecmp(arg1, "-p", 2)) && (strlen(arg1) > 2)) {
168                                 port = arg1 + 2;
169                         } else {
170                                 fprintf(stderr, "Unknown argument: %s!\n", arg1);
171                                 return;
172                         }
173                 }
174
175                 if(!host && !port) {
176                         fprintf(stderr, "Unable to set Canonical address because no valid arguments are found!\n");
177                         return;
178                 }
179
180                 set = meshlink_set_canonical_address(mesh, meshlink_get_self(mesh), host, port);
181
182                 if(!set) {
183                         fprintf(stderr, "Could not set canonical address '%s:%s': %s\n", host, port, meshlink_strerror(meshlink_errno));
184                         return;
185                 }
186
187                 fprintf(stderr, "Canonical address set as '%s:%s'\n", host, port);
188         } else if(!strcasecmp(buf, "group")) {
189                 if(!arg) {
190                         fprintf(stderr, "/group requires an argument!\n");
191                         return;
192                 }
193
194                 if(!(s = meshlink_submesh_open(mesh, arg))) {
195                         fprintf(stderr, "Could not create group: %s\n", meshlink_strerror(meshlink_errno));
196                 } else {
197                         fprintf(stderr, "Group '%s' created!\n", s->name);
198                 }
199         } else if(!strcasecmp(buf, "join")) {
200                 if(!arg) {
201                         fprintf(stderr, "/join requires an argument!\n");
202                         return;
203                 }
204
205                 meshlink_stop(mesh);
206
207                 if(!meshlink_join(mesh, arg)) {
208                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
209                 } else {
210                         fprintf(stderr, "Invitation accepted!\n");
211                 }
212
213                 if(!meshlink_start(mesh)) {
214                         fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
215                         exit(1);
216                 }
217         } else if(!strcasecmp(buf, "kick")) {
218                 if(!arg) {
219                         fprintf(stderr, "/kick requires an argument!\n");
220                         return;
221                 }
222
223                 meshlink_node_t *node = meshlink_get_node(mesh, arg);
224
225                 if(!node) {
226                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
227                         return;
228                 }
229
230                 meshlink_blacklist(mesh, node);
231
232                 fprintf(stderr, "Node '%s' blacklisted.\n", arg);
233         } else if(!strcasecmp(buf, "whitelist")) {
234                 if(!arg) {
235                         fprintf(stderr, "/whitelist requires an argument!\n");
236                         return;
237                 }
238
239                 meshlink_node_t *node = meshlink_get_node(mesh, arg);
240
241                 if(!node) {
242                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
243                         return;
244                 }
245
246                 meshlink_whitelist(mesh, node);
247
248                 fprintf(stderr, "Node '%s' whitelisted.\n", arg);
249         } else if(!strcasecmp(buf, "who")) {
250                 meshlink_submesh_t *node_group = NULL;
251
252                 if(!arg) {
253                         nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
254
255                         if(!nnodes) {
256                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
257                         } else {
258                                 fprintf(stderr, "%lu known nodes:\n", (unsigned long)nnodes);
259
260                                 for(size_t i = 0; i < nnodes; i++) {
261                                         fprintf(stderr, " %lu. %s", (unsigned long)i, nodes[i]->name);
262
263                                         if((node_group = meshlink_get_node_submesh(mesh, nodes[i]))) {
264                                                 fprintf(stderr, "\t%s", node_group->name);
265                                         }
266
267                                         fprintf(stderr, "\n");
268                                 }
269
270                                 fprintf(stderr, "\n");
271                         }
272                 } else {
273                         meshlink_node_t *node = meshlink_get_node(mesh, arg);
274
275                         if(!node) {
276                                 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
277                         } else {
278                                 fprintf(stderr, "Node %s found", arg);
279
280                                 if((node_group = meshlink_get_node_submesh(mesh, node))) {
281                                         fprintf(stderr, " in group %s", node_group->name);
282                                 }
283
284                                 fprintf(stderr, "\n");
285                         }
286                 }
287         } else if(!strcasecmp(buf, "listgroup")) {
288                 if(!arg) {
289                         fprintf(stderr, "/listgroup requires an argument!\n");
290                         return;
291                 }
292
293                 submesh = devtool_get_all_submeshes(mesh, submesh, &nmemb);
294
295                 if(!submesh || !nmemb) {
296                         fprintf(stderr, "Group does not exist!\n");
297                         return;
298                 }
299
300                 for(i = 0; i < nmemb; i++) {
301                         if(!strcmp(arg, submesh[i]->name)) {
302                                 s = submesh[i];
303                                 break;
304                         } else {
305                                 s = NULL;
306                         }
307                 }
308
309                 if(!s) {
310                         fprintf(stderr, "Group %s does not exist!\n", arg);
311                         return;
312                 }
313
314                 nodes = meshlink_get_all_nodes_by_submesh(mesh, s, nodes, &nnodes);
315
316                 if(!nodes) {
317                         fprintf(stderr, "Group %s does not contain any nodes!\n", arg);
318                         return;
319                 }
320
321                 if(nnodes <= 0) {
322                         fprintf(stderr, "Could not get list of nodes for group %s: %s\n", arg, meshlink_strerror(meshlink_errno));
323                 } else {
324                         fprintf(stderr, "%zu known nodes in group %s:", nnodes, arg);
325
326                         for(size_t i = 0; i < nnodes; i++) {
327                                 fprintf(stderr, " %s", nodes[i]->name);
328                         }
329
330                         fprintf(stderr, "\n");
331                 }
332         } else if(!strcasecmp(buf, "quit")) {
333                 fprintf(stderr, "Bye!\n");
334                 fclose(stdin);
335         } else if(!strcasecmp(buf, "help")) {
336                 fprintf(stderr,
337                         "<name>: <message>                        Send a message to the given node.\n"
338                         "                                         Subsequent messages don't need the <name>: prefix.\n"
339                         "/group <name>                            Create a new group"
340                         "/invite <name> [submesh]                 Create an invitation for a new node.\n"
341                         "                                         Node joins either coremesh or submesh depending on submesh parameter.\n"
342                         "/join <invitation>                               Join an existing mesh using an invitation.\n"
343                         "/kick <name>                             Blacklist the given node.\n"
344                         "/who [<name>]                            List all nodes or show information about the given node.\n"
345                         "/listgroup <name>                        List all nodes in a given group.\n"
346                         "/canonical -h<hostname> -p<port> Set Canonical address to be present in invitation.\n"
347                         "                                         Any one of two options an be specified. At least one option must be present\n"
348                         "/quit                                    Exit this program.\n"
349                        );
350         } else {
351                 fprintf(stderr, "Unknown command '/%s'\n", buf);
352         }
353 }
354
355 static void parse_input(meshlink_handle_t *mesh, char *buf) {
356         static meshlink_node_t *destination;
357         size_t len;
358
359         if(!buf) {
360                 return;
361         }
362
363         // Remove newline.
364
365         len = strlen(buf);
366
367         if(len && buf[len - 1] == '\n') {
368                 buf[--len] = 0;
369         }
370
371         if(len && buf[len - 1] == '\r') {
372                 buf[--len] = 0;
373         }
374
375         // Ignore empty lines.
376
377         if(!len) {
378                 return;
379         }
380
381         // Commands start with '/'
382
383         if(*buf == '/') {
384                 parse_command(mesh, buf + 1);
385                 return;
386         }
387
388         // Lines in the form "name: message..." set the destination node.
389
390         char *msg = buf;
391         char *colon = strchr(buf, ':');
392
393         if(colon) {
394                 *colon = 0;
395                 msg = colon + 1;
396
397                 if(*msg == ' ') {
398                         msg++;
399                 }
400
401                 destination = meshlink_get_node(mesh, buf);
402
403                 if(!destination) {
404                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
405                         return;
406                 }
407         }
408
409         if(!destination) {
410                 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
411                 return;
412         }
413
414         // We want to have one channel per node.
415         // We keep the pointer to the meshlink_channel_t in the priv field of that node.
416         meshlink_channel_t *channel = destination->priv;
417
418         if(!channel) {
419                 fprintf(stderr, "Opening chat channel to '%s'\n", destination->name);
420                 channel = meshlink_channel_open(mesh, destination, CHAT_PORT, channel_receive, NULL, 0);
421
422                 if(!channel) {
423                         fprintf(stderr, "Could not create channel to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
424                         return;
425                 }
426
427                 destination->priv = channel;
428                 meshlink_set_channel_poll_cb(mesh, channel, channel_poll);
429         }
430
431         if(!meshlink_channel_send(mesh, channel, msg, strlen(msg))) {
432                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
433                 return;
434         }
435
436         fprintf(stderr, "Message sent to '%s'.\n", destination->name);
437 }
438
439 int main(int argc, char *argv[]) {
440         const char *confbase = ".chat";
441         const char *nick = NULL;
442         char buf[1024];
443
444         if(argc > 1) {
445                 confbase = argv[1];
446         }
447
448         if(argc > 2) {
449                 nick = argv[2];
450         }
451
452         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message);
453
454         meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
455
456         if(!mesh) {
457                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
458                 return 1;
459         }
460
461         meshlink_set_node_status_cb(mesh, node_status);
462         meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
463
464         // Set the channel accept callback. This implicitly turns on channels for all nodes.
465         // This replaces the call to meshlink_set_receive_cb().
466         meshlink_set_channel_accept_cb(mesh, channel_accept);
467
468         if(!meshlink_start(mesh)) {
469                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
470                 return 1;
471         }
472
473         fprintf(stderr, "Chat started.\nType /help for a list of commands.\n");
474
475         while(fgets(buf, sizeof(buf), stdin)) {
476                 parse_input(mesh, buf);
477         }
478
479         fprintf(stderr, "Chat stopping.\n");
480
481         meshlink_stop(mesh);
482         meshlink_close(mesh);
483
484         return 0;
485 }