mirror of
https://github.com/Tha14/toxic.git
synced 2024-11-27 00:03:26 +01:00
add command to list pending friend requests, a few related fixes
This commit is contained in:
parent
a3a8f7608a
commit
75e8486061
@ -52,6 +52,7 @@ static struct cmd_func global_commands[] = {
|
|||||||
{ "/note", cmd_note },
|
{ "/note", cmd_note },
|
||||||
{ "/q", cmd_quit },
|
{ "/q", cmd_quit },
|
||||||
{ "/quit", cmd_quit },
|
{ "/quit", cmd_quit },
|
||||||
|
{ "/requests", cmd_requests },
|
||||||
{ "/status", cmd_status },
|
{ "/status", cmd_status },
|
||||||
|
|
||||||
#ifdef _AUDIO
|
#ifdef _AUDIO
|
||||||
|
@ -29,10 +29,10 @@
|
|||||||
#define MAX_NUM_ARGS 4 /* Includes command */
|
#define MAX_NUM_ARGS 4 /* Includes command */
|
||||||
|
|
||||||
#ifdef _AUDIO
|
#ifdef _AUDIO
|
||||||
#define GLOBAL_NUM_COMMANDS 16
|
#define GLOBAL_NUM_COMMANDS 17
|
||||||
#define CHAT_NUM_COMMANDS 12
|
#define CHAT_NUM_COMMANDS 12
|
||||||
#else
|
#else
|
||||||
#define GLOBAL_NUM_COMMANDS 14
|
#define GLOBAL_NUM_COMMANDS 15
|
||||||
#define CHAT_NUM_COMMANDS 5
|
#define CHAT_NUM_COMMANDS 5
|
||||||
#endif /* _AUDIO */
|
#endif /* _AUDIO */
|
||||||
|
|
||||||
|
@ -791,7 +791,7 @@ static void blocklist_onDraw(ToxWindow *self, Tox *m, int y2, int x2)
|
|||||||
wmove(self->window, y2 - 1, 1);
|
wmove(self->window, y2 - 1, 1);
|
||||||
|
|
||||||
wattron(self->window, A_BOLD);
|
wattron(self->window, A_BOLD);
|
||||||
wprintw(self->window, "ID: ");
|
wprintw(self->window, "Key: ");
|
||||||
wattroff(self->window, A_BOLD);
|
wattroff(self->window, A_BOLD);
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
@ -50,18 +50,18 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[
|
|||||||
|
|
||||||
int req = atoi(argv[1]);
|
int req = atoi(argv[1]);
|
||||||
|
|
||||||
if ((req == 0 && strcmp(argv[1], "0")) || req < 0 || req >= MAX_FRIEND_REQUESTS) {
|
if ((req == 0 && strcmp(argv[1], "0")) || req < 0 || req > MAX_FRIEND_REQUESTS) {
|
||||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID.");
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (FriendRequests.list[req][0] == '\0') {
|
if (!FriendRequests.request[req].active) {
|
||||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID.");
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *msg;
|
const char *msg;
|
||||||
int32_t friendnum = tox_add_friend_norequest(m, FriendRequests.list[req]);
|
int32_t friendnum = tox_add_friend_norequest(m, FriendRequests.request[req].key);
|
||||||
|
|
||||||
if (friendnum == -1)
|
if (friendnum == -1)
|
||||||
msg = "Failed to add friend.";
|
msg = "Failed to add friend.";
|
||||||
@ -70,17 +70,17 @@ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[
|
|||||||
on_friendadded(m, friendnum, true);
|
on_friendadded(m, friendnum, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(&FriendRequests.list[req], 0, TOX_CLIENT_ID_SIZE);
|
memset(&FriendRequests.request[req], 0, sizeof(struct _friend_request));
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = FriendRequests.index; i > 0; --i) {
|
for (i = FriendRequests.max_idx; i > 0; --i) {
|
||||||
if (FriendRequests.list[i - 1][0] == '\0')
|
if (FriendRequests.request[i - 1].active)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
FriendRequests.index = i;
|
FriendRequests.max_idx = i;
|
||||||
|
--FriendRequests.num_requests;
|
||||||
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", msg);
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -365,6 +365,38 @@ void cmd_quit(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MA
|
|||||||
exit_toxic_success(m);
|
exit_toxic_success(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void cmd_requests(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
||||||
|
{
|
||||||
|
if (FriendRequests.num_requests == 0) {
|
||||||
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend requests.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int i;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
for (i = 0; i < FriendRequests.max_idx; ++i) {
|
||||||
|
if (!FriendRequests.request[i].active)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
char id[TOX_CLIENT_ID_SIZE * 2 + 1] = {0};
|
||||||
|
size_t j;
|
||||||
|
|
||||||
|
for (j = 0; j < TOX_CLIENT_ID_SIZE; ++j) {
|
||||||
|
char d[3];
|
||||||
|
snprintf(d, sizeof(d), "%02X", FriendRequests.request[i].key[j] & 0xff);
|
||||||
|
strcat(id, d);
|
||||||
|
}
|
||||||
|
|
||||||
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "id: %d, Key: %s", i, id);
|
||||||
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Message: %s", FriendRequests.request[i].msg);
|
||||||
|
|
||||||
|
if (++count < FriendRequests.num_requests)
|
||||||
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE])
|
||||||
{
|
{
|
||||||
bool have_note = false;
|
bool have_note = false;
|
||||||
|
@ -37,6 +37,7 @@ void cmd_nick(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]
|
|||||||
void cmd_note(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
|
void cmd_note(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_prompt_help(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
|
void cmd_prompt_help(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_quit(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
|
void cmd_quit(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
|
void cmd_requests(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
void cmd_status(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
|
void cmd_status(WINDOW *, ToxWindow *, Tox *, int argc, char (*argv)[MAX_STR_SIZE]);
|
||||||
|
|
||||||
void cmd_add_helper(ToxWindow *self, Tox *m, char *id_bin, char *msg);
|
void cmd_add_helper(ToxWindow *self, Tox *m, char *id_bin, char *msg);
|
||||||
|
@ -138,6 +138,7 @@ static void help_draw_global(ToxWindow *self)
|
|||||||
|
|
||||||
wprintw(win, " /add <addr> <msg> : Add contact with optional message\n");
|
wprintw(win, " /add <addr> <msg> : Add contact with optional message\n");
|
||||||
wprintw(win, " /accept <id> : Accept friend request\n");
|
wprintw(win, " /accept <id> : Accept friend request\n");
|
||||||
|
wprintw(win, " /requests : List pending friend requests\n");
|
||||||
wprintw(win, " /connect <ip> <port> <key> : Manually connect to a DHT node\n");
|
wprintw(win, " /connect <ip> <port> <key> : Manually connect to a DHT node\n");
|
||||||
wprintw(win, " /status <type> <msg> : Set status with optional note\n");
|
wprintw(win, " /status <type> <msg> : Set status with optional note\n");
|
||||||
wprintw(win, " /note <msg> : Set a personal note\n");
|
wprintw(win, " /note <msg> : Set a personal note\n");
|
||||||
@ -264,9 +265,9 @@ void help_onKey(ToxWindow *self, wint_t key)
|
|||||||
|
|
||||||
case 'g':
|
case 'g':
|
||||||
#ifdef _AUDIO
|
#ifdef _AUDIO
|
||||||
help_init_window(self, 21, 80);
|
help_init_window(self, 22, 80);
|
||||||
#else
|
#else
|
||||||
help_init_window(self, 17, 80);
|
help_init_window(self, 18, 80);
|
||||||
#endif
|
#endif
|
||||||
self->help->type = HELP_GLOBAL;
|
self->help->type = HELP_GLOBAL;
|
||||||
break;
|
break;
|
||||||
|
21
src/prompt.c
21
src/prompt.c
@ -63,6 +63,7 @@ const char glob_cmd_list[AC_NUM_GLOB_COMMANDS][MAX_CMDNAME_SIZE] = {
|
|||||||
{ "/nick" },
|
{ "/nick" },
|
||||||
{ "/note" },
|
{ "/note" },
|
||||||
{ "/quit" },
|
{ "/quit" },
|
||||||
|
{ "/requests" },
|
||||||
{ "/status" },
|
{ "/status" },
|
||||||
|
|
||||||
#ifdef _AUDIO
|
#ifdef _AUDIO
|
||||||
@ -128,19 +129,23 @@ void prompt_update_connectionstatus(ToxWindow *prompt, bool is_connected)
|
|||||||
|
|
||||||
/* Adds friend request to pending friend requests.
|
/* Adds friend request to pending friend requests.
|
||||||
Returns request number on success, -1 if queue is full. */
|
Returns request number on success, -1 if queue is full. */
|
||||||
static int add_friend_request(const char *public_key)
|
static int add_friend_request(const char *public_key, const char *data)
|
||||||
{
|
{
|
||||||
if (FriendRequests.index >= MAX_FRIEND_REQUESTS)
|
if (FriendRequests.max_idx >= MAX_FRIEND_REQUESTS)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i <= FriendRequests.index; ++i) {
|
for (i = 0; i <= FriendRequests.max_idx; ++i) {
|
||||||
if (FriendRequests.list[i][0] == '\0') {
|
if (!FriendRequests.request[i].active) {
|
||||||
memcpy(FriendRequests.list[i], public_key, TOX_CLIENT_ID_SIZE);
|
FriendRequests.request[i].active = true;
|
||||||
|
memcpy(FriendRequests.request[i].key, public_key, TOX_CLIENT_ID_SIZE);
|
||||||
|
snprintf(FriendRequests.request[i].msg, sizeof(FriendRequests.request[i].msg), "%s", data);
|
||||||
|
|
||||||
if (i == FriendRequests.index)
|
if (i == FriendRequests.max_idx)
|
||||||
++FriendRequests.index;
|
++FriendRequests.max_idx;
|
||||||
|
|
||||||
|
++FriendRequests.num_requests;
|
||||||
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
@ -364,7 +369,7 @@ static void prompt_onFriendRequest(ToxWindow *self, Tox *m, const char *key, con
|
|||||||
line_info_add(self, timefrmt, NULL, NULL, SYS_MSG, 0, 0, "Friend request with the message '%s'", data);
|
line_info_add(self, timefrmt, NULL, NULL, SYS_MSG, 0, 0, "Friend request with the message '%s'", data);
|
||||||
write_to_log("Friend request with the message '%s'", "", ctx->log, true);
|
write_to_log("Friend request with the message '%s'", "", ctx->log, true);
|
||||||
|
|
||||||
int n = add_friend_request(key);
|
int n = add_friend_request(key, data);
|
||||||
|
|
||||||
if (n == -1) {
|
if (n == -1) {
|
||||||
const char *errmsg = "Friend request queue is full. Discarding request.";
|
const char *errmsg = "Friend request queue is full. Discarding request.";
|
||||||
|
15
src/prompt.h
15
src/prompt.h
@ -27,16 +27,23 @@
|
|||||||
#include "windows.h"
|
#include "windows.h"
|
||||||
|
|
||||||
#ifdef _AUDIO
|
#ifdef _AUDIO
|
||||||
#define AC_NUM_GLOB_COMMANDS 16
|
#define AC_NUM_GLOB_COMMANDS 17
|
||||||
#else
|
#else
|
||||||
#define AC_NUM_GLOB_COMMANDS 14
|
#define AC_NUM_GLOB_COMMANDS 15
|
||||||
#endif /* _AUDIO */
|
#endif /* _AUDIO */
|
||||||
|
|
||||||
#define MAX_FRIEND_REQUESTS 32
|
#define MAX_FRIEND_REQUESTS 32
|
||||||
|
|
||||||
|
struct _friend_request {
|
||||||
|
bool active;
|
||||||
|
char msg[MAX_STR_SIZE];
|
||||||
|
uint8_t key[TOX_CLIENT_ID_SIZE];
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int index;
|
int max_idx;
|
||||||
uint8_t list[MAX_FRIEND_REQUESTS][TOX_CLIENT_ID_SIZE];
|
int num_requests;
|
||||||
|
struct _friend_request request[MAX_FRIEND_REQUESTS];
|
||||||
} _FriendRequests;
|
} _FriendRequests;
|
||||||
|
|
||||||
ToxWindow new_prompt(void);
|
ToxWindow new_prompt(void);
|
||||||
|
Loading…
Reference in New Issue
Block a user