2014-02-25 08:28:24 +01:00
|
|
|
/* groupchat.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2013-09-15 22:38:38 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "toxic_windows.h"
|
2013-11-10 03:43:56 +01:00
|
|
|
#include "execute.h"
|
2013-09-21 02:35:03 +02:00
|
|
|
#include "misc_tools.h"
|
2013-11-24 03:19:59 +01:00
|
|
|
#include "groupchat.h"
|
2013-12-09 23:56:20 +01:00
|
|
|
#include "prompt.h"
|
2013-12-11 06:10:09 +01:00
|
|
|
#include "toxic_strings.h"
|
2013-09-15 22:38:38 +02:00
|
|
|
|
|
|
|
extern char *DATA_FILE;
|
|
|
|
extern int store_data(Tox *m, char *path);
|
|
|
|
|
2013-11-30 11:35:25 +01:00
|
|
|
static GroupChat groupchats[MAX_WINDOWS_NUM];
|
2013-11-30 00:52:21 +01:00
|
|
|
static int max_groupchat_index = 0;
|
|
|
|
|
2013-12-09 23:56:20 +01:00
|
|
|
/* temporary until group chats have unique commands */
|
|
|
|
extern glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE];
|
2013-12-09 00:14:57 +01:00
|
|
|
|
2013-09-17 12:03:08 +02:00
|
|
|
int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum)
|
2013-09-15 22:38:38 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-11-17 22:09:14 +01:00
|
|
|
for (i = 0; i <= max_groupchat_index; ++i) {
|
2013-09-15 22:38:38 +02:00
|
|
|
if (!groupchats[i].active) {
|
2013-11-19 21:32:35 +01:00
|
|
|
groupchats[i].chatwin = add_window(m, new_group_chat(m, groupnum));
|
2013-09-15 22:38:38 +02:00
|
|
|
groupchats[i].active = true;
|
2013-11-24 03:19:59 +01:00
|
|
|
groupchats[i].num_peers = 0;
|
2013-11-27 07:54:22 +01:00
|
|
|
groupchats[i].peer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH);
|
|
|
|
groupchats[i].oldpeer_names = malloc(sizeof(uint8_t) * TOX_MAX_NAME_LENGTH);
|
2013-11-28 04:46:09 +01:00
|
|
|
|
|
|
|
/* temp fix */
|
|
|
|
memcpy(&groupchats[i].oldpeer_names[0], UNKNOWN_NAME, sizeof(UNKNOWN_NAME));
|
2013-11-27 04:15:48 +01:00
|
|
|
|
2013-11-27 04:16:18 +01:00
|
|
|
set_active_window(groupchats[i].chatwin);
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2013-11-17 22:09:14 +01:00
|
|
|
if (i == max_groupchat_index)
|
|
|
|
++max_groupchat_index;
|
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void close_groupchatwin(Tox *m, int groupnum)
|
|
|
|
{
|
|
|
|
tox_del_groupchat(m, groupnum);
|
2013-11-27 02:45:08 +01:00
|
|
|
|
|
|
|
free(groupchats[groupnum].peer_names);
|
|
|
|
free(groupchats[groupnum].oldpeer_names);
|
2013-11-18 01:45:53 +01:00
|
|
|
memset(&groupchats[groupnum], 0, sizeof(GroupChat));
|
2013-09-15 22:38:38 +02:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2013-11-17 22:09:14 +01:00
|
|
|
for (i = max_groupchat_index; i > 0; --i) {
|
2013-09-17 12:05:31 +02:00
|
|
|
if (groupchats[i-1].active)
|
2013-09-15 22:38:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-11-17 22:09:14 +01:00
|
|
|
max_groupchat_index = i;
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
|
|
|
|
2013-09-19 12:37:42 +02:00
|
|
|
static void print_groupchat_help(ChatContext *ctx)
|
|
|
|
{
|
|
|
|
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
|
|
|
wprintw(ctx->history, "Group chat commands:\n");
|
2013-12-04 00:01:17 +01:00
|
|
|
wattroff(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
2013-09-19 12:37:42 +02:00
|
|
|
|
2013-11-24 03:19:59 +01:00
|
|
|
wprintw(ctx->history, " /add <id> <msg> : Add friend with optional message\n");
|
|
|
|
wprintw(ctx->history, " /status <type> <msg>: Set your status with optional note\n");
|
2013-12-04 00:01:17 +01:00
|
|
|
wprintw(ctx->history, " /note <msg> : Set a personal note\n");
|
2013-11-24 03:19:59 +01:00
|
|
|
wprintw(ctx->history, " /nick <nick> : Set your nickname\n");
|
|
|
|
wprintw(ctx->history, " /groupchat : Create a group chat\n");
|
|
|
|
wprintw(ctx->history, " /myid : Print your ID\n");
|
|
|
|
wprintw(ctx->history, " /clear : Clear the screen\n");
|
|
|
|
wprintw(ctx->history, " /close : Close the current group chat\n");
|
|
|
|
wprintw(ctx->history, " /quit or /exit : Exit Toxic\n");
|
|
|
|
wprintw(ctx->history, " /help : Print this message again\n");
|
2013-09-19 12:37:42 +02:00
|
|
|
|
2013-12-04 00:01:17 +01:00
|
|
|
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
|
|
|
wprintw(ctx->history, " * Argument messages must be enclosed in quotation marks.\n");
|
|
|
|
wprintw(ctx->history, " * Scroll peer list with the Page Up/Page Down keys.\n\n");
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
2013-09-19 12:37:42 +02:00
|
|
|
}
|
|
|
|
|
2013-10-11 10:42:30 +02:00
|
|
|
static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int peernum,
|
|
|
|
uint8_t *msg, uint16_t len)
|
2013-09-15 22:38:38 +02:00
|
|
|
{
|
|
|
|
if (self->num != groupnum)
|
|
|
|
return;
|
|
|
|
|
2013-11-29 23:48:08 +01:00
|
|
|
ChatContext *ctx = self->chatwin;
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2014-02-10 04:55:42 +01:00
|
|
|
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 */
|
|
|
|
|
2013-12-07 01:41:53 +01:00
|
|
|
/* 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);
|
2014-02-17 05:15:27 +01:00
|
|
|
int nick_clr = strcmp(nick, selfnick) == 0 ? GREEN : CYAN;
|
2013-12-07 01:41:53 +01:00
|
|
|
|
2014-02-10 04:55:42 +01:00
|
|
|
bool nick_match = strcasestr(msg, selfnick) && strncmp(selfnick, nick, TOXIC_MAX_NAME_LENGTH);
|
2013-12-07 01:41:53 +01:00
|
|
|
|
|
|
|
if (nick_match) {
|
|
|
|
alert_type = WINDOW_ALERT_0;
|
|
|
|
beep = true;
|
2013-12-08 11:45:23 +01:00
|
|
|
nick_clr = RED;
|
2013-12-07 01:41:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
alert_window(self, alert_type, beep);
|
|
|
|
|
2013-10-11 07:11:43 +02:00
|
|
|
print_time(ctx->history);
|
2013-12-08 11:45:23 +01:00
|
|
|
wattron(ctx->history, COLOR_PAIR(nick_clr));
|
2013-11-26 02:20:51 +01:00
|
|
|
wprintw(ctx->history, "%s: ", nick);
|
2013-12-08 11:45:23 +01:00
|
|
|
wattroff(ctx->history, COLOR_PAIR(nick_clr));
|
2013-09-26 06:33:51 +02:00
|
|
|
|
2013-12-08 11:45:23 +01:00
|
|
|
if (msg[0] == '>') {
|
2013-09-26 06:33:51 +02:00
|
|
|
wattron(ctx->history, COLOR_PAIR(GREEN));
|
|
|
|
wprintw(ctx->history, "%s\n", msg);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(GREEN));
|
2013-12-07 01:41:53 +01:00
|
|
|
} else {
|
2013-09-26 06:33:51 +02:00
|
|
|
wprintw(ctx->history, "%s\n", msg);
|
2013-12-07 01:41:53 +01:00
|
|
|
}
|
2014-02-26 09:51:26 +01:00
|
|
|
|
|
|
|
add_to_log_buf(msg, nick, ctx);
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
|
|
|
|
2013-12-14 02:57:32 +01:00
|
|
|
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));
|
2014-02-26 09:51:26 +01:00
|
|
|
|
|
|
|
add_to_log_buf(action, nick, ctx);
|
2013-12-14 02:57:32 +01:00
|
|
|
}
|
|
|
|
|
2013-11-27 02:45:08 +01:00
|
|
|
/* Puts two copies of peerlist in chat instance */
|
|
|
|
static void copy_peernames(int gnum, int npeers, uint8_t tmp_peerlist[][TOX_MAX_NAME_LENGTH])
|
|
|
|
{
|
2013-11-28 01:29:58 +01:00
|
|
|
/* Assumes these are initiated in init_groupchat_win */
|
2013-11-27 02:45:08 +01:00
|
|
|
free(groupchats[gnum].peer_names);
|
|
|
|
free(groupchats[gnum].oldpeer_names);
|
|
|
|
|
2013-11-27 03:06:06 +01:00
|
|
|
int N = TOX_MAX_NAME_LENGTH;
|
|
|
|
|
2013-11-27 07:54:22 +01:00
|
|
|
groupchats[gnum].peer_names = malloc(sizeof(uint8_t) * npeers * N);
|
|
|
|
groupchats[gnum].oldpeer_names = malloc(sizeof(uint8_t) * npeers * N);
|
2013-11-27 02:45:08 +01:00
|
|
|
|
|
|
|
if (groupchats[gnum].peer_names == NULL || groupchats[gnum].oldpeer_names == NULL) {
|
|
|
|
endwin();
|
|
|
|
fprintf(stderr, "malloc() failed. Aborting...\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < npeers; ++i) {
|
2014-02-11 02:13:22 +01:00
|
|
|
if (string_is_empty(tmp_peerlist[i])) {
|
2013-11-28 01:29:58 +01:00
|
|
|
memcpy(&groupchats[gnum].peer_names[i*N], UNKNOWN_NAME, sizeof(UNKNOWN_NAME));
|
2014-02-11 02:13:22 +01:00
|
|
|
} else {
|
2013-11-28 01:29:58 +01:00
|
|
|
memcpy(&groupchats[gnum].peer_names[i*N], tmp_peerlist[i], N);
|
2014-02-11 02:13:22 +01:00
|
|
|
groupchats[gnum].peer_names[i*N+TOXIC_MAX_NAME_LENGTH] = '\0';
|
|
|
|
}
|
2013-11-27 02:45:08 +01:00
|
|
|
}
|
2013-11-28 01:29:58 +01:00
|
|
|
|
|
|
|
memcpy(groupchats[gnum].oldpeer_names, groupchats[gnum].peer_names, N*npeers);
|
2013-11-27 02:45:08 +01:00
|
|
|
}
|
|
|
|
|
2013-11-26 00:49:31 +01:00
|
|
|
static void groupchat_onGroupNamelistChange(ToxWindow *self, Tox *m, int groupnum, int peernum,
|
|
|
|
uint8_t change)
|
2013-11-24 03:19:59 +01:00
|
|
|
{
|
|
|
|
if (self->num != groupnum)
|
|
|
|
return;
|
|
|
|
|
2013-11-26 23:39:11 +01:00
|
|
|
groupchats[groupnum].num_peers = tox_group_number_peers(m, groupnum);
|
2013-11-27 02:45:08 +01:00
|
|
|
int num_peers = groupchats[groupnum].num_peers;
|
2013-11-26 00:49:31 +01:00
|
|
|
|
|
|
|
/* get old peer name before updating name list */
|
|
|
|
uint8_t oldpeername[TOX_MAX_NAME_LENGTH] = {0};
|
|
|
|
|
2014-02-12 00:52:04 +01:00
|
|
|
if (change != TOX_CHAT_CHANGE_PEER_ADD)
|
2013-11-28 04:46:09 +01:00
|
|
|
memcpy(oldpeername, &groupchats[groupnum].oldpeer_names[peernum*TOX_MAX_NAME_LENGTH],
|
|
|
|
sizeof(oldpeername));
|
2013-11-26 00:49:31 +01:00
|
|
|
|
2013-11-28 01:29:58 +01:00
|
|
|
/* Update name lists */
|
2013-11-27 02:45:08 +01:00
|
|
|
uint8_t tmp_peerlist[num_peers][TOX_MAX_NAME_LENGTH];
|
2013-11-29 16:14:59 +01:00
|
|
|
tox_group_get_names(m, groupnum, tmp_peerlist, num_peers);
|
2013-11-28 01:29:58 +01:00
|
|
|
copy_peernames(groupnum, num_peers, tmp_peerlist);
|
2013-11-26 00:49:31 +01:00
|
|
|
|
2013-11-28 01:29:58 +01:00
|
|
|
/* get current peername then sort namelist */
|
|
|
|
uint8_t peername[TOX_MAX_NAME_LENGTH] = {0};
|
|
|
|
memcpy(peername, &groupchats[groupnum].peer_names[peernum*TOX_MAX_NAME_LENGTH], sizeof(peername));
|
2013-11-26 00:49:31 +01:00
|
|
|
|
2013-12-08 07:18:10 +01:00
|
|
|
qsort(groupchats[groupnum].peer_names, groupchats[groupnum].num_peers, TOX_MAX_NAME_LENGTH, qsort_strcasecmp_hlpr);
|
2013-11-26 00:49:31 +01:00
|
|
|
|
2013-11-29 23:48:08 +01:00
|
|
|
ChatContext *ctx = self->chatwin;
|
2013-11-26 00:49:31 +01:00
|
|
|
print_time(ctx->history);
|
|
|
|
|
|
|
|
switch (change) {
|
|
|
|
case TOX_CHAT_CHANGE_PEER_ADD:
|
|
|
|
wattron(ctx->history, COLOR_PAIR(GREEN));
|
|
|
|
wattron(ctx->history, A_BOLD);
|
2013-11-26 02:20:51 +01:00
|
|
|
wprintw(ctx->history, "* %s", peername);
|
2013-11-26 00:49:31 +01:00
|
|
|
wattroff(ctx->history, A_BOLD);
|
2013-11-28 01:29:58 +01:00
|
|
|
wprintw(ctx->history, " has joined the room\n");
|
2013-11-26 00:49:31 +01:00
|
|
|
wattroff(ctx->history, COLOR_PAIR(GREEN));
|
|
|
|
break;
|
|
|
|
case TOX_CHAT_CHANGE_PEER_DEL:
|
|
|
|
wattron(ctx->history, A_BOLD);
|
2013-11-26 02:20:51 +01:00
|
|
|
wprintw(ctx->history, "* %s", oldpeername);
|
2013-11-26 00:49:31 +01:00
|
|
|
wattroff(ctx->history, A_BOLD);
|
2013-11-28 01:29:58 +01:00
|
|
|
wprintw(ctx->history, " has left the room\n");
|
2013-12-03 11:18:49 +01:00
|
|
|
|
|
|
|
if (groupchats[self->num].side_pos > 0)
|
|
|
|
--groupchats[self->num].side_pos;
|
|
|
|
|
2013-11-26 00:49:31 +01:00
|
|
|
break;
|
|
|
|
case TOX_CHAT_CHANGE_PEER_NAME:
|
|
|
|
wattron(ctx->history, COLOR_PAIR(MAGENTA));
|
|
|
|
wattron(ctx->history, A_BOLD);
|
2013-11-26 02:20:51 +01:00
|
|
|
wprintw(ctx->history, "* %s", oldpeername);
|
2013-11-26 00:49:31 +01:00
|
|
|
wattroff(ctx->history, A_BOLD);
|
|
|
|
|
|
|
|
wprintw(ctx->history, " is now known as ");
|
|
|
|
|
|
|
|
wattron(ctx->history, A_BOLD);
|
2013-11-28 01:29:58 +01:00
|
|
|
wprintw(ctx->history, "%s\n", peername);
|
2013-11-26 00:49:31 +01:00
|
|
|
wattroff(ctx->history, A_BOLD);
|
|
|
|
wattroff(ctx->history, COLOR_PAIR(MAGENTA));
|
|
|
|
break;
|
|
|
|
}
|
2013-11-29 01:45:28 +01:00
|
|
|
|
|
|
|
alert_window(self, WINDOW_ALERT_2, false);
|
2013-11-24 03:19:59 +01:00
|
|
|
}
|
|
|
|
|
2013-12-14 02:57:32 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key)
|
|
|
|
{
|
2013-11-29 23:48:08 +01:00
|
|
|
ChatContext *ctx = self->chatwin;
|
2013-09-15 22:38:38 +02:00
|
|
|
|
|
|
|
int x, y, y2, x2;
|
|
|
|
getyx(self->window, y, x);
|
|
|
|
getmaxyx(self->window, y2, x2);
|
2013-12-12 06:55:42 +01:00
|
|
|
int cur_len = 0;
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2013-12-01 08:58:21 +01:00
|
|
|
if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */
|
2013-09-26 13:52:17 +02:00
|
|
|
if (ctx->pos > 0) {
|
2013-12-14 21:38:21 +01:00
|
|
|
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1]));
|
2013-12-01 08:58:21 +01:00
|
|
|
del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len);
|
2013-09-26 13:52:17 +02:00
|
|
|
|
|
|
|
if (x == 0)
|
2013-12-12 06:55:42 +01:00
|
|
|
wmove(self->window, y-1, x2 - cur_len);
|
2013-09-26 13:52:17 +02:00
|
|
|
else
|
2013-12-12 06:55:42 +01:00
|
|
|
wmove(self->window, y, x - cur_len);
|
2013-12-16 02:52:10 +01:00
|
|
|
} else {
|
|
|
|
beep();
|
2013-09-26 13:52:17 +02:00
|
|
|
}
|
2013-12-01 22:59:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (key == KEY_DC) { /* DEL key: Remove character at pos */
|
2013-12-04 22:21:32 +01:00
|
|
|
if (ctx->pos != ctx->len)
|
|
|
|
del_char_buf_frnt(ctx->line, &ctx->pos, &ctx->len);
|
2013-12-16 02:52:10 +01:00
|
|
|
else
|
|
|
|
beep();
|
2013-12-04 22:21:32 +01:00
|
|
|
}
|
2013-12-01 22:59:46 +01:00
|
|
|
|
2013-12-06 04:55:14 +01:00
|
|
|
else if (key == T_KEY_DISCARD) { /* CTRL-U: Delete entire line behind pos */
|
|
|
|
if (ctx->pos > 0) {
|
|
|
|
discard_buf(ctx->line, &ctx->pos, &ctx->len);
|
|
|
|
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
|
2013-12-16 02:52:10 +01:00
|
|
|
} else {
|
|
|
|
beep();
|
2013-12-06 04:55:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (key == T_KEY_KILL) { /* CTRL-K: Delete entire line in front of pos */
|
|
|
|
if (ctx->pos != ctx->len)
|
|
|
|
kill_buf(ctx->line, &ctx->pos, &ctx->len);
|
2013-12-16 02:52:10 +01:00
|
|
|
else
|
|
|
|
beep();
|
2013-12-06 04:55:14 +01:00
|
|
|
}
|
|
|
|
|
2013-12-01 22:59:46 +01:00
|
|
|
else if (key == KEY_HOME) { /* HOME key: Move cursor to beginning of line */
|
2013-12-04 22:21:32 +01:00
|
|
|
if (ctx->pos > 0) {
|
|
|
|
ctx->pos = 0;
|
|
|
|
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
|
|
|
|
}
|
|
|
|
}
|
2013-12-01 22:59:46 +01:00
|
|
|
|
|
|
|
else if (key == KEY_END) { /* END key: move cursor to end of line */
|
2013-12-04 22:21:32 +01:00
|
|
|
if (ctx->pos != ctx->len) {
|
|
|
|
ctx->pos = ctx->len;
|
2013-12-14 07:10:22 +01:00
|
|
|
mv_curs_end(self->window, MAX(0, wcswidth(ctx->line, (CHATBOX_HEIGHT-1)*x2)), y2, x2);
|
2013-12-04 22:21:32 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-01 22:59:46 +01:00
|
|
|
|
|
|
|
else if (key == KEY_LEFT) {
|
2013-12-01 19:25:03 +01:00
|
|
|
if (ctx->pos > 0) {
|
|
|
|
--ctx->pos;
|
2013-12-14 07:10:22 +01:00
|
|
|
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
|
2013-12-01 04:12:43 +01:00
|
|
|
|
2013-12-01 19:25:03 +01:00
|
|
|
if (x == 0)
|
2013-12-12 06:55:42 +01:00
|
|
|
wmove(self->window, y-1, x2 - cur_len);
|
2013-12-01 19:25:03 +01:00
|
|
|
else
|
2013-12-12 06:55:42 +01:00
|
|
|
wmove(self->window, y, x - cur_len);
|
2013-12-16 02:52:10 +01:00
|
|
|
} else {
|
|
|
|
beep();
|
2013-12-01 19:25:03 +01:00
|
|
|
}
|
2013-12-01 22:59:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (key == KEY_RIGHT) {
|
2013-12-01 19:25:03 +01:00
|
|
|
if (ctx->pos < ctx->len) {
|
2013-12-14 07:10:22 +01:00
|
|
|
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos]));
|
2013-12-01 19:25:03 +01:00
|
|
|
++ctx->pos;
|
2013-12-01 04:12:43 +01:00
|
|
|
|
2013-12-01 19:25:03 +01:00
|
|
|
if (x == x2-1)
|
|
|
|
wmove(self->window, y+1, 0);
|
|
|
|
else
|
2013-12-12 06:55:42 +01:00
|
|
|
wmove(self->window, y, x + cur_len);
|
2013-12-16 02:52:10 +01:00
|
|
|
} else {
|
|
|
|
beep();
|
2013-12-01 19:25:03 +01:00
|
|
|
}
|
2013-12-03 09:44:02 +01:00
|
|
|
}
|
|
|
|
|
2013-12-11 06:10:09 +01:00
|
|
|
else if (key == KEY_UP) { /* fetches previous item in history */
|
2013-12-14 21:38:21 +01:00
|
|
|
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot,
|
|
|
|
&ctx->hst_pos, LN_HIST_MV_UP);
|
|
|
|
mv_curs_end(self->window, ctx->len, y2, x2);
|
2013-12-11 06:10:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else if (key == KEY_DOWN) { /* fetches next item in history */
|
2013-12-14 21:38:21 +01:00
|
|
|
fetch_hist_item(ctx->line, &ctx->pos, &ctx->len, ctx->ln_history, ctx->hst_tot,
|
|
|
|
&ctx->hst_pos, LN_HIST_MV_DWN);
|
|
|
|
mv_curs_end(self->window, ctx->len, y2, x2);
|
2013-12-11 06:10:09 +01:00
|
|
|
}
|
|
|
|
|
2013-12-08 04:10:32 +01:00
|
|
|
else if (key == '\t') { /* TAB key: completes peer name */
|
2013-12-08 07:07:22 +01:00
|
|
|
if (ctx->len > 0) {
|
2013-12-09 00:14:57 +01:00
|
|
|
int diff;
|
2013-12-19 02:18:02 +01:00
|
|
|
|
|
|
|
if ((ctx->line[0] != '/') || (ctx->line[1] == 'm' && ctx->line[2] == 'e'))
|
2013-12-09 00:14:57 +01:00
|
|
|
diff = complete_line(ctx->line, &ctx->pos, &ctx->len, groupchats[self->num].peer_names,
|
2013-12-08 04:10:32 +01:00
|
|
|
groupchats[self->num].num_peers, TOX_MAX_NAME_LENGTH);
|
2013-12-09 00:14:57 +01:00
|
|
|
else
|
2013-12-09 23:56:20 +01:00
|
|
|
diff = complete_line(ctx->line, &ctx->pos, &ctx->len, glob_cmd_list, AC_NUM_GLOB_COMMANDS,
|
2013-12-09 00:14:57 +01:00
|
|
|
MAX_CMDNAME_SIZE);
|
2013-12-08 04:10:32 +01:00
|
|
|
|
2013-12-08 10:16:49 +01:00
|
|
|
if (diff != -1) {
|
2013-12-08 04:10:32 +01:00
|
|
|
if (x + diff > x2 - 1) {
|
|
|
|
int ofst = (x + diff - 1) - (x2 - 1);
|
|
|
|
wmove(self->window, y+1, ofst);
|
|
|
|
} else {
|
|
|
|
wmove(self->window, y, x+diff);
|
|
|
|
}
|
2013-12-16 02:52:10 +01:00
|
|
|
} else {
|
|
|
|
beep();
|
2013-12-08 04:10:32 +01:00
|
|
|
}
|
2013-12-16 02:52:10 +01:00
|
|
|
} else {
|
|
|
|
beep();
|
2013-12-08 04:10:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-03 09:44:02 +01:00
|
|
|
/* Scroll peerlist up and down one position if list overflows window */
|
|
|
|
else if (key == KEY_NPAGE) {
|
|
|
|
int L = y2 - CHATBOX_HEIGHT - SDBAR_OFST;
|
|
|
|
|
|
|
|
if (groupchats[self->num].side_pos < groupchats[self->num].num_peers - L)
|
|
|
|
++groupchats[self->num].side_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (key == KEY_PPAGE) {
|
|
|
|
if (groupchats[self->num].side_pos > 0)
|
|
|
|
--groupchats[self->num].side_pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
2013-09-15 22:38:38 +02:00
|
|
|
#if HAVE_WIDECHAR
|
2013-11-29 04:28:40 +01:00
|
|
|
if (iswprint(key))
|
2013-09-15 22:38:38 +02:00
|
|
|
#else
|
2013-11-29 04:28:40 +01:00
|
|
|
if (isprint(key))
|
2013-09-15 22:38:38 +02:00
|
|
|
#endif
|
2013-12-01 04:12:43 +01:00
|
|
|
{ /* prevents buffer overflows and strange behaviour when cursor goes past the window */
|
|
|
|
if ( (ctx->len < MAX_STR_SIZE-1) && (ctx->len < (x2 * (CHATBOX_HEIGHT - 1)-1)) ) {
|
2013-12-03 00:23:04 +01:00
|
|
|
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key);
|
2013-12-01 04:12:43 +01:00
|
|
|
|
|
|
|
if (x == x2-1)
|
|
|
|
wmove(self->window, y+1, 0);
|
|
|
|
else
|
2013-12-14 07:10:22 +01:00
|
|
|
wmove(self->window, y, x + MAX(1, wcwidth(key)));
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* RETURN key: Execute command or print line */
|
|
|
|
else if (key == '\n') {
|
2013-12-08 10:16:49 +01:00
|
|
|
uint8_t line[MAX_STR_SIZE];
|
|
|
|
|
|
|
|
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1)
|
|
|
|
memset(&line, 0, sizeof(line));
|
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
wclear(ctx->linewin);
|
|
|
|
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
|
|
|
|
wclrtobot(self->window);
|
|
|
|
bool close_win = false;
|
|
|
|
|
2013-12-11 06:10:09 +01:00
|
|
|
if (!string_is_empty(line))
|
|
|
|
add_line_to_hist(ctx->line, ctx->len, ctx->ln_history, &ctx->hst_tot, &ctx->hst_pos);
|
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
if (line[0] == '/') {
|
2013-11-19 21:32:35 +01:00
|
|
|
if (close_win = strcmp(line, "/close") == 0) {
|
2014-02-26 09:51:26 +01:00
|
|
|
write_to_log(ctx);
|
2013-09-15 22:38:38 +02:00
|
|
|
set_active_window(0);
|
2013-09-17 12:03:08 +02:00
|
|
|
int groupnum = self->num;
|
2013-09-15 22:38:38 +02:00
|
|
|
delwin(ctx->linewin);
|
|
|
|
del_window(self);
|
2013-09-17 12:03:08 +02:00
|
|
|
close_groupchatwin(m, groupnum);
|
2013-12-14 02:57:32 +01:00
|
|
|
} else if (strcmp(line, "/help") == 0)
|
2013-09-19 12:37:42 +02:00
|
|
|
print_groupchat_help(ctx);
|
2013-12-14 02:57:32 +01:00
|
|
|
else if (strncmp(line, "/me ", strlen("/me ")) == 0)
|
|
|
|
send_group_action(self, ctx, m, line + strlen("/me "));
|
2013-09-19 12:37:42 +02:00
|
|
|
else
|
2013-11-19 21:32:35 +01:00
|
|
|
execute(ctx->history, self, m, line, GROUPCHAT_COMMAND_MODE);
|
2013-12-14 02:57:32 +01:00
|
|
|
} 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));
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (close_win)
|
|
|
|
free(ctx);
|
2013-12-14 02:57:32 +01:00
|
|
|
else
|
2013-12-01 04:12:43 +01:00
|
|
|
reset_buf(ctx->line, &ctx->pos, &ctx->len);
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void groupchat_onDraw(ToxWindow *self, Tox *m)
|
|
|
|
{
|
|
|
|
curs_set(1);
|
2013-12-03 09:44:02 +01:00
|
|
|
int x2, y2;
|
|
|
|
getmaxyx(self->window, y2, x2);
|
2013-11-24 03:19:59 +01:00
|
|
|
|
2013-11-29 23:48:08 +01:00
|
|
|
ChatContext *ctx = self->chatwin;
|
2013-12-01 04:12:43 +01:00
|
|
|
|
|
|
|
wclear(ctx->linewin);
|
2013-12-08 10:16:49 +01:00
|
|
|
|
2013-12-12 06:37:29 +01:00
|
|
|
if (ctx->len > 0) {
|
|
|
|
uint8_t line[MAX_STR_SIZE];
|
2013-12-08 10:16:49 +01:00
|
|
|
|
2013-12-12 06:37:29 +01:00
|
|
|
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) {
|
|
|
|
reset_buf(ctx->line, &ctx->pos, &ctx->len);
|
|
|
|
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
|
|
|
|
} else {
|
|
|
|
mvwprintw(ctx->linewin, 1, 0, "%s", line);
|
|
|
|
}
|
|
|
|
}
|
2013-12-01 04:12:43 +01:00
|
|
|
|
2013-12-03 09:44:02 +01:00
|
|
|
wclear(ctx->sidebar);
|
|
|
|
mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x2);
|
|
|
|
mvwvline(ctx->sidebar, 0, 0, ACS_VLINE, y2-CHATBOX_HEIGHT);
|
|
|
|
mvwaddch(ctx->sidebar, y2-CHATBOX_HEIGHT, 0, ACS_BTEE);
|
2013-11-24 03:19:59 +01:00
|
|
|
|
|
|
|
int num_peers = groupchats[self->num].num_peers;
|
2013-11-29 07:30:10 +01:00
|
|
|
|
|
|
|
wmove(ctx->sidebar, 0, 1);
|
|
|
|
wattron(ctx->sidebar, A_BOLD);
|
|
|
|
wprintw(ctx->sidebar, "Peers: %d\n", num_peers);
|
|
|
|
wattroff(ctx->sidebar, A_BOLD);
|
|
|
|
|
|
|
|
mvwaddch(ctx->sidebar, 1, 0, ACS_LTEE);
|
|
|
|
mvwhline(ctx->sidebar, 1, 1, ACS_HLINE, SIDEBAR_WIDTH-1);
|
|
|
|
|
2013-11-27 03:06:06 +01:00
|
|
|
int N = TOX_MAX_NAME_LENGTH;
|
2013-12-03 09:44:02 +01:00
|
|
|
int maxlines = y2 - SDBAR_OFST - CHATBOX_HEIGHT;
|
2013-11-27 03:06:06 +01:00
|
|
|
int i;
|
|
|
|
|
2013-11-27 07:54:22 +01:00
|
|
|
for (i = 0; i < num_peers && i < maxlines; ++i) {
|
2013-11-29 07:30:10 +01:00
|
|
|
wmove(ctx->sidebar, i+2, 1);
|
2013-12-03 09:44:02 +01:00
|
|
|
int peer = i + groupchats[self->num].side_pos;
|
2014-01-31 04:01:08 +01:00
|
|
|
|
|
|
|
/* truncate nick to fit in side panel without modifying list */
|
|
|
|
uint8_t tmpnck[TOX_MAX_NAME_LENGTH];
|
2014-01-31 04:03:26 +01:00
|
|
|
memcpy(tmpnck, &groupchats[self->num].peer_names[peer*N], SIDEBAR_WIDTH-2);
|
2014-01-31 04:01:08 +01:00
|
|
|
tmpnck[SIDEBAR_WIDTH-2] = '\0';
|
|
|
|
|
|
|
|
wprintw(ctx->sidebar, "%s\n", tmpnck);
|
2013-11-24 03:19:59 +01:00
|
|
|
}
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void groupchat_onInit(ToxWindow *self, Tox *m)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
getmaxyx(self->window, y, x);
|
2013-11-24 03:19:59 +01:00
|
|
|
|
2013-11-29 23:48:08 +01:00
|
|
|
ChatContext *ctx = self->chatwin;
|
2013-11-24 03:19:59 +01:00
|
|
|
ctx->history = subwin(self->window, y-CHATBOX_HEIGHT+1, x-SIDEBAR_WIDTH-1, 0, 0);
|
2013-09-15 22:38:38 +02:00
|
|
|
scrollok(ctx->history, 1);
|
2013-12-01 04:12:43 +01:00
|
|
|
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x, y-CHATBOX_HEIGHT, 0);
|
2013-11-24 03:19:59 +01:00
|
|
|
ctx->sidebar = subwin(self->window, y-CHATBOX_HEIGHT+1, SIDEBAR_WIDTH, 0, x-SIDEBAR_WIDTH);
|
|
|
|
|
2013-09-19 12:37:42 +02:00
|
|
|
print_groupchat_help(ctx);
|
2013-11-24 03:19:59 +01:00
|
|
|
wmove(self->window, y-CURS_Y_OFFSET, 0);
|
2014-02-26 09:51:26 +01:00
|
|
|
|
|
|
|
ctx->log.log_on = true;
|
|
|
|
init_logging_session(self->name, groupchats[self->num].groupkey, ctx);
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
ToxWindow new_group_chat(Tox *m, int groupnum)
|
2013-09-15 22:38:38 +02:00
|
|
|
{
|
|
|
|
ToxWindow ret;
|
|
|
|
memset(&ret, 0, sizeof(ret));
|
|
|
|
|
2013-11-30 11:35:25 +01:00
|
|
|
ret.active = true;
|
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
ret.onKey = &groupchat_onKey;
|
|
|
|
ret.onDraw = &groupchat_onDraw;
|
|
|
|
ret.onInit = &groupchat_onInit;
|
|
|
|
ret.onGroupMessage = &groupchat_onGroupMessage;
|
2013-11-24 03:19:59 +01:00
|
|
|
ret.onGroupNamelistChange = &groupchat_onGroupNamelistChange;
|
2013-12-14 02:57:32 +01:00
|
|
|
ret.onGroupAction = &groupchat_onGroupAction;
|
2013-09-15 22:38:38 +02:00
|
|
|
|
|
|
|
snprintf(ret.name, sizeof(ret.name), "Room #%d", groupnum);
|
|
|
|
|
|
|
|
ChatContext *chatwin = calloc(1, sizeof(ChatContext));
|
|
|
|
|
|
|
|
if (chatwin != NULL)
|
|
|
|
ret.chatwin = chatwin;
|
|
|
|
else {
|
|
|
|
endwin();
|
|
|
|
fprintf(stderr, "calloc() failed. Aborting...\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.num = groupnum;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|