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