1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-26 23:03:27 +01:00

Merge pull request #21 from JFreegman/master

properly implemented friend statuses and status messages
This commit is contained in:
JFreegman 2013-09-02 15:35:35 -07:00
commit f8519415b7
5 changed files with 67 additions and 39 deletions

View File

@ -104,9 +104,7 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t
nick[len - 1] = '\0'; nick[len - 1] = '\0';
snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
wattron(ctx->history, COLOR_PAIR(3));
wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick); wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick);
wattroff(ctx->history, COLOR_PAIR(3));
} }
static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
@ -123,10 +121,7 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1
status[len - 1] = '\0'; status[len - 1] = '\0';
wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, "* Your partner changed status message to '%s'\n", status);
wprintw(ctx->history, "* Your partner changed status to '%s'\n", status);
wattroff(ctx->history, COLOR_PAIR(3));
} }
/* check that the string has one non-space character */ /* check that the string has one non-space character */
@ -322,17 +317,17 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
if (!strncmp(status, "online", strlen("online"))) { if (!strncmp(status, "online", strlen("online"))) {
status_kind = TOX_USERSTATUS_NONE; status_kind = TOX_USERSTATUS_NONE;
status_text = "ONLINE"; status_text = "Online";
} }
else if (!strncmp(status, "away", strlen("away"))) { else if (!strncmp(status, "away", strlen("away"))) {
status_kind = TOX_USERSTATUS_AWAY; status_kind = TOX_USERSTATUS_AWAY;
status_text = "AWAY"; status_text = "Away";
} }
else if (!strncmp(status, "busy", strlen("busy"))) { else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = TOX_USERSTATUS_BUSY; status_kind = TOX_USERSTATUS_BUSY;
status_text = "BUSY"; status_text = "Busy";
} }
else { else {
@ -344,15 +339,22 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
if (msg == NULL) { if (msg == NULL) {
tox_set_userstatus(m, status_kind); tox_set_userstatus(m, status_kind);
wprintw(ctx->history, "Status set to: %s\n", status_text); wprintw(ctx->history, "Status message set to: %s\n", status_text);
} else { } else {
msg++; msg++;
tox_set_userstatus(m, status_kind); tox_set_userstatus(m, status_kind);
tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); wprintw(ctx->history, "Status message set to: %s, %s\n", status_text, msg);
} }
} }
else if (!strncmp(cmd, "/statusmsg ", strlen("/statusmsg "))) {
char *msg = strchr(cmd, ' ');
msg++;
wprintw(ctx->history, "Status message set to: %s\n", msg);
tox_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1);
}
else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
char *nick; char *nick;
nick = strchr(cmd, ' '); nick = strchr(cmd, ' ');
@ -416,12 +418,13 @@ void print_help(ChatContext *self)
wattroff(self->history, A_BOLD); wattroff(self->history, A_BOLD);
wprintw(self->history, " /status <type> <message> : Set your status\n"); wprintw(self->history, " /status <type> <message> : Set your status\n");
wprintw(self->history, " /statusmsg <message> : Set your status message\n");
wprintw(self->history, " /nick <nickname> : Set your nickname\n"); wprintw(self->history, " /nick <nickname> : Set your nickname\n");
wprintw(self->history, " /me <action> : Do an action\n"); wprintw(self->history, " /me <action> : Do an action\n");
wprintw(self->history, " /myid : Print your ID\n"); wprintw(self->history, " /myid : Print your ID\n");
wprintw(self->history, " /clear : Clear the screen\n"); wprintw(self->history, " /clear : Clear the screen\n");
wprintw(self->history, " /close : Close the current chat window\n"); wprintw(self->history, " /close : Close the current chat window\n");
wprintw(self->history, " /quit or /exit : Exit program\n"); wprintw(self->history, " /quit or /exit : Exit Toxic\n");
wprintw(self->history, " /help : Print this message again\n\n"); wprintw(self->history, " /help : Print this message again\n\n");
wattroff(self->history, COLOR_PAIR(2)); wattroff(self->history, COLOR_PAIR(2));

