1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-12-23 15:43:24 +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

@ -52,12 +52,12 @@ static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint1
return;
tox_getname(m, num, (uint8_t *) &nick);
msg[len - 1] = '\0';
nick[TOX_MAX_NAME_LENGTH - 1] = '\0';
msg[len-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);
wattroff(ctx->history, COLOR_PAIR(2));
wattroff(ctx->history, COLOR_PAIR(CYAN));
wattron(ctx->history, COLOR_PAIR(4));
wprintw(ctx->history, "%s: ", nick);
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();
}
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)
{
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';
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);
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);
wattroff(ctx->history, COLOR_PAIR(5));
wattroff(ctx->history, COLOR_PAIR(YELLOW));
self->blink = true;
beep();
@ -100,9 +118,9 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t
if (ctx->friendnum != num)
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);
wattroff(ctx->history, COLOR_PAIR(2));
wattroff(ctx->history, COLOR_PAIR(CYAN));
nick[len - 1] = '\0';
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);
}
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)
{
ChatContext *ctx = (ChatContext *) self->x;
@ -118,13 +168,14 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status
if (ctx->friendnum != num)
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';
wprintw(ctx->history, "* Chat partner changed personal note to: %s\n", status);
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);
}
}
/* check that the string has one non-space character */
@ -230,18 +281,18 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
uint8_t selfname[TOX_MAX_NAME_LENGTH];
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);
wattroff(ctx->history, COLOR_PAIR(2));
wattron(ctx->history, COLOR_PAIR(1));
wattroff(ctx->history, COLOR_PAIR(CYAN));
wattron(ctx->history, COLOR_PAIR(GREEN));
wprintw(ctx->history, "%s: ", selfname);
wattroff(ctx->history, COLOR_PAIR(1));
wattroff(ctx->history, COLOR_PAIR(GREEN));
wprintw(ctx->history, "%s\n", line);
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");
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++;
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);
wattroff(ctx->history, COLOR_PAIR(2));
wattroff(ctx->history, COLOR_PAIR(CYAN));
uint8_t selfname[TOX_MAX_NAME_LENGTH];
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);
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) {
wattron(ctx->history, COLOR_PAIR(3));
wattron(ctx->history, COLOR_PAIR(RED));
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"))) {
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"))) {
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"))) {
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 {
@ -340,7 +400,6 @@ void execute(ToxWindow *self, ChatContext *ctx, Tox *m, char *cmd)
return;
}
wprintw(ctx->history, "Status set to: %s\n", status_text);
tox_set_userstatus(m, status_kind);
msg = strchr(status, ' ');
@ -416,7 +475,7 @@ static void chat_onInit(ToxWindow *self, Tox *m)
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");
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, " /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)
@ -442,7 +501,9 @@ ToxWindow new_chat(Tox *m, int friendnum)
ret.onDraw = &chat_onDraw;
ret.onInit = &chat_onInit;
ret.onMessage = &chat_onMessage;
ret.onConnectionChange = &chat_onConnectionChange;
ret.onNickChange = &chat_onNickChange;
ret.onStatusChange = &chat_onStatusChange;
ret.onStatusMessageChange = &chat_onStatusMessageChange;
ret.onAction = &chat_onAction;

View File

@ -24,7 +24,9 @@ typedef struct {
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH];
int num;
int chatwin;
bool active;
bool active;
bool online;
TOX_USERSTATUS status;
} friend_t;
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)
{
if (num >= num_friends)
if (num < 0 || num >= num_friends)
return;
if (friends[num].chatwin == -1)
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)
{
if (len >= TOX_MAX_NAME_LENGTH || num >= num_friends)
if (len >= TOX_MAX_NAME_LENGTH || num < 0 || num >= num_friends)
return;
memcpy((char *) &friends[num].name, (char *) str, len);
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)
{
if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num >= num_friends)
if (len >= TOX_MAX_STATUSMESSAGE_LENGTH || num < 0 || num >= num_friends)
return;
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].active = true;
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')
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;
}
if (store_data(m, DATA_FILE))
wprintw(self->window, "\nFailed to store messenger data\n");
num_friends = i;
store_data(m, DATA_FILE);
select_friend(m, KEY_DOWN);
}
@ -157,10 +179,10 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
if (num_friends == 0) {
wprintw(self->window, "Empty. Add some friends! :-)\n");
} 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, " 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;
@ -172,26 +194,26 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
else
wprintw(self->window, " ");
if (tox_friendstatus(m, friends[i].num) == TOX_FRIEND_ONLINE) {
TOX_USERSTATUS status = tox_get_userstatus(m, friends[i].num);
int colour = 7; /* Invalid or other errors default to black */
if (friends[i].online) {
TOX_USERSTATUS status = friends[i].status;
int colour = WHITE;
switch(status) {
case TOX_USERSTATUS_NONE:
colour = 1;
colour = GREEN;
break;
case TOX_USERSTATUS_AWAY:
colour = 5;
colour = YELLOW;
break;
case TOX_USERSTATUS_BUSY:
colour = 3;
colour = RED;
break;
}
wprintw(self->window, "[");
wattron(self->window, COLOR_PAIR(colour));
wattron(self->window, COLOR_PAIR(colour) | A_BOLD);
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);
if (friends[i].statusmsg[0])
@ -203,7 +225,7 @@ static void friendlist_onDraw(ToxWindow *self, Tox *m)
}
}
}
wrefresh(self->window);
}
@ -226,8 +248,10 @@ ToxWindow new_friendlist()
ret.onDraw = &friendlist_onDraw;
ret.onInit = &friendlist_onInit;
ret.onMessage = &friendlist_onMessage;
ret.onConnectionChange = &friendlist_onConnectionChange;
ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message
ret.onNickChange = &friendlist_onNickChange;
ret.onStatusChange = &friendlist_onStatusChange;
ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
strcpy(ret.title, "[friends]");

