]> git.meshlink.io Git - meshlink/blob - examples/chatpp.cc
Convert sizeof foo to sizeof(foo).
[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 static meshlink::node **nodes;
34 static size_t nnodes;
35
36 static void parse_command(meshlink::mesh *mesh, char *buf) {
37         char *arg = strchr(buf, ' ');
38         if(arg)
39                 *arg++ = 0;
40
41         if(!strcasecmp(buf, "invite")) {
42                 char *invitation;
43
44                 if(!arg) {
45                         fprintf(stderr, "/invite requires an argument!\n");
46                         return;
47                 }
48
49                 invitation = mesh->invite(arg);
50                 if(!invitation) {
51                         fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink::strerror());
52                         return;
53                 }
54
55                 printf("Invitation for %s: %s\n", arg, invitation);
56                 free(invitation);
57         } else if(!strcasecmp(buf, "join")) {
58                 if(!arg) {
59                         fprintf(stderr, "/join requires an argument!\n");
60                         return;
61                 }
62
63                 mesh->stop();
64
65                 if(!mesh->join(arg))
66                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink::strerror());
67                 else
68                         fprintf(stderr, "Invitation accepted!\n");
69
70                 if(!mesh->start()) {
71                         fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink::strerror());
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 *node = mesh->get_node(arg);
81                 if(!node) {
82                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
83                         return;
84                 }
85
86                 mesh->blacklist(node);
87
88                 printf("Node '%s' blacklisted.\n", arg);
89         } else if(!strcasecmp(buf, "who")) {
90                 if(!arg) {
91                         nodes = mesh->get_all_nodes(nodes, &nnodes);
92                         if(!nodes)
93                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink::strerror());
94                         else {
95                                 printf("%zu known nodes:", nnodes);
96                                 for(size_t i = 0; i < nnodes; i++)
97                                         printf(" %s", nodes[i]->name);
98                                 printf("\n");
99                         }
100                 } else {
101                         meshlink::node *node = mesh->get_node(arg);
102                         if(!node)
103                                 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink::strerror());
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::mesh *mesh, char *buf) {
125         static meshlink::node *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 = mesh->get_node(buf);
163                 if(!destination) {
164                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink::strerror());
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(!mesh->send(destination, msg, strlen(msg) + 1)) {
175                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink::strerror());
176                 return;
177         }
178
179         printf("Message sent to '%s'.\n", destination->name);
180 }
181
182
183 int main(int argc, char *argv[]) {
184         const char *confbase = ".chat";
185         const char *nick = NULL;
186         char buf[1024];
187
188         if(argc > 1)
189                 confbase = argv[1];
190
191         if(argc > 2)
192                 nick = argv[2];
193
194         ChatMesh mesh;
195         mesh.open(confbase, nick, "chatpp", DEV_CLASS_STATIONARY);
196
197         if(!mesh.isOpen()) {
198                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink::strerror());
199                 return 1;
200         }
201
202         if(!mesh.start()) {
203                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink::strerror());
204                 return 1;
205         }
206
207         printf("Chat started.\nType /help for a list of commands.\n");
208
209         while(fgets(buf, sizeof(buf), stdin))
210                 parse_input(&mesh, buf);
211
212         printf("Chat stopping.\n");
213
214         mesh.stop();
215         mesh.close();
216
217         return 0;
218 }