mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-22 21:43:02 +01:00
refactored sendfile and invite commands to not require names and to only work in chat windows
This commit is contained in:
parent
75d356e52a
commit
371fbc87a5
87
src/chat.c
87
src/chat.c
@ -100,14 +100,14 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status
|
||||
}
|
||||
|
||||
static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum,
|
||||
uint64_t filesize, uint8_t *filename, uint16_t filename_len)
|
||||
uint64_t filesize, uint8_t *pathname, uint16_t path_len)
|
||||
{
|
||||
if (self-> num != num)
|
||||
return;
|
||||
|
||||
ChatContext *ctx = (ChatContext *) self->chatwin;
|
||||
|
||||
wprintw(ctx->history, "File transfer request for '%s' of size %llu.\n", filename,
|
||||
wprintw(ctx->history, "File transfer request for '%s' of size %llu.\n", pathname,
|
||||
(long long unsigned int)filesize);
|
||||
|
||||
if (filenum > MAX_FILENUMBER) {
|
||||
@ -115,7 +115,7 @@ static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t fil
|
||||
return;
|
||||
}
|
||||
|
||||
wprintw(ctx->history, "Type '/file %d' to accept the file transfer.\n", filenum);
|
||||
wprintw(ctx->history, "Type '/savefile %d' to accept the file transfer.\n", filenum);
|
||||
|
||||
pending_file_transfers[filenum] = num;
|
||||
|
||||
@ -156,10 +156,10 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u
|
||||
|
||||
ChatContext *ctx = (ChatContext *) self->chatwin;
|
||||
|
||||
char filename[MAX_STR_SIZE];
|
||||
snprintf(filename, sizeof(filename), "%d.%u.bin", num, filenum);
|
||||
char pathname[MAX_STR_SIZE];
|
||||
snprintf(pathname, sizeof(pathname), "%d.%u.bin", num, filenum);
|
||||
|
||||
FILE *file_to_save = fopen(filename, "a");
|
||||
FILE *file_to_save = fopen(pathname, "a");
|
||||
|
||||
if (fwrite(data, length, 1, file_to_save) != 1) {
|
||||
wattron(ctx->history, COLOR_PAIR(RED));
|
||||
@ -170,6 +170,67 @@ static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, u
|
||||
fclose(file_to_save);
|
||||
}
|
||||
|
||||
static void chat_groupinvite(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *line)
|
||||
{
|
||||
int groupnum = atoi(line);
|
||||
|
||||
if (groupnum == 0 && strcmp(line, "0")) { /* atoi returns 0 value on invalid input */
|
||||
wprintw(ctx->history, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (tox_invite_friend(m, self->num, groupnum) == -1) {
|
||||
wprintw(ctx->history, "Failed to invite friend.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
wprintw(ctx->history, "Invited friend to group chat %d.\n", groupnum);
|
||||
}
|
||||
|
||||
static void chat_sendfile(ToxWindow *self, ChatContext *ctx, Tox *m, uint8_t *path)
|
||||
{
|
||||
int path_len = strlen(path);
|
||||
|
||||
if (path_len > MAX_STR_SIZE) {
|
||||
wprintw(ctx->history, "File path exceeds character limit.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
FILE *file_to_send = fopen(path, "r");
|
||||
|
||||
if (file_to_send == NULL) {
|
||||
wprintw(ctx->history, "File '%s' not found.\n", path);
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(file_to_send, 0, SEEK_END);
|
||||
uint64_t filesize = ftell(file_to_send);
|
||||
fseek(file_to_send, 0, SEEK_SET);
|
||||
|
||||
int friendnum = self->num;
|
||||
uint8_t friendname[TOX_MAX_NAME_LENGTH] = {'\0'};
|
||||
tox_getname(m, friendnum, friendname);
|
||||
|
||||
int filenum = tox_new_filesender(m, friendnum, filesize, path, path_len + 1);
|
||||
|
||||
if (filenum == -1) {
|
||||
wprintw(ctx->history, "Error sending file.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(file_senders[num_file_senders].pathname, path, path_len + 1);
|
||||
memcpy(file_senders[num_file_senders].friendname, friendname, strlen(friendname) + 1);
|
||||
file_senders[num_file_senders].file = file_to_send;
|
||||
file_senders[num_file_senders].filenum = filenum;
|
||||
file_senders[num_file_senders].friendnum = friendnum;
|
||||
file_senders[num_file_senders].piecelen = fread(file_senders[num_file_senders].nextpiece, 1,
|
||||
tox_filedata_size(m, friendnum), file_to_send);
|
||||
|
||||
|
||||
wprintw(ctx->history, "Sending file '%s'...\n", path);
|
||||
++num_file_senders;
|
||||
}
|
||||
|
||||
static void print_chat_help(ChatContext *ctx)
|
||||
{
|
||||
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
||||
@ -179,13 +240,13 @@ static void print_chat_help(ChatContext *ctx)
|
||||
wprintw(ctx->history, " /status <type> <message> : Set your status with optional note\n");
|
||||
wprintw(ctx->history, " /note <message> : Set a personal note\n");
|
||||
wprintw(ctx->history, " /nick <nickname> : Set your nickname\n");
|
||||
wprintw(ctx->history, " /invite <nickname> <n> : Invite friend to a groupchat\n");
|
||||
wprintw(ctx->history, " /invite <n> : Invite friend to a groupchat\n");
|
||||
wprintw(ctx->history, " /me <action> : Do an action\n");
|
||||
wprintw(ctx->history, " /myid : Print your ID\n");
|
||||
wprintw(ctx->history, " /clear : Clear the screen\n");
|
||||
wprintw(ctx->history, " /close : Close the current chat window\n");
|
||||
wprintw(ctx->history, " /sendfile <nickname> <file>: Send a file\n");
|
||||
wprintw(ctx->history, " /file <n> : Accept a file\n");
|
||||
wprintw(ctx->history, " /sendfile <filepath> : Send a file\n");
|
||||
wprintw(ctx->history, " /savefile <n> : Receive a file\n");
|
||||
wprintw(ctx->history, " /quit or /exit : Exit Toxic\n");
|
||||
wprintw(ctx->history, " /help : Print this message again\n");
|
||||
|
||||
@ -251,11 +312,11 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
|
||||
/* RETURN key: Execute command or print line */
|
||||
else if (key == '\n') {
|
||||
uint8_t *line = wcs_to_char(ctx->line);
|
||||
line[ctx->pos+1] = '\0';
|
||||
wclear(ctx->linewin);
|
||||
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
|
||||
wclrtobot(self->window);
|
||||
bool close_win = false;
|
||||
|
||||
if (line[0] == '/') {
|
||||
if (close_win = !strncmp(line, "/close", strlen("/close"))) {
|
||||
int f_num = self->num;
|
||||
@ -264,9 +325,13 @@ static void chat_onKey(ToxWindow *self, Tox *m, wint_t key)
|
||||
del_window(self);
|
||||
disable_chatwin(f_num);
|
||||
} else if (!strncmp(line, "/me ", strlen("/me ")))
|
||||
send_action(self, ctx, m, line+4);
|
||||
send_action(self, ctx, m, line + strlen("/me "));
|
||||
else if (!strncmp(line, "/help", strlen("/help")))
|
||||
print_chat_help(ctx);
|
||||
else if (!strncmp(line, "/invite", strlen("/invite")))
|
||||
chat_groupinvite(self, ctx, m, line + strlen("/invite "));
|
||||
else if(!strncmp(line, "/sendfile ", strlen("/sendfile ")))
|
||||
chat_sendfile(self, ctx, m, line + strlen("/sendfile "));
|
||||
else
|
||||
execute(ctx->history, self->prompt, m, line, ctx->pos);
|
||||
} else {
|
||||
|
104
src/commands.c
104
src/commands.c
@ -180,7 +180,7 @@ void cmd_connect(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **arg
|
||||
free(binary_string);
|
||||
}
|
||||
|
||||
void cmd_file(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
void cmd_savefile(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc < 1) {
|
||||
wprintw(window, "Wrong number of arguments.\n");
|
||||
@ -228,39 +228,6 @@ void cmd_groupchat(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **a
|
||||
wprintw(window, "Group chat created as %d.\n", groupnum);
|
||||
}
|
||||
|
||||
void cmd_invite(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc != 2) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (argv[1] == NULL || argv[2] == NULL) {
|
||||
wprintw(window, "Invalid syntax.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *friendname = argv[1];
|
||||
int groupnum = atoi(argv[2]);
|
||||
|
||||
if (friendname[0] == '\"')
|
||||
friendname[strlen(++friendname)-1] = L'\0';
|
||||
|
||||
int friendnum = get_friendnum(friendname);
|
||||
|
||||
if (friendnum == -1) {
|
||||
wprintw(window, "Friend '%s' not found.\n", friendname);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tox_invite_friend(m, friendnum, groupnum) == -1) {
|
||||
wprintw(window, "Failed to invite friend.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
wprintw(window, "Invited '%s' to group chat %d.\n", friendname, groupnum);
|
||||
}
|
||||
|
||||
void cmd_join(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc != 1) {
|
||||
@ -400,7 +367,7 @@ void cmd_note(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
}
|
||||
|
||||
if (msg[0] != '\"') {
|
||||
wprintw(window, "Messages must be enclosed in quotes.\n");
|
||||
wprintw(window, "Note must be enclosed in quotes.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -415,71 +382,6 @@ void cmd_quit(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
exit_toxic(m);
|
||||
}
|
||||
|
||||
void cmd_sendfile(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
if (argc < 1) {
|
||||
wprintw(window, "Wrong number of arguments.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t *friendname = argv[1];
|
||||
|
||||
int friendnum = get_friendnum(friendname);
|
||||
|
||||
if (friendnum == -1) {
|
||||
wprintw(window, "Friend '%s' not found.\n", friendname);
|
||||
return;
|
||||
}
|
||||
|
||||
if (friendname[0] == '\"')
|
||||
friendname[strlen(++friendname)-1] = L'\0';
|
||||
|
||||
uint8_t *filename = argv[2];
|
||||
int filename_len = strlen(filename);
|
||||
|
||||
if (filename[0] != '\"') {
|
||||
wprintw(window, "File name must be enclosed in quotes.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
filename[strlen(++filename)-1] = L'\0';
|
||||
|
||||
if (filename_len > MAX_STR_SIZE) {
|
||||
wprintw(window, "File path exceeds character limit.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
FILE *file_to_send = fopen(filename, "r");
|
||||
|
||||
if (file_to_send == NULL) {
|
||||
wprintw(window, "File '%s' not found.\n", filename);
|
||||
return;
|
||||
}
|
||||
|
||||
fseek(file_to_send, 0, SEEK_END);
|
||||
uint64_t filesize = ftell(file_to_send);
|
||||
fseek(file_to_send, 0, SEEK_SET);
|
||||
|
||||
int filenum = tox_new_filesender(m, friendnum, filesize, filename, filename_len + 1);
|
||||
|
||||
if (filenum == -1) {
|
||||
wprintw(window, "Error sending file\n");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(file_senders[num_file_senders].filename, filename, filename_len + 1);
|
||||
memcpy(file_senders[num_file_senders].friendname, friendname, strlen(friendname) + 1);
|
||||
file_senders[num_file_senders].file = file_to_send;
|
||||
file_senders[num_file_senders].filenum = filenum;
|
||||
file_senders[num_file_senders].friendnum = friendnum;
|
||||
file_senders[num_file_senders].piecelen = fread(file_senders[num_file_senders].nextpiece, 1,
|
||||
tox_filedata_size(m, friendnum), file_to_send);
|
||||
|
||||
|
||||
wprintw(window, "Sending file '%s' to %s...\n", filename, friendname);
|
||||
++num_file_senders;
|
||||
}
|
||||
|
||||
void cmd_status(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
uint8_t *msg = NULL;
|
||||
@ -494,7 +396,7 @@ void cmd_status(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv
|
||||
}
|
||||
|
||||
if (msg[0] != '\"') {
|
||||
wprintw(window, "Messages must be enclosed in quotes.\n");
|
||||
wprintw(window, "Note must be enclosed in quotes.\n");
|
||||
return;
|
||||
}
|
||||
} else if (argc != 1) {
|
||||
|
@ -7,21 +7,19 @@ void cmd_accept(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_add(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_clear(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_connect(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_file(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_savefile(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_groupchat(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_invite(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_join(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_msg(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_myid(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_nick(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_note(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_quit(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_sendfile(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
void cmd_status(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||
|
||||
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len);
|
||||
|
||||
#define NUM_COMMANDS 17
|
||||
#define NUM_COMMANDS 15
|
||||
|
||||
static struct {
|
||||
char *name;
|
||||
@ -32,9 +30,8 @@ static struct {
|
||||
{ "/clear", cmd_clear },
|
||||
{ "/connect", cmd_connect },
|
||||
{ "/exit", cmd_quit },
|
||||
{ "/file", cmd_file },
|
||||
{ "/savefile", cmd_savefile },
|
||||
{ "/groupchat", cmd_groupchat },
|
||||
{ "/invite", cmd_invite },
|
||||
{ "/join", cmd_join },
|
||||
{ "/msg", cmd_msg },
|
||||
{ "/myid", cmd_myid },
|
||||
@ -42,6 +39,5 @@ static struct {
|
||||
{ "/note", cmd_note },
|
||||
{ "/q", cmd_quit },
|
||||
{ "/quit", cmd_quit },
|
||||
{ "/sendfile", cmd_sendfile },
|
||||
{ "/status", cmd_status },
|
||||
};
|
||||
|
@ -79,7 +79,6 @@ static void print_groupchat_help(ChatContext *ctx)
|
||||
wprintw(ctx->history, " /status <type> <message> : Set your status with optional note\n");
|
||||
wprintw(ctx->history, " /note <message> : Set a personal note\n");
|
||||
wprintw(ctx->history, " /nick <nickname> : Set your nickname\n");
|
||||
wprintw(ctx->history, " /invite <nickname> <n> : Invite friend to a groupchat\n");
|
||||
wprintw(ctx->history, " /groupchat : Create a group chat\n");
|
||||
wprintw(ctx->history, " /myid : Print your ID\n");
|
||||
wprintw(ctx->history, " /clear : Clear the screen\n");
|
||||
|
@ -371,7 +371,7 @@ void do_file_senders(Tox *m)
|
||||
|
||||
/* TODO: move this alert to chat window */
|
||||
wprintw(prompt->window, "File '%s' successfuly sent to %s.\n",
|
||||
file_senders[i].filename, file_senders[i].friendname);
|
||||
file_senders[i].pathname, file_senders[i].friendname);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
@ -92,7 +92,6 @@ static void print_prompt_help(ToxWindow *self)
|
||||
wprintw(self->window, " /note <message> : Set a personal note\n");
|
||||
wprintw(self->window, " /nick <nickname> : Set your nickname\n");
|
||||
wprintw(self->window, " /join <n> : Join a group chat\n");
|
||||
wprintw(self->window, " /invite <nickname> <n> : Invite friend to a groupchat\n");
|
||||
wprintw(self->window, " /groupchat : Create a group chat\n");
|
||||
wprintw(self->window, " /myid : Print your ID\n");
|
||||
wprintw(self->window, " /quit or /exit : Exit Toxic\n");
|
||||
|
@ -108,7 +108,7 @@ typedef struct {
|
||||
uint8_t nextpiece[FILE_PIECE_SIZE];
|
||||
uint16_t piecelen;
|
||||
uint8_t friendname[TOXIC_MAX_NAME_LENGTH];
|
||||
uint8_t filename[MAX_STR_SIZE];
|
||||
uint8_t pathname[MAX_STR_SIZE];
|
||||
} FileSender;
|
||||
|
||||
FileSender file_senders[NUM_FILE_SENDERS];
|
||||
@ -128,7 +128,7 @@ void on_statusmessagechange(Tox *m, int friendnumber, uint8_t *string, uint16_t
|
||||
void on_friendadded(Tox *m, int friendnumber);
|
||||
void on_groupmessage(Tox *m, int groupnumber, int peernumber, uint8_t *message, uint16_t length, void *userdata);
|
||||
void on_groupinvite(Tox *m, int friendnumber, uint8_t *group_pub_key, void *userdata);
|
||||
void on_file_sendrequest(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *filename, uint16_t filename_length, void *userdata);
|
||||
void on_file_sendrequest(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize, uint8_t *pathname, uint16_t pathname_length, void *userdata);
|
||||
void on_file_control(Tox *m, int friendnumber, uint8_t receive_send, uint8_t filenumber, uint8_t control_type, uint8_t *data, uint16_t length, void *userdata);
|
||||
void on_file_data(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user