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

Merge pull request #26 from JFreegman/master

implemented status and connectionstatus callbacks
This commit is contained in:
JFreegman 2013-09-05 15:17:32 -07:00
commit e51ad84b3c
6 changed files with 234 additions and 80 deletions

View File

@ -55,9 +55,9 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1
msg[len-1] = '\0'; msg[len-1] = '\0';
nick[TOX_MAX_NAME_LENGTH-1] = '\0'; nick[TOX_MAX_NAME_LENGTH-1] = '\0';
wattron(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(CYAN));
wattron(ctx->history, COLOR_PAIR(4)); wattron(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s: ", nick); wprintw(ctx->history, "%s: ", nick);
wattroff(ctx->history, COLOR_PAIR(4)); wattroff(ctx->history, COLOR_PAIR(4));
@ -67,6 +67,24 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1
beep(); beep();
} }
void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status)
{
ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(CYAN));
if (status == 1)
wprintw(ctx->history, "* Chat partner has come online\n");
else
wprintw(ctx->history, "* Chat partner went offline\n");
}
static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len) static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len)
{ {
ChatContext *ctx = (ChatContext *) self->x; ChatContext *ctx = (ChatContext *) self->x;
@ -80,13 +98,13 @@ static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uin
action[len - 1] = '\0'; action[len - 1] = '\0';
wattron(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(CYAN));
wattron(ctx->history, COLOR_PAIR(5)); wattron(ctx->history, COLOR_PAIR(YELLOW));
wprintw(ctx->history, "* %s %s\n", nick, action); wprintw(ctx->history, "* %s %s\n", nick, action);
wattroff(ctx->history, COLOR_PAIR(5)); wattroff(ctx->history, COLOR_PAIR(YELLOW));
self->blink = true; self->blink = true;
beep(); beep();
@ -100,9 +118,9 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t
if (ctx->friendnum != num) if (ctx->friendnum != num)
return; return;
wattron(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(CYAN));
nick[len - 1] = '\0'; nick[len - 1] = '\0';
snprintf(self->title, sizeof(self->title), "[%s]", nick); snprintf(self->title, sizeof(self->title), "[%s]", nick);
@ -110,6 +128,38 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t
wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick); wprintw(ctx->history, "* Chat partner changed nick to '%s'\n", nick);
} }
static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status)
{
ChatContext *ctx = (ChatContext *) self->x;
struct tm *timeinfo = get_time();
if (ctx->friendnum != num)
return;
char *status_msg = NULL;
int colour = 0;
if (status == TOX_USERSTATUS_BUSY) {
status_msg = "[Busy]";
colour = RED;
}
else if (status == TOX_USERSTATUS_AWAY) {
status_msg = "[Away]";
colour = YELLOW;
}
if (status_msg != NULL) {
wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "* Chat partner set status to: ");
wattron(ctx->history, COLOR_PAIR(colour) | A_BOLD);
wprintw(ctx->history, "%s\n", status_msg);
wattroff(ctx->history, COLOR_PAIR(colour) | A_BOLD);
}
}
static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
{ {
ChatContext *ctx = (ChatContext *) self->x; ChatContext *ctx = (ChatContext *) self->x;
@ -118,14 +168,15 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status
if (ctx->friendnum != num) if (ctx->friendnum != num)
return; return;
wattron(ctx->history, COLOR_PAIR(2));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2));
status[len - 1] = '\0'; status[len - 1] = '\0';
if (strncmp(status, "Online", strlen("status"))) { /* Ignore default "Online" message */
wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status); wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status);
} }
}
/* check that the string has one non-space character */ /* check that the string has one non-space character */
int string_is_empty(char *string) int string_is_empty(char *string)
@ -230,18 +281,18 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
uint8_t selfname[TOX_MAX_NAME_LENGTH]; uint8_t selfname[TOX_MAX_NAME_LENGTH];
tox_getselfname(m, selfname, sizeof(selfname)); tox_getselfname(m, selfname, sizeof(selfname));
wattron(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(CYAN));
wattron(ctx->history, COLOR_PAIR(1)); wattron(ctx->history, COLOR_PAIR(GREEN));
wprintw(ctx->history, "%s: ", selfname); wprintw(ctx->history, "%s: ", selfname);
wattroff(ctx->history, COLOR_PAIR(1)); wattroff(ctx->history, COLOR_PAIR(GREEN));
wprintw(ctx->history, "%s\n", line); wprintw(ctx->history, "%s\n", line);
if (tox_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { if (tox_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) {
wattron(ctx->history, COLOR_PAIR(3)); wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, " * Failed to send message.\n"); wprintw(ctx->history, " * Failed to send message.\n");
wattroff(ctx->history, COLOR_PAIR(3)); wattroff(ctx->history, COLOR_PAIR(RED));
} }
} }
} }
@ -289,21 +340,21 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
action++; action++;
wattron(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(CYAN));
wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
wattroff(ctx->history, COLOR_PAIR(2)); wattroff(ctx->history, COLOR_PAIR(CYAN));
uint8_t selfname[TOX_MAX_NAME_LENGTH]; uint8_t selfname[TOX_MAX_NAME_LENGTH];
tox_getselfname(m, selfname, sizeof(selfname)); tox_getselfname(m, selfname, sizeof(selfname));
wattron(ctx->history, COLOR_PAIR(5)); wattron(ctx->history, COLOR_PAIR(YELLOW));
wprintw(ctx->history, "* %s %s\n", selfname, action); wprintw(ctx->history, "* %s %s\n", selfname, action);
wattroff(ctx->history, COLOR_PAIR(5)); wattroff(ctx->history, COLOR_PAIR(YELLOW));
if (tox_sendaction(m, ctx->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) { if (tox_sendaction(m, ctx->friendnum, (uint8_t *) action, strlen(action) + 1) == 0) {
wattron(ctx->history, COLOR_PAIR(3)); wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, " * Failed to send action\n"); wprintw(ctx->history, " * Failed to send action\n");
wattroff(ctx->history, COLOR_PAIR(3)); wattroff(ctx->history, COLOR_PAIR(RED));
} }
} }
@ -322,17 +373,26 @@ 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"; wprintw(ctx->history, "Status set to: ");
wattron(ctx->history, COLOR_PAIR(GREEN) | A_BOLD);
wprintw(ctx->history, "[Online]\n");
wattroff(ctx->history, COLOR_PAIR(GREEN) | A_BOLD);
} }
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"; wprintw(ctx->history, "Status set to: ");
wattron(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD);
wprintw(ctx->history, "[Away]\n");
wattroff(ctx->history, COLOR_PAIR(YELLOW) | A_BOLD);
} }
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"; wprintw(ctx->history, "Status set to: ");
wattron(ctx->history, COLOR_PAIR(RED) | A_BOLD);
wprintw(ctx->history, "[Busy]\n");
wattroff(ctx->history, COLOR_PAIR(RED) | A_BOLD);
} }
else { else {
@ -340,7 +400,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
return; return;
} }
wprintw(ctx->history, "Status set to: %s\n", status_text);
tox_set_userstatus(m, status_kind); tox_set_userstatus(m, status_kind);
msg = strchr(status, ' '); msg = strchr(status, ' ');
@ -416,7 +475,7 @@ static void chat_onInit(ToxWindow *self, Tox *m)
void print_help(ChatContext *self) void print_help(ChatContext *self)
{ {
wattron(self->history, COLOR_PAIR(2) | A_BOLD); wattron(self->history, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(self->history, "Commands:\n"); wprintw(self->history, "Commands:\n");
wattroff(self->history, A_BOLD); wattroff(self->history, A_BOLD);
@ -430,7 +489,7 @@ void print_help(ChatContext *self)
wprintw(self->history, " /quit or /exit : Exit Toxic\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(CYAN));
} }
ToxWindow new_chat(Tox *m, int friendnum) ToxWindow new_chat(Tox *m, int friendnum)
@ -442,7 +501,9 @@ ToxWindow new_chat(Tox *m, int friendnum)
ret.onDraw = &chat_onDraw; ret.onDraw = &chat_onDraw;
ret.onInit = &chat_onInit; ret.onInit = &chat_onInit;
ret.onMessage = &chat_onMessage; ret.onMessage = &chat_onMessage;
ret.onConnectionChange = &chat_onConnectionChange;
ret.onNickChange = &chat_onNickChange; ret.onNickChange = &chat_onNickChange;
ret.onStatusChange = &chat_onStatusChange;
ret.onStatusMessageChange = &chat_onStatusMessageChange; ret.onStatusMessageChange = &chat_onStatusMessageChange;
ret.onAction = &chat_onAction; ret.onAction = &chat_onAction;

View File

@ -25,6 +25,8 @@ typedef struct {
int num; int num;
int chatwin; int chatwin;
bool active; bool active;
bool online;
TOX_USERSTATUS status;
} friend_t; } friend_t;
static friend_t friends[MAX_FRIENDS_NUM]; static friend_t friends[MAX_FRIENDS_NUM];
@ -34,25 +36,44 @@ static int num_selected = 0;
void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len) void friendlist_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *str, uint16_t len)
{ {
if (num >= num_friends) if (num < 0 || num >= num_friends)
return; return;
if (friends[num].chatwin == -1) if (friends[num].chatwin == -1)
friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); friends[num].chatwin = add_window(m, new_chat(m, friends[num].num));
} }
void friendlist_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status)
{
if (num < 0 || num >= num_friends)
return;
if (status == 1)
friends[num].online = true;
else
friends[num].online = false;
}
void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{ {
if (len >= TOX_MAX_NAME_LENGTH || num >= num_friends) if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= num_friends)
return; return;
memcpy((char *) &friends[num].name, (char *) str, len); memcpy((char *) &friends[num].name, (char *) str, len);
friends[num].name[len] = 0; friends[num].name[len] = 0;
} }
void friendlist_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status)
{
if (num < 0 || num >= num_friends)
return;
friends[num].status = status;
}
void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len) void friendlist_onStatusMessageChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
{ {
if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends) if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends)
return; return;
memcpy((char *) &friends[num].statusmsg, (char *) str, len); memcpy((char *) &friends[num].statusmsg, (char *) str, len);
@ -71,6 +92,8 @@ 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;
friends[i].online = false;
friends[i].status = TOX_USERSTATUS_NONE;
if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0') if (tox_getname(m, num, friends[i].name) != 0 || friends[i].name[0] == '\0')
strcpy((char *) friends[i].name, "unknown"); strcpy((char *) friends[i].name, "unknown");
@ -127,10 +150,9 @@ static void delete_friend(Tox *m, ToxWindow *self, int f_num, wint_t key)
break; break;
} }
if (store_data(m, DATA_FILE))
wprintw(self->window, "\nFailed to store messenger data\n");
num_friends = i; num_friends = i;
store_data(m, DATA_FILE);
select_friend(m, KEY_DOWN); select_friend(m, KEY_DOWN);
} }
@ -157,10 +179,10 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
if (num_friends == 0) { if (num_friends == 0) {
wprintw(self->window, "Empty. Add some friends! :-)\n"); wprintw(self->window, "Empty. Add some friends! :-)\n");
} else { } else {
wattron(self->window, COLOR_PAIR(2) | A_BOLD); wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(self->window, " Open chat with up/down keys and enter.\n"); wprintw(self->window, " Open chat with up/down keys and enter.\n");
wprintw(self->window, " Delete friends with the backspace key.\n\n"); wprintw(self->window, " Delete friends with the backspace key.\n\n");
wattroff(self->window, COLOR_PAIR(2) | A_BOLD); wattroff(self->window, COLOR_PAIR(CYAN) | A_BOLD);
} }
int i; int i;
@ -172,26 +194,26 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
else else
wprintw(self->window, " "); wprintw(self->window, " ");
if (tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE) { if (friends[i].online) {
TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num); TOX_USERSTATUS status = friends[i].status;
int colour = 7; /* Invalid or other errors default to black */ int colour = WHITE;
switch(status) { switch(status) {
case TOX_USERSTATUS_NONE: case TOX_USERSTATUS_NONE:
colour = 1; colour = GREEN;
break; break;
case TOX_USERSTATUS_AWAY: case TOX_USERSTATUS_AWAY:
colour = 5; colour = YELLOW;
break; break;
case TOX_USERSTATUS_BUSY: case TOX_USERSTATUS_BUSY:
colour = 3; colour = RED;
break; break;
} }
wprintw(self->window, "["); wprintw(self->window, "[");
wattron(self->window, COLOR_PAIR(colour)); wattron(self->window, COLOR_PAIR(colour) | A_BOLD);
wprintw(self->window, "O"); wprintw(self->window, "O");
wattroff(self->window, COLOR_PAIR(colour)); wattroff(self->window, COLOR_PAIR(colour) | A_BOLD);
wprintw(self->window, "] %s", friends[i].name); wprintw(self->window, "] %s", friends[i].name);
if (friends[i].statusmsg[0]) if (friends[i].statusmsg[0])
@ -226,8 +248,10 @@ ToxWindow new_friendlist()
ret.onDraw = &friendlist_onDraw; ret.onDraw = &friendlist_onDraw;
ret.onInit = &friendlist_onInit; ret.onInit = &friendlist_onInit;
ret.onMessage = &friendlist_onMessage; ret.onMessage = &friendlist_onMessage;
ret.onConnectionChange = &friendlist_onConnectionChange;
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
ret.onNickChange = &friendlist_onNickChange; ret.onNickChange = &friendlist_onNickChange;
ret.onStatusChange = &friendlist_onStatusChange;
ret.onStatusMessageChange = &friendlist_onStatusMessageChange; ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
strcpy(ret.title, "[friends]"); strcpy(ret.title, "[friends]");

View File

@ -71,6 +71,7 @@ static void init_term()
if (has_colors()) { if (has_colors()) {
start_color(); start_color();
init_pair(0, COLOR_WHITE, COLOR_BLACK);
init_pair(1, COLOR_GREEN, COLOR_BLACK); init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_CYAN, COLOR_BLACK); init_pair(2, COLOR_CYAN, COLOR_BLACK);
init_pair(3, COLOR_RED, COLOR_BLACK); init_pair(3, COLOR_RED, COLOR_BLACK);
@ -79,7 +80,6 @@ static void init_term()
init_pair(6, COLOR_MAGENTA, COLOR_BLACK); init_pair(6, COLOR_MAGENTA, COLOR_BLACK);
init_pair(7, COLOR_BLACK, COLOR_BLACK); init_pair(7, COLOR_BLACK, COLOR_BLACK);
init_pair(8, COLOR_BLACK, COLOR_WHITE); init_pair(8, COLOR_BLACK, COLOR_WHITE);
} }
refresh(); refresh();
@ -91,9 +91,11 @@ static Tox *init_tox()
Tox *m = tox_new(); Tox *m = tox_new();
/* Callbacks */ /* Callbacks */
tox_callback_connectionstatus(m, on_connectionchange, NULL);
tox_callback_friendrequest(m, on_request, NULL); tox_callback_friendrequest(m, on_request, NULL);
tox_callback_friendmessage(m, on_message, NULL); tox_callback_friendmessage(m, on_message, NULL);
tox_callback_namechange(m, on_nickchange, NULL); tox_callback_namechange(m, on_nickchange, NULL);
tox_callback_userstatus(m, on_statuschange, NULL);
tox_callback_statusmessage(m, on_statusmessagechange, NULL); tox_callback_statusmessage(m, on_statusmessagechange, NULL);
tox_callback_action(m, on_action, NULL); tox_callback_action(m, on_action, NULL);
#ifdef __linux__ #ifdef __linux__
@ -396,17 +398,17 @@ int main(int argc, char *argv[])
load_data(m, DATA_FILE); load_data(m, DATA_FILE);
if (f_flag == -1) { if (f_flag == -1) {
attron(COLOR_PAIR(3) | A_BOLD); attron(COLOR_PAIR(RED) | A_BOLD);
wprintw(prompt->window, "You passed '-f' without giving an argument.\n" wprintw(prompt->window, "You passed '-f' without giving an argument.\n"
"defaulting to 'data' for a keyfile...\n"); "defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD); attroff(COLOR_PAIR(RED) | A_BOLD);
} }
if (config_err) { if (config_err) {
attron(COLOR_PAIR(3) | A_BOLD); attron(COLOR_PAIR(RED) | A_BOLD);
wprintw(prompt->window, "Unable to determine configuration directory.\n" wprintw(prompt->window, "Unable to determine configuration directory.\n"
"defaulting to 'data' for a keyfile...\n"); "defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD); attroff(COLOR_PAIR(RED) | A_BOLD);
} }
while (true) { while (true) {

View File

@ -243,7 +243,7 @@ void cmd_quit(ToxWindow *self, Tox *m, int argc, char **argv)
void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv)
{ {
wclear(self->window); wclear(self->window);
wattron(self->window, COLOR_PAIR(2) | A_BOLD); wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(self->window, "Commands:\n"); wprintw(self->window, "Commands:\n");
wattroff(self->window, A_BOLD); wattroff(self->window, A_BOLD);
@ -264,7 +264,7 @@ void cmd_help(ToxWindow *self, Tox *m, int argc, char **argv)
wprintw(self->window, " * Use the TAB key to navigate through the tabs.\n\n"); wprintw(self->window, " * Use the TAB key to navigate through the tabs.\n\n");
wattroff(self->window, A_BOLD); wattroff(self->window, A_BOLD);
wattroff(self->window, COLOR_PAIR(2)); wattroff(self->window, COLOR_PAIR(CYAN));
} }
void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv) void cmd_msg(ToxWindow *self, Tox *m, int argc, char **argv)
@ -314,6 +314,8 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv)
} }
nick = argv[1]; nick = argv[1];
if (nick[0] == '\"')
nick[strlen(++nick)-1] = L'\0';
tox_setname(m, (uint8_t *) nick, strlen(nick) + 1); tox_setname(m, (uint8_t *) nick, strlen(nick) + 1);
wprintw(self->window, "Nickname set to: %s\n", nick); wprintw(self->window, "Nickname set to: %s\n", nick);
@ -357,19 +359,31 @@ void cmd_status(ToxWindow *self, Tox *m, int argc, char **argv)
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"; wprintw(self->window, "Status set to: ");
} else if (!strncmp(status, "away", strlen("away"))) { wattron(self->window, COLOR_PAIR(GREEN) | A_BOLD);
status_kind = TOX_USERSTATUS_AWAY; wprintw(self->window, "[Online]\n");
status_text = "Away"; wattroff(self->window, COLOR_PAIR(GREEN) | A_BOLD);
} else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = TOX_USERSTATUS_BUSY;
status_text = "Busy";
} else {
wprintw(self->window, "Invalid status.\n");
return;
} }
wprintw(self->window, "Status set to: %s\n", status_text); else if (!strncmp(status, "away", strlen("away"))) {
status_kind = TOX_USERSTATUS_AWAY;
wprintw(self->window, "Status set to: ");
wattron(self->window, COLOR_PAIR(YELLOW) | A_BOLD);
wprintw(self->window, "[Away]\n");
wattroff(self->window, COLOR_PAIR(YELLOW) | A_BOLD);
}
else if (!strncmp(status, "busy", strlen("busy"))) {
status_kind = TOX_USERSTATUS_BUSY;
wprintw(self->window, "Status set to: ");
wattron(self->window, COLOR_PAIR(RED) | A_BOLD);
wprintw(self->window, "[Busy]\n");
wattroff(self->window, COLOR_PAIR(RED) | A_BOLD);
}
else
wprintw(self->window, "Invalid status.\n");
tox_set_userstatus(m, status_kind); tox_set_userstatus(m, status_kind);
if (msg != NULL) { if (msg != NULL) {
@ -529,9 +543,9 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
--y; --y;
} }
wattron(self->window, COLOR_PAIR(1)); wattron(self->window, COLOR_PAIR(GREEN));
mvwprintw(self->window, y, 0, "# "); mvwprintw(self->window, y, 0, "# ");
wattroff(self->window, COLOR_PAIR(1)); wattroff(self->window, COLOR_PAIR(GREEN));
mvwprintw(self->window, y, 2, "%s", prompt_buf); mvwprintw(self->window, y, 2, "%s", prompt_buf);
wclrtoeol(self->window); wclrtoeol(self->window);
wrefresh(self->window); wrefresh(self->window);

