Skip to content

Commit 635410b

Browse files
committed
Add command to auto-accept inbound file transfers
This command is disabled by default, and is only temporarily enabled for an individual friend for a given client session
1 parent 5cc8681 commit 635410b

File tree

7 files changed

+112
-23
lines changed

7 files changed

+112
-23
lines changed

Diff for: src/chat.c

+17-8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static void kill_infobox(ToxWindow *self);
6767
static const char *chat_cmd_list[] = {
6868
"/accept",
6969
"/add",
70+
"/autoaccept",
7071
"/avatar",
7172
"/cancel",
7273
"/cinvite",
@@ -506,17 +507,12 @@ static void chat_onFileControl(ToxWindow *self, Tox *m, uint32_t friendnum, uint
506507
}
507508

508509
switch (control) {
509-
case TOX_FILE_CONTROL_RESUME: {
510-
/* transfer is accepted */
510+
case TOX_FILE_CONTROL_RESUME: { /* transfer is accepted */
511511
if (ft->state == FILE_TRANSFER_PENDING) {
512512
ft->state = FILE_TRANSFER_STARTED;
513513
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "File transfer [%zu] for '%s' accepted.",
514514
ft->index, ft->file_name);
515-
char progline[MAX_STR_SIZE];
516-
init_progress_bar(progline);
517-
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", progline);
518515
sound_notify(self, silent, NT_NOFOCUS | user_settings->bell_on_filetrans_accept | NT_WNDALERT_2, NULL);
519-
ft->line_id = self->chatwin->hst->line_end->id + 2;
520516
} else if (ft->state == FILE_TRANSFER_PAUSED) { /* transfer is resumed */
521517
ft->state = FILE_TRANSFER_STARTED;
522518
}
@@ -706,8 +702,6 @@ static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_
706702
}
707703
}
708704

709-
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Type '/savefile %zu' to accept the file transfer.", ft->index);
710-
711705
ft->file_size = file_size;
712706
snprintf(ft->file_path, sizeof(ft->file_path), "%s", file_path);
713707
snprintf(ft->file_name, sizeof(ft->file_name), "%s", filename);
@@ -722,6 +716,19 @@ static void chat_onFileRecv(ToxWindow *self, Tox *m, uint32_t friendnum, uint32_
722716
box_notify(self, transfer_pending, NT_WNDALERT_0 | NT_NOFOCUS | user_settings->bell_on_filetrans,
723717
&self->active_box, self->name, "Incoming file: %s", filename);
724718
}
719+
720+
const bool auto_accept_files = friend_get_auto_accept_files(friendnum);
721+
722+
if (auto_accept_files) {
723+
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Auto-accepting file transfer %zu", ft->index);
724+
725+
char cmd[MAX_STR_SIZE];
726+
snprintf(cmd, sizeof(cmd), "/savefile %zu", ft->index);
727+
execute(self->window, self, m, cmd, CHAT_COMMAND_MODE);
728+
} else {
729+
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0,
730+
"Type '/savefile %zu' to accept the file transfer.", ft->index);
731+
}
725732
}
726733

727734
static void chat_onConferenceInvite(ToxWindow *self, Tox *m, int32_t friendnumber, uint8_t type,
@@ -1596,6 +1603,8 @@ static void chat_onInit(ToxWindow *self, Tox *m)
15961603

15971604
scrollok(ctx->history, 0);
15981605
wmove(self->window, y2 - CURS_Y_OFFSET, 0);
1606+
1607+
line_info_print(self);
15991608
}
16001609

16011610
ToxWindow *new_chat(Tox *m, uint32_t friendnum)

Diff for: src/chat_commands.c

+41-6
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,40 @@
3737
extern ToxWindow *prompt;
3838
extern FriendsList Friends;
3939

40+
void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
41+
{
42+
UNUSED_VAR(window);
43+
UNUSED_VAR(m);
44+
45+
const char *msg;
46+
const bool auto_accept_files = friend_get_auto_accept_files(self->num);
47+
48+
if (argc == 0) {
49+
if (auto_accept_files) {
50+
msg = "Auto-file accept for this friend is enabled; type \"/autoaccept off\" to disable";
51+
} else {
52+
msg = "Auto-file accept for this friend is disabled; type \"/autoaccept on\" to enable";
53+
}
54+
55+
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, msg);
56+
return;
57+
}
58+
59+
const char *option = argv[1];
60+
61+
if (!strcmp(option, "1") || !strcmp(option, "on")) {
62+
friend_set_auto_file_accept(self->num, true);
63+
msg = "Auto-accepting file transfers has been enabled for this friend";
64+
} else if (!strcmp(option, "0") || !strcmp(option, "off")) {
65+
friend_set_auto_file_accept(self->num, false);
66+
msg = "Auto-accepting file transfers has been disabled for this friend";
67+
} else {
68+
msg = "Invalid option. Use \"/autoaccept on\" and \"/autoaccept off\" to toggle auto-file accept";
69+
}
70+
71+
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, msg);
72+
}
73+
4074
void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
4175
{
4276
UNUSED_VAR(window);
@@ -313,19 +347,19 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
313347
long int idx = strtol(argv[1], NULL, 10);
314348

315349
if ((idx == 0 && strcmp(argv[1], "0")) || idx < 0 || idx >= MAX_FILES) {
316-
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with that ID.");
350+
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld", idx);
317351
return;
318352
}
319353

320354
FileTransfer *ft = get_file_transfer_struct_index(self->num, idx, FILE_TRANSFER_RECV);
321355

322356
if (!ft) {
323-
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with that ID.");
357+
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld.", idx);
324358
return;
325359
}
326360

327361
if (ft->state != FILE_TRANSFER_PENDING) {
328-
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with that ID.");
362+
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "No pending file transfers with ID %ld.", idx);
329363
return;
330364
}
331365

