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