View File

@ -71,6 +71,7 @@ static void init_term()
if (has_colors()) {
start_color();
init_pair(0, COLOR_WHITE, COLOR_BLACK);
init_pair(1, COLOR_GREEN, COLOR_BLACK);
init_pair(2, COLOR_CYAN, 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(7, COLOR_BLACK, COLOR_BLACK);
init_pair(8, COLOR_BLACK, COLOR_WHITE);
}
refresh();
@ -91,9 +91,11 @@ static Tox *init_tox()
Tox *m = tox_new();
/* Callbacks */
tox_callback_connectionstatus(m, on_connectionchange, NULL);
tox_callback_friendrequest(m, on_request, NULL);
tox_callback_friendmessage(m, on_message, NULL);
tox_callback_namechange(m, on_nickchange, NULL);
tox_callback_userstatus(m, on_statuschange, NULL);
tox_callback_statusmessage(m, on_statusmessagechange, NULL);
tox_callback_action(m, on_action, NULL);
#ifdef __linux__
@ -396,17 +398,17 @@ int main(int argc, char *argv[])
load_data(m, DATA_FILE);
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"
"defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD);
attroff(COLOR_PAIR(RED) | A_BOLD);
}
if (config_err) {
attron(COLOR_PAIR(3) | A_BOLD);
attron(COLOR_PAIR(RED) | A_BOLD);
wprintw(prompt->window, "Unable to determine configuration directory.\n"
"defaulting to 'data' for a keyfile...\n");
attroff(COLOR_PAIR(3) | A_BOLD);
attroff(COLOR_PAIR(RED) | A_BOLD);
}
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)
{
wclear(self->window);
wattron(self->window, COLOR_PAIR(2) | A_BOLD);
wattron(self->window, COLOR_PAIR(CYAN) | A_BOLD);
wprintw(self->window, "Commands:\n");
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");
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)
@ -314,6 +314,8 @@ void cmd_nick(ToxWindow *self, Tox *m, int argc, char **argv)
}
nick = argv[1];
if (nick[0] == '\"')
nick[strlen(++nick)-1] = L'\0';
tox_setname(m, (uint8_t *) nick, strlen(nick) + 1);
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"))) {
status_kind = TOX_USERSTATUS_NONE;
status_text = "Online";
} else if (!strncmp(status, "away", strlen("away"))) {
status_kind = TOX_USERSTATUS_AWAY;
status_text = "Away";
} 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: ");
wattron(self->window, COLOR_PAIR(GREEN) | A_BOLD);
wprintw(self->window, "[Online]\n");
wattroff(self->window, COLOR_PAIR(GREEN) | A_BOLD);
}
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);
if (msg != NULL) {
@ -529,9 +543,9 @@ static void prompt_onDraw(ToxWindow *self, Tox *m)
--y;
}
wattron(self->window, COLOR_PAIR(1));
wattron(self->window, COLOR_PAIR(GREEN));
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);
wclrtoeol(self->window);
wrefresh(self->window);

View File

@ -24,6 +24,16 @@
#define TOXICVER "NOVER" //Use the -D flag to set this
#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;
struct ToxWindow_ {
@ -31,8 +41,10 @@ struct ToxWindow_ {
void(*onDraw)(ToxWindow *, Tox *);
void(*onInit)(ToxWindow *, Tox *);
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(*onNickChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onStatusChange)(ToxWindow *, Tox *, int, TOX_USERSTATUS);
void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t);
void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t);
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_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_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_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_friendadded(Tox *m, int friendnumber);
ToxWindow *init_windows();

View File

@ -17,6 +17,8 @@ static ToxWindow *active_window;
static ToxWindow *prompt;
static Tox *m;
#define UNKNOWN_NAME "Unknown"
/* CALLBACKS START */
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, "\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) {
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)
{
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)
{
wprintw(prompt->window, "\n(nick change) %d: %s\n", friendnumber, string);
int 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)
{
wprintw(prompt->window, "\n(note change) %d: %s\n", friendnumber, string);
int 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)
{
friendlist_onFriendAdded(m, friendnumber);
@ -179,15 +218,15 @@ static void draw_bar()
static int odd = 0;
int blinkrate = 30;
attron(COLOR_PAIR(4));
attron(COLOR_PAIR(BLUE));
mvhline(LINES - 2, 0, '_', COLS);
attroff(COLOR_PAIR(4));
attroff(COLOR_PAIR(BLUE));
move(LINES - 1, 0);
attron(COLOR_PAIR(4) | A_BOLD);
attron(COLOR_PAIR(BLUE) | A_BOLD);
printw(" TOXIC " TOXICVER " |");
attroff(COLOR_PAIR(4) | A_BOLD);
attroff(COLOR_PAIR(BLUE) | A_BOLD);
int i;
@ -199,13 +238,13 @@ static void draw_bar()
odd = (odd + 1) % blinkrate;
if (windows[i].blink && (odd < (blinkrate / 2)))
attron(COLOR_PAIR(3));
attron(COLOR_PAIR(RED));
clrtoeol();
printw(" %s", windows[i].title);
if (windows[i].blink && (odd < (blinkrate / 2)))
attroff(COLOR_PAIR(3));
attroff(COLOR_PAIR(RED));
if (windows + i == active_window) {
attroff(A_BOLD);