1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-15 07:33:01 +01:00
toxic/src/groupchat.c

736 lines
20 KiB
C
Raw Normal View History

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
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* needed for strcasestr() and wcswidth() */
#endif
2014-06-22 03:41:38 +02:00
2013-09-15 22:38:38 +02:00
#include <stdlib.h>
#include <string.h>
2014-08-02 00:37:02 +02:00
#include <assert.h>
2013-09-15 22:38:38 +02:00
#include <time.h>
2014-06-22 03:41:38 +02:00
#include <wchar.h>
#include <unistd.h>
2013-09-15 22:38:38 +02:00
2014-11-29 15:36:31 +01:00
#ifdef AUDIO
#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <AL/al.h>
#include <AL/alc.h>
/* compatibility with older versions of OpenAL */
#ifndef ALC_ALL_DEVICES_SPECIFIER
#include <AL/alext.h>
2018-10-08 19:39:04 +02:00
#endif /* ALC_ALL_DEVICES_SPECIFIER */
#endif /* __APPLE__ */
#endif /* AUDIO */
2014-11-29 15:36:31 +01:00
#include "windows.h"
#include "toxic.h"
2013-11-10 03:43:56 +01:00
#include "execute.h"
#include "misc_tools.h"
#include "groupchat.h"
#include "prompt.h"
2013-12-11 06:10:09 +01:00
#include "toxic_strings.h"
#include "log.h"
2014-03-25 13:21:50 +01:00
#include "line_info.h"
2014-04-07 10:42:10 +02:00
#include "settings.h"
#include "input.h"
#include "help.h"
2014-07-21 01:12:13 +02:00
#include "notify.h"
#include "autocomplete.h"
#include "audio_device.h"
2013-09-15 22:38:38 +02:00
extern char *DATA_FILE;
2014-06-18 18:10:00 +02:00
static GroupChat groupchats[MAX_GROUPCHAT_NUM];
2013-11-30 00:52:21 +01:00
static int max_groupchat_index = 0;
extern struct user_settings *user_settings;
extern struct Winthread Winthread;
2014-04-07 10:42:10 +02:00
2018-06-16 05:07:16 +02:00
#ifdef PYTHON
#define AC_NUM_GROUP_COMMANDS_PYTHON 1
#else
#define AC_NUM_GROUP_COMMANDS_PYTHON 0
#endif /* PYTHON */
#ifdef QRCODE
#define AC_NUM_GROUP_COMMANDS_QRCODE 1
2014-11-15 02:13:08 +01:00
#else
2018-06-16 05:07:16 +02:00
#define AC_NUM_GROUP_COMMANDS_QRCODE 0
#endif /* QRCODE */
#define AC_NUM_GROUP_COMMANDS (19 + AC_NUM_GROUP_COMMANDS_PYTHON + AC_NUM_GROUP_COMMANDS_QRCODE)
2014-11-15 02:13:08 +01:00
/* Array of groupchat command names used for tab completion. */
static const char group_cmd_list[AC_NUM_GROUP_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/accept" },
{ "/add" },
{ "/avatar" },
{ "/clear" },
{ "/close" },
{ "/connect" },
{ "/decline" },
{ "/exit" },
{ "/group" },
{ "/help" },
{ "/log" },
{ "/myid" },
2018-06-16 05:07:16 +02:00
#ifdef QRCODE
{ "/myqr" },
2018-06-16 05:07:16 +02:00
#endif /* QRCODE */
2014-11-15 02:13:08 +01:00
{ "/nick" },
{ "/note" },
{ "/nospam" },
2014-11-15 02:13:08 +01:00
{ "/quit" },
{ "/requests" },
{ "/status" },
2014-11-15 04:15:59 +01:00
{ "/title" },
#ifdef PYTHON
{ "/run" },
#endif /* PYTHON */
2014-11-15 02:13:08 +01:00
};
2013-12-09 00:14:57 +01:00
2016-12-18 06:01:51 +01:00
int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t type)
2013-09-15 22:38:38 +02:00
{
2016-12-18 06:01:51 +01:00
if (groupnum > MAX_GROUPCHAT_NUM) {
2014-06-18 18:10:00 +02:00
return -1;
2016-12-18 06:01:51 +01:00
}
2014-06-18 18:10:00 +02:00
2014-11-26 21:22:34 +01:00
ToxWindow self = new_group_chat(m, 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) {
2014-11-26 21:22:34 +01:00
groupchats[i].chatwin = add_window(m, self);
2013-09-15 22:38:38 +02:00
groupchats[i].active = true;
groupchats[i].num_peers = 0;
2014-11-12 02:49:05 +01:00
groupchats[i].type = type;
groupchats[i].start_time = get_unix_time();
2014-09-24 04:51:56 +02:00
2013-11-27 04:16:18 +01:00
set_active_window(groupchats[i].chatwin);
2013-09-15 22:38:38 +02:00
if (i == max_groupchat_index) {
2013-11-17 22:09:14 +01:00
++max_groupchat_index;
}
2013-11-17 22:09:14 +01:00
2013-09-15 22:38:38 +02:00
return 0;
}
}
return -1;
}
2014-12-07 19:51:12 +01:00
static void kill_groupchat_window(ToxWindow *self)
2013-09-15 22:38:38 +02:00
{
2014-02-26 11:23:11 +01:00
ChatContext *ctx = self->chatwin;
log_disable(ctx->log);
2014-03-27 22:59:31 +01:00
line_info_cleanup(ctx->hst);
2014-02-26 11:23:11 +01:00
delwin(ctx->linewin);
2014-07-01 05:56:47 +02:00
delwin(ctx->history);
delwin(ctx->sidebar);
free(ctx->log);
2014-02-26 11:23:11 +01:00
free(ctx);
free(self->help);
del_window(self);
2014-02-26 11:23:11 +01:00
}
void free_groupchat(ToxWindow *self, Tox *m, uint32_t groupnum)
2014-02-26 11:23:11 +01:00
{
free(groupchats[groupnum].name_list);
free(groupchats[groupnum].peer_list);
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) {
if (groupchats[i - 1].active) {
2013-09-15 22:38:38 +02:00
break;
}
2013-09-15 22:38:38 +02:00
}
2013-11-17 22:09:14 +01:00
max_groupchat_index = i;
2014-02-26 11:23:11 +01:00
kill_groupchat_window(self);
2013-09-15 22:38:38 +02:00
}
static void delete_groupchat(ToxWindow *self, Tox *m, uint32_t groupnum)
{
tox_conference_delete(m, groupnum, NULL);
free_groupchat(self, m, groupnum);
}
/* destroys and re-creates groupchat window with or without the peerlist */
void redraw_groupchat_win(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
endwin();
refresh();
clear();
int x2, y2;
getmaxyx(stdscr, y2, x2);
y2 -= 2;
if (y2 <= 0 || x2 <= 0) {
return;
}
if (ctx->sidebar) {
delwin(ctx->sidebar);
ctx->sidebar = NULL;
}
delwin(ctx->linewin);
delwin(ctx->history);
delwin(self->window);
self->window = newwin(y2, x2, 0, 0);
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2 - CHATBOX_HEIGHT, 0);
if (self->show_peerlist) {
ctx->history = subwin(self->window, y2 - CHATBOX_HEIGHT + 1, x2 - SIDEBAR_WIDTH - 1, 0, 0);
ctx->sidebar = subwin(self->window, y2 - CHATBOX_HEIGHT + 1, SIDEBAR_WIDTH, 0, x2 - SIDEBAR_WIDTH);
} else {
ctx->history = subwin(self->window, y2 - CHATBOX_HEIGHT + 1, x2, 0, 0);
}
scrollok(ctx->history, 0);
}
2016-12-18 06:01:51 +01:00
static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, uint32_t groupnum, uint32_t peernum,
TOX_MESSAGE_TYPE type, const char *msg, size_t len)
2013-09-15 22:38:38 +02:00
{
if (self->num != groupnum) {
2013-09-15 22:38:38 +02:00
return;
}
2013-09-15 22:38:38 +02:00
ChatContext *ctx = self->chatwin;
2013-09-15 22:38:38 +02:00
char nick[TOX_MAX_NAME_LENGTH];
get_group_nick_truncate(m, nick, peernum, groupnum);
char selfnick[TOX_MAX_NAME_LENGTH];
2015-03-26 03:56:45 +01:00
tox_self_get_name(m, (uint8_t *) selfnick);
size_t sn_len = tox_self_get_name_size(m);
2014-04-01 09:53:12 +02:00
selfnick[sn_len] = '\0';
2014-02-17 05:15:27 +01:00
int nick_clr = strcmp(nick, selfnick) == 0 ? GREEN : CYAN;
2014-10-04 03:08:33 +02:00
/* Only play sound if mentioned by someone else */
if (strcasestr(msg, selfnick) && strcmp(selfnick, nick)) {
sound_notify(self, generic_message, NT_WNDALERT_0 | user_settings->bell_on_message, NULL);
2015-03-26 03:56:45 +01:00
if (self->active_box != -1) {
2014-10-03 23:53:50 +02:00
box_silent_notify2(self, NT_NOFOCUS, self->active_box, "%s %s", nick, msg);
} else {
2014-10-03 23:53:50 +02:00
box_silent_notify(self, NT_NOFOCUS, &self->active_box, self->name, "%s %s", nick, msg);
}
2014-08-02 00:37:02 +02:00
2013-12-08 11:45:23 +01:00
nick_clr = RED;
} else {
sound_notify(self, silent, NT_WNDALERT_1, NULL);
}
char timefrmt[TIME_STR_SIZE];
2014-06-24 00:54:23 +02:00
get_time_str(timefrmt, sizeof(timefrmt));
2014-02-26 09:51:26 +01:00
2016-12-18 06:01:51 +01:00
line_info_add(self, timefrmt, nick, NULL, type == TOX_MESSAGE_TYPE_NORMAL ? IN_MSG : IN_ACTION, 0, nick_clr, "%s", msg);
2014-10-03 23:53:50 +02:00
write_to_log(msg, nick, ctx->log, false);
2013-09-15 22:38:38 +02:00
}
static void groupchat_onGroupTitleChange(ToxWindow *self, Tox *m, uint32_t groupnum, uint32_t peernum,
const char *title,
2016-12-18 06:01:51 +01:00
size_t length)
2014-11-15 04:15:59 +01:00
{
ChatContext *ctx = self->chatwin;
if (self->num != groupnum) {
2014-11-15 04:15:59 +01:00
return;
}
2014-11-15 04:15:59 +01:00
set_window_title(self, title, length);
char timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt, sizeof(timefrmt));
2014-11-16 04:39:24 +01:00
/* don't announce title when we join the room */
if (!timed_out(groupchats[self->num].start_time, GROUP_EVENT_WAIT)) {
2014-11-15 04:15:59 +01:00
return;
}
2014-11-15 04:15:59 +01:00
char nick[TOX_MAX_NAME_LENGTH];
get_group_nick_truncate(m, nick, peernum, groupnum);
line_info_add(self, timefrmt, nick, NULL, NAME_CHANGE, 0, 0, " set the group title to: %s", title);
2014-11-15 04:15:59 +01:00
char tmp_event[MAX_STR_SIZE];
snprintf(tmp_event, sizeof(tmp_event), "set title to %s", title);
2014-11-15 04:15:59 +01:00
write_to_log(tmp_event, nick, ctx->log, true);
}
static void group_update_name_list(uint32_t groupnum)
{
GroupChat *chat = &groupchats[groupnum];
if (!chat) {
return;
}
if (chat->name_list) {
free(chat->name_list);
}
2016-12-18 06:01:51 +01:00
chat->name_list = malloc(sizeof(char *) * chat->num_peers * TOX_MAX_NAME_LENGTH);
if (chat->name_list == NULL) {
exit_toxic_err("failed in group_update_name_list", FATALERR_MEMORY);
}
2016-12-18 06:01:51 +01:00
uint32_t i, count = 0;
2016-12-18 06:01:51 +01:00
for (i = 0; i < chat->max_idx; ++i) {
if (chat->peer_list[i].active) {
memcpy(&chat->name_list[count * TOX_MAX_NAME_LENGTH], chat->peer_list[i].name, chat->peer_list[i].name_length + 1);
++count;
2015-03-26 03:56:45 +01:00
}
}
2013-11-28 01:29:58 +01:00
2018-02-25 07:21:22 +01:00
qsort(chat->name_list, count, TOX_MAX_NAME_LENGTH, qsort_strcasecmp_hlpr);
}
/* Reallocates groupnum's peer list. Increase is true if the list needs to grow.
*
* Returns 0 on success.
* Returns -1 on failure.
*/
static int realloc_peer_list(GroupChat *chat, uint32_t num_peers)
{
if (!chat) {
return -1;
}
if (num_peers == 0) {
free(chat->peer_list);
chat->peer_list = NULL;
return 0;
}
struct GroupPeer *tmp_list = realloc(chat->peer_list, num_peers * sizeof(struct GroupPeer));
if (!tmp_list) {
return -1;
}
chat->peer_list = tmp_list;
return 0;
}
static void update_peer_list(Tox *m, uint32_t groupnum, uint32_t num_peers)
{
GroupChat *chat = &groupchats[groupnum];
if (!chat) {
2014-06-18 18:10:00 +02:00
return;
2016-12-18 06:01:51 +01:00
}
2013-11-26 00:49:31 +01:00
realloc_peer_list(chat, num_peers);
2014-06-18 18:10:00 +02:00
uint32_t i;
2013-11-26 00:49:31 +01:00
for (i = 0; i < num_peers; ++i) {
GroupPeer *peer = &chat->peer_list[i];
2013-11-26 00:49:31 +01:00
TOX_ERR_CONFERENCE_PEER_QUERY err;
size_t length = tox_conference_peer_get_name_size(m, groupnum, i, &err);
2014-02-26 12:35:19 +01:00
if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK || length >= TOX_MAX_NAME_LENGTH) {
continue;
}
tox_conference_peer_get_name(m, groupnum, i, (uint8_t *) peer->name, &err);
peer->name[length] = 0;
if (err != TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
continue;
}
peer->active = true;
peer->name_length = length;
peer->peernumber = i;
}
group_update_name_list(groupnum);
}
static void groupchat_onGroupNameListChange(ToxWindow *self, Tox *m, uint32_t groupnum)
{
if (self->num != groupnum) {
return;
}
if (groupnum > max_groupchat_index) {
return;
}
GroupChat *chat = &groupchats[groupnum];
TOX_ERR_CONFERENCE_PEER_QUERY err;
uint32_t num_peers = tox_conference_peer_count(m, groupnum, &err);
uint32_t old_num = chat->num_peers;
if (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) {
chat->num_peers = num_peers;
} else {
num_peers = old_num;
2013-11-26 00:49:31 +01:00
}
2013-11-29 01:45:28 +01:00
chat->max_idx = num_peers;
update_peer_list(m, groupnum, num_peers);
}
static void groupchat_onGroupPeerNameChange(ToxWindow *self, Tox *m, uint32_t groupnum, uint32_t peernum,
const char *name, size_t length)
{
if (self->num != groupnum) {
return;
}
GroupChat *chat = &groupchats[groupnum];
if (!chat) {
return;
}
size_t i;
for (i = 0; i < chat->max_idx; ++i) {
GroupPeer *peer = &chat->peer_list[i];
// Test against default tox name to prevent nick change spam on initial join (TODO: this is disgusting)
if (peer->active && peer->peernumber == peernum && peer->name_length > 0) {
ChatContext *ctx = self->chatwin;
char timefrmt[TIME_STR_SIZE];
get_time_str(timefrmt, sizeof(timefrmt));
char tmp_event[TOXIC_MAX_NAME_LENGTH * 2 + 32];
snprintf(tmp_event, sizeof(tmp_event), "is now known as %s", (const char *) name);
write_to_log(tmp_event, peer->name, ctx->log, true);
line_info_add(self, timefrmt, peer->name, (const char *) name, NAME_CHANGE, 0, 0, " is now known as ");
break;
}
}
groupchat_onGroupNameListChange(self, m, groupnum);
}
static void send_group_action(ToxWindow *self, ChatContext *ctx, Tox *m, char *action)
{
2013-12-14 02:57:32 +01:00
if (action == NULL) {
wprintw(ctx->history, "Invalid syntax.\n");
2013-12-14 02:57:32 +01:00
return;
}
2016-12-18 06:01:51 +01:00
TOX_ERR_CONFERENCE_SEND_MESSAGE err;
if (!tox_conference_send_message(m, self->num, TOX_MESSAGE_TYPE_ACTION, (uint8_t *) action, strlen(action), &err)) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send action (error %d)", err);
2013-12-14 02:57:32 +01:00
}
}
2014-03-30 22:40:13 +02:00
static void groupchat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
2013-09-15 22:38:38 +02: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);
2014-06-21 07:26:33 +02:00
if (x2 <= 0 || y2 <= 0) {
2014-06-21 07:26:33 +02:00
return;
}
2014-06-21 07:26:33 +02:00
if (self->help->active) {
help_onKey(self, key);
return;
}
if (ctx->pastemode && key == '\r') {
key = '\n';
}
if (ltr || key == '\n') { /* char is printable */
input_new_char(self, key, x, y, x2, y2);
return;
}
2013-12-01 22:59:46 +01:00
if (line_info_onKey(self, key)) {
return;
}
2014-03-30 22:40:13 +02:00
if (input_handle(self, key, x, y, x2, y2)) {
return;
}
2013-12-06 04:55:14 +01:00
if (key == '\t') { /* TAB key: auto-completes peer name or command */
if (ctx->len > 0) {
int diff;
2013-12-06 04:55:14 +01:00
/* TODO: make this not suck */
if (ctx->line[0] != L'/' || wcscmp(ctx->line, L"/me") == 0) {
diff = complete_line(self, groupchats[self->num].name_list, groupchats[self->num].num_peers,
TOX_MAX_NAME_LENGTH);
} else if (wcsncmp(ctx->line, L"/avatar \"", wcslen(L"/avatar \"")) == 0) {
diff = dir_match(self, m, ctx->line, L"/avatar");
}
#ifdef PYTHON
else if (wcsncmp(ctx->line, L"/run \"", wcslen(L"/run \"")) == 0) {
diff = dir_match(self, m, ctx->line, L"/run");
}
#endif
else {
2014-11-15 02:13:08 +01:00
diff = complete_line(self, group_cmd_list, AC_NUM_GROUP_COMMANDS, MAX_CMDNAME_SIZE);
}
2013-12-01 22:59:46 +01:00
if (diff != -1) {
if (x + diff > x2 - 1) {
int wlen = MAX(0, wcswidth(ctx->line, sizeof(ctx->line) / sizeof(wchar_t)));
ctx->start = wlen < x2 ? 0 : wlen - x2 + 1;
}
2014-07-01 05:56:47 +02:00
} else {
2015-03-26 03:56:45 +01:00
sound_notify(self, notif_error, 0, NULL);
2014-07-01 05:56:47 +02:00
}
} else {
2015-03-26 03:56:45 +01:00
sound_notify(self, notif_error, 0, NULL);
2014-07-01 05:56:47 +02:00
}
} else if (key == user_settings->key_peer_list_down) { /* Scroll peerlist up and down one position */
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 == user_settings->key_peer_list_up) {
if (groupchats[self->num].side_pos > 0) {
--groupchats[self->num].side_pos;
}
} else if (key == '\r') {
rm_trailing_spaces_buf(ctx);
2013-12-03 09:44:02 +01:00
if (!wstring_is_empty(ctx->line)) {
add_line_to_hist(ctx);
2013-12-11 06:10:09 +01:00
wstrsubst(ctx->line, L'', L'\n');
2013-12-19 02:18:02 +01:00
char line[MAX_STR_SIZE];
2014-03-30 22:40:13 +02:00
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1) {
memset(&line, 0, sizeof(line));
}
if (line[0] == '/') {
if (strcmp(line, "/close") == 0) {
delete_groupchat(self, m, self->num);
return;
} 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);
}
2013-12-16 02:52:10 +01:00
} else {
2016-12-18 06:01:51 +01:00
TOX_ERR_CONFERENCE_SEND_MESSAGE err;
if (!tox_conference_send_message(m, self->num, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) line, strlen(line), &err)) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Failed to send message (error %d)", err);
}
2013-09-15 22:38:38 +02:00
}
2014-03-30 22:40:13 +02:00
}
wclear(ctx->linewin);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
reset_buf(ctx);
2013-09-15 22:38:38 +02:00
}
}
static void groupchat_onDraw(ToxWindow *self, Tox *m)
{
2013-12-03 09:44:02 +01:00
int x2, y2;
getmaxyx(self->window, y2, x2);
if (x2 <= 0 || y2 <= 0) {
return;
}
ChatContext *ctx = self->chatwin;
pthread_mutex_lock(&Winthread.lock);
2014-03-25 13:21:50 +01:00
line_info_print(self);
pthread_mutex_unlock(&Winthread.lock);
wclear(ctx->linewin);
curs_set(1);
if (ctx->len > 0) {
mvwprintw(ctx->linewin, 1, 0, "%ls", &ctx->line[ctx->start]);
}
2013-12-03 09:44:02 +01:00
wclear(ctx->sidebar);
2014-07-01 05:56:47 +02:00
mvwhline(self->window, y2 - CHATBOX_HEIGHT, 0, ACS_HLINE, x2);
if (self->show_peerlist) {
mvwvline(ctx->sidebar, 0, 0, ACS_VLINE, y2 - CHATBOX_HEIGHT);
mvwaddch(ctx->sidebar, y2 - CHATBOX_HEIGHT, 0, ACS_BTEE);
2013-11-29 07:30:10 +01:00
2016-09-23 00:00:14 +02:00
pthread_mutex_lock(&Winthread.lock);
int num_peers = groupchats[self->num].num_peers;
2016-09-23 00:00:14 +02:00
pthread_mutex_unlock(&Winthread.lock);
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);
2013-11-29 07:30:10 +01:00
mvwaddch(ctx->sidebar, 1, 0, ACS_LTEE);
mvwhline(ctx->sidebar, 1, 1, ACS_HLINE, SIDEBAR_WIDTH - 1);
int maxlines = y2 - SDBAR_OFST - CHATBOX_HEIGHT;
int i;
2013-11-27 03:06:06 +01:00
for (i = 0; i < num_peers && i < maxlines; ++i) {
wmove(ctx->sidebar, i + 2, 1);
2016-09-23 00:00:14 +02:00
pthread_mutex_lock(&Winthread.lock);
2016-12-18 06:01:51 +01:00
uint32_t peer = i + groupchats[self->num].side_pos;
2016-09-23 00:00:14 +02:00
pthread_mutex_unlock(&Winthread.lock);
/* truncate nick to fit in side panel without modifying list */
char tmpnck[TOX_MAX_NAME_LENGTH];
int maxlen = SIDEBAR_WIDTH - 2;
2016-09-23 00:00:14 +02:00
pthread_mutex_lock(&Winthread.lock);
memcpy(tmpnck, &groupchats[self->num].name_list[peer * TOX_MAX_NAME_LENGTH], maxlen);
2016-09-23 00:00:14 +02:00
pthread_mutex_unlock(&Winthread.lock);
tmpnck[maxlen] = '\0';
wprintw(ctx->sidebar, "%s\n", tmpnck);
}
}
2014-07-01 05:56:47 +02:00
int y, x;
getyx(self->window, y, x);
(void) x;
int new_x = ctx->start ? x2 - 1 : MAX(0, wcswidth(ctx->line, ctx->pos));
2014-07-01 05:56:47 +02:00
wmove(self->window, y + 1, new_x);
2017-10-29 16:50:42 +01:00
wnoutrefresh(self->window);
if (self->help->active) {
help_onDraw(self);
}
2013-09-15 22:38:38 +02:00
}
static void groupchat_onInit(ToxWindow *self, Tox *m)
{
2014-07-01 05:56:47 +02:00
int x2, y2;
getmaxyx(self->window, y2, x2);
if (x2 <= 0 || y2 <= 0) {
exit_toxic_err("failed in groupchat_onInit", FATALERR_CURSES);
}
ChatContext *ctx = self->chatwin;
2014-03-25 13:21:50 +01:00
2014-07-01 05:56:47 +02:00
ctx->history = subwin(self->window, y2 - CHATBOX_HEIGHT + 1, x2 - SIDEBAR_WIDTH - 1, 0, 0);
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2 - CHATBOX_HEIGHT, 0);
ctx->sidebar = subwin(self->window, y2 - CHATBOX_HEIGHT + 1, SIDEBAR_WIDTH, 0, x2 - SIDEBAR_WIDTH);
2014-07-04 19:24:18 +02:00
ctx->hst = calloc(1, sizeof(struct history));
ctx->log = calloc(1, sizeof(struct chatlog));
if (ctx->log == NULL || ctx->hst == NULL) {
exit_toxic_err("failed in groupchat_onInit", FATALERR_MEMORY);
}
2014-03-25 13:21:50 +01:00
line_info_init(ctx->hst);
2014-04-07 10:42:10 +02:00
if (user_settings->autolog == AUTOLOG_ON) {
2015-03-26 03:56:45 +01:00
char myid[TOX_ADDRESS_SIZE];
tox_self_get_address(m, (uint8_t *) myid);
if (log_enable(self->name, myid, NULL, ctx->log, LOG_GROUP) == -1) {
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize.");
}
}
2014-04-07 10:42:10 +02:00
2014-02-27 01:00:13 +01:00
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);
2014-02-26 09:51:26 +01:00
2014-07-01 05:56:47 +02:00
scrollok(ctx->history, 0);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
2013-09-15 22:38:38 +02:00
}
2016-12-18 06:01:51 +01:00
ToxWindow new_group_chat(Tox *m, uint32_t groupnum)
2013-09-15 22:38:38 +02:00
{
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.active = true;
2014-02-26 11:23:11 +01:00
ret.is_groupchat = 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;
ret.onGroupNameListChange = &groupchat_onGroupNameListChange;
ret.onGroupPeerNameChange = &groupchat_onGroupPeerNameChange;
2014-11-15 04:15:59 +01:00
ret.onGroupTitleChange = &groupchat_onGroupTitleChange;
2013-09-15 22:38:38 +02:00
snprintf(ret.name, sizeof(ret.name), "Group %d", groupnum);
2013-09-15 22:38:38 +02:00
ChatContext *chatwin = calloc(1, sizeof(ChatContext));
Help *help = calloc(1, sizeof(Help));
2013-09-15 22:38:38 +02:00
if (chatwin == NULL || help == NULL) {
exit_toxic_err("failed in new_group_chat", FATALERR_MEMORY);
}
2013-09-15 22:38:38 +02:00
2014-06-18 21:54:05 +02:00
ret.chatwin = chatwin;
ret.help = help;
2013-09-15 22:38:38 +02:00
ret.num = groupnum;
ret.show_peerlist = true;
2014-08-02 00:37:02 +02:00
ret.active_box = -1;
2013-09-15 22:38:38 +02:00
return ret;
}