1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-07-01 17:57:45 +02:00
toxic/src/chat.c

886 lines
26 KiB
C
Raw Normal View History

2014-02-25 08:28:24 +01:00
/* chat.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/>.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2013-07-31 19:20:03 +02:00
#include <stdlib.h>
#include <string.h>
#include <time.h>
2013-07-31 19:20:03 +02:00
#include "toxic_windows.h"
2013-11-10 03:43:56 +01:00
#include "execute.h"
#include "misc_tools.h"
#include "friendlist.h"
2013-12-10 09:03:45 +01:00
#include "toxic_strings.h"
#include "log.h"
2013-07-31 19:20:03 +02:00
2014-03-07 03:14:04 +01:00
#ifdef _SUPPORT_AUDIO
#include "audio_call.h"
#endif /* _SUPPORT_AUDIO */
2013-09-04 08:05:36 +02:00
extern char *DATA_FILE;
extern int store_data(Tox *m, char *path);
2013-11-30 00:52:21 +01:00
extern FileSender file_senders[MAX_FILES];
extern ToxicFriend friends[MAX_FRIENDS_NUM];
2014-03-13 11:06:53 +01:00
extern struct _Winthread Winthread;
2014-03-07 03:27:48 +01:00
#ifdef _SUPPORT_AUDIO
2014-03-08 01:12:51 +01:00
#define AC_NUM_CHAT_COMMANDS 22
2014-03-07 03:27:48 +01:00
#else
2014-03-08 01:12:51 +01:00
#define AC_NUM_CHAT_COMMANDS 18
2014-03-07 03:27:48 +01:00
#endif /* _SUPPORT_AUDIO */
/* Array of chat command names used for tab completion. */
static const uint8_t chat_cmd_list[AC_NUM_CHAT_COMMANDS][MAX_CMDNAME_SIZE] = {
{ "/accept" },
{ "/add" },
{ "/clear" },
{ "/close" },
{ "/connect" },
{ "/exit" },
{ "/groupchat" },
{ "/help" },
{ "/invite" },
{ "/join" },
2014-02-27 01:00:13 +01:00
{ "/log" },
{ "/myid" },
{ "/nick" },
{ "/note" },
{ "/quit" },
{ "/savefile" },
{ "/sendfile" },
{ "/status" },
2014-03-07 03:14:04 +01:00
#ifdef _SUPPORT_AUDIO
{ "/call" },
{ "/cancel" },
{ "/answer" },
{ "/hangup" },
#endif /* _SUPPORT_AUDIO */
};
2013-12-09 00:14:57 +01:00
2014-02-23 10:28:33 +01:00
static void set_typingstatus(ToxWindow *self, Tox *m, bool is_typing)
{
ChatContext *ctx = self->chatwin;
tox_set_user_is_typing(m, self->num, is_typing);
ctx->self_is_typing = is_typing;
}
2014-02-26 11:23:11 +01:00
void kill_chat_window(ToxWindow *self)
{
set_active_window(0);
ChatContext *ctx = self->chatwin;
StatusBar *statusbar = self->stb;
log_disable(ctx->log);
2014-02-26 11:23:11 +01:00
int f_num = self->num;
delwin(ctx->linewin);
delwin(statusbar->topline);
del_window(self);
disable_chatwin(f_num);
free(ctx->log);
2014-02-26 11:23:11 +01:00
free(ctx);
free(statusbar);
}
2013-08-23 23:03:44 +02:00
static void chat_onMessage(ToxWindow *self, Tox *m, int num, uint8_t *msg, uint16_t len)
2013-08-07 00:27:51 +02:00
{
2013-09-15 22:38:38 +02:00
if (self->num != num)
return;
ChatContext *ctx = self->chatwin;
2013-08-16 19:11:09 +02:00
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_get_name(m, num, nick);
2013-10-19 05:08:37 +02:00
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
2013-08-16 19:11:09 +02:00
print_time(ctx->history);
2013-12-04 00:01:17 +01:00
wattron(ctx->history, COLOR_PAIR(CYAN));
2013-08-16 19:11:09 +02:00
wprintw(ctx->history, "%s: ", nick);
2013-12-04 00:01:17 +01:00
wattroff(ctx->history, COLOR_PAIR(CYAN));
2013-09-26 06:33:51 +02:00
if (msg[0] == '>') {
wattron(ctx->history, COLOR_PAIR(GREEN));
wprintw(ctx->history, "%s\n", msg);
wattroff(ctx->history, COLOR_PAIR(GREEN));
} else
wprintw(ctx->history, "%s\n", msg);
2013-08-16 19:11:09 +02:00
2014-03-04 01:21:52 +01:00
write_to_log(msg, nick, ctx->log, false);
2013-11-29 01:45:28 +01:00
alert_window(self, WINDOW_ALERT_1, true);
2013-07-31 19:20:03 +02:00
}
static void chat_onConnectionChange(ToxWindow *self, Tox *m, int num, uint8_t status)
{
2013-09-15 22:38:38 +02:00
if (self->num != num)
return;
StatusBar *statusbar = self->stb;
2014-02-23 10:28:33 +01:00
if (status == 1) {
statusbar->is_online = true;
friends[num].is_typing = tox_get_is_typing(m, num);
} else {
statusbar->is_online = false;
friends[num].is_typing = false;
}
}
static void chat_onTypingChange(ToxWindow *self, Tox *m, int num, int is_typing)
{
if (self->num != num)
return;
friends[num].is_typing = is_typing;
}
2013-08-23 23:03:44 +02:00
static void chat_onAction(ToxWindow *self, Tox *m, int num, uint8_t *action, uint16_t len)
2013-08-08 21:01:33 +02:00
{
2013-09-15 22:38:38 +02:00
if (self->num != num)
return;
ChatContext *ctx = self->chatwin;
2013-08-08 21:01:33 +02:00
uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'};
tox_get_name(m, num, nick);
2013-10-19 05:08:37 +02:00
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
2013-08-31 08:22:07 +02:00
print_time(ctx->history);
wattron(ctx->history, COLOR_PAIR(YELLOW));
2013-08-31 08:22:07 +02:00
wprintw(ctx->history, "* %s %s\n", nick, action);
wattroff(ctx->history, COLOR_PAIR(YELLOW));
2013-08-08 21:01:33 +02:00
2014-03-04 01:21:52 +01:00
write_to_log(action, nick, ctx->log, true);
2013-11-29 01:45:28 +01:00
alert_window(self, WINDOW_ALERT_1, true);
2013-08-08 21:01:33 +02:00
}
static void chat_onNickChange(ToxWindow *self, Tox *m, int num, uint8_t *nick, uint16_t len)
2013-08-07 00:27:51 +02:00
{
2013-09-15 22:38:38 +02:00
if (self->num != num)
2013-08-16 19:11:09 +02:00
return;
2013-10-19 05:08:37 +02:00
nick[TOXIC_MAX_NAME_LENGTH] = '\0';
len = strlen(nick) + 1;
2013-09-23 07:22:21 +02:00
memcpy(self->name, nick, len);
2013-07-31 19:20:03 +02:00
}
static void chat_onStatusChange(ToxWindow *self, Tox *m, int num, TOX_USERSTATUS status)
2013-08-07 00:27:51 +02:00
{
2013-09-15 22:38:38 +02:00
if (self->num != num)
2013-08-16 19:11:09 +02:00
return;
StatusBar *statusbar = self->stb;
statusbar->status = status;
}
static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
{
2013-09-15 22:38:38 +02:00
if (self->num != num)
return;
StatusBar *statusbar = self->stb;
statusbar->statusmsg_len = len;
2013-09-23 07:22:21 +02:00
memcpy(statusbar->statusmsg, status, len);
2013-07-31 19:20:03 +02:00
}
2013-10-11 10:42:30 +02:00
static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum,
uint64_t filesize, uint8_t *pathname, uint16_t path_len)
2013-10-11 06:23:39 +02:00
{
if (self->num != num)
2013-10-11 06:23:39 +02:00
return;
ChatContext *ctx = self->chatwin;
2013-10-11 06:23:39 +02:00
2014-02-12 01:12:26 +01:00
uint8_t filename[MAX_STR_SIZE];
get_file_name(pathname, filename);
wprintw(ctx->history, "File transfer request for '%s' (%llu bytes).\n", filename,
2013-10-11 10:42:30 +02:00
(long long unsigned int)filesize);
2013-10-11 06:23:39 +02:00
if (filenum >= MAX_FILES) {
2013-10-11 06:23:39 +02:00
wprintw(ctx->history, "Too many pending file requests; discarding.\n");
return;
}
2013-11-12 22:53:41 +01:00
/* Append a number to duplicate file names */
FILE *filecheck = NULL;
2013-11-12 08:18:43 +01:00
int count = 1;
int len = strlen(filename);
while ((filecheck = fopen(filename, "r"))) {
filename[len] = '\0';
char d[9];
sprintf(d,"(%d)", count++);
strcat(filename, d);
filename[len + strlen(d)] = '\0';
if (count > 999) {
2013-11-12 08:18:43 +01:00
wprintw(ctx->history, "Error saving file to disk.\n");
return;
}
}
wprintw(ctx->history, "Type '/savefile %d' to accept the file transfer.\n", filenum);
2013-10-11 06:23:39 +02:00
friends[num].file_receiver.pending[filenum] = true;
2013-10-18 01:53:29 +02:00
strcpy(friends[num].file_receiver.filenames[filenum], filename);
2013-10-11 06:23:39 +02:00
2013-11-29 01:45:28 +01:00
alert_window(self, WINDOW_ALERT_2, true);
2013-10-11 06:23:39 +02:00
}
static void chat_close_file_receiver(int num, uint8_t filenum)
{
friends[num].file_receiver.pending[filenum] = false;
FILE *file = friends[num].file_receiver.files[filenum];
if (file != NULL)
fclose(file);
}
2013-10-11 10:42:30 +02:00
static void chat_onFileControl(ToxWindow *self, Tox *m, int num, uint8_t receive_send,
uint8_t filenum, uint8_t control_type, uint8_t *data, uint16_t length)
2013-10-11 06:23:39 +02:00
{
if (self->num != num)
return;
ChatContext *ctx = self->chatwin;
uint8_t *filename;
2013-10-11 06:23:39 +02:00
if (receive_send == 0)
filename = friends[num].file_receiver.filenames[filenum];
else
filename = file_senders[filenum].pathname;
2013-10-18 01:53:29 +02:00
2013-11-12 07:50:04 +01:00
switch (control_type) {
case TOX_FILECONTROL_ACCEPT:
2013-10-18 01:53:29 +02:00
wprintw(ctx->history, "File transfer for '%s' accepted.\n", filename);
2013-10-11 06:23:39 +02:00
break;
/*case TOX_FILECONTROL_PAUSE:
wprintw(ctx->history, "File transfer for '%s' paused.\n", filename);
break; */
case TOX_FILECONTROL_KILL:
2013-10-18 01:53:29 +02:00
wprintw(ctx->history, "File transfer for '%s' failed.\n", filename);
2013-11-30 01:26:59 +01:00
2013-11-30 01:31:47 +01:00
if (receive_send == 0)
chat_close_file_receiver(num, filenum);
2013-11-30 01:26:59 +01:00
else
chat_close_file_receiver(num, filenum);
2013-10-11 06:23:39 +02:00
break;
case TOX_FILECONTROL_FINISHED:
2013-10-18 01:53:29 +02:00
wprintw(ctx->history, "File transfer for '%s' complete.\n", filename);
chat_close_file_receiver(num, filenum);
2013-10-11 06:23:39 +02:00
break;
}
2013-11-29 01:45:28 +01:00
alert_window(self, WINDOW_ALERT_2, true);
2013-10-11 06:23:39 +02:00
}
2013-10-11 10:42:30 +02:00
static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, uint8_t *data,
uint16_t length)
2013-10-11 06:23:39 +02:00
{
if (self->num != num)
return;
ChatContext *ctx = self->chatwin;
2013-10-11 06:23:39 +02:00
if (fwrite(data, length, 1, friends[num].file_receiver.files[filenum]) != 1) {
2013-11-29 00:56:56 +01:00
wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, "* Error writing to file.\n");
wattroff(ctx->history, COLOR_PAIR(RED));
2014-03-07 01:39:57 +01:00
2013-11-30 01:26:59 +01:00
tox_file_send_control(m, num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
2014-03-07 01:39:57 +01:00
chat_close_file_receiver(num, filenum);
}
2013-10-11 06:23:39 +02:00
}
2013-11-10 03:43:56 +01:00
static void chat_onGroupInvite(ToxWindow *self, Tox *m, int friendnumber, uint8_t *group_pub_key)
{
2013-11-12 23:26:13 +01:00
if (self->num != friendnumber)
return;
ChatContext *ctx = self->chatwin;
2013-11-10 03:43:56 +01:00
uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'};
if (tox_get_name(m, friendnumber, name) == -1)
return;
2013-11-11 05:17:46 +01:00
wprintw(ctx->history, "%s has invited you to a group chat.\n", name);
2013-11-12 23:26:13 +01:00
memcpy(friends[friendnumber].pending_groupchat, group_pub_key, TOX_CLIENT_ID_SIZE);
wprintw(ctx->history, "Type \"/join\" to join the chat.\n");
2013-11-29 01:45:28 +01:00
alert_window(self, WINDOW_ALERT_2, true);
}
2013-08-03 16:00:48 +02:00
2014-03-07 03:14:04 +01:00
/* Av Stuff */
#ifdef _SUPPORT_AUDIO
void chat_onInvite (ToxWindow *self, ToxAv *av)
{
if (self->num != toxav_get_peer_id(av, 0))
return;
ChatContext *ctx = self->chatwin;
2014-03-12 03:54:09 +01:00
wprintw(ctx->history, "Incoming audio call!\n"
2014-03-07 03:14:04 +01:00
"Answer: \"/answer\" \"/cancel\"\n");
alert_window(self, WINDOW_ALERT_0, true);
}
void chat_onRinging (ToxWindow *self, ToxAv *av)
{
if (self->num != toxav_get_peer_id(av, 0))
return;
ChatContext *ctx = self->chatwin;
wprintw(ctx->history, "Ringing...\n"
"\"/cancel\" ?\n");
}
void chat_onStarting (ToxWindow *self, ToxAv *av)
{
if (self->num != toxav_get_peer_id(av, 0))
return;
ChatContext *ctx = self->chatwin;
if ( 0 != start_transmission() ) {/* YEAH! */
wprintw(ctx->history, "Error starting transmission!\n");
return;
}
wprintw(ctx->history, "Call started! \n"
"Type: \"/hangup\" to end it.\n");
}
void chat_onEnding (ToxWindow *self, ToxAv *av)
{
if (self->num != toxav_get_peer_id(av, 0))
return;
ChatContext *ctx = self->chatwin;
toxav_kill_transmission(av);
wprintw(ctx->history, "Call ended! \n");
}
void chat_onError (ToxWindow *self, ToxAv *av)
{
if (self->num != toxav_get_peer_id(av, 0))
return;
ChatContext *ctx = self->chatwin;
wprintw(ctx->history, "Error! \n");
}
void chat_onStart (ToxWindow *self, ToxAv *av)
{
if (self->num != toxav_get_peer_id(av, 0))
return;
ChatContext *ctx = self->chatwin;
if ( 0 != start_transmission() ) {/* YEAH! */
wprintw(ctx->history, "Error starting transmission!\n");
return;
}
wprintw(ctx->history, "Call started! \n"
"Type: \"/hangup\" to end it.\n");
}
void chat_onCancel (ToxWindow *self, ToxAv *av)
{
if (self->num != toxav_get_peer_id(av, 0))
return;
ChatContext *ctx = self->chatwin;
wprintw(ctx->history, "Call canceled! \n");
}
void chat_onReject (ToxWindow *self, ToxAv *av)
{
if (self->num != toxav_get_peer_id(av, 0))
return;
ChatContext *ctx = self->chatwin;
wprintw(ctx->history, "Rejected! \n");
}
void chat_onEnd (ToxWindow *self, ToxAv *av)
{
if (self->num != toxav_get_peer_id(av, 0))
return;
ChatContext *ctx = self->chatwin;
toxav_kill_transmission(av);
wprintw(ctx->history, "Call ended! \n");
}
void chat_onTimeout (ToxWindow *self, ToxAv *av)
{
if (self->num != toxav_get_peer_id(av, 0))
return;
ChatContext *ctx = self->chatwin;
wprintw(ctx->history, "No answer! \n");
}
#endif /* _SUPPORT_AUDIO */
static void send_action(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *action) {
if (action == NULL) {
wprintw(ctx->history, "Invalid syntax.\n");
return;
2013-09-02 07:14:51 +02:00
}
uint8_t selfname[TOX_MAX_NAME_LENGTH];
tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH);
2013-08-16 19:11:09 +02:00
print_time(ctx->history);
wattron(ctx->history, COLOR_PAIR(YELLOW));
wprintw(ctx->history, "* %s %s\n", selfname, action);
wattroff(ctx->history, COLOR_PAIR(YELLOW));
2013-08-16 19:11:09 +02:00
if (tox_send_action(m, self->num, action, strlen(action) + 1) == 0) {
wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, " * Failed to send action\n");
wattroff(ctx->history, COLOR_PAIR(RED));
2014-02-26 09:51:26 +01:00
} else {
2014-03-04 01:21:52 +01:00
write_to_log(action, selfname, ctx->log, true);
}
2013-07-31 19:20:03 +02:00
}
static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
{
ChatContext *ctx = self->chatwin;
StatusBar *statusbar = self->stb;
int x, y, y2, x2;
getyx(self->window, y, x);
getmaxyx(self->window, y2, x2);
2013-12-12 06:41:16 +01:00
int cur_len = 0;
2013-10-23 09:24:08 +02:00
if (key == 0x107 || key == 0x8 || key == 0x7f) { /* BACKSPACE key: Remove character behind pos */
if (ctx->pos > 0) {
2013-12-14 07:10:22 +01:00
cur_len = MAX(1, wcwidth(ctx->line[ctx->pos - 1]));
del_char_buf_bck(ctx->line, &ctx->pos, &ctx->len);
if (x == 0)
2013-12-12 06:41:16 +01:00
wmove(self->window, y-1, x2 - cur_len);
else
2013-12-12 06:41:16 +01:00
wmove(self->window, y, x - cur_len);
2013-12-16 02:52:10 +01:00
} else {
beep();
}
2013-12-01 22:57:05 +01:00
}
else if (key == KEY_DC) { /* DEL key: Remove character at pos */
2013-12-04 22:14:33 +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:57:05 +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
}
else if (key == KEY_HOME || key == T_KEY_C_A) { /* HOME/C-a key: Move cursor to start 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:57:05 +01:00
}
else if (key == KEY_END || key == T_KEY_C_E) { /* END/C-e 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:57:05 +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 19:25:03 +01:00
if (x == 0)
2013-12-12 06:41:16 +01:00
wmove(self->window, y-1, x2 - cur_len);
2013-12-01 19:25:03 +01:00
else
2013-12-12 06:41:16 +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:57:05 +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 19:25:03 +01:00
if (x == x2-1)
wmove(self->window, y+1, 0);
else
2013-12-12 06:41:16 +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-09 00:14:57 +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-19 02:18:02 +01:00
else if (key == '\t') { /* TAB key: completes command */
2013-12-09 00:14:57 +01:00
if (ctx->len > 1 && ctx->line[0] == '/') {
int diff = complete_line(ctx->line, &ctx->pos, &ctx->len, chat_cmd_list, AC_NUM_CHAT_COMMANDS,
2013-12-09 00:14:57 +01:00
MAX_CMDNAME_SIZE);
if (diff != -1) {
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-09 00:14:57 +01:00
}
2013-12-16 02:52:10 +01:00
} else {
beep();
2013-12-09 00:14:57 +01:00
}
}
else
#if HAVE_WIDECHAR
2013-11-29 02:23:37 +01:00
if (iswprint(key))
#else
2013-11-29 02:23:37 +01:00
if (isprint(key))
#endif
{ /* 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)) ) {
add_char_to_buf(ctx->line, &ctx->pos, &ctx->len, key);
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)));
}
2014-02-23 10:28:33 +01:00
if (!ctx->self_is_typing && ctx->line[0] != '/')
2014-02-23 10:28:33 +01:00
set_typingstatus(self, m, true);
}
/* RETURN key: Execute command or print line */
else if (key == '\n') {
uint8_t line[MAX_STR_SIZE];
if (wcs_to_mbs_buf(line, ctx->line, MAX_STR_SIZE) == -1)
memset(&line, 0, sizeof(line));
wclear(ctx->linewin);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
wclrtobot(self->window);
2013-10-23 09:24:08 +02:00
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);
if (line[0] == '/') {
2014-02-26 11:23:11 +01:00
if (strcmp(line, "/close") == 0) {
if (ctx->self_is_typing)
set_typingstatus(self, m, false);
kill_chat_window(self);
return;
} else if (strncmp(line, "/me ", strlen("/me ")) == 0) {
send_action(self, ctx, m, line + strlen("/me "));
2014-02-26 11:23:11 +01:00
} else {
execute(ctx->history, self, m, line, CHAT_COMMAND_MODE);
2014-02-26 11:23:11 +01:00
}
2013-12-14 02:57:32 +01:00
} else if (!string_is_empty(line)) {
uint8_t selfname[TOX_MAX_NAME_LENGTH];
tox_get_self_name(m, selfname, TOX_MAX_NAME_LENGTH);
print_time(ctx->history);
wattron(ctx->history, COLOR_PAIR(GREEN));
wprintw(ctx->history, "%s: ", selfname);
wattroff(ctx->history, COLOR_PAIR(GREEN));
2013-12-14 02:57:32 +01:00
if (line[0] == '>') {
wattron(ctx->history, COLOR_PAIR(GREEN));
2013-12-14 02:57:32 +01:00
wprintw(ctx->history, "%s\n", line);
wattroff(ctx->history, COLOR_PAIR(GREEN));
2013-12-14 02:57:32 +01:00
} else
wprintw(ctx->history, "%s\n", line);
2013-09-26 06:33:51 +02:00
2013-12-14 02:57:32 +01:00
if (!statusbar->is_online || tox_send_message(m, self->num, line, strlen(line) + 1) == 0) {
wattron(ctx->history, COLOR_PAIR(RED));
wprintw(ctx->history, " * Failed to send message.\n");
wattroff(ctx->history, COLOR_PAIR(RED));
2014-02-26 09:51:26 +01:00
} else {
2014-03-04 01:21:52 +01:00
write_to_log(line, selfname, ctx->log, false);
}
}
2014-02-26 11:23:11 +01:00
reset_buf(ctx->line, &ctx->pos, &ctx->len);
}
2014-02-26 11:23:11 +01:00
if (ctx->len <= 0 && ctx->self_is_typing)
set_typingstatus(self, m, false);
}
2013-08-23 23:03:44 +02:00
static void chat_onDraw(ToxWindow *self, Tox *m)
2013-08-07 00:27:51 +02:00
{
2013-08-16 19:11:09 +02:00
curs_set(1);
int x2, y2;
getmaxyx(self->window, y2, x2);
ChatContext *ctx = self->chatwin;
wclear(ctx->linewin);
if (ctx->len > 0) {
uint8_t line[MAX_STR_SIZE];
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);
}
}
/* Draw status bar */
StatusBar *statusbar = self->stb;
mvwhline(statusbar->topline, 1, 0, ACS_HLINE, x2);
wmove(statusbar->topline, 0, 0);
/* Draw name, status and note in statusbar */
if (statusbar->is_online) {
char *status_text = "Unknown";
int colour = WHITE;
TOX_USERSTATUS status = statusbar->status;
2013-11-12 07:50:04 +01:00
switch (status) {
case TOX_USERSTATUS_NONE:
status_text = "Online";
colour = GREEN;
break;
case TOX_USERSTATUS_AWAY:
status_text = "Away";
colour = YELLOW;
break;
case TOX_USERSTATUS_BUSY:
status_text = "Busy";
colour = RED;
break;
}
2014-02-23 10:28:33 +01:00
if (friends[self->num].is_typing)
wattron(statusbar->topline, COLOR_PAIR(YELLOW));
wattron(statusbar->topline, A_BOLD);
2013-09-06 06:56:55 +02:00
wprintw(statusbar->topline, " %s ", self->name);
wattroff(statusbar->topline, A_BOLD);
2014-02-23 10:28:33 +01:00
if (friends[self->num].is_typing)
wattroff(statusbar->topline, COLOR_PAIR(YELLOW));
wattron(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
2013-09-06 06:56:55 +02:00
wprintw(statusbar->topline, "[%s]", status_text);
wattroff(statusbar->topline, COLOR_PAIR(colour) | A_BOLD);
} else {
wattron(statusbar->topline, A_BOLD);
2013-09-06 06:56:55 +02:00
wprintw(statusbar->topline, " %s ", self->name);
wattroff(statusbar->topline, A_BOLD);
2013-09-06 06:56:55 +02:00
wprintw(statusbar->topline, "[Offline]");
}
/* Reset statusbar->statusmsg on window resize */
if (x2 != self->x) {
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
2014-03-13 11:06:53 +01:00
pthread_mutex_lock(&Winthread.lock);
tox_get_status_message(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
2014-03-13 11:06:53 +01:00
pthread_mutex_unlock(&Winthread.lock);
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
statusbar->statusmsg_len = tox_get_status_message_size(m, self->num);
}
self->x = x2;
/* Truncate note if it doesn't fit in statusbar */
2014-02-22 23:58:36 +01:00
uint16_t maxlen = x2 - getcurx(statusbar->topline) - (KEY_IDENT_DIGITS * 2) - 7;
if (statusbar->statusmsg_len > maxlen) {
statusbar->statusmsg[maxlen] = '\0';
statusbar->statusmsg_len = maxlen;
}
if (statusbar->statusmsg[0]) {
wattron(statusbar->topline, A_BOLD);
2013-11-24 05:46:46 +01:00
wprintw(statusbar->topline, " - %s ", statusbar->statusmsg);
wattroff(statusbar->topline, A_BOLD);
}
2014-02-22 23:58:36 +01:00
wclrtoeol(statusbar->topline);
wmove(statusbar->topline, 0, x2 - (KEY_IDENT_DIGITS * 2) - 3);
wprintw(statusbar->topline, "{");
int i;
for (i = 0; i < KEY_IDENT_DIGITS; ++i)
wprintw(statusbar->topline, "%02X", friends[self->num].pub_key[i] & 0xff);
wprintw(statusbar->topline, "}\n");
mvwhline(ctx->linewin, 0, 0, ACS_HLINE, x2);
2013-07-31 19:20:03 +02:00
}
2013-08-23 23:03:44 +02:00
static void chat_onInit(ToxWindow *self, Tox *m)
2013-08-07 00:27:51 +02:00
{
int x2, y2;
getmaxyx(self->window, y2, x2);
self->x = x2;
/* Init statusbar info */
StatusBar *statusbar = self->stb;
statusbar->status = tox_get_user_status(m, self->num);
statusbar->is_online = tox_get_friend_connection_status(m, self->num) == 1;
2013-09-08 09:18:34 +02:00
uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH] = {'\0'};
tox_get_status_message(m, self->num, statusmsg, TOX_MAX_STATUSMESSAGE_LENGTH);
2013-09-08 09:18:34 +02:00
snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg);
statusbar->statusmsg_len = tox_get_status_message_size(m, self->num);
/* Init subwindows */
ChatContext *ctx = self->chatwin;
statusbar->topline = subwin(self->window, 2, x2, 0, 0);
ctx->history = subwin(self->window, y2-CHATBOX_HEIGHT+1, x2, 0, 0);
2013-08-16 19:11:09 +02:00
scrollok(ctx->history, 1);
ctx->linewin = subwin(self->window, CHATBOX_HEIGHT, x2, y2-CHATBOX_HEIGHT, 0);
ctx->log = malloc(sizeof(struct chatlog));
if (ctx->log == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
memset(ctx->log, 0, sizeof(struct chatlog));
if (friends[self->num].logging_on)
2014-03-02 00:06:35 +01:00
log_enable(self->name, friends[self->num].pub_key, ctx->log);
2013-09-06 00:36:46 +02:00
wprintw(ctx->history, "\n\n");
execute(ctx->history, self, m, "/help", CHAT_COMMAND_MODE);
2014-02-27 01:00:13 +01:00
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
2013-07-31 19:20:03 +02:00
}
ToxWindow new_chat(Tox *m, int friendnum)
2013-08-07 00:27:51 +02:00
{
2013-08-16 19:11:09 +02:00
ToxWindow ret;
memset(&ret, 0, sizeof(ret));
ret.active = true;
2014-02-26 11:23:11 +01:00
ret.is_chat = true;
2013-08-16 19:11:09 +02:00
ret.onKey = &chat_onKey;
ret.onDraw = &chat_onDraw;
ret.onInit = &chat_onInit;
ret.onMessage = &chat_onMessage;
ret.onConnectionChange = &chat_onConnectionChange;
2014-02-23 10:28:33 +01:00
ret.onTypingChange = & chat_onTypingChange;
2013-11-10 03:43:56 +01:00
ret.onGroupInvite = &chat_onGroupInvite;
2013-08-16 19:11:09 +02:00
ret.onNickChange = &chat_onNickChange;
ret.onStatusChange = &chat_onStatusChange;
ret.onStatusMessageChange = &chat_onStatusMessageChange;
2013-08-16 19:11:09 +02:00
ret.onAction = &chat_onAction;
2013-10-11 06:23:39 +02:00
ret.onFileSendRequest = &chat_onFileSendRequest;
ret.onFileControl = &chat_onFileControl;
ret.onFileData = &chat_onFileData;
2014-03-07 03:14:04 +01:00
#ifdef _SUPPORT_AUDIO
ret.onInvite = &chat_onInvite;
ret.onRinging = &chat_onRinging;
ret.onStarting = &chat_onStarting;
ret.onEnding = &chat_onEnding;
ret.onError = &chat_onError;
ret.onStart = &chat_onStart;
ret.onCancel = &chat_onCancel;
ret.onReject = &chat_onReject;
ret.onEnd = &chat_onEnd;
ret.onTimeout = &chat_onTimeout;
#endif /* _SUPPORT_AUDIO */
2013-08-16 19:11:09 +02:00
uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'};
uint16_t len = tox_get_name(m, friendnum, name);
2013-10-19 05:46:58 +02:00
memcpy(ret.name, name, len);
ret.name[TOXIC_MAX_NAME_LENGTH] = '\0';
2013-08-16 19:11:09 +02:00
ChatContext *chatwin = calloc(1, sizeof(ChatContext));
StatusBar *stb = calloc(1, sizeof(StatusBar));
2013-09-12 00:07:26 +02:00
if (stb != NULL && chatwin != NULL) {
ret.chatwin = chatwin;
ret.stb = stb;
2013-09-12 00:07:26 +02:00
} else {
endwin();
2013-09-12 07:33:41 +02:00
fprintf(stderr, "calloc() failed. Aborting...\n");
2013-09-12 00:07:26 +02:00
exit(EXIT_FAILURE);
}
2013-09-15 22:38:38 +02:00
ret.num = friendnum;
2013-08-16 19:11:09 +02:00
return ret;
2013-07-31 19:20:03 +02:00
}