4 #include "../src/meshlink.h"
6 static void log(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
7 const char *levelstr[] = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"};
8 fprintf(stderr, "%s: %s\n", levelstr[level], text);
11 static void receive(meshlink_handle_t *mesh, meshlink_node_t *source, const char *data, size_t len) {
12 if(!len || data[len - 1]) {
13 fprintf(stderr, "Received invalid data from %s\n", source->name);
17 printf("%s says: %s\n", source->name, data);
20 static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
22 printf("%s joined.\n", node->name);
24 printf("%s left.\n", node->name);
27 static void parse_command(meshlink_handle_t *mesh, char *buf) {
28 char *arg = strchr(buf, ' ');
32 if(!strcasecmp(buf, "invite")) {
36 fprintf(stderr, "/invite requires an argument!\n");
40 invitation = meshlink_invite(mesh, arg);
42 fprintf(stderr, "Could not invite '%s': %s\n", arg, meshlink_errstr);
46 printf("Invitation for %s: %s\n", arg, invitation);
48 } else if(!strcasecmp(buf, "join")) {
50 fprintf(stderr, "/join requires an argument!\n");
54 if(!meshlink_join(mesh, arg))
55 fprintf(stderr, "Could not join using invitation: %s\n", meshlink_errstr);
57 fprintf(stderr, "Invitation accepted!\n");
58 } else if(!strcasecmp(buf, "kick")) {
60 fprintf(stderr, "/kick requires an argument!\n");
64 meshlink_node_t *node = meshlink_get_node(mesh, arg);
66 fprintf(stderr, "Unknown node '%s'\n", arg);
70 meshlink_blacklist(mesh, node);
72 printf("Node '%s' blacklisted.\n", arg);
73 } else if(!strcasecmp(buf, "quit")) {
77 fprintf(stderr, "Unknown command '/%s'\n", buf);
81 static void parse_input(meshlink_handle_t *mesh, char *buf) {
82 static meshlink_node_t *destination;
92 if(len && buf[len - 1] == '\n')
95 if(len && buf[len - 1] == '\r')
98 // Ignore empty lines.
103 // Commands start with '/'
106 return parse_command(mesh, buf + 1);
108 // Lines in the form "name: message..." set the destination node.
111 char *colon = strchr(buf, ':');
119 destination = meshlink_get_node(mesh, buf);
121 fprintf(stderr, "Unknown node '%s'\n", buf);
127 fprintf(stderr, "Who are you talking to? Write 'name: message...'\n");
131 if(!meshlink_send(mesh, destination, msg, strlen(msg) + 1)) {
132 fprintf(stderr, "Could not send message to '%s': %s\n", destination->name, meshlink_errstr);
136 printf("Message sent to '%s'.\n", destination->name);
139 int main(int argc, char *argv[]) {
140 const char *confbase = ".chat";
141 const char *nick = NULL;
150 meshlink_handle_t *mesh = meshlink_open(confbase, nick);
152 fprintf(stderr, "Could not open MeshLink: %s\n", meshlink_errstr);
156 meshlink_set_receive_cb(mesh, receive);
157 meshlink_set_node_status_cb(mesh, node_changed);
158 meshlink_set_log_cb(mesh, MESHLINK_INFO, log);
160 if(!meshlink_start(mesh)) {
161 fprintf(stderr, "Could not start MeshLink: %s\n", meshlink_errstr);
165 printf("Chat started.\n");
167 while(fgets(buf, sizeof buf, stdin))
168 parse_input(mesh, buf);
170 printf("Chat stopping.\n");
173 meshlink_close(mesh);