mirror of
https://github.com/Tha14/toxic.git
synced 2026-01-26 21:23:14 +01:00
Merge with upstream
This commit is contained in:
136
src/chat.c
136
src/chat.c
@@ -189,8 +189,8 @@ static void chat_onMessage(ToxWindow *self, Tox *m, uint32_t num, TOX_MESSAGE_TY
|
||||
return recv_action_helper(self, m, num, msg, len, nick, timefrmt);
|
||||
}
|
||||
|
||||
static void chat_resume_file_transfers(Tox *m, uint32_t fnum);
|
||||
static void chat_stop_file_senders(Tox *m, uint32_t friendnum);
|
||||
static void chat_pause_file_transfers(Tox *m, uint32_t friendnum);
|
||||
static void chat_resume_file_senders(ToxWindow *self, Tox *m, uint32_t fnum);
|
||||
|
||||
static void chat_onConnectionChange(ToxWindow *self, Tox *m, uint32_t num, TOX_CONNECTION connection_status)
|
||||
{
|
||||
@@ -210,7 +210,7 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, uint32_t num, TOX_C
|
||||
if (connection_status != TOX_CONNECTION_NONE && statusbar->connection == TOX_CONNECTION_NONE) {
|
||||
Friends.list[num].is_typing = user_settings->show_typing_other == SHOW_TYPING_ON
|
||||
? tox_friend_get_typing(m, num, NULL) : false;
|
||||
chat_resume_file_transfers(m, num);
|
||||
chat_resume_file_senders(self, m, num);
|
||||
|
||||
msg = "has come online";
|
||||
line_info_add(self, timefrmt, nick, NULL, CONNECTION, 0, GREEN, msg);
|
||||
@@ -221,7 +221,7 @@ static void chat_onConnectionChange(ToxWindow *self, Tox *m, uint32_t num, TOX_C
|
||||
if (self->chatwin->self_is_typing)
|
||||
set_self_typingstatus(self, m, 0);
|
||||
|
||||
chat_stop_file_senders(m, num);
|
||||
chat_pause_file_transfers(m, num);
|
||||
|
||||
msg = "has gone offline";
|
||||
line_info_add(self, timefrmt, nick, NULL, DISCONNECTION, 0, RED, msg);
|
||||
@@ -278,17 +278,44 @@ static void chat_onReadReceipt(ToxWindow *self, Tox *m, uint32_t num, uint32_t r
|
||||
cqueue_remove(self, m, receipt);
|
||||
}
|
||||
|
||||
/* Stops active file senders for this friend. Call when a friend goes offline */
|
||||
static void chat_stop_file_senders(Tox *m, uint32_t friendnum)
|
||||
/* Stops active file transfers for this friend. Called when a friend goes offline */
|
||||
static void chat_pause_file_transfers(Tox *m, uint32_t friendnum)
|
||||
{
|
||||
// TODO: core purges file transfers when a friend goes offline. Ideally we want to repair/resume
|
||||
kill_all_file_transfers_friend(m, friendnum);
|
||||
ToxicFriend *friend = &Friends.list[friendnum];
|
||||
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < MAX_FILES; ++i) {
|
||||
if (friend->file_sender[i].state >= FILE_TRANSFER_STARTED)
|
||||
friend->file_sender[i].state = FILE_TRANSFER_PAUSED;
|
||||
|
||||
if (friend->file_receiver[i].state >= FILE_TRANSFER_STARTED)
|
||||
friend->file_receiver[i].state = FILE_TRANSFER_PAUSED;
|
||||
}
|
||||
}
|
||||
|
||||
/* Tries to resume broken file transfers. Call when a friend comes online */
|
||||
static void chat_resume_file_transfers(Tox *m, uint32_t fnum)
|
||||
/* Tries to resume broken file senders. Called when a friend comes online */
|
||||
static void chat_resume_file_senders(ToxWindow *self, Tox *m, uint32_t friendnum)
|
||||
{
|
||||
// TODO
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < MAX_FILES; ++i) {
|
||||
struct FileTransfer *ft = &Friends.list[friendnum].file_sender[i];
|
||||
|
||||
if (ft->state != FILE_TRANSFER_PAUSED)
|
||||
continue;
|
||||
|
||||
TOX_ERR_FILE_SEND err;
|
||||
ft->filenum = tox_file_send(m, friendnum, TOX_FILE_KIND_DATA, ft->file_size, ft->file_id,
|
||||
(uint8_t *) ft->file_name, strlen(ft->file_name), &err);
|
||||
|
||||
if (err != TOX_ERR_FILE_SEND_OK) {
|
||||
char msg[MAX_STR_SIZE];
|
||||
snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", ft->file_name);
|
||||
close_file_transfer(self, m, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void chat_onFileChunkRequest(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_t filenum, uint64_t position,
|
||||
@@ -347,6 +374,7 @@ static void chat_onFileChunkRequest(ToxWindow *self, Tox *m, uint32_t friendnum,
|
||||
|
||||
ft->position += send_length;
|
||||
ft->bps += send_length;
|
||||
ft->last_keep_alive = get_unix_time();
|
||||
}
|
||||
|
||||
static void chat_onFileRecvChunk(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_t filenum, uint64_t position,
|
||||
@@ -386,6 +414,7 @@ static void chat_onFileRecvChunk(ToxWindow *self, Tox *m, uint32_t friendnum, ui
|
||||
|
||||
ft->bps += length;
|
||||
ft->position += length;
|
||||
ft->last_keep_alive = get_unix_time();
|
||||
}
|
||||
|
||||
static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_t filenum, TOX_FILE_CONTROL control)
|
||||
@@ -401,14 +430,16 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint
|
||||
char msg[MAX_STR_SIZE];
|
||||
|
||||
switch (control) {
|
||||
case TOX_FILE_CONTROL_RESUME:
|
||||
case TOX_FILE_CONTROL_RESUME: {
|
||||
ft->last_keep_alive = get_unix_time();
|
||||
|
||||
/* transfer is accepted */
|
||||
if (ft->state == FILE_TRANSFER_PENDING) {
|
||||
ft->state = FILE_TRANSFER_STARTED;
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer [%d] for '%s' accepted.",
|
||||
ft->index, ft->file_name);
|
||||
char progline[MAX_STR_SIZE];
|
||||
prep_prog_line(progline);
|
||||
init_progress_bar(progline);
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", progline);
|
||||
sound_notify(self, silent, NT_NOFOCUS | NT_BEEP | NT_WNDALERT_2, NULL);
|
||||
ft->line_id = self->chatwin->hst->line_end->id + 2;
|
||||
@@ -417,25 +448,78 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case TOX_FILE_CONTROL_PAUSE:
|
||||
}
|
||||
case TOX_FILE_CONTROL_PAUSE: {
|
||||
ft->state = FILE_TRANSFER_PAUSED;
|
||||
break;
|
||||
|
||||
case TOX_FILE_CONTROL_CANCEL:
|
||||
}
|
||||
case TOX_FILE_CONTROL_CANCEL: {
|
||||
snprintf(msg, sizeof(msg), "File transfer for '%s' was aborted.", ft->file_name);
|
||||
close_file_transfer(self, m, ft, -1, msg, notif_error);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Attempts to resume a broken inbound file transfer.
|
||||
*
|
||||
* Returns true if resume is successful.
|
||||
*/
|
||||
static bool chat_resume_broken_ft(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_t filenum)
|
||||
{
|
||||
char msg[MAX_STR_SIZE];
|
||||
uint8_t file_id[TOX_FILE_ID_LENGTH];
|
||||
|
||||
if (!tox_file_get_file_id(m, friendnum, filenum, file_id, NULL))
|
||||
return false;
|
||||
|
||||
bool resuming = false;
|
||||
struct FileTransfer *ft = NULL;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < MAX_FILES; ++i) {
|
||||
ft = &Friends.list[friendnum].file_receiver[i];
|
||||
|
||||
if (ft->state == FILE_TRANSFER_INACTIVE)
|
||||
continue;
|
||||
|
||||
if (memcmp(ft->file_id, file_id, TOX_FILE_ID_LENGTH) == 0) {
|
||||
ft->filenum = filenum;
|
||||
ft->state = FILE_TRANSFER_STARTED;
|
||||
ft->last_keep_alive = get_unix_time();
|
||||
resuming = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!resuming || !ft)
|
||||
return false;
|
||||
|
||||
if (!tox_file_seek(m, ft->friendnum, ft->filenum, ft->position, NULL))
|
||||
goto on_error;
|
||||
|
||||
if (!tox_file_control(m, ft->friendnum, ft->filenum, TOX_FILE_CONTROL_RESUME, NULL))
|
||||
goto on_error;
|
||||
|
||||
return true;
|
||||
|
||||
on_error:
|
||||
snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", ft->file_name);
|
||||
close_file_transfer(self, m, ft, TOX_FILE_CONTROL_CANCEL, msg, notif_error);
|
||||
return false;
|
||||
}
|
||||
|
||||
static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_t filenum, uint64_t file_size,
|
||||
const char *filename, size_t name_length)
|
||||
{
|
||||
if (self->num != friendnum)
|
||||
return;
|
||||
|
||||
struct FileTransfer *ft = get_new_file_receiver(friendnum);
|
||||
/* first check if we need to resume a broken transfer */
|
||||
if (chat_resume_broken_ft(self, m, friendnum, filenum))
|
||||
return;
|
||||
|
||||
struct FileTransfer *ft = new_file_transfer(self, friendnum, filenum, FILE_TRANSFER_RECV, TOX_FILE_KIND_DATA);
|
||||
|
||||
if (!ft) {
|
||||
tox_file_control(m, friendnum, filenum, TOX_FILE_CONTROL_CANCEL, NULL);
|
||||
@@ -458,7 +542,7 @@ static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_
|
||||
snprintf(file_path, sizeof(file_path), "%s", filename);
|
||||
}
|
||||
|
||||
if (path_len >= sizeof(ft->file_path) || name_length >= sizeof(ft->file_name)) {
|
||||
if (path_len >= sizeof(file_path) || path_len >= sizeof(ft->file_path) || name_length >= sizeof(ft->file_name)) {
|
||||
tox_file_control(m, friendnum, filenum, TOX_FILE_CONTROL_CANCEL, NULL);
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer faield: File path too long.");
|
||||
return;
|
||||
@@ -493,14 +577,10 @@ static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_
|
||||
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Type '/savefile %d' to accept the file transfer.", ft->index);
|
||||
|
||||
ft->state = FILE_TRANSFER_PENDING;
|
||||
ft->direction = FILE_TRANSFER_RECV;
|
||||
ft->file_size = file_size;
|
||||
ft->friendnum = friendnum;
|
||||
ft->filenum = filenum;
|
||||
ft->file_type = TOX_FILE_KIND_DATA;
|
||||
snprintf(ft->file_path, sizeof(ft->file_path), "%s", file_path);
|
||||
snprintf(ft->file_name, sizeof(ft->file_name), "%s", filename);
|
||||
tox_file_get_file_id(m, friendnum, filenum, ft->file_id, NULL);
|
||||
|
||||
if (self->active_box != -1)
|
||||
box_notify2(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS, self->active_box,
|
||||
@@ -851,7 +931,7 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key, bool ltr)
|
||||
|
||||
if (diff != -1) {
|
||||
if (x + diff > x2 - 1) {
|
||||
int wlen = wcswidth(ctx->line, sizeof(ctx->line));
|
||||
int wlen = MAX(0, wcswidth(ctx->line, sizeof(ctx->line) / sizeof(wchar_t)));
|
||||
ctx->start = wlen < x2 ? 0 : wlen - x2 + 1;
|
||||
}
|
||||
} else {
|
||||
@@ -1005,7 +1085,7 @@ static void chat_onDraw(ToxWindow *self, Tox *m)
|
||||
int y, x;
|
||||
getyx(self->window, y, x);
|
||||
(void) x;
|
||||
int new_x = ctx->start ? x2 - 1 : wcswidth(ctx->line, ctx->pos);
|
||||
int new_x = ctx->start ? x2 - 1 : MAX(0, wcswidth(ctx->line, ctx->pos));
|
||||
wmove(self->window, y + 1, new_x);
|
||||
|
||||
wrefresh(self->window);
|
||||
@@ -1072,11 +1152,13 @@ static void chat_onInit(ToxWindow *self, Tox *m)
|
||||
char myid[TOX_ADDRESS_SIZE];
|
||||
tox_self_get_address(m, (uint8_t *) myid);
|
||||
|
||||
log_enable(nick, myid, Friends.list[self->num].pub_key, ctx->log, LOG_CHAT);
|
||||
int log_ret = log_enable(nick, myid, Friends.list[self->num].pub_key, ctx->log, LOG_CHAT);
|
||||
load_chat_history(self, ctx->log);
|
||||
|
||||
if (!Friends.list[self->num].logging_on)
|
||||
log_disable(ctx->log);
|
||||
else if (log_ret == -1)
|
||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Warning: Log failed to initialize.");
|
||||
|
||||
execute(ctx->history, self, m, "/log", GLOBAL_COMMAND_MODE);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user