View File

@ -24,6 +24,16 @@
#define TOXICVER "NOVER" //Use the -D flag to set this #define TOXICVER "NOVER" //Use the -D flag to set this
#endif #endif
/* Curses foreground colours (background is black) */
#define WHITE 0
#define GREEN 1
#define CYAN 2
#define RED 3
#define BLUE 4
#define YELLOW 5
#define MAGENTA 6
#define BLACK 7
typedef struct ToxWindow_ ToxWindow; typedef struct ToxWindow_ ToxWindow;
struct ToxWindow_ { struct ToxWindow_ {
@ -31,8 +41,10 @@ struct ToxWindow_ {
void(*onDraw)(ToxWindow *, Tox *); void(*onDraw)(ToxWindow *, Tox *);
void(*onInit)(ToxWindow *, Tox *); void(*onInit)(ToxWindow *, Tox *);
void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t); void(*onFriendRequest)(ToxWindow *, uint8_t *, uint8_t *, uint16_t);
void(*onConnectionChange)(ToxWindow *, Tox *, int, uint8_t);
void(*onMessage)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onMessage)(ToxWindow *, Tox *, int, uint8_t *, uint16_t);
void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onStatusChange)(ToxWindow *, Tox *, int, TOX_USERSTATUS);
void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t);
char title[256]; char title[256];
@ -44,9 +56,11 @@ struct ToxWindow_ {
}; };
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata); void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata);
void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata);
void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *userdata);
void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
void on_friendadded(Tox *m, int friendnumber); void on_friendadded(Tox *m, int friendnumber);
ToxWindow *init_windows(); ToxWindow *init_windows();

