5 #include "../src/meshlink-tiny.h"
7 static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
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",
18 fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text);
21 static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
24 const char *msg = data;
26 if(!len || msg[len - 1]) {
27 fprintf(stderr, "Received invalid data from %s\n", source->name);
31 printf("%s says: %s\n", source->name, msg);
34 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
38 printf("%s joined.\n", node->name);
40 printf("%s left.\n", node->name);
44 static void parse_command(meshlink_handle_t *mesh, char *buf) {
45 char *arg = strchr(buf, ' ');
51 if(!strcasecmp(buf, "join")) {
53 fprintf(stderr, "/join requires an argument!\n");
59 if(!meshlink_join(mesh, arg)) {
60 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_strerror(meshlink_errno));
62 fprintf(stderr, "Invitation accepted!\n");
65 if(!meshlink_start(mesh)) {
66 fprintf(stderr, "Could not restart MeshLink: %s\n", meshlink_strerror(meshlink_errno));
69 } else if(!strcasecmp(buf, "quit")) {
72 } else if(!strcasecmp(buf, "help")) {
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"
82 fprintf(stderr, "Unknown command '/%s'\n", buf);
86 static void parse_input(meshlink_handle_t *mesh, char *buf) {
87 static meshlink_node_t *destination;
98 if(len && buf[len - 1] == '\n') {
102 if(len && buf[len - 1] == '\r') {
106 // Ignore empty lines.
112 // Commands start with '/'
115 parse_command(mesh, buf + 1);
119 // Lines in the form "name: message..." set the destination node.
122 char *colon = strchr(buf, ':');
132 destination = meshlink_get_node(mesh, buf);
135 fprintf(stderr, "Error looking up '%s': %s\n", buf, meshlink_strerror(meshlink_errno));
141 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
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));
150 printf("Message sent to '%s'.\n", destination->name);
153 int main(int argc, char *argv[]) {
154 const char *confbase = ".chat";
155 const char *nick = NULL;
166 meshlink_set_log_cb(NULL, MESHLINK_INFO, log_message);
168 meshlink_handle_t *mesh = meshlink_open(confbase, nick, "chat", DEV_CLASS_STATIONARY);
171 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_strerror(meshlink_errno));
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);
179 if(!meshlink_start(mesh)) {
180 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_strerror(meshlink_errno));
184 printf("Chat started.\nType /help for a list of commands.\n");
186 while(fgets(buf, sizeof(buf), stdin)) {
187 parse_input(mesh, buf);
190 printf("Chat stopping.\n");
193 meshlink_close(mesh);