From 321f4ddf7190f40d3c6df434ff91ded11ef70f40 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Wed, 30 Mar 2022 18:31:30 +0000 Subject: [PATCH] Use session struct instead of globals Later, when we want to implement multithreading, we need to use separate variables. This is also a bit cleaner FossilOrigin-Name: b720083c13871db7a6b0f8573a91a7ac057d39d3a409df2232574bc001821e74 --- src/account.c | 4 +-- src/account.h | 3 +- src/base_page.c | 14 +++++----- src/base_page.h | 4 +-- src/cookie.c | 10 +++---- src/cookie.h | 4 +-- src/error.c | 4 +-- src/error.h | 3 +- src/index.c | 4 +-- src/index.h | 3 +- src/lists.c | 4 +-- src/lists.h | 3 +- src/local_config.h | 2 -- src/login.c | 12 ++++---- src/login.h | 3 +- src/main.c | 28 +++++++++---------- src/notifications.c | 6 ++-- src/notifications.h | 4 +-- src/page_config.c | 12 ++++---- src/page_config.h | 3 +- src/path.c | 17 ++++++++---- src/path.h | 15 ++++++++-- src/query.c | 26 ++++++++--------- src/query.h | 7 ++--- src/{local_config.c => session.h} | 15 ++++++++-- src/status.c | 46 ++++++++++++++----------------- src/status.h | 16 +++++------ src/test.c | 4 +-- src/test.h | 3 +- src/timeline.c | 34 ++++++++++------------- src/timeline.h | 11 ++++---- 31 files changed, 169 insertions(+), 155 deletions(-) rename src/{local_config.c => session.h} (76%) diff --git a/src/account.c b/src/account.c index ee56050..d4e68e8 100644 --- a/src/account.c +++ b/src/account.c @@ -65,7 +65,7 @@ char* construct_account_page(struct mstdnt_account* acct, return result; } -void content_account(mastodont_t* api, char** data, size_t size) +void content_account(struct session* ssn, mastodont_t* api, char** data) { int cleanup = 0; char* account_page; @@ -100,7 +100,7 @@ void content_account(mastodont_t* api, char** data, size_t size) }; /* Output */ - render_base_page(&b, api); + render_base_page(&b, ssn, api); /* Cleanup */ mastodont_storage_cleanup(&storage); diff --git a/src/account.h b/src/account.h index 1414a1d..67428d1 100644 --- a/src/account.h +++ b/src/account.h @@ -20,11 +20,12 @@ #define ACCOUNT_H #include #include +#include "session.h" char* construct_account_page(struct mstdnt_account* acct, struct mstdnt_status* statuses, size_t statuses_len, size_t* res_size); -void content_account(mastodont_t* api, char** data, size_t data_size); +void content_account(struct session* ssn, mastodont_t* api, char** data); #endif // ACCOUNT_H diff --git a/src/base_page.c b/src/base_page.c index c7f6c37..f51403f 100644 --- a/src/base_page.c +++ b/src/base_page.c @@ -29,7 +29,7 @@ // Files #include "../static/index.chtml" -void render_base_page(struct base_page* page, mastodont_t* api) +void render_base_page(struct base_page* page, struct session* ssn, mastodont_t* api) { char* cookie = getenv("HTTP_COOKIE"); enum l10n_locale locale = page->locale; @@ -40,16 +40,16 @@ void render_base_page(struct base_page* page, mastodont_t* api) struct mstdnt_notification* notifs = NULL; size_t notifs_len; - if (!g_config.changed && cookie) + if (!ssn->config.changed && cookie) { - if (cookies.theme) - g_config.theme = cookies.theme; - if (cookies.logged_in) + if (ssn->cookies.theme) + ssn->config.theme = ssn->cookies.theme; + if (ssn->cookies.logged_in) login_string = ""; } // Get / Show notifications on sidebar - if (cookies.logged_in && cookies.access_token) + if (ssn->cookies.logged_in && ssn->cookies.access_token) { struct mstdnt_get_notifications_args args = { .exclude_types = 0, @@ -73,7 +73,7 @@ void render_base_page(struct base_page* page, mastodont_t* api) char* data; int len = easprintf(&data, data_index_html, L10N[locale][L10N_APP_NAME], - g_config.theme, + ssn->config.theme, config_url_prefix, L10N[locale][L10N_APP_NAME], login_string, diff --git a/src/base_page.h b/src/base_page.h index b954a1a..e8f0bc3 100644 --- a/src/base_page.h +++ b/src/base_page.h @@ -21,7 +21,7 @@ #include #include "l10n.h" #include "local_config.h" - +#include "session.h" enum base_category { BASE_CAT_NONE, @@ -42,6 +42,6 @@ struct base_page char* sidebar_left; }; -void render_base_page(struct base_page* page, mastodont_t* api); +void render_base_page(struct base_page* page, struct session* ssn, mastodont_t* api); #endif // BASE_PAGE_H diff --git a/src/cookie.c b/src/cookie.c index cc51e17..ccad13e 100644 --- a/src/cookie.c +++ b/src/cookie.c @@ -30,9 +30,7 @@ enum cookie_state STATE_V_START, }; -struct cookie_values cookies = { 0 }; - -char* read_cookies_env() +char* read_cookies_env(struct cookie_values* cookies) { struct http_cookie_info info; char* cookies_env = getenv("HTTP_COOKIE"); @@ -52,9 +50,9 @@ char* read_cookies_env() // Will loop through these struct key_value_refs refs[] = { - { "access_token", &(cookies.access_token) }, - { "logged_in", &(cookies.logged_in) }, - { "theme", &(cookies.theme) } + { "access_token", &(cookies->access_token) }, + { "logged_in", &(cookies->logged_in) }, + { "theme", &(cookies->theme) } }; do diff --git a/src/cookie.h b/src/cookie.h index dba254c..5fb39f3 100644 --- a/src/cookie.h +++ b/src/cookie.h @@ -34,11 +34,9 @@ struct http_cookie_info size_t val_len; // Val may be large, like CSS property }; -extern struct cookie_values cookies; - // Stupidly fast simple cookie parser char* parse_cookies(char* begin, struct http_cookie_info* info); -char* read_cookies_env(); +char* read_cookies_env(struct cookie_values* cookies); int cookie_get_val(char* src, char* key, struct http_cookie_info* info); #endif // COOKIE_H diff --git a/src/error.c b/src/error.c index e800b24..e0f5b90 100644 --- a/src/error.c +++ b/src/error.c @@ -34,7 +34,7 @@ char* construct_error(char* error, size_t* size) return error_html; } -void content_not_found(mastodont_t* api, char* path) +void content_not_found(struct session* ssn, mastodont_t* api, char* path) { char* page; easprintf(&page, @@ -47,6 +47,6 @@ void content_not_found(mastodont_t* api, char* path) .sidebar_left = NULL }; - render_base_page(&b, api); + render_base_page(&b, ssn, api); free(page); } diff --git a/src/error.h b/src/error.h index 4ac020f..176e16c 100644 --- a/src/error.h +++ b/src/error.h @@ -20,8 +20,9 @@ #define ERROR_H #include #include +#include "session.h" char* construct_error(char* message, size_t* size); -void content_not_found(mastodont_t* api, char* path); +void content_not_found(struct session* ssn, mastodont_t* api, char* path); #endif // ERROR_H diff --git a/src/index.c b/src/index.c index 110c49a..02a96e9 100644 --- a/src/index.c +++ b/src/index.c @@ -19,8 +19,8 @@ #include #include "timeline.h" -void content_index(mastodont_t* api) +void content_index(struct session* ssn, mastodont_t* api) { // Check logins - tl_public(api, 0); + tl_public(ssn, api, 0); } diff --git a/src/index.h b/src/index.h index b81181e..d7db943 100644 --- a/src/index.h +++ b/src/index.h @@ -19,7 +19,8 @@ #ifndef INDEX_H #define INDEX_H #include +#include "session.h" -void content_index(mastodont_t* api); +void content_index(struct session* ssn, mastodont_t* api); #endif // INDEX_H diff --git a/src/lists.c b/src/lists.c index 11eafed..3a29827 100644 --- a/src/lists.c +++ b/src/lists.c @@ -61,7 +61,7 @@ char* construct_lists_view(char* lists_string, int* size) return list_string; } -void content_lists(mastodont_t* api, char** data, size_t size) +void content_lists(struct session* ssn, mastodont_t* api, char** data) { int cleanup = 0; struct mstdnt_list* lists; @@ -90,7 +90,7 @@ void content_lists(mastodont_t* api, char** data, size_t size) }; // Output - render_base_page(&b, api); + render_base_page(&b, ssn, api); // Cleanup mastodont_storage_cleanup(&storage); diff --git a/src/lists.h b/src/lists.h index d98badb..f5ab64e 100644 --- a/src/lists.h +++ b/src/lists.h @@ -20,10 +20,11 @@ #define LISTS_H #include #include +#include "session.h" char* construct_list(struct mstdnt_list* list, int* size); char* construct_lists(struct mstdnt_list* lists, size_t size, size_t* ret_size); char* construct_lists_view(char* lists_string, int* size); -void content_lists(mastodont_t* api, char** data, size_t size); +void content_lists(struct session* ssn, mastodont_t* api, char** data); #endif // LISTS_H diff --git a/src/local_config.h b/src/local_config.h index 413f00c..61b95b0 100644 --- a/src/local_config.h +++ b/src/local_config.h @@ -25,6 +25,4 @@ struct local_config char* theme; }; -extern struct local_config g_config; - #endif // LOCAL_CONFIG_H diff --git a/src/login.c b/src/login.c index 2169d48..2a76f71 100644 --- a/src/login.c +++ b/src/login.c @@ -29,7 +29,7 @@ // Files #include "../static/login.chtml" -void content_login(mastodont_t* api, char** data, size_t data_size) +void content_login(struct session* ssn, mastodont_t* api, char** data) { struct mstdnt_storage storage = { 0 }, oauth_store = { 0 }; struct mstdnt_app app; @@ -37,8 +37,8 @@ void content_login(mastodont_t* api, char** data, size_t data_size) char* error = NULL; char* page; - printf("%s: %s\r\n", post.username ? post.username: "none", post.password ? post.password : "none"); - if (post.username && post.password) + printf("%s: %s\r\n", ssn->post.username ? ssn->post.username: "none", ssn->post.password ? ssn->post.password : "none"); + if (ssn->post.username && ssn->post.password) { // Getting the client id/secret struct mstdnt_args args_app = { @@ -57,8 +57,8 @@ void content_login(mastodont_t* api, char** data, size_t data_size) .redirect_uri = NULL, .scope = NULL, .code = NULL, - .username = post.username, - .password = post.password + .username = ssn->post.username, + .password = ssn->post.password }; if (mastodont_obtain_oauth_token(api, &args_token, &oauth_store, @@ -96,7 +96,7 @@ void content_login(mastodont_t* api, char** data, size_t data_size) }; // Output - render_base_page(&b, api); + render_base_page(&b, ssn, api); // Cleanup mastodont_storage_cleanup(&storage); diff --git a/src/login.h b/src/login.h index e163755..6a04b4d 100644 --- a/src/login.h +++ b/src/login.h @@ -20,7 +20,8 @@ #define LOGIN_H #include #include +#include "session.h" -void content_login(mastodont_t* api, char** data, size_t data_size); +void content_login(struct session* ssn, mastodont_t* api, char** data); #endif // LOGIN_H diff --git a/src/main.c b/src/main.c index d083535..46923db 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,7 @@ #include "status.h" #include "lists.h" #include "timeline.h" +#include "session.h" #include "notifications.h" #include "test.h" @@ -49,15 +50,19 @@ int main(void) api.url = config_instance_url; mastodont_init(&api, MSTDNT_FLAG_NO_URI_SANITIZE | config_library_flags); - // Load cookies - char* cookies_str = read_cookies_env(); - api.token = cookies.access_token; // Load token now - char* post_str = read_post_data(); - char* get_str = read_query_data(); - - // Config defaults - g_config.theme = "treebird20"; + struct session ssn = { + .config = { + .changed = 0, + .theme = "treebird20" + } + }; + // Load cookies + char* cookies_str = read_cookies_env(&(ssn.cookies)); + api.token = ssn.cookies.access_token; // Load token now + char* post_str = read_post_data(&(ssn.post)); + char* get_str = read_query_data(&(ssn.query)); + /******************* * Path handling * ******************/ @@ -77,7 +82,7 @@ int main(void) { "/notifications", content_notifications }, }; - handle_paths(&api, paths, sizeof(paths)/sizeof(paths[0])); + handle_paths(&ssn, &api, paths, sizeof(paths)/sizeof(paths[0])); // Cleanup if (cookies_str) free(cookies_str); @@ -85,11 +90,6 @@ int main(void) if (get_str) free(get_str); mastodont_free(&api); - // Obliterate all global values, so the next client - // can't even think reading them - memset(&cookies, 0, sizeof(cookies)); - memset(&post, 0, sizeof(post)); - ++run_count; } diff --git a/src/notifications.c b/src/notifications.c index 9076383..39fb68b 100644 --- a/src/notifications.c +++ b/src/notifications.c @@ -107,14 +107,14 @@ char* construct_notifications_compact(struct mstdnt_notification* notifs, ret_size); } -void content_notifications(mastodont_t* api, char** data, size_t data_size) +void content_notifications(struct session* ssn, mastodont_t* api, char** data) { char* page, *notif_html = NULL; struct mstdnt_storage storage; struct mstdnt_notification* notifs; size_t notifs_len; - if (cookies.logged_in) + if (ssn->cookies.logged_in) { struct mstdnt_get_notifications_args args = { .exclude_types = 0, @@ -146,7 +146,7 @@ void content_notifications(mastodont_t* api, char** data, size_t data_size) }; // Output - render_base_page(&b, api); + render_base_page(&b, ssn, api); if (notif_html) free(notif_html); if (page) free(page); } diff --git a/src/notifications.h b/src/notifications.h index dcfd742..8858681 100644 --- a/src/notifications.h +++ b/src/notifications.h @@ -19,7 +19,7 @@ #ifndef NOTIFICATIONS_H #define NOTIFICATIONS_H #include -#include "cookie.h" +#include "session.h" #include "type_string.h" char* construct_notification(struct mstdnt_notification* notif, int* size); @@ -32,6 +32,6 @@ char* construct_notifications_compact(struct mstdnt_notification* notifs, size_t* ret_size); // Page contents -void content_notifications(mastodont_t* api, char** data, size_t data_size); +void content_notifications(struct session* ssn, mastodont_t* api, char** data); #endif // NOTIFICATION_H diff --git a/src/page_config.c b/src/page_config.c index 80a1737..21ef7e0 100644 --- a/src/page_config.c +++ b/src/page_config.c @@ -58,15 +58,15 @@ static char* construct_config_sidebar(enum config_category cat, size_t* size) return sidebar_html; } -void content_config(mastodont_t* api, char** data, size_t size) +void content_config(struct session* ssn, mastodont_t* api, char** data) { char* sidebar_html = construct_config_sidebar(CONFIG_CAT_GENERAL, NULL); - if (post.theme) + if (ssn->post.theme) { - g_config.theme = post.theme; + ssn->config.theme = ssn->post.theme; printf("Set-Cookie: %s=%s; HttpOnly; SameSite=Strict;", - "theme", post.theme); - g_config.changed = 1; + "theme", ssn->post.theme); + ssn->config.changed = 1; } struct base_page b = { @@ -76,7 +76,7 @@ void content_config(mastodont_t* api, char** data, size_t size) .sidebar_left = sidebar_html }; - render_base_page(&b, api); + render_base_page(&b, ssn, api); // Cleanup free(sidebar_html); } diff --git a/src/page_config.h b/src/page_config.h index 5355da6..59a3043 100644 --- a/src/page_config.h +++ b/src/page_config.h @@ -20,7 +20,8 @@ #define PAGE_CONFIG_H #include #include +#include "session.h" -void content_config(mastodont_t* api, char** data, size_t data_size); +void content_config(struct session* ssn, mastodont_t* api, char** data); #endif // PAGE_CONFIG_H diff --git a/src/path.c b/src/path.c index 987d76a..63faba2 100644 --- a/src/path.c +++ b/src/path.c @@ -29,7 +29,9 @@ enum path_state PARSE_READ, }; -int parse_path(mastodont_t* api, struct path_info* path_info) +int parse_path(struct session* ssn, + mastodont_t* api, + struct path_info* path_info) { int fail = 0, fin = 0; enum path_state state = PARSE_NEUTRAL; @@ -103,7 +105,7 @@ breakpt: if (fail) return 1; - path_info->callback(api, data, size); + path_info->callback(ssn, api, data); // Cleanup for (size_t i = 0; i < size; ++i) @@ -114,22 +116,25 @@ breakpt: return 0; } -void handle_paths(mastodont_t* api, struct path_info* paths, size_t paths_len) +void handle_paths(struct session* ssn, + mastodont_t* api, + struct path_info* paths, + size_t paths_len) { char* path = getenv("PATH_INFO"); // "default" path if (path == NULL || (path && strcmp(path, "/") == 0)) { - content_index(api); + content_index(ssn, api); } else { // Generic path for (size_t i = 0; i < paths_len; ++i) { - if (parse_path(api, paths + i) == 0) + if (parse_path(ssn, api, paths + i) == 0) return; } // Fell out, return 404 - content_not_found(api, path); + content_not_found(ssn, api, path); } } diff --git a/src/path.h b/src/path.h index ba94596..8557eb4 100644 --- a/src/path.h +++ b/src/path.h @@ -20,14 +20,23 @@ #define PATH_H #include #include +#include "session.h" struct path_info { char* path; - void (*callback)(mastodont_t*, char**, size_t); + void (*callback)(struct session* ssn, + mastodont_t*, + char**); }; -void handle_paths(mastodont_t* api, struct path_info* paths, size_t paths_len); -int parse_path(mastodont_t* api, struct path_info* path_info); +void handle_paths(struct session* ssn, + mastodont_t* api, + struct path_info* paths, + size_t paths_len); + +int parse_path(struct session* ssn, + mastodont_t* api, + struct path_info* path_info); #endif // PATH_H diff --git a/src/query.c b/src/query.c index 9762b59..e2b8182 100644 --- a/src/query.c +++ b/src/query.c @@ -26,7 +26,7 @@ struct query_values post = { 0 }; struct get_values query = { 0 }; -char* read_query_data() +char* read_query_data(struct get_values* query) { struct http_query_info info; char* query_string = getenv("QUERY_STRING"); @@ -34,7 +34,7 @@ char* read_query_data() // BEGIN Query references struct key_value_refs refs[] = { - { "offset", &(query.offset) }, + { "offset", &(query->offset) }, }; // END Query references @@ -65,7 +65,7 @@ char* read_query_data() return get_query; } -char* read_post_data() +char* read_post_data(struct query_values* post) { struct http_query_info info; char* request_method = getenv("REQUEST_METHOD"); @@ -73,16 +73,16 @@ char* read_post_data() // BEGIN Query references struct key_value_refs refs[] = { - { "content", &(post.content) }, - { "itype", &(post.itype) }, - { "id", &(post.id) }, - { "theme", &(post.theme) }, - { "username", &(post.username) }, - { "password", &(post.password) }, - { "replyid", &(post.replyid) }, - { "min_id", &(post.min_id) }, - { "max_id", &(post.max_id) }, - { "start_id", &(post.start_id) }, + { "content", &(post->content) }, + { "itype", &(post->itype) }, + { "id", &(post->id) }, + { "theme", &(post->theme) }, + { "username", &(post->username) }, + { "password", &(post->password) }, + { "replyid", &(post->replyid) }, + { "min_id", &(post->min_id) }, + { "max_id", &(post->max_id) }, + { "start_id", &(post->start_id) }, }; // END Query references diff --git a/src/query.h b/src/query.h index fc8d60e..6cac334 100644 --- a/src/query.h +++ b/src/query.h @@ -46,11 +46,8 @@ struct get_values char* offset; }; -extern struct query_values post; -extern struct get_values query; - -char* read_query_data(); -char* read_post_data(); +char* read_query_data(struct get_values* query); +char* read_post_data(struct query_values* post); /* A stupidly quick query parser */ char* parse_query(char* begin, struct http_query_info* info); char* try_handle_post(void (*call)(struct http_query_info*, void*), void* arg); diff --git a/src/local_config.c b/src/session.h similarity index 76% rename from src/local_config.c rename to src/session.h index 5aaad1e..f7fee27 100644 --- a/src/local_config.c +++ b/src/session.h @@ -16,9 +16,18 @@ * along with this program. If not, see . */ +#ifndef SESSION_H +#define SESSION_H +#include "query.h" #include "local_config.h" +#include "cookie.h" -struct local_config g_config = { - .changed = 0, - .theme = "treebird20" +struct session +{ + struct query_values post; + struct get_values query; + struct cookie_values cookies; + struct local_config config; }; + +#endif // SESSION_H diff --git a/src/status.c b/src/status.c index 734f0cd..f5b49c8 100644 --- a/src/status.c +++ b/src/status.c @@ -36,9 +36,9 @@ #define NUM_STR "%u" -int try_post_status(mastodont_t* api) +int try_post_status(struct session* ssn, mastodont_t* api) { - if (!post.content) return 1; + if (!(ssn->post.content)) return 1; struct mstdnt_storage storage; @@ -47,7 +47,7 @@ int try_post_status(mastodont_t* api) .content_type = "text/plain", .expires_in = 0, .in_reply_to_conversation_id = NULL, - .in_reply_to_id = post.replyid, + .in_reply_to_id = ssn->post.replyid, .language = NULL, .media_ids = NULL, .poll = NULL, @@ -55,7 +55,7 @@ int try_post_status(mastodont_t* api) .scheduled_at = NULL, .sensitive = 0, .spoiler_text = NULL, - .status = post.content, + .status = ssn->post.content, .visibility = "public", }; @@ -67,11 +67,11 @@ int try_post_status(mastodont_t* api) return 0; } -void content_status_create(mastodont_t* api, char** data, size_t data_size) +void content_status_create(struct session* ssn, mastodont_t* api, char** data) { char* referer = getenv("HTTP_REFERER"); - try_post_status(api); + try_post_status(ssn, api); printf("Status: 303 See Other\r\n" "Location: %s\r\n" @@ -80,19 +80,19 @@ void content_status_create(mastodont_t* api, char** data, size_t data_size) referer ? referer : "/"); } -int try_interact_status(mastodont_t* api, char* id) +int try_interact_status(struct session* ssn, mastodont_t* api, char* id) { struct mstdnt_storage storage = { 0 }; - if (!(post.itype && id)) return 1; + if (!(ssn->post.itype && id)) return 1; // Pretty up the type - if (strcmp(post.itype, "like") == 0) + if (strcmp(ssn->post.itype, "like") == 0) mastodont_favourite_status(api, id, &storage, NULL); - else if (strcmp(post.itype, "repeat") == 0) + else if (strcmp(ssn->post.itype, "repeat") == 0) mastodont_reblog_status(api, id, &storage, NULL); - else if (strcmp(post.itype, "unlike") == 0) + else if (strcmp(ssn->post.itype, "unlike") == 0) mastodont_unfavourite_status(api, id, &storage, NULL); - else if (strcmp(post.itype, "repeat") == 0) + else if (strcmp(ssn->post.itype, "unrepeat") == 0) mastodont_unreblog_status(api, id, &storage, NULL); mastodont_storage_cleanup(&storage); @@ -179,11 +179,11 @@ char* construct_statuses(struct mstdnt_status* statuses, size_t size, size_t* re return construct_func_strings(construct_status_voidwrap, statuses, size, ret_size); } -void status_interact(mastodont_t* api, char** data, size_t data_size) +void status_interact(struct session* ssn, mastodont_t* api, char** data) { char* referer = getenv("HTTP_REFERER"); - try_interact_status(api, data[0]); + try_interact_status(ssn, api, data[0]); printf("Status: 303 See Other\r\n" "Location: %s\r\n" @@ -192,17 +192,17 @@ void status_interact(mastodont_t* api, char** data, size_t data_size) referer ? referer : "/"); } -void status_view(mastodont_t* api, char** data, size_t data_size) +void status_view(struct session* ssn, mastodont_t* api, char** data) { - content_status(api, data, data_size, 0); + content_status(ssn, api, data, 0); } -void status_reply(mastodont_t* api, char** data, size_t data_size) +void status_reply(struct session* ssn, mastodont_t* api, char** data) { - content_status(api, data, data_size, 1); + content_status(ssn, api, data, 1); } -void content_status(mastodont_t* api, char** data, size_t data_size, int is_reply) +void content_status(struct session* ssn, mastodont_t* api, char** data, int is_reply) { char* output; // Status context @@ -211,11 +211,8 @@ void content_status(mastodont_t* api, char** data, size_t data_size, int is_repl size_t stat_before_len, stat_after_len; char* before_html = NULL, *stat_html = NULL, *after_html = NULL, *stat_reply = NULL; - try_post_status(api); + try_post_status(ssn, api); -#ifdef _TEST_ -#include "test/status_test.h" -#else // Get information mastodont_get_status_context(api, data[0], &storage, &statuses_before, &statuses_after, &stat_before_len, &stat_after_len); @@ -234,7 +231,6 @@ void content_status(mastodont_t* api, char** data, size_t data_size, int is_repl // After... after_html = construct_statuses(statuses_after, stat_after_len, NULL); -#endif easprintf(&output, "%s%s%s%s", before_html ? before_html : "", @@ -250,7 +246,7 @@ void content_status(mastodont_t* api, char** data, size_t data_size, int is_repl }; // Output - render_base_page(&b, api); + render_base_page(&b, ssn, api); // Cleanup if (before_html) free(before_html); diff --git a/src/status.h b/src/status.h index cc55d44..2bce945 100644 --- a/src/status.h +++ b/src/status.h @@ -19,10 +19,10 @@ #ifndef STATUS_H #define STATUS_H #include - -int try_post_status(mastodont_t* api); -int try_interact_status(mastodont_t* api, char* id); -void content_status_create(mastodont_t* api, char** data, size_t data_size); +#include "session.h" +int try_post_status(struct session* ssn, mastodont_t* api); +int try_interact_status(struct session* ssn, mastodont_t* api, char* id); +void content_status_create(struct session* ssn, mastodont_t* api, char** data); // HTML Builders char* construct_post_box(char* reply_id, @@ -32,10 +32,10 @@ char* construct_status(struct mstdnt_status* status, int* size, struct mstdnt_no char* construct_statuses(struct mstdnt_status* statuses, size_t size, size_t* ret_size); // Status frontends -void status_view(mastodont_t* api, char** data, size_t data_size); -void status_reply(mastodont_t* api, char** data, size_t data_size); -void status_interact(mastodont_t* api, char** data, size_t data_size); +void status_view(struct session* ssn, mastodont_t* api, char** data); +void status_reply(struct session* ssn, mastodont_t* api, char** data); +void status_interact(struct session* ssn, mastodont_t* api, char** data); // Above wraps to the below function -void content_status(mastodont_t* api, char** data, size_t data_size, int is_reply); +void content_status(struct session* ssn, mastodont_t* api, char** data, int is_reply); #endif // STATUS_H diff --git a/src/test.c b/src/test.c index a374662..98adf7f 100644 --- a/src/test.c +++ b/src/test.c @@ -42,7 +42,7 @@ enum env_tbl_index #define ENV_TBL_GET(index) (env_tbl[(index)] ? env_tbl[(index)] : ENV_NOT_FOUND) -void content_test(mastodont_t* api, char** data, size_t data_size) +void content_test(struct session* ssn, mastodont_t* api, char** data) { char* env_tbl[] = { getenv("HTTP_COOKIES"), @@ -75,6 +75,6 @@ void content_test(mastodont_t* api, char** data, size_t data_size) }; // Output - render_base_page(&b, api); + render_base_page(&b, ssn, api); if (page) free(page); } diff --git a/src/test.h b/src/test.h index 57105b4..7e5b8db 100644 --- a/src/test.h +++ b/src/test.h @@ -20,7 +20,8 @@ #define TEST_H #include #include +#include "session.h" -void content_test(mastodont_t* api, char** data, size_t data_size); +void content_test(struct session* ssn, mastodont_t* api, char** data); #endif /* TEST_H */ diff --git a/src/timeline.c b/src/timeline.c index d30a66d..6c8c132 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -30,7 +30,7 @@ #include "../static/navigation.chtml" -void tl_public(mastodont_t* api, int local) +void tl_public(struct session* ssn, mastodont_t* api, int local) { int cleanup = 0; size_t status_count, statuses_html_count; @@ -46,13 +46,13 @@ void tl_public(mastodont_t* api, int local) .local = local, .remote = 0, .only_media = 0, - .max_id = post.max_id, + .max_id = ssn->post.max_id, .since_id = NULL, - .min_id = post.min_id, + .min_id = ssn->post.min_id, .limit = 20 }; - try_post_status(api); + try_post_status(ssn, api); if (mastodont_timeline_public(api, &args, &storage, &statuses, &status_count)) { @@ -71,7 +71,7 @@ void tl_public(mastodont_t* api, int local) if (statuses) { // If not set, set it - start_id = post.start_id ? post.start_id : statuses[0].id; + start_id = ssn->post.start_id ? ssn->post.start_id : statuses[0].id; navigation_box = construct_navigation_box(start_id, statuses[0].id, statuses[status_count-1].id, @@ -90,7 +90,7 @@ void tl_public(mastodont_t* api, int local) }; // Output - render_base_page(&b, api); + render_base_page(&b, ssn, api); // Cleanup mastodont_storage_cleanup(&storage); @@ -101,7 +101,7 @@ void tl_public(mastodont_t* api, int local) if (output) free(output); } -void tl_list(mastodont_t* api, char* list_id) +void tl_list(struct session* ssn, mastodont_t* api, char* list_id) { int cleanup = 0; size_t status_count, statuses_html_count; @@ -117,7 +117,7 @@ void tl_list(mastodont_t* api, char* list_id) .limit = 20, }; - try_post_status(api); + try_post_status(ssn, api); if (mastodont_timeline_list(api, list_id, &args, &storage, &statuses, &status_count)) { @@ -145,7 +145,7 @@ void tl_list(mastodont_t* api, char* list_id) }; // Output - render_base_page(&b, api); + render_base_page(&b, ssn, api); // Cleanup mastodont_storage_cleanup(&storage); @@ -156,23 +156,19 @@ void tl_list(mastodont_t* api, char* list_id) } -void content_tl_federated(mastodont_t* api, char** data, size_t data_size) +void content_tl_federated(struct session* ssn, mastodont_t* api, char** data) { (void)data; - (void)data_size; - tl_public(api, 0); + tl_public(ssn, api, 0); } -void content_tl_local(mastodont_t* api, char** data, size_t data_size) +void content_tl_local(struct session* ssn, mastodont_t* api, char** data) { (void)data; - (void)data_size; - tl_public(api, 1); + tl_public(ssn, api, 1); } -void content_tl_list(mastodont_t* api, char** data, size_t data_size) +void content_tl_list(struct session* ssn, mastodont_t* api, char** data) { - (void)data; - (void)data_size; - tl_list(api, data[0]); + tl_list(ssn, api, data[0]); } diff --git a/src/timeline.h b/src/timeline.h index 1b5bd5c..1eff0f1 100644 --- a/src/timeline.h +++ b/src/timeline.h @@ -20,13 +20,14 @@ #define TIMELINE_H #include #include +#include "session.h" // Federated and local are here -void tl_public(mastodont_t* api, int local); -void tl_list(mastodont_t* api, char* list_id); +void tl_public(struct session* ssn, mastodont_t* api, int local); +void tl_list(struct session* ssn, mastodont_t* api, char* list_id); -void content_tl_federated(mastodont_t* api, char** data, size_t data_size); -void content_tl_local(mastodont_t* api, char** data, size_t data_size); -void content_tl_list(mastodont_t* api, char** data, size_t data_size); +void content_tl_federated(struct session* ssn, mastodont_t* api, char** data); +void content_tl_local(struct session* ssn, mastodont_t* api, char** data); +void content_tl_list(struct session* ssn, mastodont_t* api, char** data); #endif // TIMELINE_H