Skip to content

Commit

Permalink
feat: add support for friend config settings
Browse files Browse the repository at this point in the history
Currently we only have one setting, but this can easily be
expanded for a number of things such as auto-accepting file
transfers, auto-logging, custom nicknames, message notifications
etc.
  • Loading branch information
JFreegman committed Jan 17, 2024
1 parent 61bfaf5 commit 0ee8312
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 42 deletions.
14 changes: 12 additions & 2 deletions doc/toxic.conf.5
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
.\" Title: toxic.conf
.\" Author: [see the "AUTHORS" section]
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\" Date: 2023-03-04
.\" Date: 2023-12-19
.\" Manual: Toxic Manual
.\" Source: toxic __VERSION__
.\" Language: English
.\"
.TH "TOXIC\&.CONF" "5" "2023\-03\-04" "toxic __VERSION__" "Toxic Manual"
.TH "TOXIC\&.CONF" "5" "2023\-12\-19" "toxic __VERSION__" "Toxic Manual"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
Expand Down Expand Up @@ -298,6 +298,16 @@ Replace password prompt by running this command and using its output as the pass
.RE
.RE
.PP
\fBfriends\fR
.RS 4
Friend config settings
.PP
\fBtab_name_colour\fR
.RS 4
The colour of the friend\(cqs tab window name\&. Valid colours are: white, red, green, cyan, purple, yellow, black\&.
.RE
.RE
.PP
\fBsounds\fR
.RS 4
Configuration related to notification sounds\&. Special value "silent" can be used to disable a specific notification\&.
Expand Down
6 changes: 6 additions & 0 deletions doc/toxic.conf.5.asc
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ OPTIONS
Replace password prompt by running this command and using its output as
the password.

*friends*::
Friend config settings

*tab_name_colour*;;
The colour of the friend's tab window name. Valid colours are: white, red, green, cyan, purple, yellow, black.

*sounds*::
Configuration related to notification sounds.
Special value "silent" can be used to disable a specific notification. +
Expand Down
14 changes: 14 additions & 0 deletions misc/toxic.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ tox = {
// chatlogs_path="/home/USERNAME/toxic_chatlogs/";
};

// Friend config settings. Public keys are used as friend identifiers
// and must begin with the prefix "pk_".
friends = {
pk_PUBLIC_KEY_1 = {
// The colour of the friend's window tab name.
// Valid colours are: white, red, green, cyan, purple, yellow, black.
tab_name_colour="white";
};

pk_PUBLIC_KEY_2 = {
tab_name_colour="white";
};
};

// To disable a sound set the path to "silent"
sounds = {
error="__DATADIR__/sounds/ToxicError.wav";
Expand Down
4 changes: 4 additions & 0 deletions src/chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,10 @@ static void chat_onInit(ToxWindow *self, Tox *tox)

line_info_init(ctx->hst);

const int tab_name_colour = friend_config_get_tab_name_colour(self->num);

self->colour = tab_name_colour > 0 ? tab_name_colour : WHITE_BAR_FG;

chat_init_log(self, tox, nick);

execute(ctx->history, self, tox, "/log", GLOBAL_COMMAND_MODE); // Print log status to screen
Expand Down
68 changes: 68 additions & 0 deletions src/friendlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ static void friendlist_onStatusMessageChange(ToxWindow *self, uint32_t num, cons
Friends.list[num].statusmsg_len = strlen(Friends.list[num].statusmsg);
}

static void set_default_friend_config_settings(ToxicFriend *friend);

void friendlist_onFriendAdded(ToxWindow *self, Tox *tox, uint32_t num, bool sort)
{
UNUSED_VAR(self);
Expand All @@ -505,6 +507,7 @@ void friendlist_onFriendAdded(ToxWindow *self, Tox *tox, uint32_t num, bool sort
Friends.list[i].connection_status = TOX_CONNECTION_NONE;
Friends.list[i].status = TOX_USER_STATUS_NONE;
Friends.list[i].logging_on = (bool) user_settings->autolog == AUTOLOG_ON;
set_default_friend_config_settings(&Friends.list[i]);

Tox_Err_Friend_Get_Public_Key pkerr;
tox_friend_get_public_key(tox, num, (uint8_t *) Friends.list[i].pub_key, &pkerr);
Expand Down Expand Up @@ -1441,6 +1444,71 @@ bool friend_get_auto_accept_files(uint32_t friendnumber)
return Friends.list[friendnumber].auto_accept_files;
}

/*
* Returns a pointer to the FriendSettings object associated with `public_key`.
* Returns NULL on failure.
*/
static FriendSettings *get_friend_settings_by_key(const char *public_key)
{
char pk_bin[TOX_PUBLIC_KEY_SIZE];

if (tox_pk_string_to_bytes(public_key, strlen(public_key), pk_bin, sizeof(pk_bin)) != 0) {
return NULL;
}

for (size_t i = 0; i < Friends.max_idx; ++i) {
ToxicFriend *friend = &Friends.list[i];

if (memcmp(pk_bin, friend->pub_key, sizeof(friend->pub_key)) == 0) {
return &friend->settings;
}
}

return NULL;
}

bool friend_config_set_tab_name_colour(const char *public_key, const char *colour)
{
FriendSettings *settings = get_friend_settings_by_key(public_key);

if (settings == NULL) {
return false;
}

const int colour_val = colour_string_to_int(colour);

if (colour_val < 0) {
return false;
}

settings->tab_name_colour = colour_val;

return true;
}

int friend_config_get_tab_name_colour(uint32_t friendnumber)
{
if (friendnumber >= Friends.max_idx) {
fprintf(stderr, "failed to get friend tab name colour for friend %u\n", friendnumber);
return -1;
}

const ToxicFriend *friend = &Friends.list[friendnumber];

return friend->settings.tab_name_colour;
}

static void set_default_friend_config_settings(ToxicFriend *friend)
{
if (friend == NULL) {
return;
}

FriendSettings *settings = &friend->settings;

settings->tab_name_colour = DefaultFriendSettingsTabNameColour;
}

ToxWindow *new_friendlist(void)
{
ToxWindow *ret = calloc(1, sizeof(ToxWindow));
Expand Down
22 changes: 22 additions & 0 deletions src/friendlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ struct GameInvite {

#endif // GAMES

typedef enum DefaultFriendSettings {
DefaultFriendSettingsTabNameColour = WHITE_BAR_FG,
} DefaultFriendSettings;

typedef struct FriendSettings {
int tab_name_colour;
} FriendSettings;

typedef struct {
char name[TOXIC_MAX_NAME_LENGTH + 1];
uint16_t namelength;
Expand Down Expand Up @@ -90,6 +98,8 @@ typedef struct {
struct FileTransfer file_receiver[MAX_FILES];
struct FileTransfer file_sender[MAX_FILES];
PendingFileTransfer file_send_queue[MAX_FILES];

FriendSettings settings;
} ToxicFriend;

typedef struct {
Expand Down Expand Up @@ -142,4 +152,16 @@ void friend_set_auto_file_accept(uint32_t friendnumber, bool auto_accept);
*/
bool friend_get_auto_accept_files(uint32_t friendnumber);

/*
* Sets the tab name colour config option for the friend associated with `public_key` to `colour`.
*
* Return true on success.
*/
bool friend_config_set_tab_name_colour(const char *public_key, const char *colour);

/* Returns a friend's tab name colour.
* Returns -1 on error.
*/
int friend_config_get_tab_name_colour(uint32_t friendnumber);

#endif /* end of include guard: FRIENDLIST_H */
20 changes: 5 additions & 15 deletions src/global_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,24 +265,14 @@ void cmd_colour(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv

const char *colour = argv[1];

if (strcasecmp(colour, "white") == 0) {
self->colour = WHITE_BAR_FG;
} else if (strcasecmp(colour, "red") == 0) {
self->colour = RED_BAR_FG;
} else if (strcasecmp(colour, "green") == 0) {
self->colour = GREEN_BAR_FG;
} else if (strcasecmp(colour, "yellow") == 0) {
self->colour = YELLOW_BAR_FG;
} else if (strcasecmp(colour, "cyan") == 0) {
self->colour = CYAN_BAR_FG;
} else if (strcasecmp(colour, "purple") == 0) {
self->colour = PURPLE_BAR_FG;
} else if (strcasecmp(colour, "black") == 0) {
self->colour = BLACK_BAR_FG;
} else {
const int colour_val = colour_string_to_int(colour);

if (colour_val < 0) {
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid colour");
return;
}

self->colour = colour_val;
}

void cmd_connect(WINDOW *window, ToxWindow *self, Tox *tox, int argc, char (*argv)[MAX_STR_SIZE])
Expand Down
33 changes: 33 additions & 0 deletions src/misc_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -751,3 +751,36 @@ unsigned int rand_not_secure(void)

return n;
}

int colour_string_to_int(const char *colour)
{
if (strcasecmp(colour, "white") == 0) {
return WHITE_BAR_FG;
}

if (strcasecmp(colour, "red") == 0) {
return RED_BAR_FG;
}

if (strcasecmp(colour, "green") == 0) {
return GREEN_BAR_FG;
}

if (strcasecmp(colour, "yellow") == 0) {
return YELLOW_BAR_FG;
}

if (strcasecmp(colour, "cyan") == 0) {
return CYAN_BAR_FG;
}

if (strcasecmp(colour, "purple") == 0) {
return PURPLE_BAR_FG;
}

if (strcasecmp(colour, "black") == 0) {
return BLACK_BAR_FG;
}

return -1;
}
10 changes: 10 additions & 0 deletions src/misc_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,14 @@ unsigned int rand_range_not_secure(unsigned int upper_bound);
*/
unsigned int rand_not_secure(void);

/*
* Returns an integer associated with an ncurses foreground colour, per the C_COLOURS enum
* in windows.h.
*
* Valid colour strings are: white, red, green, cyan, purple, yellow, black.
*
* Returns -1 if colour is invalid.
*/
int colour_string_to_int(const char *colour);

#endif /* MISC_TOOLS_H */
Loading

0 comments on commit 0ee8312

Please sign in to comment.