Skip to content

Commit

Permalink
feat: add autolog config option for friends
Browse files Browse the repository at this point in the history
  • Loading branch information
JFreegman committed Jan 18, 2024
1 parent 5dac67c commit 9b82c92
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 20 deletions.
9 changes: 7 additions & 2 deletions doc/toxic.conf.5
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,22 @@ Replace password prompt by running this command and using its output as the pass
.PP
\fBfriends\fR
.RS 4
Friend\-specific config settings (prioritized over global settings)
Friend\-specific settings (prioritized over global 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
.PP
\fBautolog\fR
.RS 4
Enable or disable autologging\&. true or false
.RE
.RE
.PP
\fBgroupchats\fR
.RS 4
Groupchat\-specific config settings (prioritized over global settings)
Groupchat\-specific settings (prioritized over global settings)
.PP
\fBtab_name_colour\fR
.RS 4
Expand Down
7 changes: 5 additions & 2 deletions doc/toxic.conf.5.asc
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,16 @@ OPTIONS
the password.

*friends*::
Friend-specific config settings (prioritized over global settings)
Friend-specific settings (prioritized over global settings)

*tab_name_colour*;;
The colour of the friend's tab window name. Valid colours are: white, red, green, cyan, purple, yellow, black.
*autolog*;;
Enable or disable autologging. true or false


*groupchats*::
Groupchat-specific config settings (prioritized over global settings)
Groupchat-specific settings (prioritized over global settings)

*tab_name_colour*;;
The colour of the group's tab window name. Valid colours are: white, red, green, cyan, purple, yellow, black.
Expand Down
5 changes: 5 additions & 0 deletions misc/toxic.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ friends = {
// The colour of the friend's window tab name.
// Valid colours are: white, red, green, cyan, purple, yellow, black.
tab_name_colour="white";

// Enable autologging for this friend
autolog=false;
};

// pk_PUBLIC_KEY_2 = {
Expand All @@ -152,6 +155,8 @@ groupchats = {
// The colour of the group's window tab name.
// Valid colours are: white, red, green, cyan, purple, yellow, black.
tab_name_colour="white";

// Enable autologging for this group
autolog=false;
};

Expand Down
1 change: 1 addition & 0 deletions src/chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,7 @@ static void chat_onInit(ToxWindow *self, Tox *tox)
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;

Friends.list[self->num].logging_on = friend_config_get_autolog(self->num);
chat_init_log(self, tox, nick);

execute(ctx->history, self, tox, "/log", GLOBAL_COMMAND_MODE); // Print log status to screen
Expand Down
69 changes: 58 additions & 11 deletions src/friendlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ static struct PendingDel {
WINDOW *popup;
} PendingDelete;

typedef enum DefaultFriendSettings {
DefaultFriendSettingsTabNameColour = WHITE_BAR_FG,
} DefaultFriendSettings;

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

FriendSettings *settings = &friend->settings;

settings->tab_name_colour = DefaultFriendSettingsTabNameColour;
settings->autolog = (bool) user_settings->autolog == AUTOLOG_ON;
}

static void realloc_friends(int n)
{
if (n <= 0) {
Expand Down Expand Up @@ -482,8 +498,6 @@ 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 @@ -506,7 +520,6 @@ void friendlist_onFriendAdded(ToxWindow *self, Tox *tox, uint32_t num, bool sort
Friends.list[i].auto_accept_files = false; // do not change
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;
Expand Down Expand Up @@ -1432,7 +1445,13 @@ void friend_set_auto_file_accept(uint32_t friendnumber, bool auto_accept)
return;
}

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

if (!friend->active) {
return;
}

friend->auto_accept_files = auto_accept;
}

bool friend_get_auto_accept_files(uint32_t friendnumber)
Expand All @@ -1441,7 +1460,13 @@ bool friend_get_auto_accept_files(uint32_t friendnumber)
return false;
}

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

if (!friend->active) {
return false;
}

return friend->auto_accept_files;
}

/*
Expand Down Expand Up @@ -1493,24 +1518,46 @@ bool friend_config_set_tab_name_colour(const char *public_key, const char *colou
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);
fprintf(stderr, "failed to get friend tab name colour (invalid friendnumber %u)\n", friendnumber);
return -1;
}

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

if (!friend->active) {
return -1;
}

return friend->settings.tab_name_colour;
}

static void set_default_friend_config_settings(ToxicFriend *friend)
bool friend_config_set_autolog(const char *public_key, bool autolog_enabled)
{
if (friend == NULL) {
return;
FriendSettings *settings = get_friend_settings_by_key(public_key);

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

FriendSettings *settings = &friend->settings;
settings->autolog = autolog_enabled;

settings->tab_name_colour = DefaultFriendSettingsTabNameColour;
return true;
}

bool friend_config_get_autolog(uint32_t friendnumber)
{
if (friendnumber >= Friends.max_idx) {
fprintf(stderr, "failed to get autolog setting (invalid friendnumber %u)\n", friendnumber);
return false;
}

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

if (!friend->active) {
return false;
}

return friend->settings.autolog;
}

ToxWindow *new_friendlist(void)
Expand Down
19 changes: 14 additions & 5 deletions src/friendlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ struct GameInvite {

#endif // GAMES

typedef enum DefaultFriendSettings {
DefaultFriendSettingsTabNameColour = WHITE_BAR_FG,
} DefaultFriendSettings;

typedef struct FriendSettings {
int tab_name_colour;
int tab_name_colour;
bool autolog;
} FriendSettings;

typedef struct {
Expand Down Expand Up @@ -165,4 +162,16 @@ bool friend_config_set_tab_name_colour(const char *public_key, const char *colou
*/
int friend_config_get_tab_name_colour(uint32_t friendnumber);

/*
* Sets the autolog config option for the friend associated with `public_key`.
*
* Return true on success.
*/
bool friend_config_set_autolog(const char *public_key, bool autolog_enabled);

/*
* Returns the friend's config setting for autologging.
*/
bool friend_config_get_autolog(uint32_t friendnumber);

#endif /* end of include guard: FRIENDLIST_H */
10 changes: 10 additions & 0 deletions src/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,11 @@ static const struct sound_strings {
static const struct friend_strings {
const char *self;
const char *tab_name_colour;
const char *autolog;
} friend_strings = {
"friends",
"tab_name_colour",
"autolog",
};

static const struct groupchat_strings {
Expand Down Expand Up @@ -487,6 +489,14 @@ int settings_load_friends(const char *patharg)
fprintf(stderr, "config error: failed to set friend tab name colour for %s: (colour: %s)\n", public_key, str);
}
}

int autolog_enabled;

if (config_setting_lookup_bool(keys, friend_strings.autolog, &autolog_enabled)) {
if (!friend_config_set_autolog(public_key, autolog_enabled != 0)) {
fprintf(stderr, "config error: failed to apply friend autolog setting for: %s\n", public_key);
}
}
}

config_destroy(cfg);
Expand Down

0 comments on commit 9b82c92

Please sign in to comment.