]> git.meshlink.io Git - meshlink/blob - examples/chat.c
Debugging for getifaddrs().
[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, "scan")) {
90                 meshlink_hint_network_change(mesh);
91         } else if(!strcasecmp(buf, "kick")) {
92                 if(!arg) {
93                         fprintf(stderr, "/kick requires an argument!\n");
94                         return;
95                 }
96
97                 meshlink_node_t *node = meshlink_get_node(mesh, arg);
98
99                 if(!node) {
100                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
101                         return;
102                 }
103
104                 if(!meshlink_blacklist(mesh, node)) {
105                         fprintf(stderr, "Error blacklising '%s': %s", arg, meshlink_strerror(meshlink_errno));
106                         return;
107                 }
108
109                 printf("Node '%s' blacklisted.\n", arg);
110         } else if(!strcasecmp(buf, "whitelist")) {
111                 if(!arg) {
112                         fprintf(stderr, "/whitelist requires an argument!\n");
113                         return;
114                 }
115
116                 meshlink_node_t *node = meshlink_get_node(mesh, arg);
117
118                 if(!node) {
119                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
120                         return;
121                 }
122
123                 if(!meshlink_whitelist(mesh, node)) {
124                         fprintf(stderr, "Error whitelising '%s': %s", arg, meshlink_strerror(meshlink_errno));
125                         return;
126                 }
127
128                 printf("Node '%s' whitelisted.\n", arg);
129         } else if(!strcasecmp(buf, "who")) {
130                 if(!arg) {
131                         nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
132
133                         if(!nnodes) {
134                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
135                         } else {
136                                 printf("%zu known nodes:", nnodes);
137
138                                 for(size_t i = 0; i < nnodes; i++) {
139                                         printf(" %s", nodes[i]->name);
140                                 }
141
142                                 printf("\n");
143                         }
144                 } else {
145                         meshlink_node_t *node = meshlink_get_node(mesh, arg);
146
147                         if(!node) {
148                                 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
149                         } else {
150                                 printf("Node %s found\n", arg);
151                         }
152                 }
153         } else if(!strcasecmp(buf, "quit")) {
154                 printf("Bye!\n");
155                 fclose(stdin);
156         } else if(!strcasecmp(buf, "help")) {
157                 printf(
158                         "<name>: <message>     Send a message to the given node.\n"
159                         "                      Subsequent messages don't need the <name>: prefix.\n"
160                         "/invite <name>        Create an invitation for a new node.\n"
161                         "/join <invitation>    Join an existing mesh using an invitation.\n"
162                         "/kick <name>          Blacklist the given node.\n"
163                         "/who [<name>]         List all nodes or show information about the given node.\n"
164                         "/quit                 Exit this program.\n"
165                 );
166         } else {
167                 fprintf(stderr, "Unknown command '/%s'\n", buf);
168         }
169 }
170
171 static void parse_input(meshlink_handle_t *mesh, char *buf) {
172         static meshlink_node_t *destination;
173         size_t len;
174
175         if(!buf) {
176                 return;
177         }
178
179         // Remove newline.
180
181         len = strlen(buf);
182
183         if(len && buf[len - 1] == '\n') {
184                 buf[--len] = 0;
185         }
186
187         if(len && buf[len - 1] == '\r') {
188                 buf[--len] = 0;
189         }
190
191         // Ignore empty lines.
192
193         if(!len) {
194                 return;
195         }
196
197         // Commands start with '/'
198
199         if(*buf == '/') {
200                 parse_command(mesh, buf + 1);
201                 return;
202         }
203
204         // Lines in the form "name: message..." set the destination node.
205
206         char *msg = buf;
207         char *colon = strchr(buf, ':');
208
209         if(colon) {
210                 *colon = 0;
211                 msg = colon + 1;
212
213                 if(*msg == ' ') {
214                         msg++;
215                 }
216
217                 destination = meshlink_get_node(mesh, buf);
218
219                 if(!destination) {
220                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
221                         return;
222                 }
223         }
224
225         if(!destination) {
226                 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
227                 return;
228         }
229
230         if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) {
231                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
232                 return;
233         }
234
235         printf("Message sent to '%s'.\n", destination->name);
236 }
237
238 int main(int argc, char *argv[]) {
239         const char *confbase = ".chat";
240         const char *nick = NULL;
241         char buf[1024];
242
243         if(argc > 1) {
244                 confbase = argv[1];
245         }
246
247         if(argc > 2) {
248                 nick = argv[2];
249         }
250
251         meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
252
253         meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
254
255         if(!mesh) {
256                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
257                 return 1;
258         }
259
260         meshlink_set_receive_cb(mesh, receive);
261         meshlink_set_node_status_cb(mesh, node_status);
262         meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
263
264         if(!meshlink_start(mesh)) {
265                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
266                 return 1;
267         }
268
269         printf("Chat started.\nType /help for a list of commands.\n");
270
271         while(fgets(buf, sizeof(buf), stdin)) {
272                 parse_input(mesh, buf);
273         }
274
275         printf("Chat stopping.\n");
276
277         meshlink_stop(mesh);
278         meshlink_close(mesh);
279
280         return 0;
281 }