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