View File

@ -17,6 +17,8 @@ static ToxWindow *active_window;
static ToxWindow *prompt; static ToxWindow *prompt;
static Tox *m; static Tox *m;
#define UNKNOWN_NAME "Unknown"
/* CALLBACKS START */ /* CALLBACKS START */
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata) void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata)
{ {
@ -30,7 +32,7 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd
} }
wprintw(prompt->window, "\nWith the message: %s\n", data); wprintw(prompt->window, "\nWith the message: %s\n", data);
wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); wprintw(prompt->window, "Type \"accept %d\" to accept it.\n", n);
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onFriendRequest != NULL) if (windows[i].onFriendRequest != NULL)
@ -38,6 +40,35 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userd
} }
} }
void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata)
{
uint8_t nick[TOX_MAX_NAME_LENGTH] = {0};
tox_getname(m, friendnumber, (uint8_t *) &nick);
if (!nick[0])
snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME);
if (status == 1) {
wattron(prompt->window, A_BOLD);
wprintw(prompt->window, "\n%s ", nick);
wattroff(prompt->window, A_BOLD);
wprintw(prompt->window, "has come online\n");
} else {
wattron(prompt->window, A_BOLD);
wprintw(prompt->window, "\n%s ", nick);
wattroff(prompt->window, A_BOLD);
wprintw(prompt->window, "has gone offline\n");
}
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onConnectionChange != NULL)
windows[i].onConnectionChange(&windows[i], m, friendnumber, status);
}
}
void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{ {
int i; int i;
@ -60,7 +91,6 @@ void on_action(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void
void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{ {
wprintw(prompt->window, "\n(nick 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) {
@ -71,7 +101,6 @@ void on_nickchange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, v
void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata)
{ {
wprintw(prompt->window, "\n(note 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) {
@ -80,6 +109,16 @@ void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t
} }
} }
void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *userdata)
{
int i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].onStatusChange != NULL)
windows[i].onStatusChange(&windows[i], m, friendnumber, status);
}
}
void on_friendadded(Tox *m, int friendnumber) void on_friendadded(Tox *m, int friendnumber)
{ {
friendlist_onFriendAdded(m, friendnumber); friendlist_onFriendAdded(m, friendnumber);
@ -179,15 +218,15 @@ static void draw_bar()
static int odd = 0; static int odd = 0;
int blinkrate = 30; int blinkrate = 30;
attron(COLOR_PAIR(4)); attron(COLOR_PAIR(BLUE));
mvhline(LINES - 2, 0, '_', COLS); mvhline(LINES - 2, 0, '_', COLS);
attroff(COLOR_PAIR(4)); attroff(COLOR_PAIR(BLUE));
move(LINES - 1, 0); move(LINES - 1, 0);
attron(COLOR_PAIR(4) | A_BOLD); attron(COLOR_PAIR(BLUE) | A_BOLD);
printw(" TOXIC " TOXICVER " |"); printw(" TOXIC " TOXICVER " |");
attroff(COLOR_PAIR(4) | A_BOLD); attroff(COLOR_PAIR(BLUE) | A_BOLD);
int i; int i;
@ -199,13 +238,13 @@ static void draw_bar()
odd = (odd + 1) % blinkrate; odd = (odd + 1) % blinkrate;
if (windows[i].blink && (odd < (blinkrate / 2))) if (windows[i].blink && (odd < (blinkrate / 2)))
attron(COLOR_PAIR(3)); attron(COLOR_PAIR(RED));
clrtoeol(); clrtoeol();
printw(" %s", windows[i].title); printw(" %s", windows[i].title);
if (windows[i].blink && (odd < (blinkrate / 2))) if (windows[i].blink && (odd < (blinkrate / 2)))
attroff(COLOR_PAIR(3)); attroff(COLOR_PAIR(RED));
if (windows + i == active_window) { if (windows + i == active_window) {
attroff(A_BOLD); attroff(A_BOLD);