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-06-27 00:20:56 +02:00
|
|
|
#include "file_senders.h"
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
extern ToxWindow *prompt;
|
2014-06-27 00:20:56 +02:00
|
|
|
extern int max_file_senders_index;
|
2013-11-30 00:52:21 +01:00
|
|
|
extern ToxicFriend friends[MAX_FRIENDS_NUM];
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
void cmd_chat_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
2013-11-10 03:43:56 +01:00
|
|
|
{
|
2014-03-27 00:14:28 +01:00
|
|
|
struct history *hst = self->chatwin->hst;
|
|
|
|
line_info_clear(hst);
|
|
|
|
struct line_info *start = hst->line_start;
|
|
|
|
|
2014-02-27 01:45:11 +01:00
|
|
|
if (argc == 1) {
|
|
|
|
if (!strcmp(argv[1], "global")) {
|
|
|
|
execute(window, self, m, "/help", GLOBAL_COMMAND_MODE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-24 12:18:58 +01:00
|
|
|
uint8_t *msg = "Chat commands:";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN);
|
|
|
|
|
|
|
|
#ifdef _SUPPORT_AUDIO
|
2014-06-22 02:18:23 +02:00
|
|
|
#define NUMLINES 16
|
2014-03-24 12:18:58 +01:00
|
|
|
#else
|
2014-04-19 23:58:13 +02:00
|
|
|
#define NUMLINES 9
|
2014-03-24 12:18:58 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
uint8_t lines[NUMLINES][MAX_STR_SIZE] = {
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-03-07 03:14:04 +01:00
|
|
|
#ifdef _SUPPORT_AUDIO
|
2014-04-19 23:58:13 +02:00
|
|
|
{ " /call : Audio call" },
|
|
|
|
{ " /cancel : Cancel call" },
|
|
|
|
{ " /answer : Answer incomming call" },
|
|
|
|
{ " /reject : Reject incoming call" },
|
|
|
|
{ " /hangup : Hangup active call" },
|
2014-06-22 02:18:23 +02:00
|
|
|
{ " /sdev <type> <id> : Change active device" },
|
|
|
|
{ " /mute <type> : Mute active device if in call" },
|
|
|
|
{ " /sense <value> : VAD sensitivity treshold" },
|
2014-03-07 03:14:04 +01:00
|
|
|
#endif /* _SUPPORT_AUDIO */
|
2014-04-19 23:58:13 +02:00
|
|
|
{ " /invite <n> : Invite friend to a group chat" },
|
|
|
|
{ " /join : Join a pending group chat" },
|
|
|
|
{ " /log <on> or <off> : Enable/disable logging" },
|
|
|
|
{ " /sendfile <filepath> : Send a file" },
|
|
|
|
{ " /savefile <n> : Receive a file" },
|
|
|
|
{ " /close : Close the current chat window" },
|
|
|
|
{ " /help : Print this message again" },
|
|
|
|
{ " /help global : Show a list of global commands" },
|
|
|
|
};
|
2014-03-24 12:18:58 +01:00
|
|
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUMLINES; ++i)
|
|
|
|
line_info_add(self, NULL, NULL, NULL, lines[i], SYS_MSG, 0, 0);
|
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
msg = " * Use Page Up/Page Down to scroll chat history\n";
|
2014-03-24 12:18:58 +01:00
|
|
|
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN);
|
2014-03-27 00:14:28 +01:00
|
|
|
|
|
|
|
hst->line_start = start;
|
2013-11-10 03:43:56 +01: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
|
|
|
{
|
2014-03-24 12:18:58 +01:00
|
|
|
uint8_t *errmsg;
|
|
|
|
|
2013-11-10 03:43:56 +01:00
|
|
|
if (argc < 1) {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "Invalid syntax";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
|
|
|
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-03-24 12:18:58 +01:00
|
|
|
errmsg = "Invalid syntax.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
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-03-24 12:18:58 +01:00
|
|
|
errmsg = "Failed to invite friend.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-24 12:18:58 +01:00
|
|
|
uint8_t msg[MAX_STR_SIZE];
|
|
|
|
snprintf(msg, sizeof(msg), "Invited friend to Room #%d.", groupnum);
|
|
|
|
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
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-24 12:18:58 +01:00
|
|
|
uint8_t *errmsg;
|
|
|
|
|
2014-03-07 01:39:57 +01:00
|
|
|
if (get_num_active_windows() >= MAX_WINDOWS_NUM) {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = " * Warning: Too many windows are open.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED);
|
2013-11-27 04:15:48 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
uint8_t *groupkey = friends[self->num].pending_groupchat;
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2013-11-12 23:26:13 +01:00
|
|
|
if (groupkey[0] == '\0') {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "No pending group chat invite.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
int groupnum = tox_join_groupchat(m, self->num, groupkey);
|
2013-11-10 03:43:56 +01:00
|
|
|
|
|
|
|
if (groupnum == -1) {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "Group chat instance failed to initialize.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (init_groupchat_win(prompt, m, groupnum) == -1) {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "Group chat window failed to initialize.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
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-03-24 12:18:58 +01:00
|
|
|
uint8_t *errmsg;
|
|
|
|
|
2013-11-10 03:43:56 +01:00
|
|
|
if (argc != 1) {
|
2014-04-19 23:58:13 +02:00
|
|
|
errmsg = "Invalid syntax.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
|
|
|
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-03-24 12:18:58 +01:00
|
|
|
errmsg = "No pending file transfers with that number.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
if (!friends[self->num].file_receiver.pending[filenum]) {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "No pending file transfers with that number.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
uint8_t *filename = friends[self->num].file_receiver.filenames[filenum];
|
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-03-24 12:18:58 +01:00
|
|
|
uint8_t msg[MAX_STR_SIZE];
|
2014-03-28 04:05:50 +01:00
|
|
|
snprintf(msg, sizeof(msg), "Saving file as: '%s' (%.1f%%)", filename, 0.0);
|
2014-03-24 12:18:58 +01:00
|
|
|
line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0);
|
2014-06-22 02:18:23 +02:00
|
|
|
friends[self->num].file_receiver.line_id[filenum] = self->chatwin->hst->line_end->id + 1;
|
2014-03-05 09:31:12 +01:00
|
|
|
|
|
|
|
if ((friends[self->num].file_receiver.files[filenum] = fopen(filename, "a")) == NULL) {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "* Error writing to file.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED);
|
2014-03-05 09:31:12 +01:00
|
|
|
tox_file_send_control(m, self->num, 1, filenum, TOX_FILECONTROL_KILL, 0, 0);
|
|
|
|
}
|
|
|
|
} else {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "File transfer failed.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2014-03-05 09:31:12 +01:00
|
|
|
}
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
friends[self->num].file_receiver.pending[filenum] = 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-03-24 12:18:58 +01:00
|
|
|
uint8_t *errmsg;
|
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
if (max_file_senders_index >= (MAX_FILES - 1)) {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "Please wait for some of your outgoing file transfers to complete.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc < 1) {
|
2014-04-19 23:58:13 +02:00
|
|
|
errmsg = "Invalid syntax.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
|
|
|
return;
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *path = argv[1];
|
|
|
|
|
|
|
|
if (path[0] != '\"') {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "File path must be enclosed in quotes.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
path[strlen(++path) - 1] = L'\0';
|
2013-11-10 03:43:56 +01:00
|
|
|
int path_len = strlen(path);
|
|
|
|
|
|
|
|
if (path_len > MAX_STR_SIZE) {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "File path exceeds character limit.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *file_to_send = fopen(path, "r");
|
|
|
|
|
|
|
|
if (file_to_send == NULL) {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "File not found.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fseek(file_to_send, 0, SEEK_END);
|
|
|
|
uint64_t filesize = ftell(file_to_send);
|
|
|
|
fseek(file_to_send, 0, SEEK_SET);
|
|
|
|
|
2014-02-12 01:12:26 +01:00
|
|
|
uint8_t filename[MAX_STR_SIZE];
|
2014-06-22 02:18:23 +02:00
|
|
|
get_file_name(filename, path);
|
2014-05-05 19:16:59 +02:00
|
|
|
int filenum = tox_new_file_sender(m, self->num, filesize, filename, strlen(filename));
|
2013-11-10 03:43:56 +01:00
|
|
|
|
|
|
|
if (filenum == -1) {
|
2014-03-24 12:18:58 +01:00
|
|
|
errmsg = "Error sending file.";
|
|
|
|
line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-27 00:20:56 +02:00
|
|
|
new_filesender_thread(self, m, path, path_len, file_to_send, filenum, filesize);
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|