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