@@ -344,12 +378,13 @@ void cmd_savefile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv
344378

345379
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Saving file [%ld] as: '%s'", idx, ft->file_path);
346380

347-
/* prep progress bar line */
381+
const bool auto_accept_files = friend_get_auto_accept_files(self->num);
382+
const uint32_t line_skip = auto_accept_files ? 4 : 2;
383+
348384
char progline[MAX_STR_SIZE];
349385
init_progress_bar(progline);
350386
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "%s", progline);
351-
352-
ft->line_id = self->chatwin->hst->line_end->id + 2;
387+
ft->line_id = self->chatwin->hst->line_end->id + line_skip;
353388
ft->state = FILE_TRANSFER_STARTED;
354389

355390
return;

Diff for: src/chat_commands.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "toxic.h"
2727
#include "windows.h"
2828

29+
void cmd_autoaccept_files(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
2930
void cmd_cancelfile(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
3031
void cmd_conference_invite(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
3132
void cmd_conference_join(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);

Diff for: src/execute.c

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ static struct cmd_func global_commands[] = {
8383
};
8484

8585
static struct cmd_func chat_commands[] = {
86+
{ "/autoaccept", cmd_autoaccept_files },
8687
{ "/cancel", cmd_cancelfile },
8788
{ "/cinvite", cmd_conference_invite },
8889
{ "/cjoin", cmd_conference_join },

Diff for: src/friendlist.c

+31
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ void friendlist_onFriendAdded(ToxWindow *self, Tox *m, uint32_t num, bool sort)
502502
Friends.list[i].num = num;
503503
Friends.list[i].active = true;
504504
Friends.list[i].chatwin = -1;
505+
Friends.list[i].auto_accept_files = false; // do not change
505506
Friends.list[i].connection_status = TOX_CONNECTION_NONE;
506507
Friends.list[i].status = TOX_USER_STATUS_NONE;
507508
Friends.list[i].logging_on = (bool) user_settings->autolog == AUTOLOG_ON;
@@ -1344,6 +1345,10 @@ void friendlist_onInit(ToxWindow *self, Tox *m)
13441345

13451346
void disable_chatwin(uint32_t f_num)
13461347
{
1348+
if (f_num >= Friends.max_idx) {
1349+
return;
1350+
}
1351+
13471352
Friends.list[f_num].chatwin = -1;
13481353
}
13491354

@@ -1381,12 +1386,20 @@ static void friendlist_onAV(ToxWindow *self, ToxAV *av, uint32_t friend_number,
13811386
/* Returns a friend's status */
13821387
Tox_User_Status get_friend_status(uint32_t friendnumber)
13831388
{
1389+
if (friendnumber >= Friends.max_idx) {
1390+
return TOX_USER_STATUS_NONE;
1391+
}
1392+
13841393
return Friends.list[friendnumber].status;
13851394
}
13861395

13871396
/* Returns a friend's connection status */
13881397
Tox_Connection get_friend_connection_status(uint32_t friendnumber)
13891398
{
1399+
if (friendnumber >= Friends.max_idx) {
1400+
return TOX_CONNECTION_NONE;
1401+
}
1402+
13901403
return Friends.list[friendnumber].connection_status;
13911404
}
13921405

@@ -1410,6 +1423,24 @@ bool friend_is_blocked(const char *public_key)
14101423
return false;
14111424
}
14121425

1426+
void friend_set_auto_file_accept(uint32_t friendnumber, bool auto_accept)
1427+
{
1428+
if (friendnumber >= Friends.max_idx) {
1429+
return;
1430+
}
1431+
1432+
Friends.list[friendnumber].auto_accept_files = auto_accept;
1433+
}
1434+
1435+
bool friend_get_auto_accept_files(uint32_t friendnumber)
1436+
{
1437+
if (friendnumber >= Friends.max_idx) {
1438+
return false;
1439+
}
1440+
1441+
return Friends.list[friendnumber].auto_accept_files;
1442+
}
1443+
14131444
ToxWindow *new_friendlist(void)
14141445
{
14151446
ToxWindow *ret = calloc(1, sizeof(ToxWindow));

Diff for: src/friendlist.h

+11
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ typedef struct {
7575
Tox_Connection connection_status;
7676
bool is_typing;
7777
bool logging_on; /* saves preference for friend irrespective of global settings */
78+
bool auto_accept_files; /* default should always be false */
7879
Tox_User_Status status;
7980

8081
struct LastOnline last_online;
@@ -131,4 +132,14 @@ void sort_friendlist_index(void);
131132
*/
132133
bool friend_is_blocked(const char *public_key);
133134

135+
/*
136+
* Enable or disable auto-accepting file transfers for this friend.
137+
*/
138+
void friend_set_auto_file_accept(uint32_t friendnumber, bool auto_accept);
139+
140+
/*
141+
* Return true if auto-accepting file transfers is enabled for this friend.
142+
*/
143+
bool friend_get_auto_accept_files(uint32_t friendnumber);
144+
134145
#endif /* end of include guard: FRIENDLIST_H */

Diff for: src/help.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ static void help_draw_global(ToxWindow *self)
185185
wprintw(win, " /note <msg> : Set a personal note\n");
186186
wprintw(win, " /nick <name> : Set your global name (doesn't affect groups)\n");
187187
wprintw(win, " /nospam <value> : Change part of your Tox ID to stop spam\n");
188-
wprintw(win, " /log <on> or <off> : Enable/disable logging\n");
188+
wprintw(win, " /log <on>|<off> : Enable/disable logging\n");
189189
wprintw(win, " /myid : Print your Tox ID\n");
190190
wprintw(win, " /group <name> : Create a new group chat\n");
191191
wprintw(win, " /join <chatid> : Join a groupchat using a Chat ID\n");
@@ -195,7 +195,7 @@ static void help_draw_global(ToxWindow *self)
195195

196196
#ifdef QRCODE
197197
#ifdef QRPNG
198-
wprintw(win, " /myqr <txt> or <png> : Print your Tox ID's QR code to a file.\n");
198+
wprintw(win, " /myqr <txt>|<png> : Print your Tox ID's QR code to a file.\n");
199199
#else
200200
wprintw(win, " /myqr : Print your Tox ID's QR code to a file.\n");
201201
#endif /* QRPNG */
@@ -246,6 +246,7 @@ static void help_draw_chat(ToxWindow *self)
246246
wprintw(win, "Chat Commands:\n");
247247
wattroff(win, A_BOLD | COLOR_PAIR(RED));
248248

249+
wprintw(win, " /autoaccept <on>|<off> : Toggle auto-accepting file transfers\n");
249250
wprintw(win, " /cinvite <n> : Invite contact to a conference \n");
250251
wprintw(win, " /cjoin : Join a pending conference\n");
251252
wprintw(win, " /invite <n> : Invite contact to a groupchat \n");
@@ -363,16 +364,16 @@ static void help_draw_conference(ToxWindow *self)
363364
wprintw(win, "Conference commands:\n");
364365
wattroff(win, A_BOLD | COLOR_PAIR(RED));
365366

366-
wprintw(win, " /title <msg> : Show/set conference title\n");
367+
wprintw(win, " /title <msg> : Show/set conference title\n");
367368
#ifdef AUDIO
368369
wattron(win, A_BOLD);
369370
wprintw(win, "\n Audio:\n");
370371
wattroff(win, A_BOLD);
371-
wprintw(win, " /audio <on> or <off> : Enable/disable audio in an audio conference\n");
372-
wprintw(win, " /mute : Toggle self audio mute status\n");
373-
wprintw(win, " /mute <nick> or <pubkey> : Toggle peer audio mute status\n");
374-
wprintw(win, " /ptt <on> or <off> : Toggle audio input Push-To-Talk (F2 to activate)\n");
375-
wprintw(win, " /sense <n> : VAD sensitivity threshold\n\n");
372+
wprintw(win, " /audio <on>|<off> : Enable/disable audio in an audio conference\n");
373+
wprintw(win, " /mute : Toggle self audio mute status\n");
374+
wprintw(win, " /mute <nick>|<pubkey> : Toggle peer audio mute status\n");
375+
wprintw(win, " /ptt <on>|<off> : Toggle audio input Push-To-Talk (F2 to activate)\n");
376+
wprintw(win, " /sense <n> : VAD sensitivity threshold\n\n");
376377
#endif
377378

378379
help_draw_bottom_menu(win);
@@ -434,7 +435,7 @@ void help_onKey(ToxWindow *self, wint_t key)
434435
break;
435436

436437
case L'c':
437-
height = 12;
438+
height = 13;
438439
#ifdef VIDEO
439440
height += 15;
440441
#elif AUDIO

0 commit comments

Comments
 (0)