From 34cc4314a507ee86b3146629a6a857f08eb3b9ac Mon Sep 17 00:00:00 2001 From: Jfreegman Date: Fri, 13 Dec 2013 20:57:32 -0500 Subject: [PATCH] added groupchat actions --- src/chat.c | 38 +++++++++----------- src/friendlist.c | 7 ++-- src/groupchat.c | 86 ++++++++++++++++++++++++++++++++++----------- src/main.c | 1 + src/prompt.c | 2 +- src/toxic_windows.h | 2 ++ src/windows.c | 11 ++++++ 7 files changed, 101 insertions(+), 46 deletions(-) diff --git a/src/chat.c b/src/chat.c index 77ad295..488ccbd 100644 --- a/src/chat.c +++ b/src/chat.c @@ -442,34 +442,30 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key) delwin(statusbar->topline); del_window(self); disable_chatwin(f_num); - } else if (!strncmp(line, "/me ", strlen("/me "))) + } else if (strncmp(line, "/me ", strlen("/me ")) == 0) send_action(self, ctx, m, line + strlen("/me ")); else execute(ctx->history, self, m, line, CHAT_COMMAND_MODE); - } else { - /* make sure the string has at least non-space character */ - if (!string_is_empty(line)) { - uint8_t selfname[TOX_MAX_NAME_LENGTH]; - tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH); + } else if (!string_is_empty(line)) { + uint8_t selfname[TOX_MAX_NAME_LENGTH]; + tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH); - print_time(ctx->history); + print_time(ctx->history); + wattron(ctx->history, COLOR_PAIR(GREEN)); + wprintw(ctx->history, "%s: ", selfname); + wattroff(ctx->history, COLOR_PAIR(GREEN)); + + if (line[0] == '>') { wattron(ctx->history, COLOR_PAIR(GREEN)); - wprintw(ctx->history, "%s: ", selfname); + wprintw(ctx->history, "%s\n", line); wattroff(ctx->history, COLOR_PAIR(GREEN)); + } else + wprintw(ctx->history, "%s\n", line); - if (line[0] == '>') { - wattron(ctx->history, COLOR_PAIR(GREEN)); - wprintw(ctx->history, "%s\n", line); - wattroff(ctx->history, COLOR_PAIR(GREEN)); - } else - wprintw(ctx->history, "%s\n", line); - - if (!statusbar->is_online - || tox_send_message(m, self->num, line, strlen(line) + 1) == 0) { - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, " * Failed to send message.\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); - } + if (!statusbar->is_online || tox_send_message(m, self->num, line, strlen(line) + 1) == 0) { + wattron(ctx->history, COLOR_PAIR(RED)); + wprintw(ctx->history, " * Failed to send message.\n"); + wattroff(ctx->history, COLOR_PAIR(RED)); } } diff --git a/src/friendlist.c b/src/friendlist.c index 2aba91d..ab71aaa 100644 --- a/src/friendlist.c +++ b/src/friendlist.c @@ -26,14 +26,15 @@ static int num_selected = 0; ToxicFriend friends[MAX_FRIENDS_NUM]; static int friendlist_index[MAX_FRIENDS_NUM] = {0}; +#define S_WEIGHT 100 + static int index_name_cmp(const void *n1, const void *n2) { int res = qsort_strcasecmp_hlpr(friends[*(int *) n1].name, friends[*(int *) n2].name); - int k = 100; /* Use weight to make qsort always put online friends before offline */ - res = friends[*(int *) n1].online ? (res - k) : (res + k); - res = friends[*(int *) n2].online ? (res + k) : (res - k); + res = friends[*(int *) n1].online ? (res - S_WEIGHT) : (res + S_WEIGHT); + res = friends[*(int *) n2].online ? (res + S_WEIGHT) : (res - S_WEIGHT); return res; } diff --git a/src/groupchat.c b/src/groupchat.c index f0c861f..98166e1 100644 --- a/src/groupchat.c +++ b/src/groupchat.c @@ -138,6 +138,40 @@ static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int } } +static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int peernum, uint8_t *action, + uint16_t len) +{ + if (self->num != groupnum) + return; + + ChatContext *ctx = self->chatwin; + + /* check if message contains own name and alert appropriately */ + int alert_type = WINDOW_ALERT_1; + bool beep = false; + + uint8_t selfnick[TOX_MAX_NAME_LENGTH] = {'\0'}; + tox_get_self_name(m, selfnick, TOX_MAX_NAME_LENGTH); + + bool nick_match = strcasestr(action, selfnick); + + if (nick_match) { + alert_type = WINDOW_ALERT_0; + beep = true; + } + + alert_window(self, alert_type, beep); + + uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; + tox_group_peername(m, groupnum, peernum, nick); + nick[TOXIC_MAX_NAME_LENGTH] = '\0'; /* enforce client max name length */ + + print_time(ctx->history); + wattron(ctx->history, COLOR_PAIR(YELLOW)); + wprintw(ctx->history, "* %s %s\n", nick, action); + wattroff(ctx->history, COLOR_PAIR(YELLOW)); +} + /* Puts two copies of peerlist in chat instance */ static void copy_peernames(int gnum, int npeers, uint8_t tmp_peerlist[][TOX_MAX_NAME_LENGTH]) { @@ -235,6 +269,27 @@ static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnu alert_window(self, WINDOW_ALERT_2, false); } +static void send_group_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *action) { + if (action == NULL) { + wprintw(ctx->history, "Invalid syntax.\n"); + return; + } + + // uint8_t selfname[TOX_MAX_NAME_LENGTH]; + // tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH); + + // print_time(ctx->history); + // wattron(ctx->history, COLOR_PAIR(YELLOW)); + // wprintw(ctx->history, "* %s %s\n", selfname, action); + // wattroff(ctx->history, COLOR_PAIR(YELLOW)); + + if (tox_group_action_send(m, self->num, action, strlen(action) + 1) == -1) { + wattron(ctx->history, COLOR_PAIR(RED)); + wprintw(ctx->history, " * Failed to send action\n"); + wattroff(ctx->history, COLOR_PAIR(RED)); + } +} + static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) { ChatContext *ctx = self->chatwin; @@ -400,35 +455,24 @@ static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key) delwin(ctx->linewin); del_window(self); close_groupchatwin(m, groupnum); - } else if (strncmp(line, "/help", strlen("/help")) == 0) + } else if (strcmp(line, "/help") == 0) print_groupchat_help(ctx); + else if (strncmp(line, "/me ", strlen("/me ")) == 0) + send_group_action(self, ctx, m, line + strlen("/me ")); else execute(ctx->history, self, m, line, GROUPCHAT_COMMAND_MODE); - } else { - /* make sure the string has at least non-space character */ - if (!string_is_empty(line)) { - // uint8_t selfname[TOX_MAX_NAME_LENGTH]; - // tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH); - - // print_time(ctx->history); - // wattron(ctx->history, COLOR_PAIR(GREEN)); - // wprintw(ctx->history, "%s: ", selfname); - // wattroff(ctx->history, COLOR_PAIR(GREEN)); - // wprintw(ctx->history, "%s\n", line); - - if (tox_group_message_send(m, self->num, line, strlen(line) + 1) == -1) { - wattron(ctx->history, COLOR_PAIR(RED)); - wprintw(ctx->history, " * Failed to send message.\n"); - wattroff(ctx->history, COLOR_PAIR(RED)); - } + } else if (!string_is_empty(line)) { + if (tox_group_message_send(m, self->num, line, strlen(line) + 1) == -1) { + wattron(ctx->history, COLOR_PAIR(RED)); + wprintw(ctx->history, " * Failed to send message.\n"); + wattroff(ctx->history, COLOR_PAIR(RED)); } } if (close_win) free(ctx); - else { + else reset_buf(ctx->line, &ctx->pos, &ctx->len); - } } } @@ -507,7 +551,7 @@ ToxWindow new_group_chat(Tox *m, int groupnum) ret.onInit = &groupchat_onInit; ret.onGroupMessage = &groupchat_onGroupMessage; ret.onGroupNamelistChange = &groupchat_onGroupNamelistChange; - // ret.onAction = &groupchat_onAction; + ret.onGroupAction = &groupchat_onGroupAction; snprintf(ret.name, sizeof(ret.name), "Room #%d", groupnum); diff --git a/src/main.c b/src/main.c index f26e02c..06e82cb 100644 --- a/src/main.c +++ b/src/main.c @@ -116,6 +116,7 @@ static Tox *init_tox(int ipv4) tox_callback_friend_action(m, on_action, NULL); tox_callback_group_invite(m, on_groupinvite, NULL); tox_callback_group_message(m, on_groupmessage, NULL); + tox_callback_group_action(m, on_groupaction, NULL); tox_callback_group_namelist_change(m, on_group_namelistchange, NULL); tox_callback_file_send_request(m, on_file_sendrequest, NULL); tox_callback_file_control(m, on_file_control, NULL); diff --git a/src/prompt.c b/src/prompt.c index 70de0a0..098053b 100644 --- a/src/prompt.c +++ b/src/prompt.c @@ -252,7 +252,7 @@ static void prompt_onDraw(ToxWindow *self, Tox *m) if (prt->len > 0) { uint8_t line[MAX_STR_SIZE]; - + if (wcs_to_mbs_buf(line, prt->line, MAX_STR_SIZE) == -1) reset_buf(prt->line, &prt->pos, &prt->len); else diff --git a/src/toxic_windows.h b/src/toxic_windows.h index b345867..30011c0 100644 --- a/src/toxic_windows.h +++ b/src/toxic_windows.h @@ -77,6 +77,7 @@ struct ToxWindow { void(*onStatusMessageChange)(ToxWindow *, int, uint8_t *, uint16_t); void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t); void(*onGroupMessage)(ToxWindow *, Tox *, int, int, uint8_t *, uint16_t); + void(*onGroupAction)(ToxWindow *, Tox *, int, int, uint8_t *, uint16_t); void(*onGroupInvite)(ToxWindow *, Tox *, int, uint8_t *); void(*onGroupNamelistChange)(ToxWindow *, Tox*, int, int, uint8_t); void(*onFileSendRequest)(ToxWindow *, Tox *, int, uint8_t, uint64_t, uint8_t *, uint16_t); @@ -178,6 +179,7 @@ void on_statuschange(Tox *m, int friendnumber, TOX_USERSTATUS status, void *user void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata); void on_friendadded(Tox *m, int friendnumber, bool sort); void on_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *message, uint16_t length, void *userdata); +void on_groupaction(Tox *m, int groupnumber, int peernumber, uint8_t *action, uint16_t length, void *userdata); void on_groupinvite(Tox *m, int friendnumber, uint8_t *group_pub_key, void *userdata); void on_group_namelistchange(Tox *m, int groupnumber, int peernumber, uint8_t change, void *userdata); void on_file_sendrequest(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *pathname, uint16_t pathname_length, void *userdata); diff --git a/src/windows.c b/src/windows.c index dcd7755..a6fb7a3 100644 --- a/src/windows.c +++ b/src/windows.c @@ -117,6 +117,17 @@ void on_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *message, } } +void on_groupaction(Tox *m, int groupnumber, int peernumber, uint8_t *action, uint16_t length, + void *userdata) +{ + int i; + + for (i = 0; i < MAX_WINDOWS_NUM; ++i) { + if (windows[i].onGroupAction != NULL) + windows[i].onGroupAction(&windows[i], m, groupnumber, peernumber, action, length); + } +} + void on_groupinvite(Tox *m, int friendnumber, uint8_t *group_pub_key, void *userdata) { int i;