]> git.meshlink.io Git - meshlink/blob - examples/chat.c
Never automatically try to bind to ports >= 32768.
[meshlink] / examples / chat.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include "../src/meshlink.h"
6
7 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
8         (void)mesh;
9
10         static 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",
16         };
17
18         fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
19 }
20
21 static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
22         (void)mesh;
23
24         const char *msg = data;
25
26         if(!len || msg[len - 1]) {
27                 fprintf(stderr, "Received invalid data from %s\n", source->name);
28                 return;
29         }
30
31         printf("%s says: %s\n", source->name, msg);
32 }
33
34 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
35         (void)mesh;
36
37         if(reachable) {
38                 printf("%s joined.\n", node->name);
39         } else {
40                 printf("%s left.\n", node->name);
41         }
42 }
43
44 static meshlink_node_t **nodes;
45 static size_t nnodes;
46
47 static void parse_command(meshlink_handle_t *mesh, char *buf) {
48         char *arg = strchr(buf, ' ');
49
50         if(arg) {
51                 *arg++ = 0;
52         }
53
54         if(!strcasecmp(buf, "invite")) {
55                 char *invitation;
56
57                 if(!arg) {
58                         fprintf(stderr, "/invite requires an argument!\n");
59                         return;
60                 }
61
62                 invitation = meshlink_invite(mesh, NULL, arg);
63
64                 if(!invitation) {
65                         fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
66                         return;
67                 }
68
69                 printf("Invitation for %s: %s\n", arg, invitation);
70                 free(invitation);
71         } else if(!strcasecmp(buf, "join")) {
72                 if(!arg) {
73                         fprintf(stderr, "/join requires an argument!\n");
74                         return;
75                 }
76
77                 meshlink_stop(mesh);
78
79                 if(!meshlink_join(mesh, arg)) {
80                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
81                 } else {
82                         fprintf(stderr, "Invitation accepted!\n");
83                 }
84
85                 if(!meshlink_start(mesh)) {
86                         fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
87                         exit(1);
88                 }
89         } else if(!strcasecmp(buf, "kick")) {
90                 if(!arg) {
91                         fprintf(stderr, "/kick requires an argument!\n");
92                         return;
93                 }
94
95                 meshlink_node_t *node = meshlink_get_node(mesh, arg);
96
97                 if(!node) {
98                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
99                         return;
100                 }
101
102                 if(!meshlink_blacklist(mesh, node)) {
103                         fprintf(stderr, "Error blacklising '%s': %s", arg, meshlink_strerror(meshlink_errno));
104                         return;
105                 }
106
107                 printf("Node '%s' blacklisted.\n", arg);
108         } else if(!strcasecmp(buf, "whitelist")) {
109                 if(!arg) {
110                         fprintf(stderr, "/whitelist requires an argument!\n");
111                         return;
112                 }
113
114                 meshlink_node_t *node = meshlink_get_node(mesh, arg);
115
116                 if(!node) {
117                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
118                         return;
119                 }
120
121                 if(!meshlink_whitelist(mesh, node)) {
122                         fprintf(stderr, "Error whitelising '%s': %s", arg, meshlink_strerror(meshlink_errno));
123                         return;
124                 }
125
126                 printf("Node '%s' whitelisted.\n", arg);
127         } else if(!strcasecmp(buf, "who")) {
128                 if(!arg) {
129                         nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
130
131                         if(!nnodes) {
132                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
133                         } else {
134                                 printf("%zu known nodes:", nnodes);
135
136                                 for(size_t i = 0; i < nnodes; i++) {
137                                         printf(" %s", nodes[i]->name);
138                                 }
139
140                                 printf("\n");
141                         }
142                 } else {
143                         meshlink_node_t *node = meshlink_get_node(mesh, arg);
144
145                         if(!node) {
146                                 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
147                         } else {
148                                 printf("Node %s found\n", arg);
149                         }
150                 }
151         } else if(!strcasecmp(buf, "quit")) {
152                 printf("Bye!\n");
153                 fclose(stdin);
154         } else if(!strcasecmp(buf, "help")) {
155                 printf(
156                         "<name>: <message>     Send a message to the given node.\n"
157                         "                      Subsequent messages don't need the <name>: prefix.\n"
158                         "/invite <name>        Create an invitation for a new node.\n"
159                         "/join <invitation>    Join an existing mesh using an invitation.\n"
160                         "/kick <name>          Blacklist the given node.\n"
161                         "/who [<name>]         List all nodes or show information about the given node.\n"
162                         "/quit                 Exit this program.\n"
163                 );
164         } else {
165                 fprintf(stderr, "Unknown command '/%s'\n", buf);
166         }
167 }
168
169 static void parse_input(meshlink_handle_t *mesh, char *buf) {
170         static meshlink_node_t *destination;
171         size_t len;
172
173         if(!buf) {
174                 return;
175         }
176
177         // Remove newline.
178
179         len = strlen(buf);
180
181         if(len && buf[len - 1] == '\n') {
182                 buf[--len] = 0;
183         }
184
185         if(len && buf[len - 1] == '\r') {
186                 buf[--len] = 0;
187         }
188
189         // Ignore empty lines.
190
191         if(!len) {
192                 return;
193         }
194
195         // Commands start with '/'
196
197         if(*buf == '/') {
198                 parse_command(mesh, buf + 1);
199                 return;
200         }
201
202         // Lines in the form "name: message..." set the destination node.
203
204         char *msg = buf;
205         char *colon = strchr(buf, ':');
206
207         if(colon) {
208                 *colon = 0;
209                 msg = colon + 1;
210
211                 if(*msg == ' ') {
212                         msg++;
213                 }
214
215                 destination = meshlink_get_node(mesh, buf);
216
217                 if(!destination) {
218                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
219                         return;
220                 }
221         }
222
223         if(!destination) {
224                 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
225                 return;
226         }
227
228         if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) {
229                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
230                 return;
231         }
232
233         printf("Message sent to '%s'.\n", destination->name);
234 }
235
236 int main(int argc, char *argv[]) {
237         const char *confbase = ".chat";
238         const char *nick = NULL;
239         char buf[1024];
240
241         if(argc > 1) {
242                 confbase = argv[1];
243         }
244
245         if(argc > 2) {
246                 nick = argv[2];
247         }
248
249         meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
250
251         meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
252
253         if(!mesh) {
254                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
255                 return 1;
256         }
257
258         meshlink_set_receive_cb(mesh, receive);
259         meshlink_set_node_status_cb(mesh, node_status);
260         meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
261
262         if(!meshlink_start(mesh)) {
263                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
264                 return 1;
265         }
266
267         printf("Chat started.\nType /help for a list of commands.\n");
268
269         while(fgets(buf, sizeof(buf), stdin)) {
270                 parse_input(mesh, buf);
271         }
272
273         printf("Chat stopping.\n");
274
275         meshlink_stop(mesh);
276         meshlink_close(mesh);
277
278         return 0;
279 }