commit 85dcb9317df7302faa01af91bebbcc1b7e6d4280
parent 7cda83ce5f90ce6222c78fa95f10452f801b5977
Author: FRIGN <dev@frign.de>
Date: Tue, 7 Oct 2014 17:18:44 +0200
Refactor user-state-logic
Diffstat:
M | ratox.c | | | 41 | ++++++++++++++++++++--------------------- |
1 file changed, 20 insertions(+), 21 deletions(-)
diff --git a/ratox.c b/ratox.c
@@ -135,6 +135,16 @@ static struct file ffiles[] = {
[FCALL_PENDING] = { .type = STATIC, .name = "call_pending", .flags = O_WRONLY | O_TRUNC | O_CREAT },
};
+struct {
+ char *name;
+ int n;
+} ustate[] = {
+ { .name = "invalid", .n = TOX_USERSTATUS_INVALID },
+ { .name = "none", .n = TOX_USERSTATUS_NONE },
+ { .name = "away", .n = TOX_USERSTATUS_AWAY },
+ { .name = "busy", .n = TOX_USERSTATUS_BUSY },
+};
+
enum {
TRANSFER_NONE,
TRANSFER_INITIATED,
@@ -752,13 +762,12 @@ cbstatusmessage(Tox *m, int32_t frnum, const uint8_t *data, uint16_t len, void *
}
static void
-cbuserstate(Tox *m, int32_t frnum, uint8_t status, void *udata)
+cbuserstate(Tox *m, int32_t frnum, uint8_t state, void *udata)
{
struct friend *f;
- char *ustate[] = { "none", "away", "busy", "invalid" };
- if (status >= LEN(ustate)) {
- weprintf("Received invalid user status: %d\n", status);
+ if (state >= LEN(ustate)) {
+ weprintf("Received invalid user status: %d\n", state);
return;
}
@@ -766,8 +775,8 @@ cbuserstate(Tox *m, int32_t frnum, uint8_t status, void *udata)
if (f->num == frnum) {
ftruncate(f->fd[FSTATE], 0);
lseek(f->fd[FSTATE], 0, SEEK_SET);
- dprintf(f->fd[FSTATE], "%s\n", ustate[status]);
- printout(": %s : State > %s\n", f->name, ustate[status]);
+ dprintf(f->fd[FSTATE], "%s\n", ustate[state].name);
+ printout(": %s : State > %s\n", f->name, ustate[state].name);
break;
}
}
@@ -1106,7 +1115,6 @@ localinit(void)
uint8_t name[TOX_MAX_NAME_LENGTH + 1];
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH + 1];
- char *ustate[] = { "none", "away", "busy" };
DIR *d;
int r;
size_t i, m;
@@ -1169,7 +1177,7 @@ localinit(void)
weprintf("Invalid user status: %d\n", r);
} else {
ftruncate(gslots[STATE].fd[OUT], 0);
- dprintf(gslots[STATE].fd[OUT], "%s\n", ustate[r]);
+ dprintf(gslots[STATE].fd[OUT], "%s\n", ustate[r].name);
}
/* Dump ID */
@@ -1294,7 +1302,6 @@ friendcreate(int32_t frnum)
{
struct friend *f;
uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH + 1];
- char *ustate[] = { "none", "away", "busy", "invalid" };
size_t i;
DIR *d;
int r;
@@ -1364,7 +1371,7 @@ friendcreate(int32_t frnum)
weprintf("Invalid user status: %d\n", r);
} else {
ftruncate(f->fd[FSTATE], 0);
- dprintf(f->fd[FSTATE], "%s\n", ustate[r]);
+ dprintf(f->fd[FSTATE], "%s\n", ustate[r].name);
}
/* Dump file pending state */
@@ -1473,14 +1480,6 @@ setuserstate(void *data)
char buf[PIPE_BUF];
ssize_t n;
size_t i;
- struct {
- char *name;
- int n;
- } ustate[] = {
- { .name = "none", .n = TOX_USERSTATUS_NONE },
- { .name = "away", .n = TOX_USERSTATUS_AWAY },
- { .name = "busy", .n = TOX_USERSTATUS_BUSY },
- };
n = fiforead(gslots[STATE].dirfd, &gslots[STATE].fd[IN], gfiles[IN],
buf, sizeof(buf) - 1);
@@ -1490,7 +1489,7 @@ setuserstate(void *data)
n--;
buf[n] = '\0';
for (i = 0; i < LEN(ustate); i++) {
- if (strcmp(buf, ustate[i].name) == 0) {
+ if (ustate[i].n != TOX_USERSTATUS_INVALID && strcmp(buf, ustate[i].name) == 0) {
tox_set_user_status(tox, ustate[i].n);
break;
}
@@ -1499,14 +1498,14 @@ setuserstate(void *data)
ftruncate(gslots[STATE].fd[ERR], 0);
lseek(gslots[STATE].fd[ERR], 0, SEEK_SET);
dprintf(gslots[STATE].fd[ERR], "invalid\n");
- weprintf("Invalid user status: %s\n", buf);
+ weprintf("Invalid state: %s\n", buf);
return;
}
ftruncate(gslots[STATE].fd[OUT], 0);
lseek(gslots[STATE].fd[OUT], 0, SEEK_SET);
dprintf(gslots[STATE].fd[OUT], "%s\n", buf);
datasave();
- printout("User state > %s\n", buf);
+ printout(": State > %s\n", buf);
}
static void