mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-26 16:03:27 +01:00
implemented file receving
This commit is contained in:
parent
cb396c0e7c
commit
e60e4f4727
75
src/chat.c
75
src/chat.c
@ -106,6 +106,75 @@ static void chat_onStatusMessageChange(ToxWindow *self, int num, uint8_t *status
|
|||||||
memcpy(statusbar->statusmsg, status, len);
|
memcpy(statusbar->statusmsg, status, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void chat_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum, uint64_t filesize,
|
||||||
|
uint8_t *filename, uint16_t filename_len)
|
||||||
|
{
|
||||||
|
if (self-> num != num)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
||||||
|
|
||||||
|
wprintw(ctx->history, "File transfer request for '%s' of size %llu.\n", filename, (long long unsigned int)filesize);
|
||||||
|
|
||||||
|
if (filenum > MAX_FILENUMBER) {
|
||||||
|
wprintw(ctx->history, "Too many pending file requests; discarding.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wprintw(ctx->history, "Type '/file %d' to accept the file transfer.\n", filenum);
|
||||||
|
|
||||||
|
pending_file_transfers[filenum] = num;
|
||||||
|
|
||||||
|
self->blink = true;
|
||||||
|
beep();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if (self->num != num)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
||||||
|
|
||||||
|
switch(control_type) {
|
||||||
|
case 0:
|
||||||
|
wprintw(ctx->history, "File transfer accepted.\n");
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
wprintw(ctx->history, "File successfully recieved.\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
wprintw(ctx->history, "Control %u receieved.\n", control_type);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
self->blink = true;
|
||||||
|
beep();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void chat_onFileData(ToxWindow *self, Tox *m, int num, uint8_t filenum, uint8_t *data, uint16_t length)
|
||||||
|
{
|
||||||
|
if (self->num != num)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ChatContext *ctx = (ChatContext *) self->chatwin;
|
||||||
|
|
||||||
|
char filename[MAX_STR_SIZE];
|
||||||
|
snprintf(filename, sizeof(filename), "%d.%u.bin", num, filenum);
|
||||||
|
|
||||||
|
FILE *file_to_save = fopen(filename, "a");
|
||||||
|
|
||||||
|
if (fwrite(data, length, 1, file_to_save) != 1) {
|
||||||
|
wattron(ctx->history, COLOR_PAIR(RED));
|
||||||
|
wprintw(ctx->history, "* Error writing to file.\n");
|
||||||
|
wattroff(ctx->history, COLOR_PAIR(RED));
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file_to_save);
|
||||||
|
}
|
||||||
|
|
||||||
static void print_chat_help(ChatContext *ctx)
|
static void print_chat_help(ChatContext *ctx)
|
||||||
{
|
{
|
||||||
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
wattron(ctx->history, COLOR_PAIR(CYAN) | A_BOLD);
|
||||||
@ -120,6 +189,9 @@ static void print_chat_help(ChatContext *ctx)
|
|||||||
wprintw(ctx->history, " /myid : Print your ID\n");
|
wprintw(ctx->history, " /myid : Print your ID\n");
|
||||||
wprintw(ctx->history, " /clear : Clear the screen\n");
|
wprintw(ctx->history, " /clear : Clear the screen\n");
|
||||||
wprintw(ctx->history, " /close : Close the current chat window\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, " /nick <nickname> : Set your nickname\n");
|
||||||
wprintw(ctx->history, " /quit or /exit : Exit Toxic\n");
|
wprintw(ctx->history, " /quit or /exit : Exit Toxic\n");
|
||||||
wprintw(ctx->history, " /help : Print this message again\n");
|
wprintw(ctx->history, " /help : Print this message again\n");
|
||||||
|
|
||||||
@ -368,6 +440,9 @@ ToxWindow new_chat(Tox *m, ToxWindow *prompt, int friendnum)
|
|||||||
ret.onStatusChange = &chat_onStatusChange;
|
ret.onStatusChange = &chat_onStatusChange;
|
||||||
ret.onStatusMessageChange = &chat_onStatusMessageChange;
|
ret.onStatusMessageChange = &chat_onStatusMessageChange;
|
||||||
ret.onAction = &chat_onAction;
|
ret.onAction = &chat_onAction;
|
||||||
|
ret.onFileSendRequest = &chat_onFileSendRequest;
|
||||||
|
ret.onFileControl = &chat_onFileControl;
|
||||||
|
ret.onFileData = &chat_onFileData;
|
||||||
|
|
||||||
uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'};
|
uint8_t name[TOX_MAX_NAME_LENGTH] = {'\0'};
|
||||||
tox_getname(m, friendnum, name);
|
tox_getname(m, friendnum, name);
|
||||||
|
@ -20,8 +20,6 @@ extern uint8_t num_frnd_requests;
|
|||||||
|
|
||||||
extern uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
|
extern uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
|
||||||
|
|
||||||
extern
|
|
||||||
|
|
||||||
/* command functions */
|
/* command functions */
|
||||||
void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||||
{
|
{
|
||||||
@ -182,6 +180,28 @@ void cmd_connect(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **arg
|
|||||||
free(binary_string);
|
free(binary_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void cmd_file(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||||
|
{
|
||||||
|
if (argc < 1) {
|
||||||
|
wprintw(window, "Wrong number of arguments.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t filenum = atoi(argv[1]);
|
||||||
|
|
||||||
|
if (filenum < 0 || filenum > MAX_FILENUMBER) {
|
||||||
|
wprintw(window, "File transfer failed.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int friendnum = pending_file_transfers[filenum];
|
||||||
|
|
||||||
|
if (tox_file_sendcontrol(m, friendnum, 1, filenum, 0, 0, 0))
|
||||||
|
wprintw(window, "Accepted file transfer %u. Saving file as %d.%u.bin.\n", filenum, friendnum, filenum);
|
||||||
|
else
|
||||||
|
wprintw(window, "File transfer failed.\n");
|
||||||
|
}
|
||||||
|
|
||||||
void cmd_groupchat(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
void cmd_groupchat(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||||
{
|
{
|
||||||
int ngc = get_num_groupchats();
|
int ngc = get_num_groupchats();
|
||||||
@ -410,6 +430,9 @@ void cmd_sendfile(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **ar
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (friendname[0] == '\"')
|
||||||
|
friendname[strlen(++friendname)-1] = L'\0';
|
||||||
|
|
||||||
uint8_t *filename = argv[2];
|
uint8_t *filename = argv[2];
|
||||||
int filename_len = strlen(filename);
|
int filename_len = strlen(filename);
|
||||||
|
|
||||||
@ -421,14 +444,14 @@ void cmd_sendfile(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **ar
|
|||||||
filename[strlen(++filename)-1] = L'\0';
|
filename[strlen(++filename)-1] = L'\0';
|
||||||
|
|
||||||
if (filename_len > MAX_STR_SIZE) {
|
if (filename_len > MAX_STR_SIZE) {
|
||||||
wprintw(window, "File path exceeds character limit\n");
|
wprintw(window, "File path exceeds character limit.\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *file_to_send = fopen(filename, "r");
|
FILE *file_to_send = fopen(filename, "r");
|
||||||
|
|
||||||
if (file_to_send == NULL) {
|
if (file_to_send == NULL) {
|
||||||
wprintw(window, "File '%s' not found\n", filename);
|
wprintw(window, "File '%s' not found.\n", filename);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ void cmd_accept(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
|||||||
void cmd_add(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_clear(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||||
void cmd_connect(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_groupchat(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_invite(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||||
void cmd_join(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
void cmd_join(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
||||||
@ -20,7 +21,7 @@ void cmd_status(WINDOW *, ToxWindow *, Tox *m, int, char **);
|
|||||||
|
|
||||||
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len);
|
void execute(WINDOW *window, ToxWindow *prompt, Tox *m, char *u_cmd, int buf_len);
|
||||||
|
|
||||||
#define NUM_COMMANDS 16
|
#define NUM_COMMANDS 17
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
char *name;
|
char *name;
|
||||||
@ -31,6 +32,7 @@ static struct {
|
|||||||
{ "/clear", cmd_clear },
|
{ "/clear", cmd_clear },
|
||||||
{ "/connect", cmd_connect },
|
{ "/connect", cmd_connect },
|
||||||
{ "/exit", cmd_quit },
|
{ "/exit", cmd_quit },
|
||||||
|
{ "/file", cmd_file },
|
||||||
{ "/groupchat", cmd_groupchat },
|
{ "/groupchat", cmd_groupchat },
|
||||||
{ "/invite", cmd_invite },
|
{ "/invite", cmd_invite },
|
||||||
{ "/join", cmd_join },
|
{ "/join", cmd_join },
|
||||||
|
@ -108,6 +108,16 @@ int friendlist_onFriendAdded(Tox *m, int num)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void friendlist_onFileSendRequest(ToxWindow *self, Tox *m, int num, uint8_t filenum, uint64_t filesize,
|
||||||
|
uint8_t *filename, uint16_t filename_len)
|
||||||
|
{
|
||||||
|
if (num < 0 || num >= num_friends)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (friends[num].chatwin == -1)
|
||||||
|
friends[num].chatwin = add_window(m, new_chat(m, prompt, friends[num].num));
|
||||||
|
}
|
||||||
|
|
||||||
static void select_friend(Tox *m, wint_t key)
|
static void select_friend(Tox *m, wint_t key)
|
||||||
{
|
{
|
||||||
if (num_friends < 1)
|
if (num_friends < 1)
|
||||||
@ -286,6 +296,7 @@ ToxWindow new_friendlist()
|
|||||||
ret.onNickChange = &friendlist_onNickChange;
|
ret.onNickChange = &friendlist_onNickChange;
|
||||||
ret.onStatusChange = &friendlist_onStatusChange;
|
ret.onStatusChange = &friendlist_onStatusChange;
|
||||||
ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
|
ret.onStatusMessageChange = &friendlist_onStatusMessageChange;
|
||||||
|
ret.onFileSendRequest = &friendlist_onFileSendRequest;
|
||||||
|
|
||||||
strcpy(ret.name, "friends");
|
strcpy(ret.name, "friends");
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -367,10 +367,10 @@ void do_file_senders(Tox *m)
|
|||||||
fclose(file_senders[i].file);
|
fclose(file_senders[i].file);
|
||||||
file_senders[i].file = NULL;
|
file_senders[i].file = NULL;
|
||||||
tox_file_sendcontrol(m, file_senders[i].friendnum, 0, file_senders[i].filenum, 3, 0, 0);
|
tox_file_sendcontrol(m, file_senders[i].friendnum, 0, file_senders[i].filenum, 3, 0, 0);
|
||||||
char msg[MAX_STR_SIZE + TOXIC_MAX_NAME_LENGTH + 50];
|
|
||||||
snprintf(msg, sizeof(msg), "File '%s' successfuly sent to %s.",
|
/* TODO: move this alert to chat window */
|
||||||
file_senders[i].filename, file_senders[i].friendname);
|
wprintw(prompt->window, "File '%s' successfuly sent to %s.\n", file_senders[i].filename,
|
||||||
wprintw(prompt->window, "%s\n", msg);
|
file_senders[i].friendname);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,10 @@ typedef struct {
|
|||||||
bool active;
|
bool active;
|
||||||
} GroupChat;
|
} GroupChat;
|
||||||
|
|
||||||
|
/* Start file transfer code */
|
||||||
|
|
||||||
#define NUM_FILE_SENDERS 256
|
#define NUM_FILE_SENDERS 256
|
||||||
|
#define MAX_FILENUMBER 100 /* fix */
|
||||||
#define FILE_PIECE_SIZE 1024
|
#define FILE_PIECE_SIZE 1024
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@ -111,6 +114,10 @@ typedef struct {
|
|||||||
FileSender file_senders[NUM_FILE_SENDERS];
|
FileSender file_senders[NUM_FILE_SENDERS];
|
||||||
uint8_t num_file_senders;
|
uint8_t num_file_senders;
|
||||||
|
|
||||||
|
uint8_t pending_file_transfers[MAX_FILENUMBER];
|
||||||
|
|
||||||
|
/* End file transfer code */
|
||||||
|
|
||||||
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata);
|
void on_request(uint8_t *public_key, uint8_t *data, uint16_t length, void *userdata);
|
||||||
void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata);
|
void on_connectionchange(Tox *m, int friendnumber, uint8_t status, void *userdata);
|
||||||
void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
|
void on_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
|
||||||
@ -122,7 +129,7 @@ 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_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_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 *filename, uint16_t filename_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_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);
|
void on_file_data(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata);
|
||||||
|
|
||||||
ToxWindow *init_windows();
|
ToxWindow *init_windows();
|
||||||
|
Loading…
Reference in New Issue
Block a user