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