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