Fix config resetting

FossilOrigin-Name: 20c585001854675bf5026baba2c8939399e8b1102420ff2b990b6f614c54ca92
This commit is contained in:
nekobit 2022-07-09 02:55:10 +00:00
parent 68528563ca
commit 708ac7b023
3 changed files with 69 additions and 41 deletions

View file

@ -26,17 +26,24 @@ int set_config_str(struct session* ssn,
char** v,
char* cookie_name,
struct key* post,
struct key* cookie)
struct key* cookie,
enum config_page page,
enum config_page curr_page)
{
if (ssn->post.set.is_set && post->is_set)
if (page == curr_page)
{
printf("Set-Cookie: %s=%s; HttpOnly; Path=/; SameSite=Strict;\r\n",
cookie_name, keypstr(post));
if (ssn->post.set.is_set && post->is_set && page == curr_page)
{
printf("Set-Cookie: %s=%s; HttpOnly; Path=/; SameSite=Strict;\r\n",
cookie_name, keypstr(post));
}
if ((ssn->post.set.is_set && post->is_set) || cookie->is_set)
*v = post->is_set ? keypstr(post) : keypstr(cookie);
}
if ((ssn->post.set.is_set && post->is_set) || cookie->is_set)
*v = post->is_set ? keypstr(post) : keypstr(cookie);
else
// Set it to the cookie
*v = keypstr(cookie);
return 0;
}
@ -44,26 +51,33 @@ int set_config_int(struct session* ssn,
int* v,
char* cookie_name,
struct key* post,
struct key* cookie)
struct key* cookie,
enum config_page page,
enum config_page curr_page)
{
if (ssn->post.set.is_set)
if (page == curr_page)
{
printf("Set-Cookie: %s=%d; HttpOnly; Path=/; Max-Age=31536000; SameSite=Strict;\r\n",
cookie_name, post_bool_intp(post));
}
if (ssn->post.set.is_set && page == curr_page)
{
printf("Set-Cookie: %s=%d; HttpOnly; Path=/; Max-Age=31536000; SameSite=Strict;\r\n",
cookie_name, post_bool_intp(post));
}
// Checks if boolean option
if (ssn->post.set.is_set || cookie->is_set)
*v = ssn->post.set.is_set ? post_bool_intp(post)
: keypint(cookie);
// Checks if boolean option
if (ssn->post.set.is_set || cookie->is_set)
*v = ssn->post.set.is_set ? post_bool_intp(post)
: keypint(cookie);
}
else
*v = keypint(cookie);
return 0;
}
// Shorthand for the arguments passed into functions below
#define LOAD_CFG_SIM(strcookie, varname) ssn, &(ssn->config.varname), (strcookie), &(ssn->post.varname), &(ssn->cookies.varname)
#define LOAD_CFG_SIM(strcookie, varname) ssn, &(ssn->config.varname), (strcookie), &(ssn->post.varname), &(ssn->cookies.varname), page
void load_config(struct session* ssn, mastodont_t* api)
void load_config(struct session* ssn, mastodont_t* api, enum config_page page)
{
if (ssn->post.set.is_set)
{
@ -72,7 +86,7 @@ void load_config(struct session* ssn, mastodont_t* api)
if (try_upload_media(&storage, ssn, api, &attachments, NULL) == 0)
{
struct key atm = { .type.s = attachments[0].url, .is_set = 1 };
set_config_str(ssn, &(ssn->config.background_url), "background_url", &(atm), &(ssn->cookies.background_url));
set_config_str(ssn, &(ssn->config.background_url), "background_url", &(atm), &(ssn->cookies.background_url), page, CONFIG_APPEARANCE);
}
if (storage)
@ -80,21 +94,21 @@ void load_config(struct session* ssn, mastodont_t* api)
}
if (!ssn->post.set.is_set)
ssn->config.background_url = keystr(ssn->cookies.background_url);
set_config_str(LOAD_CFG_SIM("theme", theme));
set_config_int(LOAD_CFG_SIM("themeclr", themeclr));
set_config_int(LOAD_CFG_SIM("jsactions", jsactions));
set_config_int(LOAD_CFG_SIM("jsreply", jsreply));
set_config_int(LOAD_CFG_SIM("jslive", jslive));
set_config_int(LOAD_CFG_SIM("js", js));
set_config_int(LOAD_CFG_SIM("statattachments", stat_attachments));
set_config_int(LOAD_CFG_SIM("statgreentexts", stat_greentexts));
set_config_int(LOAD_CFG_SIM("statdope", stat_dope));
set_config_int(LOAD_CFG_SIM("statoneclicksoftware", stat_oneclicksoftware));
set_config_int(LOAD_CFG_SIM("statemojolikes", stat_emojo_likes));
set_config_int(LOAD_CFG_SIM("stathidemuted", stat_hide_muted));
set_config_int(LOAD_CFG_SIM("instanceshowshoutbox", instance_show_shoutbox));
set_config_int(LOAD_CFG_SIM("instancepanel", instance_panel));
set_config_int(LOAD_CFG_SIM("notifembed", notif_embed));
set_config_int(LOAD_CFG_SIM("interact_img", interact_img));
set_config_int(LOAD_CFG_SIM("lang", lang));
set_config_str(LOAD_CFG_SIM("theme", theme), CONFIG_APPEARANCE);
set_config_int(LOAD_CFG_SIM("themeclr", themeclr), CONFIG_APPEARANCE);
set_config_int(LOAD_CFG_SIM("jsactions", jsactions), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("jsreply", jsreply), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("jslive", jslive), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("js", js), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("statattachments", stat_attachments), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("statgreentexts", stat_greentexts), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("statdope", stat_dope), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("statoneclicksoftware", stat_oneclicksoftware), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("statemojolikes", stat_emojo_likes), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("stathidemuted", stat_hide_muted), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("instanceshowshoutbox", instance_show_shoutbox), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("instancepanel", instance_panel), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("notifembed", notif_embed), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("interact_img", interact_img), CONFIG_GENERAL);
set_config_int(LOAD_CFG_SIM("lang", lang), CONFIG_GENERAL);
}

View file

@ -24,18 +24,28 @@
#include "attachments.h"
#include "key.h"
enum config_page
{
CONFIG_GENERAL,
CONFIG_APPEARANCE,
};
int set_config_str(struct session* ssn,
char** v,
char* cookie_name,
struct key* post,
struct key* cookie);
struct key* cookie,
enum config_page page,
enum config_page curr_page);
int set_config_int(struct session* ssn,
int* v,
char* cookie_name,
struct key* post,
struct key* cookie);
struct key* cookie,
enum config_page page,
enum config_page curr_page);
void load_config(struct session* ssn, mastodont_t* api);
void load_config(struct session* ssn, mastodont_t* api, enum config_page page);
#endif // LOCAL_CONFIG_SET_H

View file

@ -151,7 +151,11 @@ int main(void)
char* get_str = read_get_data(&(ssn.query));
// Read config options
load_config(&ssn, &api);
enum config_page page = CONFIG_GENERAL;
char* path_info = getenv("PATH_INFO");
if (path_info && strcmp(path_info, "/config/appearance") == 0)
page = CONFIG_APPEARANCE;
load_config(&ssn, &api, page);
// Load current account information
get_account_info(&api, &ssn);