View File

@ -21,7 +21,7 @@ extern int store_data(Tox *m, char *path);
typedef struct { typedef struct {
uint8_t name[TOX_MAX_NAME_LENGTH]; uint8_t name[TOX_MAX_NAME_LENGTH];
uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH]; uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
int num; int num;
int chatwin; int chatwin;
bool active; bool active;
@ -56,8 +56,8 @@ void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t
if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends) if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends)
return; return;
memcpy((char *) &friends[num].status, (char *) str, len); memcpy((char *) &friends[num].statusmsg, (char *) str, len);
friends[num].status[len] = 0; friends[num].statusmsg[len] = 0;
} }
int friendlist_onFriendAdded(Tox *m, int num) int friendlist_onFriendAdded(Tox *m, int num)
@ -72,9 +72,9 @@ int friendlist_onFriendAdded(Tox *m, int num)
friends[i].num = num; friends[i].num = num;
friends[i].active = true; friends[i].active = true;
friends[i].chatwin = -1; friends[i].chatwin = -1;
tox_getname(m, num, friends[i].name); //tox_getname(m, num, friends[i].name);
strcpy((char *) friends[i].name, "unknown"); strcpy((char *) friends[i].name, "unknown");
strcpy((char *) friends[i].status, "Offline"); strcpy((char *) friends[i].statusmsg, NOSTATUSMSG);
if (i == num_friends) if (i == num_friends)
++num_friends; ++num_friends;
@ -173,19 +173,43 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
for (i = 0; i < num_friends; ++i) { for (i = 0; i < num_friends; ++i) {
if (friends[i].active) { if (friends[i].active) {
if (i == num_selected) bool is_online = tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE;
wattron(self->window, COLOR_PAIR(3));
if (i == num_selected)
wprintw(self->window, " > "); wprintw(self->window, " > ");
else
wprintw(self->window, " ");
if (i == num_selected) if (is_online) {
wattroff(self->window, COLOR_PAIR(3)); TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num);
int colour;
attron(A_BOLD); switch(status) {
case TOX_USERSTATUS_NONE:
colour = 1;
break;
case TOX_USERSTATUS_AWAY:
colour = 5;
break;
case TOX_USERSTATUS_BUSY:
case TOX_USERSTATUS_INVALID:
default:
colour = 3;
break;
}
wattron(self->window, COLOR_PAIR(colour));
wprintw(self->window, "%s ", friends[i].name); wprintw(self->window, "%s ", friends[i].name);
attroff(A_BOLD); wattroff(self->window, COLOR_PAIR(colour));
wprintw(self->window, "(%s)\n", friends[i].status); if (strncmp(friends[i].statusmsg, NOSTATUSMSG, strlen(NOSTATUSMSG)))
wprintw(self->window, "(%s)\n", friends[i].statusmsg);
} else {
wprintw(self->window, "%s (Offline)\n", friends[i].name);
}
} }
} }

View File

@ -4,6 +4,8 @@
#include "toxic_windows.h" #include "toxic_windows.h"
#include "chat.h" #include "chat.h"
#define NOSTATUSMSG "NOSTATUSMSG" /* Friends' default status message */
ToxWindow new_friendlist(); ToxWindow new_friendlist();
int friendlist_onFriendAdded(Tox *m, int num); int friendlist_onFriendAdded(Tox *m, int num);
void disable_chatwin(int f_num); void disable_chatwin(int f_num);

View File

