]> git.meshlink.io Git - meshlink/blob - examples/chat.c
ace1e783883a4176d9516e34efbc74147a336edb
[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
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         } else if(!strcasecmp(buf, "kick")) {
65                 if(!arg) {
66                         fprintf(stderr, "/kick requires an argument!\n");
67                         return;
68                 }
69
70                 meshlink_node_t *node = meshlink_get_node(mesh, arg);
71                 if(!node) {
72                         fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
73                         return;
74                 }
75
76                 meshlink_blacklist(mesh, node);
77
78                 printf("Node '%s' blacklisted.\n", arg);
79         } else if(!strcasecmp(buf, "who")) {
80                 if(!arg) {
81                         nodes = meshlink_get_all_nodes(mesh, nodes, &nnodes);
82                         if(!nnodes) {
83                                 fprintf(stderr, "Could not get list of nodes: %s\n", meshlink_strerror(meshlink_errno));
84                         } else {
85                                 printf("%zu known nodes:", nnodes);
86                                 for(int i = 0; i < nnodes; i++)
87                                         printf(" %s", nodes[i]->name);
88                                 printf("\n");
89                         }
90                 } else {
91                         meshlink_node_t *node = meshlink_get_node(mesh, arg);
92                         if(!node) {
93                                 fprintf(stderr, "Error looking up '%s': %s\n", arg, meshlink_strerror(meshlink_errno));
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_handle_t *mesh, char *buf) {
117         static meshlink_node_t *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 = meshlink_get_node(mesh, buf);
155                 if(!destination) {
156                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
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(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) {
167                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
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_handle_t *mesh = meshlink_open(confbase, nick);
186         if(!mesh) {
187                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
188                 return 1;
189         }
190
191         meshlink_set_receive_cb(mesh, receive);
192         meshlink_set_node_status_cb(mesh, node_status);
193         meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
194
195         if(!meshlink_start(mesh)) {
196                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
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         meshlink_stop(mesh);
208         meshlink_close(mesh);
209
210         return 0;
211 }