mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-22 20:33:02 +01:00
Implemented file sending
This commit is contained in:
parent
87a29e43c9
commit
cb396c0e7c
@ -20,6 +20,8 @@ extern uint8_t num_frnd_requests;
|
||||
|
||||
extern uint8_t pending_grp_requests[MAX_FRIENDS_NUM][TOX_CLIENT_ID_SIZE];
|
||||
|
||||
extern
|
||||
|
||||
/* command functions */
|
||||
void cmd_accept(WINDOW *window, ToxWindow *prompt, Tox *m, int argc, char **argv)
|
||||
{
|
||||
@ -392,6 +394,68 @@ 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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
@ -15,11 +15,12 @@ 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 15
|
||||
#define NUM_COMMANDS 16
|
||||
|
||||
static struct {
|
||||
char *name;
|
||||
@ -39,5 +40,6 @@ static struct {
|
||||
{ "/note", cmd_note },
|
||||
{ "/q", cmd_quit },
|
||||
{ "/quit", cmd_quit },
|
||||
{ "/sendfile", cmd_sendfile },
|
||||
{ "/status", cmd_status },
|
||||
};
|
||||
|
40
src/main.c
40
src/main.c
@ -92,6 +92,7 @@ static Tox *init_tox()
|
||||
{
|
||||
/* Init core */
|
||||
Tox *m = tox_new(TOX_ENABLE_IPV6_DEFAULT);
|
||||
|
||||
if (m == NULL)
|
||||
return NULL;
|
||||
|
||||
@ -105,6 +106,10 @@ static Tox *init_tox()
|
||||
tox_callback_action(m, on_action, NULL);
|
||||
tox_callback_group_invite(m, on_groupinvite, NULL);
|
||||
tox_callback_group_message(m, on_groupmessage, NULL);
|
||||
tox_callback_file_sendrequest(m, on_file_sendrequest, NULL);
|
||||
tox_callback_file_control(m, on_file_control, NULL);
|
||||
tox_callback_file_data(m, on_file_data, NULL);
|
||||
|
||||
#ifdef __linux__
|
||||
tox_setname(m, (uint8_t *) "Cool guy", sizeof("Cool guy"));
|
||||
#elif defined(_WIN32)
|
||||
@ -114,6 +119,7 @@ static Tox *init_tox()
|
||||
#else
|
||||
tox_setname(m, (uint8_t *) "Registered Minix user #4", sizeof("Registered Minix user #4"));
|
||||
#endif
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
@ -341,6 +347,36 @@ static void load_data(Tox *m, char *path)
|
||||
}
|
||||
}
|
||||
|
||||
void do_file_senders(Tox *m)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < NUM_FILE_SENDERS; ++i) {
|
||||
if (file_senders[i].file == NULL)
|
||||
continue;
|
||||
|
||||
while (true) {
|
||||
if (!tox_file_senddata(m, file_senders[i].friendnum, file_senders[i].filenum,
|
||||
file_senders[i].nextpiece, file_senders[i].piecelen))
|
||||
return;
|
||||
|
||||
file_senders[i].piecelen = fread(file_senders[i].nextpiece, 1, tox_filedata_size(m, file_senders[i].friendnum),
|
||||
file_senders[i].file);
|
||||
|
||||
if (file_senders[i].piecelen == 0) {
|
||||
fclose(file_senders[i].file);
|
||||
file_senders[i].file = NULL;
|
||||
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.",
|
||||
file_senders[i].filename, file_senders[i].friendname);
|
||||
wprintw(prompt->window, "%s\n", msg);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void exit_toxic(Tox *m)
|
||||
{
|
||||
store_data(m, DATA_FILE);
|
||||
@ -442,10 +478,8 @@ int main(int argc, char *argv[])
|
||||
prompt_init_statusbar(prompt, m);
|
||||
|
||||
while (true) {
|
||||
/* Update tox */
|
||||
do_tox(m, prompt);
|
||||
|
||||
/* Draw */
|
||||
do_file_senders(m);
|
||||
draw_active_window(m);
|
||||
}
|
||||
|
||||
|
@ -56,6 +56,9 @@ struct ToxWindow_ {
|
||||
void(*onAction)(ToxWindow *, Tox *, int, uint8_t *, uint16_t);
|
||||
void(*onGroupMessage)(ToxWindow *, Tox *, int, int, uint8_t *, uint16_t);
|
||||
void(*onGroupInvite)(ToxWindow *, Tox *, int, uint8_t *);
|
||||
void(*onFileSendRequest)(ToxWindow *, Tox *, int, uint8_t, uint64_t, uint8_t *, uint16_t);
|
||||
void(*onFileControl)(ToxWindow *, Tox *, int, uint8_t, uint8_t, uint8_t, uint8_t *, uint16_t);
|
||||
void(*onFileData)(ToxWindow *, Tox *, int, uint8_t, uint8_t *, uint16_t);
|
||||
|
||||
char name[TOX_MAX_NAME_LENGTH];
|
||||
int num;
|
||||
@ -92,6 +95,22 @@ typedef struct {
|
||||
bool active;
|
||||
} GroupChat;
|
||||
|
||||
#define NUM_FILE_SENDERS 256
|
||||
#define FILE_PIECE_SIZE 1024
|
||||
|
||||
typedef struct {
|
||||
FILE *file;
|
||||
uint16_t friendnum;
|
||||
uint8_t filenum;
|
||||
uint8_t nextpiece[FILE_PIECE_SIZE];
|
||||
uint16_t piecelen;
|
||||
uint8_t friendname[TOXIC_MAX_NAME_LENGTH];
|
||||
uint8_t filename[MAX_STR_SIZE];
|
||||
} FileSender;
|
||||
|
||||
FileSender file_senders[NUM_FILE_SENDERS];
|
||||
uint8_t num_file_senders;
|
||||
|
||||
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_message(Tox *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata);
|
||||
@ -102,6 +121,10 @@ 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_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);
|
||||
|
||||
ToxWindow *init_windows();
|
||||
void draw_active_window(Tox *m);
|
||||
int add_window(Tox *m, ToxWindow w);
|
||||
|
@ -137,6 +137,41 @@ void on_groupinvite(Tox *m, int friendnumber, uint8_t *group_pub_key, void *user
|
||||
windows[i].onGroupInvite(&windows[i], m, friendnumber, group_pub_key);
|
||||
}
|
||||
}
|
||||
|
||||
void on_file_sendrequest(Tox *m, int friendnumber, uint8_t filenumber, uint64_t filesize,
|
||||
uint8_t *filename, uint16_t filename_length, void *userdata)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
|
||||
if (windows[i].onFileSendRequest != NULL)
|
||||
windows[i].onFileSendRequest(&windows[i], m, friendnumber, filenumber, filesize,
|
||||
filename, filename_length);
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
|
||||
if (windows[i].onFileControl != NULL)
|
||||
windows[i].onFileControl(&windows[i], m, friendnumber, receive_send, filenumber,
|
||||
control_type, data, length);
|
||||
}
|
||||
}
|
||||
|
||||
void on_file_data(Tox *m, int friendnumber, uint8_t filenumber, uint8_t *data, uint16_t length, void *userdata)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < MAX_WINDOWS_NUM; ++i) {
|
||||
if (windows[i].onFileData != NULL)
|
||||
windows[i].onFileData(&windows[i], m, friendnumber, filenumber, data, length);
|
||||
}
|
||||
}
|
||||
|
||||
/* CALLBACKS END */
|
||||
|
||||
int add_window(Tox *m, ToxWindow w)
|
||||
|
Loading…
Reference in New Issue
Block a user