@ -223,12 +223,12 @@ void cmd_help(ToxWindow *self, Tox *m, char **args)
wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
wprintw(self->window, " add <id> <message> : Add friend\n"); wprintw(self->window, " add <id> <message> : Add friend\n");
wprintw(self->window, " status <type> <message> : Set your status\n"); wprintw(self->window, " status <type> <message> : Set your status\n");
wprintw(self->window, " statusmsg <message> : Set your status\n"); wprintw(self->window, " statusmsg <message> : Set your status message\n");
wprintw(self->window, " nick <nickname> : Set your nickname\n"); wprintw(self->window, " nick <nickname> : Set your nickname\n");
wprintw(self->window, " mynick : Print your current nickname\n"); wprintw(self->window, " mynick : Print your current nickname\n");
wprintw(self->window, " accept <number> : Accept friend request\n"); wprintw(self->window, " accept <number> : Accept friend request\n");
wprintw(self->window, " myid : Print your ID\n"); wprintw(self->window, " myid : Print your ID\n");
wprintw(self->window, " quit/exit : Exit program\n"); wprintw(self->window, " quit/exit : Exit Toxic\n");
wprintw(self->window, " help : Print this message again\n"); wprintw(self->window, " help : Print this message again\n");
wprintw(self->window, " clear : Clear this window\n"); wprintw(self->window, " clear : Clear this window\n");
@ -294,13 +294,13 @@ void cmd_status(ToxWindow *self, Tox *m, char **args)
if (!strncmp(status, "online", strlen("online"))) { if (!strncmp(status, "online", strlen("online"))) {
status_kind = TOX_USERSTATUS_NONE; status_kind = TOX_USERSTATUS_NONE;
status_text = "ONLINE"; status_text = "Online";
} else if (!strncmp(status, "away", strlen("away"))) { } else if (!strncmp(status, "away", strlen("away"))) {
status_kind = TOX_USERSTATUS_AWAY; status_kind = TOX_USERSTATUS_AWAY;
status_text = "AWAY"; status_text = "Away";
} else if (!strncmp(status, "busy", strlen("busy"))) { } else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = TOX_USERSTATUS_BUSY; status_kind = TOX_USERSTATUS_BUSY;
status_text = "BUSY"; status_text = "Busy";
} else { } else {
wprintw(self->window, "Invalid status.\n"); wprintw(self->window, "Invalid status.\n");
return; return;
@ -310,11 +310,11 @@ void cmd_status(ToxWindow *self, Tox *m, char **args)
if (msg == NULL) { if (msg == NULL) {
tox_set_userstatus(m, status_kind); tox_set_userstatus(m, status_kind);
wprintw(self->window, "Status set to: %s\n", status_text); wprintw(self->window, "Status message set to: %s\n", status_text);
} else { } else {
tox_set_userstatus(m, status_kind); tox_set_userstatus(m, status_kind);
tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
wprintw(self->window, "Status set to: %s, %s\n", status_text, msg); wprintw(self->window, "Status message set to: %s, %s\n", status_text, msg);
} }
} }
@ -322,7 +322,7 @@ void cmd_statusmsg(ToxWindow *self, Tox *m, char **args)
{ {
char *msg = args[1]; char *msg = args[1];
tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); tox_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1);
wprintw(self->window, "Status set to: %s\n", msg); wprintw(self->window, "Status message set to: %s\n", msg);
} }
static void execute(ToxWindow *self, Tox *m, char *u_cmd) static void execute(ToxWindow *self, Tox *m, char *u_cmd)

View File

@ -71,7 +71,7 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v
void on_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) void on_statuschange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{ {
wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); wprintw(prompt->window, "\n(message change) %d: %s\n", friendnumber, string);
int i; int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
@ -84,10 +84,9 @@ void on_friendadded(Tox *m, int friendnumber)
{ {
friendlist_onFriendAdded(m, friendnumber); friendlist_onFriendAdded(m, friendnumber);
if (store_data(m, DATA_FILE)) { if (store_data(m, DATA_FILE))
wprintw(prompt->window, "\nCould not store Tox data\n"); wprintw(prompt->window, "\nCould not store Tox data\n");
} }
}
/* CALLBACKS END */ /* CALLBACKS END */
int add_window(Tox *m, ToxWindow w) int add_window(Tox *m, ToxWindow w)