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
|
|
|
*/
|
|
|
|
|
2014-06-24 17:35:23 +02:00
|
|
|
#ifndef _GNU_SOURCE
|
2014-07-14 20:32:38 +02:00
|
|
|
#define _GNU_SOURCE /* needed for strcasestr() and wcswidth() */
|
2014-06-24 17:35:23 +02:00
|
|
|
#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>
|
2014-10-09 07:39:22 +02:00
|
|
|
#include <unistd.h>
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2015-05-25 16:38:52 +02:00
|
|
|
#ifdef NO_GETTEXT
|
|
|
|
#define gettext(A) (A)
|
|
|
|
#else
|
|
|
|
#include <libintl.h>
|
|
|
|
#endif
|
|
|
|
|
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>
|
|
|
|
#endif /* ALC_ALL_DEVICES_SPECIFIER */
|
|
|
|
#endif /* __APPLE__ */
|
|
|
|
#endif /* AUDIO */
|
|
|
|
|
2014-06-12 00:06:55 +02:00
|
|
|
#include "windows.h"
|
|
|
|
#include "toxic.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"
|
2014-03-01 13:10:44 +01:00
|
|
|
#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"
|
2014-06-26 08:33:09 +02:00
|
|
|
#include "input.h"
|
2014-07-04 09:24:29 +02:00
|
|
|
#include "help.h"
|
2014-07-21 01:12:13 +02:00
|
|
|
#include "notify.h"
|
2014-07-18 07:29:46 +02:00
|
|
|
#include "autocomplete.h"
|
2015-05-25 23:59:06 +02:00
|
|
|
#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;
|
|
|
|
|
2014-09-23 03:24:45 +02:00
|
|
|
extern struct user_settings *user_settings;
|
2014-10-09 07:39:22 +02:00
|
|
|
extern struct Winthread Winthread;
|
2014-04-07 10:42:10 +02:00
|
|
|
|
2014-11-15 02:13:08 +01:00
|
|
|
#ifdef AUDIO
|
2014-11-15 04:15:59 +01:00
|
|
|
#define AC_NUM_GROUP_COMMANDS 22
|
2014-11-15 02:13:08 +01:00
|
|
|
#else
|
2014-11-15 04:15:59 +01:00
|
|
|
#define AC_NUM_GROUP_COMMANDS 18
|
2014-11-15 02:13:08 +01:00
|
|
|
#endif /* AUDIO */
|
|
|
|
|
|
|
|
/* 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" },
|
|
|
|
{ "/nick" },
|
|
|
|
{ "/note" },
|
|
|
|
{ "/quit" },
|
|
|
|
{ "/requests" },
|
|
|
|
{ "/status" },
|
2014-11-15 04:15:59 +01:00
|
|
|
{ "/title" },
|
2014-11-15 02:13:08 +01:00
|
|
|
|
|
|
|
#ifdef AUDIO
|
|
|
|
|
|
|
|
{ "/lsdev" },
|
|
|
|
{ "/sdev" },
|
|
|
|
{ "/mute" },
|
|
|
|
{ "/sense" },
|
|
|
|
|
|
|
|
#endif /* AUDIO */
|
|
|
|
};
|
2013-12-09 00:14:57 +01:00
|
|
|
|
2014-11-29 15:36:31 +01:00
|
|
|
#ifdef AUDIO
|
|
|
|
static int group_audio_open_out_device(int groupnum);
|
2014-12-07 18:16:49 +01:00
|
|
|
static int group_audio_close_out_device(int groupnum);
|
2014-11-29 15:36:31 +01:00
|
|
|
#endif /* AUDIO */
|
|
|
|
|
2014-11-12 02:49:05 +01:00
|
|
|
int init_groupchat_win(ToxWindow *prompt, Tox *m, int groupnum, uint8_t type)
|
2013-09-15 22:38:38 +02:00
|
|
|
{
|
2014-06-18 18:10:00 +02:00
|
|
|
if (groupnum > MAX_GROUPCHAT_NUM)
|
|
|
|
return -1;
|
|
|
|
|
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;
|
2013-11-24 03:19:59 +01:00
|
|
|
groupchats[i].num_peers = 0;
|
2014-11-12 02:49:05 +01:00
|
|
|
groupchats[i].type = type;
|
2014-10-09 07:39:22 +02:00
|
|
|
groupchats[i].start_time = get_unix_time();
|
2014-09-24 04:51:56 +02:00
|
|
|
|
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);
|
2014-04-01 04:00:17 +02:00
|
|
|
groupchats[i].peer_name_lengths = malloc(sizeof(uint16_t));
|
2014-04-01 08:38:32 +02:00
|
|
|
groupchats[i].oldpeer_name_lengths = malloc(sizeof(uint16_t));
|
2013-11-28 04:46:09 +01:00
|
|
|
|
2014-09-24 04:51:56 +02:00
|
|
|
if (groupchats[i].peer_names == NULL || groupchats[i].oldpeer_names == NULL
|
|
|
|
|| groupchats[i].peer_name_lengths == NULL || groupchats[i].oldpeer_name_lengths == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in init_groupchat_win"), FATALERR_MEMORY);
|
2014-09-24 04:51:56 +02:00
|
|
|
|
2013-11-28 04:46:09 +01:00
|
|
|
memcpy(&groupchats[i].oldpeer_names[0], UNKNOWN_NAME, sizeof(UNKNOWN_NAME));
|
2014-04-01 08:38:32 +02:00
|
|
|
groupchats[i].oldpeer_name_lengths[0] = (uint16_t) strlen(UNKNOWN_NAME);
|
2013-11-27 04:15:48 +01:00
|
|
|
|
2014-11-26 21:22:34 +01:00
|
|
|
#ifdef AUDIO
|
2014-12-07 18:16:49 +01:00
|
|
|
if (type == TOX_GROUPCHAT_TYPE_AV)
|
2014-12-10 00:29:07 +01:00
|
|
|
if (group_audio_open_out_device(i) == -1)
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("Group Audio failed to init\n"));
|
2014-11-29 15:36:31 +01:00
|
|
|
#endif /* AUDIO */
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2014-03-01 13:10:44 +01:00
|
|
|
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);
|
2014-03-01 13:10:44 +01:00
|
|
|
free(ctx->log);
|
2014-02-26 11:23:11 +01:00
|
|
|
free(ctx);
|
2014-07-04 09:24:29 +02:00
|
|
|
free(self->help);
|
|
|
|
del_window(self);
|
2014-02-26 11:23:11 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 19:51:12 +01:00
|
|
|
void close_groupchat(ToxWindow *self, Tox *m, int groupnum)
|
2014-02-26 11:23:11 +01:00
|
|
|
{
|
2013-09-15 22:38:38 +02:00
|
|
|
tox_del_groupchat(m, groupnum);
|
2014-12-10 00:55:14 +01:00
|
|
|
#ifdef AUDIO
|
2014-12-07 18:16:49 +01:00
|
|
|
group_audio_close_out_device(groupnum);
|
2014-12-10 00:55:14 +01:00
|
|
|
#endif
|
2013-11-27 02:45:08 +01:00
|
|
|
|
|
|
|
free(groupchats[groupnum].peer_names);
|
|
|
|
free(groupchats[groupnum].oldpeer_names);
|
2014-04-01 04:00:17 +02:00
|
|
|
free(groupchats[groupnum].peer_name_lengths);
|
2014-04-01 08:38:32 +02:00
|
|
|
free(groupchats[groupnum].oldpeer_name_lengths);
|
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) {
|
2014-04-19 23:58:13 +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;
|
2014-02-26 11:23:11 +01:00
|
|
|
kill_groupchat_window(self);
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 08:45:08 +02:00
|
|
|
/* 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 (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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-10-11 10:42:30 +02:00
|
|
|
static void groupchat_onGroupMessage(ToxWindow *self, Tox *m, int groupnum, int peernum,
|
2014-07-07 04:15:35 +02:00
|
|
|
const char *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-07-07 04:15:35 +02:00
|
|
|
char nick[TOX_MAX_NAME_LENGTH];
|
2014-10-09 07:39:22 +02:00
|
|
|
get_group_nick_truncate(m, nick, peernum, groupnum);
|
2014-02-10 04:55:42 +01:00
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
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-04-01 04:00:17 +02:00
|
|
|
|
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-10-04 03:08:33 +02:00
|
|
|
/* Only play sound if mentioned by someone else */
|
|
|
|
if (strcasestr(msg, selfnick) && strcmp(selfnick, nick)) {
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(self, generic_message, NT_WNDALERT_0, NULL);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2014-08-02 00:37:02 +02: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);
|
2014-08-02 00:37:02 +02:00
|
|
|
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;
|
2013-12-07 01:41:53 +01:00
|
|
|
}
|
2014-10-03 05:22:37 +02:00
|
|
|
else {
|
|
|
|
sound_notify(self, silent, NT_WNDALERT_1, NULL);
|
|
|
|
}
|
2013-12-07 01:41:53 +01:00
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
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
|
|
|
|
2014-10-03 23:53:50 +02:00
|
|
|
line_info_add(self, timefrmt, nick, NULL, IN_MSG, 0, nick_clr, "%s", msg);
|
|
|
|
write_to_log(msg, nick, ctx->log, false);
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
static void groupchat_onGroupAction(ToxWindow *self, Tox *m, int groupnum, int peernum, const char *action,
|
2013-12-14 02:57:32 +01:00
|
|
|
uint16_t len)
|
|
|
|
{
|
|
|
|
if (self->num != groupnum)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
2014-10-09 07:39:22 +02:00
|
|
|
char nick[TOX_MAX_NAME_LENGTH];
|
|
|
|
get_group_nick_truncate(m, nick, peernum, groupnum);
|
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
char selfnick[TOX_MAX_NAME_LENGTH];
|
2015-03-26 03:56:45 +01:00
|
|
|
tox_self_get_name(m, (uint8_t *) selfnick);
|
|
|
|
|
|
|
|
size_t n_len = tox_self_get_name_size(m);
|
2014-04-01 04:00:17 +02:00
|
|
|
selfnick[n_len] = '\0';
|
2013-12-14 02:57:32 +01:00
|
|
|
|
2014-07-21 01:12:13 +02:00
|
|
|
if (strcasestr(action, selfnick)) {
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(self, generic_message, NT_WNDALERT_0, NULL);
|
2015-03-26 03:56:45 +01:00
|
|
|
|
2014-08-02 00:37:02 +02:00
|
|
|
if (self->active_box != -1)
|
|
|
|
box_silent_notify2(self, NT_NOFOCUS, self->active_box, "* %s %s", nick, action );
|
|
|
|
else
|
|
|
|
box_silent_notify(self, NT_NOFOCUS, &self->active_box, self->name, "* %s %s", nick, action);
|
2013-12-14 02:57:32 +01:00
|
|
|
}
|
2014-10-05 08:29:40 +02:00
|
|
|
else {
|
2014-10-03 23:53:50 +02:00
|
|
|
sound_notify(self, silent, NT_WNDALERT_1, NULL);
|
2014-10-05 08:29:40 +02:00
|
|
|
}
|
2013-12-14 02:57:32 +01:00
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
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
|
|
|
|
2014-09-08 03:43:16 +02:00
|
|
|
line_info_add(self, timefrmt, nick, NULL, IN_ACTION, 0, 0, "%s", action);
|
2014-03-04 01:21:52 +01:00
|
|
|
write_to_log(action, nick, ctx->log, true);
|
2013-12-14 02:57:32 +01:00
|
|
|
}
|
|
|
|
|
2014-11-15 04:15:59 +01:00
|
|
|
static void groupchat_onGroupTitleChange(ToxWindow *self, Tox *m, int groupnum, int peernum, const char *title,
|
|
|
|
uint8_t length)
|
|
|
|
{
|
|
|
|
ChatContext *ctx = self->chatwin;
|
|
|
|
|
|
|
|
if (self->num != groupnum)
|
|
|
|
return;
|
|
|
|
|
|
|
|
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, get_unix_time(), GROUP_EVENT_WAIT))
|
2014-11-15 04:15:59 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
char nick[TOX_MAX_NAME_LENGTH];
|
|
|
|
get_group_nick_truncate(m, nick, peernum, groupnum);
|
2015-05-25 16:38:52 +02:00
|
|
|
line_info_add(self, timefrmt, nick, NULL, NAME_CHANGE, 0, 0, gettext(" set the group title to: %s"), title);
|
2014-11-15 04:15:59 +01:00
|
|
|
|
|
|
|
char tmp_event[MAX_STR_SIZE];
|
2015-05-25 16:38:52 +02:00
|
|
|
snprintf(tmp_event, sizeof(tmp_event), gettext("set title to %s"), title);
|
2014-11-15 04:15:59 +01:00
|
|
|
write_to_log(tmp_event, nick, ctx->log, true);
|
|
|
|
}
|
|
|
|
|
2014-04-01 04:00:17 +02:00
|
|
|
/* Puts two copies of peerlist/lengths in chat instance */
|
|
|
|
static void copy_peernames(int gnum, uint8_t peerlist[][TOX_MAX_NAME_LENGTH], uint16_t lengths[], int npeers)
|
2013-11-27 02:45:08 +01:00
|
|
|
{
|
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);
|
2014-04-01 04:00:17 +02:00
|
|
|
free(groupchats[gnum].peer_name_lengths);
|
|
|
|
free(groupchats[gnum].oldpeer_name_lengths);
|
2013-11-27 02:45:08 +01:00
|
|
|
|
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);
|
2014-04-01 04:00:17 +02:00
|
|
|
groupchats[gnum].peer_name_lengths = malloc(sizeof(uint16_t) * npeers);
|
|
|
|
groupchats[gnum].oldpeer_name_lengths = malloc(sizeof(uint16_t) * npeers);
|
2013-11-27 02:45:08 +01:00
|
|
|
|
2014-04-01 04:00:17 +02:00
|
|
|
if (groupchats[gnum].peer_names == NULL || groupchats[gnum].oldpeer_names == NULL
|
2014-06-18 21:54:05 +02:00
|
|
|
|| groupchats[gnum].peer_name_lengths == NULL || groupchats[gnum].oldpeer_name_lengths == NULL) {
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in copy_peernames"), FATALERR_MEMORY);
|
2013-11-27 02:45:08 +01:00
|
|
|
}
|
|
|
|
|
2014-10-03 23:55:36 +02:00
|
|
|
uint16_t u_len = strlen(UNKNOWN_NAME);
|
2013-11-27 02:45:08 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < npeers; ++i) {
|
2014-10-04 00:57:32 +02:00
|
|
|
if (!lengths[i]) {
|
2014-10-03 05:22:37 +02:00
|
|
|
memcpy(&groupchats[gnum].peer_names[i * N], UNKNOWN_NAME, u_len);
|
|
|
|
groupchats[gnum].peer_names[i * N + u_len] = '\0';
|
|
|
|
groupchats[gnum].peer_name_lengths[i] = u_len;
|
2014-02-11 02:13:22 +01:00
|
|
|
} else {
|
2014-10-03 05:22:37 +02:00
|
|
|
uint16_t n_len = MIN(lengths[i], TOXIC_MAX_NAME_LENGTH - 1);
|
|
|
|
memcpy(&groupchats[gnum].peer_names[i * N], peerlist[i], n_len);
|
2014-04-19 23:58:13 +02:00
|
|
|
groupchats[gnum].peer_names[i * N + n_len] = '\0';
|
2014-04-01 04:00:17 +02:00
|
|
|
groupchats[gnum].peer_name_lengths[i] = n_len;
|
2014-10-07 22:18:06 +02:00
|
|
|
filter_str((char *) &groupchats[gnum].peer_names[i * N], n_len);
|
2015-03-26 03:56:45 +01:00
|
|
|
}
|
2013-11-27 02:45:08 +01:00
|
|
|
}
|
2013-11-28 01:29:58 +01:00
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
memcpy(groupchats[gnum].oldpeer_names, groupchats[gnum].peer_names, N * npeers);
|
2014-10-03 05:22:37 +02:00
|
|
|
memcpy(groupchats[gnum].oldpeer_name_lengths, groupchats[gnum].peer_name_lengths, sizeof(uint16_t) * npeers);
|
2013-11-27 02:45:08 +01:00
|
|
|
}
|
|
|
|
|
2014-10-09 07:39:22 +02:00
|
|
|
struct group_add_thrd {
|
|
|
|
Tox *m;
|
|
|
|
ToxWindow *self;
|
|
|
|
int peernum;
|
|
|
|
int groupnum;
|
|
|
|
uint64_t timestamp;
|
|
|
|
pthread_t tid;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Waits GROUP_EVENT_WAIT seconds for a new peer to set their name before announcing them */
|
|
|
|
void *group_add_wait(void *data)
|
|
|
|
{
|
|
|
|
struct group_add_thrd *thrd = (struct group_add_thrd *) data;
|
|
|
|
ToxWindow *self = thrd->self;
|
|
|
|
Tox *m = thrd->m;
|
|
|
|
char peername[TOX_MAX_NAME_LENGTH];
|
|
|
|
|
|
|
|
/* keep polling for a name that differs from the default until we run out of time */
|
|
|
|
while (true) {
|
|
|
|
usleep(100000);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
get_group_nick_truncate(m, peername, thrd->peernum, thrd->groupnum);
|
|
|
|
|
|
|
|
if (strcmp(peername, DEFAULT_TOX_NAME) || timed_out(thrd->timestamp, get_unix_time(), GROUP_EVENT_WAIT)) {
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
}
|
|
|
|
|
2015-05-25 16:38:52 +02:00
|
|
|
const char *event = gettext("has joined the room");
|
2014-10-09 07:39:22 +02:00
|
|
|
char timefrmt[TIME_STR_SIZE];
|
|
|
|
get_time_str(timefrmt, sizeof(timefrmt));
|
|
|
|
|
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
|
|
|
line_info_add(self, timefrmt, (char *) peername, NULL, CONNECTION, 0, GREEN, event);
|
|
|
|
write_to_log(event, (char *) peername, self->chatwin->log, true);
|
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
|
|
|
|
pthread_attr_destroy(&thrd->attr);
|
|
|
|
free(thrd);
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2014-11-26 23:39:02 +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;
|
|
|
|
|
2014-06-19 19:50:41 +02:00
|
|
|
if (groupnum > max_groupchat_index)
|
2014-06-18 18:10:00 +02:00
|
|
|
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
|
|
|
|
2014-07-02 19:46:57 +02:00
|
|
|
if (peernum > num_peers)
|
2014-06-18 18:10:00 +02:00
|
|
|
return;
|
|
|
|
|
2013-11-26 00:49:31 +01:00
|
|
|
/* get old peer name before updating name list */
|
2014-04-01 04:00:17 +02:00
|
|
|
uint8_t oldpeername[TOX_MAX_NAME_LENGTH];
|
2013-11-26 00:49:31 +01:00
|
|
|
|
2014-04-01 04:00:17 +02:00
|
|
|
if (change != TOX_CHAT_CHANGE_PEER_ADD) {
|
2014-04-19 23:58:13 +02:00
|
|
|
memcpy(oldpeername, &groupchats[groupnum].oldpeer_names[peernum * TOX_MAX_NAME_LENGTH],
|
2013-11-28 04:46:09 +01:00
|
|
|
sizeof(oldpeername));
|
2014-04-01 04:00:17 +02:00
|
|
|
uint16_t old_n_len = groupchats[groupnum].oldpeer_name_lengths[peernum];
|
|
|
|
oldpeername[old_n_len] = '\0';
|
|
|
|
}
|
2013-11-26 00:49:31 +01:00
|
|
|
|
2014-04-01 04:00:17 +02:00
|
|
|
/* Update name/len lists */
|
2013-11-27 02:45:08 +01:00
|
|
|
uint8_t tmp_peerlist[num_peers][TOX_MAX_NAME_LENGTH];
|
2014-04-01 04:00:17 +02:00
|
|
|
uint16_t tmp_peerlens[num_peers];
|
2014-10-04 00:57:32 +02:00
|
|
|
|
2014-10-05 08:29:40 +02:00
|
|
|
if (tox_group_get_names(m, groupnum, tmp_peerlist, tmp_peerlens, num_peers) == -1) {
|
2014-10-04 00:57:32 +02:00
|
|
|
memset(tmp_peerlist, 0, sizeof(tmp_peerlist));
|
|
|
|
memset(tmp_peerlens, 0, sizeof(tmp_peerlens));
|
|
|
|
}
|
|
|
|
|
2014-04-01 04:00:17 +02:00
|
|
|
copy_peernames(groupnum, tmp_peerlist, tmp_peerlens, num_peers);
|
2013-11-26 00:49:31 +01:00
|
|
|
|
2013-11-28 01:29:58 +01:00
|
|
|
/* get current peername then sort namelist */
|
2014-04-01 04:00:17 +02:00
|
|
|
uint8_t peername[TOX_MAX_NAME_LENGTH];
|
2014-04-03 00:57:11 +02:00
|
|
|
|
|
|
|
if (change != TOX_CHAT_CHANGE_PEER_DEL) {
|
|
|
|
uint16_t n_len = groupchats[groupnum].peer_name_lengths[peernum];
|
2014-04-19 23:58:13 +02:00
|
|
|
memcpy(peername, &groupchats[groupnum].peer_names[peernum * TOX_MAX_NAME_LENGTH], sizeof(peername));
|
2014-04-03 00:57:11 +02:00
|
|
|
peername[n_len] = '\0';
|
|
|
|
}
|
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
|
|
|
|
2014-07-29 20:54:34 +02:00
|
|
|
const char *event;
|
2014-07-07 04:15:35 +02:00
|
|
|
char timefrmt[TIME_STR_SIZE];
|
2014-06-24 00:54:23 +02:00
|
|
|
get_time_str(timefrmt, sizeof(timefrmt));
|
2014-02-26 12:35:19 +01:00
|
|
|
|
2013-11-26 00:49:31 +01:00
|
|
|
switch (change) {
|
2014-04-19 23:58:13 +02:00
|
|
|
case TOX_CHAT_CHANGE_PEER_ADD:
|
2014-11-29 15:36:31 +01:00
|
|
|
if (!timed_out(groupchats[groupnum].start_time, get_unix_time(), GROUP_EVENT_WAIT))
|
2014-10-09 07:39:22 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
struct group_add_thrd *thrd = malloc(sizeof(struct group_add_thrd));
|
|
|
|
thrd->m = m;
|
|
|
|
thrd->peernum = peernum;
|
|
|
|
thrd->groupnum = groupnum;
|
|
|
|
thrd->self = self;
|
|
|
|
thrd->timestamp = get_unix_time();
|
|
|
|
|
2014-10-09 22:39:01 +02:00
|
|
|
if (pthread_attr_init(&thrd->attr) != 0) {
|
|
|
|
free(thrd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_attr_setdetachstate(&thrd->attr, PTHREAD_CREATE_DETACHED) != 0) {
|
|
|
|
pthread_attr_destroy(&thrd->attr);
|
|
|
|
free(thrd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_create(&thrd->tid, &thrd->attr, group_add_wait, (void *) thrd) != 0) {
|
|
|
|
pthread_attr_destroy(&thrd->attr);
|
|
|
|
free(thrd);
|
|
|
|
return;
|
|
|
|
}
|
2014-10-09 07:39:22 +02:00
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TOX_CHAT_CHANGE_PEER_DEL:
|
2015-05-25 16:38:52 +02:00
|
|
|
event = gettext("has left the room");
|
2015-02-08 13:49:05 +01:00
|
|
|
line_info_add(self, timefrmt, (char *) oldpeername, NULL, DISCONNECTION, 0, RED, event);
|
2014-04-19 23:58:13 +02:00
|
|
|
|
|
|
|
if (groupchats[self->num].side_pos > 0)
|
|
|
|
--groupchats[self->num].side_pos;
|
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
write_to_log(event, (char *) oldpeername, ctx->log, true);
|
2014-04-19 23:58:13 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TOX_CHAT_CHANGE_PEER_NAME:
|
2014-10-09 07:39:22 +02:00
|
|
|
if (!timed_out(groupchats[self->num].start_time, get_unix_time(), GROUP_EVENT_WAIT))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* ignore initial name change (TODO: this is a bad way to do this) */
|
|
|
|
if (strcmp((char *) oldpeername, DEFAULT_TOX_NAME) == 0)
|
|
|
|
return;
|
|
|
|
|
2015-05-25 16:38:52 +02:00
|
|
|
event = gettext(" is now known as ");
|
2014-07-25 04:43:32 +02:00
|
|
|
line_info_add(self, timefrmt, (char *) oldpeername, (char *) peername, NAME_CHANGE, 0, 0, event);
|
2014-04-19 23:58:13 +02:00
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
char tmp_event[TOXIC_MAX_NAME_LENGTH * 2 + 32];
|
2015-05-25 16:38:52 +02:00
|
|
|
snprintf(tmp_event, sizeof(tmp_event), gettext("is now known as %s"), (char *) peername);
|
2014-07-07 04:15:35 +02:00
|
|
|
write_to_log(tmp_event, (char *) oldpeername, ctx->log, true);
|
2014-04-19 23:58:13 +02:00
|
|
|
break;
|
2013-11-26 00:49:31 +01:00
|
|
|
}
|
2013-11-29 01:45:28 +01:00
|
|
|
|
2014-08-02 00:37:02 +02:00
|
|
|
sound_notify(self, silent, NT_WNDALERT_2, NULL);
|
2013-11-24 03:19:59 +01:00
|
|
|
}
|
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
static void send_group_action(ToxWindow *self, ChatContext *ctx, Tox *m, char *action)
|
2014-04-19 23:58:13 +02:00
|
|
|
{
|
2013-12-14 02:57:32 +01:00
|
|
|
if (action == NULL) {
|
2015-05-25 16:38:52 +02:00
|
|
|
wprintw(ctx->history, gettext("Invalid syntax.\n"));
|
2013-12-14 02:57:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
if (tox_group_action_send(m, self->num, (uint8_t *) action, strlen(action)) == -1) {
|
2015-05-25 16:38:52 +02:00
|
|
|
const char *errmsg = gettext(" * Failed to send action.");
|
2014-07-25 04:43:32 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg);
|
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
|
|
|
{
|
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);
|
2014-06-21 07:26:33 +02:00
|
|
|
|
|
|
|
if (x2 <= 0)
|
|
|
|
return;
|
|
|
|
|
2014-07-04 09:24:29 +02:00
|
|
|
if (self->help->active) {
|
|
|
|
help_onKey(self, key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-21 05:08:13 +02:00
|
|
|
if (ltr) { /* char is printable */
|
2014-06-26 08:33:09 +02:00
|
|
|
input_new_char(self, key, x, y, x2, y2);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-01 22:59:46 +01:00
|
|
|
|
2014-06-26 08:33:09 +02:00
|
|
|
if (line_info_onKey(self, key))
|
|
|
|
return;
|
2014-03-30 22:40:13 +02:00
|
|
|
|
2014-06-26 08:33:09 +02:00
|
|
|
if (input_handle(self, key, x, y, x2, y2))
|
|
|
|
return;
|
2013-12-06 04:55:14 +01:00
|
|
|
|
2014-06-26 08:33:09 +02: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
|
|
|
|
2014-09-26 09:10:44 +02:00
|
|
|
/* TODO: make this not suck */
|
|
|
|
if (ctx->line[0] != L'/' || wcscmp(ctx->line, L"/me") == 0) {
|
2015-03-26 03:56:45 +01:00
|
|
|
diff = complete_line(self, groupchats[self->num].peer_names, groupchats[self->num].num_peers,
|
2014-09-26 09:10:44 +02:00
|
|
|
TOX_MAX_NAME_LENGTH);
|
|
|
|
} else if (wcsncmp(ctx->line, L"/avatar \"", wcslen(L"/avatar \"")) == 0) {
|
|
|
|
diff = dir_match(self, m, ctx->line, L"/avatar");
|
|
|
|
} else {
|
2014-11-15 02:13:08 +01:00
|
|
|
diff = complete_line(self, group_cmd_list, AC_NUM_GROUP_COMMANDS, MAX_CMDNAME_SIZE);
|
2014-09-26 09:10:44 +02:00
|
|
|
}
|
2013-12-01 22:59:46 +01:00
|
|
|
|
2014-06-26 08:33:09 +02:00
|
|
|
if (diff != -1) {
|
|
|
|
if (x + diff > x2 - 1) {
|
2014-07-18 18:42:53 +02:00
|
|
|
int wlen = wcswidth(ctx->line, sizeof(ctx->line));
|
|
|
|
ctx->start = wlen < x2 ? 0 : wlen - x2 + 1;
|
2014-06-21 05:08:13 +02:00
|
|
|
}
|
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
|
|
|
}
|
2014-09-23 03:24:45 +02:00
|
|
|
} else if (key == user_settings->key_peer_list_down) { /* Scroll peerlist up and down one position */
|
2014-06-26 08:33:09 +02:00
|
|
|
int L = y2 - CHATBOX_HEIGHT - SDBAR_OFST;
|
|
|
|
|
|
|
|
if (groupchats[self->num].side_pos < groupchats[self->num].num_peers - L)
|
|
|
|
++groupchats[self->num].side_pos;
|
2014-09-23 03:24:45 +02:00
|
|
|
} else if (key == user_settings->key_peer_list_up) {
|
2014-06-26 08:33:09 +02:00
|
|
|
if (groupchats[self->num].side_pos > 0)
|
|
|
|
--groupchats[self->num].side_pos;
|
|
|
|
} else if (key == '\n') {
|
|
|
|
rm_trailing_spaces_buf(ctx);
|
2013-12-03 09:44:02 +01:00
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
char line[MAX_STR_SIZE];
|
2013-12-11 06:10:09 +01:00
|
|
|
|
2014-06-26 08:33:09 +02:00
|
|
|
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1)
|
|
|
|
memset(&line, 0, sizeof(line));
|
2013-12-19 02:18:02 +01:00
|
|
|
|
2014-06-26 08:33:09 +02:00
|
|
|
if (!string_is_empty(line))
|
|
|
|
add_line_to_hist(ctx);
|
2014-03-30 22:40:13 +02:00
|
|
|
|
2014-06-26 08:33:09 +02:00
|
|
|
if (line[0] == '/') {
|
|
|
|
if (strcmp(line, "/close") == 0) {
|
|
|
|
close_groupchat(self, m, self->num);
|
|
|
|
return;
|
|
|
|
} else if (strncmp(line, "/me ", strlen("/me ")) == 0) {
|
|
|
|
send_group_action(self, ctx, m, line + strlen("/me "));
|
2013-12-16 02:52:10 +01:00
|
|
|
} else {
|
2014-06-26 08:33:09 +02:00
|
|
|
execute(ctx->history, self, m, line, GROUPCHAT_COMMAND_MODE);
|
2013-12-08 04:10:32 +01:00
|
|
|
}
|
2014-06-26 08:33:09 +02:00
|
|
|
} else if (!string_is_empty(line)) {
|
2014-07-07 04:15:35 +02:00
|
|
|
if (tox_group_message_send(m, self->num, (uint8_t *) line, strlen(line)) == -1) {
|
2015-05-25 16:38:52 +02:00
|
|
|
const char *errmsg = gettext(" * Failed to send message.");
|
2014-07-25 04:43:32 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg);
|
2013-09-15 22:38:38 +02:00
|
|
|
}
|
2014-03-30 22:40:13 +02:00
|
|
|
}
|
2014-06-26 08:33:09 +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);
|
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
|
|
|
|
2015-07-01 04:40:45 +02:00
|
|
|
pthread_mutex_lock(&Winthread.lock);
|
2014-03-25 13:21:50 +01:00
|
|
|
line_info_print(self);
|
2015-07-01 04:40:45 +02:00
|
|
|
pthread_mutex_unlock(&Winthread.lock);
|
|
|
|
|
2014-03-27 10:08:48 +01:00
|
|
|
wclear(ctx->linewin);
|
2013-12-08 10:16:49 +01:00
|
|
|
|
2014-06-13 07:42:20 +02:00
|
|
|
curs_set(1);
|
2013-12-08 10:16:49 +01:00
|
|
|
|
2014-06-26 08:33:09 +02:00
|
|
|
if (ctx->len > 0)
|
|
|
|
mvwprintw(ctx->linewin, 1, 0, "%ls", &ctx->line[ctx->start]);
|
2013-12-01 04:12:43 +01:00
|
|
|
|
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);
|
2013-11-24 03:19:59 +01:00
|
|
|
|
2014-10-08 08:45:08 +02:00
|
|
|
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
|
|
|
|
2014-10-08 08:45:08 +02:00
|
|
|
int num_peers = groupchats[self->num].num_peers;
|
2013-11-29 07:30:10 +01:00
|
|
|
|
2014-10-08 08:45:08 +02:00
|
|
|
wmove(ctx->sidebar, 0, 1);
|
|
|
|
wattron(ctx->sidebar, A_BOLD);
|
2015-05-25 16:38:52 +02:00
|
|
|
wprintw(ctx->sidebar, gettext("Peers: %d\n"), num_peers);
|
2014-10-08 08:45:08 +02:00
|
|
|
wattroff(ctx->sidebar, A_BOLD);
|
2013-11-29 07:30:10 +01:00
|
|
|
|
2014-10-08 08:45:08 +02: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
|
|
|
|
2014-10-08 08:45:08 +02:00
|
|
|
for (i = 0; i < num_peers && i < maxlines; ++i) {
|
|
|
|
wmove(ctx->sidebar, i + 2, 1);
|
|
|
|
int peer = i + groupchats[self->num].side_pos;
|
2014-01-31 04:01:08 +01:00
|
|
|
|
2014-10-08 08:45:08 +02:00
|
|
|
/* truncate nick to fit in side panel without modifying list */
|
|
|
|
char tmpnck[TOX_MAX_NAME_LENGTH];
|
|
|
|
int maxlen = SIDEBAR_WIDTH - 2;
|
2014-12-07 18:16:49 +01:00
|
|
|
memcpy(tmpnck, &groupchats[self->num].peer_names[peer * TOX_MAX_NAME_LENGTH], maxlen);
|
2014-10-08 08:45:08 +02:00
|
|
|
tmpnck[maxlen] = '\0';
|
2014-01-31 04:01:08 +01:00
|
|
|
|
2014-10-08 08:45:08 +02:00
|
|
|
wprintw(ctx->sidebar, "%s\n", tmpnck);
|
|
|
|
}
|
2013-11-24 03:19:59 +01:00
|
|
|
}
|
2014-07-01 05:56:47 +02:00
|
|
|
|
|
|
|
int y, x;
|
|
|
|
getyx(self->window, y, x);
|
2014-07-07 04:15:35 +02:00
|
|
|
(void) x;
|
2014-07-14 20:32:38 +02:00
|
|
|
int new_x = ctx->start ? x2 - 1 : wcswidth(ctx->line, ctx->pos);
|
2014-07-01 05:56:47 +02:00
|
|
|
wmove(self->window, y + 1, new_x);
|
2014-07-04 09:24:29 +02:00
|
|
|
|
2014-07-05 20:46:16 +02:00
|
|
|
wrefresh(self->window);
|
|
|
|
|
|
|
|
if (self->help->active)
|
2014-07-04 09:24:29 +02:00
|
|
|
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);
|
2013-11-24 03:19:59 +01:00
|
|
|
|
2013-11-29 23:48:08 +01:00
|
|
|
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);
|
2013-11-24 03:19:59 +01:00
|
|
|
|
2014-07-04 19:24:18 +02:00
|
|
|
ctx->hst = calloc(1, sizeof(struct history));
|
|
|
|
ctx->log = calloc(1, sizeof(struct chatlog));
|
2014-03-01 13:10:44 +01:00
|
|
|
|
2014-06-18 21:54:05 +02:00
|
|
|
if (ctx->log == NULL || ctx->hst == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("failed in groupchat_onInit"), FATALERR_MEMORY);
|
2014-03-01 13:10:44 +01:00
|
|
|
|
2014-03-25 13:21:50 +01:00
|
|
|
line_info_init(ctx->hst);
|
2014-04-07 10:42:10 +02:00
|
|
|
|
2014-09-23 03:24:45 +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);
|
2014-09-22 23:09:39 +02:00
|
|
|
log_enable(self->name, myid, NULL, ctx->log, LOG_GROUP);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-11-29 15:36:31 +01:00
|
|
|
|
2014-11-15 02:13:08 +01:00
|
|
|
#ifdef AUDIO
|
2015-03-26 03:56:45 +01:00
|
|
|
static int group_audio_open_out_device(int groupnum)
|
2014-11-15 02:13:08 +01:00
|
|
|
{
|
2014-11-29 15:36:31 +01:00
|
|
|
char dname[MAX_STR_SIZE];
|
|
|
|
get_primary_device_name(output, dname, sizeof(dname));
|
2014-12-08 03:48:29 +01:00
|
|
|
dname[MAX_STR_SIZE - 1] = '\0';
|
2014-11-29 15:36:31 +01:00
|
|
|
|
|
|
|
groupchats[groupnum].audio.dvhandle = alcOpenDevice(dname);
|
2014-11-26 23:39:02 +01:00
|
|
|
|
2014-11-29 15:36:31 +01:00
|
|
|
if (groupchats[groupnum].audio.dvhandle == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
groupchats[groupnum].audio.dvctx = alcCreateContext(groupchats[groupnum].audio.dvhandle, NULL);
|
2014-12-10 00:29:07 +01:00
|
|
|
alcMakeContextCurrent(groupchats[groupnum].audio.dvctx);
|
|
|
|
alGenBuffers(OPENAL_BUFS, groupchats[groupnum].audio.buffers);
|
|
|
|
alGenSources((uint32_t) 1, &groupchats[groupnum].audio.source);
|
|
|
|
alSourcei(groupchats[groupnum].audio.source, AL_LOOPING, AL_FALSE);
|
|
|
|
|
|
|
|
if (alcGetError(groupchats[groupnum].audio.dvhandle) != AL_NO_ERROR) {
|
|
|
|
group_audio_close_out_device(groupnum);
|
|
|
|
groupchats[groupnum].audio.dvhandle = NULL;
|
|
|
|
groupchats[groupnum].audio.dvctx = NULL;
|
2014-11-29 15:36:31 +01:00
|
|
|
return -1;
|
2014-12-10 00:29:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
alSourceQueueBuffers(groupchats[groupnum].audio.source, OPENAL_BUFS, groupchats[groupnum].audio.buffers);
|
|
|
|
alSourcePlay(groupchats[groupnum].audio.source);
|
2014-11-29 15:36:31 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-12-07 18:16:49 +01:00
|
|
|
static int group_audio_close_out_device(int groupnum)
|
2014-11-29 15:36:31 +01:00
|
|
|
{
|
2014-12-07 18:16:49 +01:00
|
|
|
if (!groupchats[groupnum].audio.dvhandle)
|
|
|
|
return -1;
|
2014-11-29 15:36:31 +01:00
|
|
|
|
2014-12-07 18:16:49 +01:00
|
|
|
if (!groupchats[groupnum].audio.dvctx)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (alcGetCurrentContext() != groupchats[groupnum].audio.dvctx)
|
|
|
|
alcMakeContextCurrent(groupchats[groupnum].audio.dvctx);
|
|
|
|
|
2014-12-10 00:29:07 +01:00
|
|
|
alDeleteSources((uint32_t) 1, &groupchats[groupnum].audio.source);
|
2014-12-10 00:35:14 +01:00
|
|
|
alDeleteBuffers(OPENAL_BUFS, groupchats[groupnum].audio.buffers);
|
2014-12-07 18:16:49 +01:00
|
|
|
|
|
|
|
alcMakeContextCurrent(NULL);
|
|
|
|
alcDestroyContext(groupchats[groupnum].audio.dvctx);
|
|
|
|
|
|
|
|
if (!alcCloseDevice(groupchats[groupnum].audio.dvhandle))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
2014-11-29 15:36:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int group_audio_write(int peernum, int groupnum, const int16_t *pcm, unsigned int samples, uint8_t channels,
|
|
|
|
unsigned int sample_rate)
|
|
|
|
{
|
|
|
|
if (!pcm)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (channels == 0 || channels > 2)
|
|
|
|
return -2;
|
|
|
|
|
|
|
|
ALuint bufid;
|
2014-12-10 00:35:14 +01:00
|
|
|
ALint processed = 0, queued = 0;
|
2014-11-29 15:36:31 +01:00
|
|
|
|
2014-12-10 00:29:07 +01:00
|
|
|
alGetSourcei(groupchats[groupnum].audio.source, AL_BUFFERS_PROCESSED, &processed);
|
|
|
|
alGetSourcei(groupchats[groupnum].audio.source, AL_BUFFERS_QUEUED, &queued);
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("source: %d, queued: %d, processed: %d\n"), groupchats[groupnum].audio.source, queued, processed);
|
2014-11-29 15:36:31 +01:00
|
|
|
|
|
|
|
if (processed) {
|
|
|
|
ALuint bufids[processed];
|
2014-12-10 00:29:07 +01:00
|
|
|
alSourceUnqueueBuffers(groupchats[groupnum].audio.source, processed, bufids);
|
2014-11-29 15:36:31 +01:00
|
|
|
alDeleteBuffers(processed - 1, bufids + 1);
|
|
|
|
bufid = bufids[0];
|
|
|
|
} else if (queued < 16) {
|
|
|
|
alGenBuffers(1, &bufid);
|
|
|
|
} else {
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
int length = samples * channels * sizeof(int16_t);
|
|
|
|
|
|
|
|
alBufferData(bufid, (channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16, pcm, length, sample_rate);
|
2014-12-10 00:29:07 +01:00
|
|
|
alSourceQueueBuffers(groupchats[groupnum].audio.source, 1, &bufid);
|
2014-11-29 15:36:31 +01:00
|
|
|
|
|
|
|
ALint state;
|
2014-12-10 00:29:07 +01:00
|
|
|
alGetSourcei(groupchats[groupnum].audio.source, AL_SOURCE_STATE, &state);
|
2014-11-29 15:36:31 +01:00
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
if (state != AL_PLAYING)
|
2014-12-10 00:29:07 +01:00
|
|
|
alSourcePlay(groupchats[groupnum].audio.source);
|
2014-11-29 15:36:31 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 03:56:45 +01:00
|
|
|
static void groupchat_onWriteDevice(ToxWindow *self, Tox *m, int groupnum, int peernum, const int16_t *pcm,
|
2014-12-06 19:26:27 +01:00
|
|
|
unsigned int samples, uint8_t channels, unsigned int sample_rate)
|
2014-11-29 15:36:31 +01:00
|
|
|
{
|
2014-12-10 02:30:13 +01:00
|
|
|
return;
|
|
|
|
|
2014-11-26 21:22:34 +01:00
|
|
|
if (groupnum != self->num)
|
|
|
|
return;
|
2014-11-15 02:13:08 +01:00
|
|
|
|
2014-12-10 00:29:07 +01:00
|
|
|
if (peernum < 0)
|
2014-11-26 21:22:34 +01:00
|
|
|
return;
|
2014-11-15 02:13:08 +01:00
|
|
|
|
2014-11-29 15:36:31 +01:00
|
|
|
if (groupchats[groupnum].audio.dvhandle == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("dvhandle is null)\n"));
|
2014-11-29 15:36:31 +01:00
|
|
|
|
|
|
|
if (groupchats[groupnum].audio.dvctx == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("ctx is null\n"));
|
2014-11-29 15:36:31 +01:00
|
|
|
|
2014-12-10 00:29:07 +01:00
|
|
|
int ret = group_audio_write(peernum, groupnum, pcm, samples, channels, sample_rate);
|
2015-05-25 16:38:52 +02:00
|
|
|
fprintf(stderr, gettext("write: %d\n"), ret);
|
2014-11-15 02:13:08 +01:00
|
|
|
}
|
|
|
|
#endif /* AUDIO */
|
|
|
|
|
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;
|
2014-02-26 11:23:11 +01:00
|
|
|
ret.is_groupchat = true;
|
2013-11-30 11:35:25 +01:00
|
|
|
|
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;
|
2014-11-15 04:15:59 +01:00
|
|
|
ret.onGroupTitleChange = &groupchat_onGroupTitleChange;
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2014-11-15 02:13:08 +01:00
|
|
|
#ifdef AUDIO
|
|
|
|
ret.onWriteDevice = &groupchat_onWriteDevice;
|
|
|
|
#endif
|
|
|
|
|
2015-05-25 16:38:52 +02:00
|
|
|
snprintf(ret.name, sizeof(ret.name), gettext("Group %d"), groupnum);
|
2013-09-15 22:38:38 +02:00
|
|
|
|
|
|
|
ChatContext *chatwin = calloc(1, sizeof(ChatContext));
|
2014-07-04 09:24:29 +02:00
|
|
|
Help *help = calloc(1, sizeof(Help));
|
2013-09-15 22:38:38 +02:00
|
|
|
|
2014-07-04 09:24:29 +02:00
|
|
|
if (chatwin == NULL || help == NULL)
|
2015-05-25 16:38:52 +02:00
|
|
|
exit_toxic_err(gettext("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;
|
2014-07-04 09:24:29 +02:00
|
|
|
ret.help = help;
|
|
|
|
|
2013-09-15 22:38:38 +02:00
|
|
|
ret.num = groupnum;
|
2014-10-08 08:45:08 +02:00
|
|
|
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;
|
|
|
|
}
|