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