2014-02-25 08:28:24 +01:00
|
|
|
/* chat_commands.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-11-10 03:43:56 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
#include "toxic.h"
|
|
|
|
#include "windows.h"
|
2013-11-10 03:43:56 +01:00
|
|
|
#include "misc_tools.h"
|
2013-11-29 23:48:08 +01:00
|
|
|
#include "friendlist.h"
|
2014-02-27 01:45:11 +01:00
|
|
|
#include "execute.h"
|
2014-03-24 12:18:58 +01:00
|
|
|
#include "line_info.h"
|
2014-06-22 03:41:38 +02:00
|
|
|
#include "groupchat.h"
|
2014-08-04 07:56:43 +02:00
|
|
|
#include "chat.h"
|
|
|
|
#include "file_senders.h"
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
extern ToxWindow *prompt;
|
2014-08-07 19:05:42 +02:00
|
|
|
extern _Friends Friends;
|
2013-11-30 00:52:21 +01:00
|
|
|
|
2014-06-28 18:14:43 +02:00
|
|
|
extern FileSender file_senders[MAX_FILES];
|
|
|
|
extern uint8_t max_file_senders_index;
|
2014-07-28 03:19:17 +02:00
|
|
|
extern uint8_t num_active_file_senders;
|
2014-06-28 18:14:43 +02:00
|
|
|
|
2014-08-04 07:56:43 +02:00
|
|
|
void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
|
|
|
{
|
|
|
|
if (argc < 2) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Requires type in|out and the file ID.");
|
2014-08-04 07:56:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *inoutstr = argv[1];
|
|
|
|
int filenum = atoi(argv[2]);
|
|
|
|
|
2014-08-04 20:35:34 +02:00
|
|
|
if (filenum >= MAX_FILES || filenum < 0) {
|
2014-08-04 07:56:43 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-04 20:35:34 +02:00
|
|
|
if (strcasecmp(inoutstr, "in") == 0) { /* cancel an incoming file transfer */
|
2014-08-07 19:05:42 +02:00
|
|
|
if (!Friends.list[self->num].file_receiver[filenum].active) {
|
2014-08-04 07:56:43 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-07 19:05:42 +02:00
|
|
|
const char *filepath = Friends.list[self->num].file_receiver[filenum].filename;
|
2014-08-04 07:56:43 +02:00
|
|
|
char name[MAX_STR_SIZE];
|
|
|
|
get_file_name(name, sizeof(name), filepath);
|
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer for '%s' canceled.", name);
|
2014-08-04 22:22:29 +02:00
|
|
|
chat_close_file_receiver(m, filenum, self->num, TOX_FILECONTROL_KILL);
|
2014-08-04 20:35:34 +02:00
|
|
|
return;
|
|
|
|
} else if (strcasecmp(inoutstr, "out") == 0) { /* cancel an outgoing file transfer */
|
2014-08-04 07:56:43 +02:00
|
|
|
int i;
|
|
|
|
bool match = false;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_FILES; ++i) {
|
|
|
|
if (file_senders[i].active && file_senders[i].filenum == filenum) {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match) {
|
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid file ID.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *filename = file_senders[i].filename;
|
|
|
|
char msg[MAX_STR_SIZE];
|
|
|
|
snprintf(msg, sizeof(msg), "File transfer for '%s' canceled.", filename);
|
|
|
|
close_file_sender(self, m, i, msg, TOX_FILECONTROL_KILL, filenum, self->num);
|
2014-08-04 20:35:34 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Type must be 'in' or 'out'.");
|
|
|
|
return;
|
2014-08-04 07:56:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
void cmd_groupinvite(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
2013-11-10 03:43:56 +01:00
|
|
|
{
|
|
|
|
if (argc < 1) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group number required.");
|
2014-03-24 12:18:58 +01:00
|
|
|
return;
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int groupnum = atoi(argv[1]);
|
|
|
|
|
|
|
|
if (groupnum == 0 && strcmp(argv[1], "0")) { /* atoi returns 0 value on invalid input */
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid group number.");
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
if (tox_invite_friend(m, self->num, groupnum) == -1) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to invite contact to group.");
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-02 21:35:57 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invited contact to Group %d.", groupnum);
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
2013-11-10 03:43:56 +01:00
|
|
|
{
|
2014-03-07 01:39:57 +01:00
|
|
|
if (get_num_active_windows() >= MAX_WINDOWS_NUM) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open.");
|
2013-11-27 04:15:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-07 19:05:42 +02:00
|
|
|
const char *groupkey = Friends.list[self->num].groupchat_key;
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-08-07 19:05:42 +02:00
|
|
|
if (!Friends.list[self->num].groupchat_pending) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending group chat invite.");
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
int groupnum = tox_join_groupchat(m, self->num, (uint8_t *) groupkey);
|
2013-11-10 03:43:56 +01:00
|
|
|
|
|
|
|
if (groupnum == -1) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat instance failed to initialize.");
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (init_groupchat_win(prompt, m, groupnum) == -1) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize.");
|
2013-11-10 03:43:56 +01:00
|
|
|
tox_del_groupchat(m, groupnum);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
2013-11-10 03:43:56 +01:00
|
|
|
{
|
2014-08-04 20:35:34 +02:00
|
|
|
if (argc < 1) {
|
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File ID required.");
|
2014-04-19 23:58:13 +02:00
|
|
|
return;
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t filenum = atoi(argv[1]);
|
|
|
|
|
|
|
|
if ((filenum == 0 && strcmp(argv[1], "0")) || filenum >= MAX_FILES) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with that ID.");
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-07 19:05:42 +02:00
|
|
|
if (!Friends.list[self->num].file_receiver[filenum].pending) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with that ID.");
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-07 19:05:42 +02:00
|
|
|
const char *filename = Friends.list[self->num].file_receiver[filenum].filename;
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-03-05 09:31:12 +01:00
|
|
|
if (tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_ACCEPT, 0, 0) == 0) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%d] as: '%s'", filenum, filename);
|
2014-07-27 23:14:28 +02:00
|
|
|
|
|
|
|
/* prep progress bar line */
|
|
|
|
char progline[MAX_STR_SIZE];
|
|
|
|
prep_prog_line(progline);
|
2014-08-02 21:35:57 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", progline);
|
2014-08-07 19:05:42 +02:00
|
|
|
Friends.list[self->num].file_receiver[filenum].line_id = self->chatwin->hst->line_end->id + 2;
|
2014-03-05 09:31:12 +01:00
|
|
|
|
2014-08-07 19:05:42 +02:00
|
|
|
if ((Friends.list[self->num].file_receiver[filenum].file = fopen(filename, "a")) == NULL) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, "* Error writing to file.");
|
2014-03-05 09:31:12 +01:00
|
|
|
tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
2014-08-16 23:20:53 +02:00
|
|
|
} else {
|
|
|
|
Friends.list[self->num].file_receiver[filenum].active = true;
|
|
|
|
++Friends.list[self->num].active_file_receivers;
|
2014-03-05 09:31:12 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File transfer failed.");
|
2014-03-05 09:31:12 +01:00
|
|
|
}
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-08-07 19:05:42 +02:00
|
|
|
Friends.list[self->num].file_receiver[filenum].pending = false;
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
void cmd_sendfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
2013-11-10 03:43:56 +01:00
|
|
|
{
|
2014-04-19 23:58:13 +02:00
|
|
|
if (max_file_senders_index >= (MAX_FILES - 1)) {
|
2014-08-04 20:35:34 +02:00
|
|
|
const char *errmsg = "Please wait for some of your outgoing file transfers to complete.";
|
2014-07-25 04:43:32 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 1) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File path required.");
|
2014-04-19 23:58:13 +02:00
|
|
|
return;
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 20:54:34 +02:00
|
|
|
if (argv[1][0] != '\"') {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File path must be enclosed in quotes.");
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-29 20:54:34 +02:00
|
|
|
/* remove opening and closing quotes */
|
|
|
|
char path[MAX_STR_SIZE];
|
|
|
|
snprintf(path, sizeof(path), "%s", &argv[1][1]);
|
2014-07-07 05:08:36 +02:00
|
|
|
int path_len = strlen(path) - 1;
|
|
|
|
path[path_len] = '\0';
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-07-25 23:55:21 +02:00
|
|
|
if (path_len >= MAX_STR_SIZE) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File path exceeds character limit.");
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *file_to_send = fopen(path, "r");
|
|
|
|
|
|
|
|
if (file_to_send == NULL) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File not found.");
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-24 06:06:02 +02:00
|
|
|
uint64_t filesize = file_size(path);
|
2014-09-24 04:51:56 +02:00
|
|
|
|
|
|
|
if (filesize == -1) {
|
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "File corrupt.");
|
|
|
|
fclose(file_to_send);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-25 23:55:21 +02:00
|
|
|
char filename[MAX_STR_SIZE] = {0};
|
2014-07-09 06:05:13 +02:00
|
|
|
get_file_name(filename, sizeof(filename), path);
|
2014-08-03 21:12:55 +02:00
|
|
|
int namelen = strlen(filename);
|
|
|
|
int filenum = tox_new_file_sender(m, self->num, filesize, (const uint8_t *) filename, namelen);
|
2013-11-10 03:43:56 +01:00
|
|
|
|
|
|
|
if (filenum == -1) {
|
2014-08-04 20:35:34 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Error sending file.");
|
2014-09-24 04:51:56 +02:00
|
|
|
fclose(file_to_send);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-28 18:14:43 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_FILES; ++i) {
|
|
|
|
if (!file_senders[i].active) {
|
2014-08-03 21:12:55 +02:00
|
|
|
memcpy(file_senders[i].filename, filename, namelen + 1);
|
2014-06-28 18:14:43 +02:00
|
|
|
file_senders[i].active = true;
|
|
|
|
file_senders[i].toxwin = self;
|
|
|
|
file_senders[i].file = file_to_send;
|
|
|
|
file_senders[i].filenum = filenum;
|
|
|
|
file_senders[i].friendnum = self->num;
|
|
|
|
file_senders[i].timestamp = get_unix_time();
|
|
|
|
file_senders[i].size = filesize;
|
|
|
|
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1,
|
|
|
|
tox_file_data_size(m, self->num), file_to_send);
|
2014-08-08 01:24:56 +02:00
|
|
|
|
|
|
|
char sizestr[32];
|
|
|
|
bytes_convert_str(sizestr, sizeof(sizestr), filesize);
|
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0,
|
|
|
|
"Sending file [%d]: '%s' (%s)", filenum, filename, sizestr);
|
2014-06-28 18:14:43 +02:00
|
|
|
|
2014-07-28 03:19:17 +02:00
|
|
|
++num_active_file_senders;
|
|
|
|
|
2014-06-28 18:14:43 +02:00
|
|
|
if (i == max_file_senders_index)
|
|
|
|
++max_file_senders_index;
|
|
|
|
|
2014-08-05 23:38:33 +02:00
|
|
|
reset_file_sender_queue();
|
2014-06-28 18:14:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|