From cd7ff86b2a9af3a16245c3e0b3de56675e137adc Mon Sep 17 00:00:00 2001 From: sin Date: Tue, 16 Sep 2014 15:30:59 +0100 Subject: Name setting and printing the ID are now exposed via the fs We will gradually expose the other commands as well. --- ratatox.c | 184 +++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 121 insertions(+), 63 deletions(-) (limited to 'ratatox.c') diff --git a/ratatox.c b/ratatox.c index ce7129e..4ad70b8 100644 --- a/ratatox.c +++ b/ratatox.c @@ -31,19 +31,34 @@ struct node { #include "config.h" +struct fifo { + const char *name; + int flags; + mode_t mode; +}; + +enum { + NAME_FIFO, + NR_GFIFOS +}; + +/* Global FIFOs for modifying our own state, they go in $(PWD)/{name,status}_in */ +static struct fifo gfifos[] = { + { .name = "name_in", .flags = O_RDONLY | O_NONBLOCK, .mode = 0644 }, +}; + +static int globalfd[NR_GFIFOS]; + enum { TEXT_IN_FIFO, FILE_IN_FIFO, - NR_FIFOS + NR_FFIFOS }; -static struct fifo { - const char *name; - int flags; - mode_t mode; -} fifos[] = { +/* Friend related FIFOs, they go in TOX_MAX_NAME_LENGTH) + r = TOX_MAX_NAME_LENGTH; + name[r] = '\0'; + fp = fopen("name_out", "w"); + if (!fp) { + perror("fopen"); + exit(EXIT_FAILURE); + } + fputs(name, fp); + fputc('\n', fp); + fclose(fp); + + /* Dump ID */ + fp = fopen("id", "w"); + if (!fp) { + perror("fopen"); + exit(EXIT_FAILURE); + } + tox_get_address(tox, address); + for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) + fprintf(fp, "%02x", address[i]); + fputc('\n', fp); + fclose(fp); + + return 0; +} +static int +toxinit(void) +{ /* IPv4 only */ tox = tox_new(0); dataload(); @@ -538,13 +601,6 @@ toxinit(void) tox_callback_status_message(tox, cb_status_message, NULL); tox_callback_user_status(tox, cb_user_status, NULL); tox_callback_file_control(tox, cb_file_control, NULL); - - tox_get_address(tox, address); - printf("ID: "); - for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) - printf("%02x", address[i]); - printf("\n"); - return 0; } @@ -616,15 +672,15 @@ friendcreate(int32_t fid) exit(EXIT_FAILURE); } - for (i = 0; i < LEN(fifos); i++) { + for (i = 0; i < LEN(ffifos); i++) { snprintf(path, sizeof(path), "%s/%s", f->idstr, - fifos[i].name); - r = mkfifo(path, fifos[i].mode); + ffifos[i].name); + r = mkfifo(path, ffifos[i].mode); if (r < 0 && errno != EEXIST) { perror("mkfifo"); exit(EXIT_FAILURE); } - r = open(path, fifos[i].flags, 0); + r = open(path, ffifos[i].flags, 0); if (r < 0) { perror("open"); exit(EXIT_FAILURE); @@ -676,8 +732,6 @@ struct cmd { } cmds[] = { { .cmd = "a", .cb = cmdaccept, .usage = "usage: a [id]\tAccept or list pending requests\n" }, { .cmd = "f", .cb = cmdfriend, .usage = "usage: f id\tSend friend request to ID\n" }, - { .cmd = "i", .cb = cmdid, .usage = "usage: i\tShow ID\n" }, - { .cmd = "n", .cb = cmdname, .usage = "usage: n [name]\tChange or show current name\n" }, { .cmd = "h", .cb = cmdhelp, .usage = NULL }, }; @@ -757,41 +811,6 @@ cmdfriend(char *cmd, size_t sz) return 0; } -static int -cmdid(char *cmd, size_t sz) -{ - uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; - int i; - - tox_get_address(tox, address); - for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) - printf("%02x", address[i]); - printf("\n"); - return 0; -} - -static int -cmdname(char *cmd, size_t sz) -{ - char *args[2]; - uint8_t name[TOX_MAX_NAME_LENGTH + 1]; - uint16_t len; - int r; - - r = tokenize(cmd, args, 2); - - if (r == 1) { - len = tox_get_self_name(tox, name); - name[len] = '\0'; - printf("%s\n", name); - } else { - tox_set_name(tox, (uint8_t *)args[1], strlen(args[1])); - datasave(); - } - - return 0; -} - static int cmdhelp(char *cmd, size_t sz) { @@ -858,10 +877,11 @@ writeparam(struct friend *f, const char *file, const char *mode, static void loop(void) { + FILE *fp; struct friend *f; time_t t0, t1; int connected = 0; - int i, n; + int i, n, r; int fdmax; fd_set rfds; struct timeval tv; @@ -890,10 +910,16 @@ loop(void) FD_SET(STDIN_FILENO, &rfds); fdmax = STDIN_FILENO; + for (i = 0; i < NR_GFIFOS; i++) { + FD_SET(globalfd[i], &rfds); + if (globalfd[i] > fdmax) + fdmax = globalfd[i]; + } + TAILQ_FOREACH(f, &friendhead, entry) { /* Only monitor friends that are online */ if (tox_get_friend_connection_status(tox, f->fid) == 1) { - for (i = 0; i < NR_FIFOS; i++) { + for (i = 0; i < NR_FFIFOS; i++) { FD_SET(f->fd[i], &rfds); if (f->fd[i] > fdmax) fdmax = f->fd[i]; @@ -957,8 +983,39 @@ loop(void) if (FD_ISSET(STDIN_FILENO, &rfds) != 0) cmdrun(); + for (i = 0; i < NR_GFIFOS; i++) { + if (FD_ISSET(globalfd[i], &rfds) == 0) + continue; + if (strcmp(gfifos[i].name, "name_in") == 0) { + uint8_t name[TOX_MAX_NAME_LENGTH + 1]; +again: + r = read(globalfd[i], name, TOX_MAX_NAME_LENGTH); + if (r < 0) { + if (errno == EINTR) + goto again; + if (errno == EWOULDBLOCK) + continue; + perror("read"); + continue; + } + if (name[r - 1] == '\n') + r--; + name[r] = '\0'; + tox_set_name(tox, name, r); + printout("Changed name to %s\n", name); + fp = fopen("name_out", "w"); + if (!fp) { + perror("fopen"); + exit(EXIT_FAILURE); + } + fputs(name, fp); + fputc('\n', fp); + fclose(fp); + } + } + TAILQ_FOREACH(f, &friendhead, entry) { - for (i = 0; i < NR_FIFOS; i++) { + for (i = 0; i < NR_FFIFOS; i++) { if (FD_ISSET(f->fd[i], &rfds) == 0) continue; switch (i) { @@ -999,6 +1056,7 @@ main(int argc, char *argv[]) printrat(); printf("Type h for help\n"); toxinit(); + localinit(); friendload(); loop(); return EXIT_SUCCESS; -- cgit v1.2.3