]> git.meshlink.io Git - meshlink-tiny/blob - examples/chatpp.cc
Remove support for meshlink_invite().
[meshlink-tiny] / 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, "join")) {
45                 if(!arg) {
46                         fprintf(stderr, "/join requires an argument!\n");
47                         return;
48                 }
49
50                 mesh->stop();
51
52                 if(!mesh->join(arg)) {
53                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
54                 } else {
55                         fprintf(stderr, "Invitation accepted!\n");
56                 }
57
58                 if(!mesh->start()) {
59                         fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink::strerror());
60                         exit(1);
61                 }
62         } else if(!strcasecmp(buf, "kick")) {
63                 if(!arg) {
64                         fprintf(stderr, "/kick requires an argument!\n");
65                         return;
66                 }
67
68                 meshlink::node *node = mesh->get_node(arg);
69
70                 if(!node) {
71                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
72                         return;
73                 }
74
75                 mesh->blacklist(node);
76
77                 printf("Node '%s' blacklisted.\n", arg);
78         } else if(!strcasecmp(buf, "who")) {
79                 if(!arg) {
80                         nodes = mesh->get_all_nodes(nodes, &nnodes);
81
82                         if(!nodes) {
83                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
84                         } else {
85                                 printf("%lu known nodes:", (unsigned long)nnodes);
86
87                                 for(size_t i = 0; i < nnodes; i++) {
88                                         printf(" %s", nodes[i]->name);
89                                 }
90
91                                 printf("\n");
92                         }
93                 } else {
94                         meshlink::node *node = mesh->get_node(arg);
95
96                         if(!node) {
97                                 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
98                         } else {
99                                 printf("Node %s found\n", arg);
100                         }
101                 }
102         } else if(!strcasecmp(buf, "quit")) {
103                 printf("Bye!\n");
104                 fclose(stdin);
105         } else if(!strcasecmp(buf, "help")) {
106                 printf(
107                         "<name>: <message>     Send a message to the given node.\n"
108                         "                      Subsequent messages don't need the <name>: prefix.\n"
109                         "/join <invitation>    Join an existing mesh using an invitation.\n"
110                         "/kick <name>          Blacklist the given node.\n"
111                         "/who [<name>]         List all nodes or show information about the given node.\n"
112                         "/quit                 Exit this program.\n"
113                 );
114         } else {
115                 fprintf(stderr, "Unknown command '/%s'\n", buf);
116         }
117 }
118
119 static void parse_input(meshlink::mesh *mesh, char *buf) {
120         static meshlink::node *destination;
121         size_t len;
122
123         if(!buf) {
124                 return;
125         }
126
127         // Remove newline.
128
129         len = strlen(buf);
130
131         if(len && buf[len - 1] == '\n') {
132                 buf[--len] = 0;
133         }
134
135         if(len && buf[len - 1] == '\r') {
136                 buf[--len] = 0;
137         }
138
139         // Ignore empty lines.
140
141         if(!len) {
142                 return;
143         }
144
145         // Commands start with '/'
146
147         if(*buf == '/') {
148                 return parse_command(mesh, buf + 1);
149         }
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
160                 if(*msg == ' ') {
161                         msg++;
162                 }
163
164                 destination = mesh->get_node(buf);
165
166                 if(!destination) {
167                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
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(!mesh->send(destination, msg, strlen(msg) + 1)) {
178                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror());
179                 return;
180         }
181
182         printf("Message sent to '%s'.\n", destination->name);
183 }
184
185
186 int main(int argc, char *argv[]) {
187         const char *confbase = ".chat";
188         const char *nick = NULL;
189         char buf[1024];
190
191         if(argc > 1) {
192                 confbase = argv[1];
193         }
194
195         if(argc > 2) {
196                 nick = argv[2];
197         }
198
199         ChatMesh mesh;
200         mesh.open(confbase, nick, "chatpp", DEV_CLASS_STATIONARY);
201
202         if(!mesh.isOpen()) {
203                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
204                 return 1;
205         }
206
207         if(!mesh.start()) {
208                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
209                 return 1;
210         }
211
212         printf("Chat started.\nType /help for a list of commands.\n");
213
214         while(fgets(buf, sizeof(buf), stdin)) {
215                 parse_input(&mesh, buf);
216         }
217
218         printf("Chat stopping.\n");
219
220         mesh.stop();
221         mesh.close();
222
223         return 0;
224 }