]> git.meshlink.io Git - meshlink-tiny/blob - examples/chat.c
Add a metering test.
[meshlink-tiny] / examples / chat.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <strings.h>
5 #include "../src/meshlink-tiny.h"
6
7 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
8         (void)mesh;
9
10         static const char *levelstr[] = {
11                 [MESHLINK_DEBUG] = "\x1b[34mDEBUG",
12                 [MESHLINK_INFO] = "\x1b[32mINFO",
13                 [MESHLINK_WARNING] = "\x1b[33mWARNING",
14                 [MESHLINK_ERROR] = "\x1b[31mERROR",
15                 [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL",
16         };
17
18         fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
19 }
20
21 static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
22         (void)mesh;
23
24         const char *msg = data;
25
26         if(!len || msg[len - 1]) {
27                 fprintf(stderr, "Received invalid data from %s\n", source->name);
28                 return;
29         }
30
31         printf("%s says: %s\n", source->name, msg);
32 }
33
34 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
35         (void)mesh;
36
37         if(reachable) {
38                 printf("%s joined.\n", node->name);
39         } else {
40                 printf("%s left.\n", node->name);
41         }
42 }
43
44 static void parse_command(meshlink_handle_t *mesh, char *buf) {
45         char *arg = strchr(buf, ' ');
46
47         if(arg) {
48                 *arg++ = 0;
49         }
50
51         if(!strcasecmp(buf, "join")) {
52                 if(!arg) {
53                         fprintf(stderr, "/join requires an argument!\n");
54                         return;
55                 }
56
57                 meshlink_stop(mesh);
58
59                 if(!meshlink_join(mesh, arg)) {
60                         fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
61                 } else {
62                         fprintf(stderr, "Invitation accepted!\n");
63                 }
64
65                 if(!meshlink_start(mesh)) {
66                         fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
67                         exit(1);
68                 }
69         } else if(!strcasecmp(buf, "quit")) {
70                 printf("Bye!\n");
71                 fclose(stdin);
72         } else if(!strcasecmp(buf, "help")) {
73                 printf(
74                         "<name>: <message>     Send a message to the given node.\n"
75                         "                      Subsequent messages don't need the <name>: prefix.\n"
76                         "/join <invitation>    Join an existing mesh using an invitation.\n"
77                         "/kick <name>          Blacklist the given node.\n"
78                         "/who [<name>]         List all nodes or show information about the given node.\n"
79                         "/quit                 Exit this program.\n"
80                 );
81         } else {
82                 fprintf(stderr, "Unknown command '/%s'\n", buf);
83         }
84 }
85
86 static void parse_input(meshlink_handle_t *mesh, char *buf) {
87         static meshlink_node_t *destination;
88         size_t len;
89
90         if(!buf) {
91                 return;
92         }
93
94         // Remove newline.
95
96         len = strlen(buf);
97
98         if(len && buf[len - 1] == '\n') {
99                 buf[--len] = 0;
100         }
101
102         if(len && buf[len - 1] == '\r') {
103                 buf[--len] = 0;
104         }
105
106         // Ignore empty lines.
107
108         if(!len) {
109                 return;
110         }
111
112         // Commands start with '/'
113
114         if(*buf == '/') {
115                 parse_command(mesh, buf + 1);
116                 return;
117         }
118
119         // Lines in the form "name: message..." set the destination node.
120
121         char *msg = buf;
122         char *colon = strchr(buf, ':');
123
124         if(colon) {
125                 *colon = 0;
126                 msg = colon + 1;
127
128                 if(*msg == ' ') {
129                         msg++;
130                 }
131
132                 destination = meshlink_get_node(mesh, buf);
133
134                 if(!destination) {
135                         fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
136                         return;
137                 }
138         }
139
140         if(!destination) {
141                 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
142                 return;
143         }
144
145         if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) {
146                 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_strerror(meshlink_errno));
147                 return;
148         }
149
150         printf("Message sent to '%s'.\n", destination->name);
151 }
152
153 int main(int argc, char *argv[]) {
154         const char *confbase = ".chat";
155         const char *nick = NULL;
156         char buf[1024];
157
158         if(argc > 1) {
159                 confbase = argv[1];
160         }
161
162         if(argc > 2) {
163                 nick = argv[2];
164         }
165
166         meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
167
168         meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
169
170         if(!mesh) {
171                 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
172                 return 1;
173         }
174
175         meshlink_set_receive_cb(mesh, receive);
176         meshlink_set_node_status_cb(mesh, node_status);
177         meshlink_set_log_cb(mesh, MESHLINK_INFO, log_message);
178
179         if(!meshlink_start(mesh)) {
180                 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
181                 return 1;
182         }
183
184         printf("Chat started.\nType /help for a list of commands.\n");
185
186         while(fgets(buf, sizeof(buf), stdin)) {
187                 parse_input(mesh, buf);
188         }
189
190         printf("Chat stopping.\n");
191
192         meshlink_stop(mesh);
193         meshlink_close(mesh);
194
195         return 0;
196 }