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