From 477e1ba2977435ae7bb75c7dbd95cf28247f89bd Mon Sep 17 00:00:00 2001 From: nirav Date: Thu, 28 Mar 2019 09:10:52 +0530 Subject: Move APIs into libgs dir --- src/account.c | 92 --------------------------- src/account.h | 29 --------- src/auth.c | 146 ------------------------------------------ src/auth.h | 9 --- src/config.c | 14 ++--- src/http.c | 178 ---------------------------------------------------- src/http.h | 10 --- src/instance_info.c | 38 ----------- src/instance_info.h | 31 --------- src/log.c | 3 +- src/log.h | 3 +- src/login_window.c | 48 +++++++------- src/login_window.h | 4 +- src/main.c | 26 ++++---- src/main_window.c | 26 ++++---- src/register.c | 128 ------------------------------------- src/register.h | 8 --- src/status.c | 120 ----------------------------------- src/status.h | 31 --------- src/string-util.c | 123 ------------------------------------ src/string-util.h | 10 --- src/timeline.c | 118 ---------------------------------- src/timeline.h | 16 ----- 23 files changed, 66 insertions(+), 1145 deletions(-) delete mode 100644 src/account.c delete mode 100644 src/account.h delete mode 100644 src/auth.c delete mode 100644 src/auth.h delete mode 100644 src/http.c delete mode 100644 src/http.h delete mode 100644 src/instance_info.c delete mode 100644 src/instance_info.h delete mode 100644 src/register.c delete mode 100644 src/register.h delete mode 100644 src/status.c delete mode 100644 src/status.h delete mode 100644 src/string-util.c delete mode 100644 src/string-util.h delete mode 100644 src/timeline.c delete mode 100644 src/timeline.h (limited to 'src') diff --git a/src/account.c b/src/account.c deleted file mode 100644 index c407d56..0000000 --- a/src/account.c +++ /dev/null @@ -1,92 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include "account.h" -#include "log.h" - -struct account *account_from_json(const char *json_data) -{ - struct account *a; - json_t *root; - json_error_t error; - - root = json_loads(json_data, 0, &error); - if (!root) { - log_msg(LOG_WARNING, "account_from_json", "json parse error: line %d: %s", - error.line, error.text); - return NULL; - } - - a = account_from_json_t(root); - json_decref(root); - return a; -} - -struct account *account_from_json_t(const json_t *root) -{ - struct account *a; - if (!root) { - log_msg(LOG_WARNING, "account_from_json_t", "json data is null"); - return NULL; - } - - if (!json_is_object(root)) { - log_msg(LOG_WARNING, "account_from_json_t", "json root is not object"); - return NULL; - } - - a = calloc(1, sizeof(struct account)); - if (!a) { - err(1, "account_from_json_t"); - return NULL; - } - - json_t *id = json_object_get(root, "id"); - if (json_is_string(id)) { - a->id = strdup(json_string_value(id)); - } - - json_t *username = json_object_get(root, "username"); - if (json_is_string(username)) { - a->username = strdup(json_string_value(username)); - } - - json_t *acct = json_object_get(root, "acct"); - if (json_is_string(acct)) { - a->acct = strdup(json_string_value(acct)); - } - - json_t *display_name = json_object_get(root, "display_name"); - if (json_is_string(display_name)) { - a->display_name = strdup(json_string_value(display_name)); - } - - return a; -} - -void account_free(struct account *a) -{ - if (a->id) - free(a->id); - if (a->username) - free(a->username); - if (a->acct) - free(a->acct); - if (a->display_name) - free(a->display_name); - if (a->note) - free(a->note); - if (a->url) - free(a->url); - if (a->avatar) - free(a->avatar); - if (a->avatar_static) - free(a->avatar_static); - if (a->header) - free(a->header); - if (a->header_static) - free(a->header_static); - free(a); -} - diff --git a/src/account.h b/src/account.h deleted file mode 100644 index 547f276..0000000 --- a/src/account.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef __account_H -#define __account_H - -#include - -struct account { - char *id; - char *username; - char *acct; - char *display_name; - bool locked; - unsigned int follower_count; - unsigned int following_count; - unsigned int statuses_count; - char *note; - char *url; - char *avatar; - char *avatar_static; - char *header; - char *header_static; - struct account *moved; - bool bot; -}; - -struct account *account_from_json(const char *json_data); -struct account *account_from_json_t(const json_t *); -void account_free(struct account *); - -#endif diff --git a/src/auth.c b/src/auth.c deleted file mode 100644 index c903e67..0000000 --- a/src/auth.c +++ /dev/null @@ -1,146 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include -#include "string-util.h" -#include "auth.h" -#include "http.h" -#include "config.h" -#include "log.h" - -#define LOGIN_URL "https://%s/oauth/token" - -struct login_response { - char *access_token; - char *scope; -}; - -static char *get_login_url(const char *domain) -{ - char *url; - size_t size; - - size = strlen(LOGIN_URL) + strlen(domain) - 1; - url = malloc(size); - if (!url) { - err(1, NULL); - return NULL; - } - - sprintf(url, LOGIN_URL, domain); - return url; -} - -static char *get_login_req(const char *email, const char *password) -{ - char *req; - json_t *json_root; - json_error_t error; - - json_root = json_pack_ex(&error, 1, "{s:s, s:s, s:s, s:s, s:s}", - "client_id", config->client_id, "client_secret", - config->client_secret, "grant_type", "password", "username", email, - "password", password); - if (!json_root) { - return NULL; - } - - req = json_dumps(json_root, 0); - json_decref(json_root); - - return req; -} - -static struct login_response *get_login_resp(char *data) -{ - struct login_response *resp; - json_t *json_root; - json_t *access_token, *scope; - - json_root = json_loads(data, 0, NULL); - if (!json_root) { - return NULL; - } - - if (!json_is_object(json_root)) { - json_decref(json_root); - return NULL; - } - - access_token = json_object_get(json_root, "access_token"); - scope = json_object_get(json_root, "scope"); - if (!json_is_string(access_token) || !json_is_string(scope)) { - json_decref(json_root); - return NULL; - } - - resp = calloc(1, sizeof(struct login_response)); - resp->access_token = strdup(json_string_value(access_token)); - resp->scope = strdup(json_string_value(scope)); - - json_decref(json_root); - return resp; -} - -int login(const char *email, const char *password) -{ - char *url, *req_data, *resp_data; - struct login_response *resp; - - if (!email || !*email || !password || !*password) { - log_msg(LOG_WARNING, "login", "invalid argument"); - return -1; - } - - if (!is_registered()) { - log_msg(LOG_WARNING, "login", "app not registred"); - return -1; - } - - url = get_login_url(config->instance_url); - if (!url) { - err(1, NULL); - return -1; - } - - req_data = get_login_req(email, password); - if (!req_data) { - free(url); - return -1; - } - - resp_data = post_request(url, req_data); - free(url); - free(req_data); - if (!resp_data) { - log_msg(LOG_WARNING, "register_app", "login request failed"); - return -1; - } - - if (!resp_data) { - log_msg(LOG_WARNING, "register_app", "invalid response"); - return -1; - } - - resp = get_login_resp(resp_data); - free(resp_data); - if (!resp) { - log_msg(LOG_WARNING, "register_app", "invalid response"); - return -1; - } - - config_set_access_token(resp->access_token); - /* scope = strdup(json_string_value(scope_j)); */ - - if (config_save()) { - log_msg(LOG_WARNING, "login_call", "failed to save config"); - } - - free(resp->access_token); - free(resp->scope); - free(resp); - - return 0; -} diff --git a/src/auth.h b/src/auth.h deleted file mode 100644 index 9c3ee03..0000000 --- a/src/auth.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef __AUTH_H -#define __AUTH_H - -#include - -int login(const char *email, const char *password); - -#endif - diff --git a/src/config.c b/src/config.c index f409700..24e3b54 100644 --- a/src/config.c +++ b/src/config.c @@ -1,15 +1,15 @@ #define _POSIX_C_SOURCE 200809L +#include +#include #include -#include #include -#include +#include #include #include #include -#include -#include +#include +#include #include -#include "string-util.h" #include "config.h" #include "log.h" @@ -54,8 +54,8 @@ char *get_config_path() return NULL; } char config_path[PATH_MAX]; - strlcpy(config_path, home, sizeof(config_path)); - strlcat(config_path, CONFIG_PATH_SUFFIX, sizeof(config_path)); + g_strlcpy(config_path, home, sizeof(config_path)); + g_strlcat(config_path, CONFIG_PATH_SUFFIX, sizeof(config_path)); return strdup(config_path); } diff --git a/src/http.c b/src/http.c deleted file mode 100644 index ddd0470..0000000 --- a/src/http.c +++ /dev/null @@ -1,178 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include -#include "string-util.h" -#include "auth.h" -#include "config.h" -#include "log.h" - -#define BUFFER_SIZE (256 * 1024) -#define AUTH_HEADER_STR_PREFIX "Authorization: Bearer " - -int http_init() -{ - if (curl_global_init(CURL_GLOBAL_ALL)) { - log_msg(LOG_WARNING, "http_init", "failed to init curl"); - return -1; - } - return 0; -} - -void http_cleanup() -{ - curl_global_cleanup(); - return; -} - -struct write_result { - char *data; - int pos; -}; - -static size_t write_response(void *ptr, size_t size, size_t nmemb, void *stream) -{ - struct write_result *result = (struct write_result *)stream; - - if (result->pos + size * nmemb >= BUFFER_SIZE - 1) { - log_msg(LOG_WARNING, "write_response", "buffer too small"); - return 0; - } - - memcpy(result->data + result->pos, ptr, size * nmemb); - result->pos += size * nmemb; - - return size * nmemb; -} - -char *get_request(const char *url) -{ - CURL *curl = NULL; - CURLcode status; - struct curl_slist *headers = NULL; - char *data = NULL; - long code; - - curl = curl_easy_init(); - if (!curl) - goto error; - - data = malloc(BUFFER_SIZE); - if (!data) - goto error; - - struct write_result write_result = {.data = data, .pos = 0}; - - if (is_logged_in()) { - const char *token = get_access_token(); - size_t s = strlen(AUTH_HEADER_STR_PREFIX) + strlen(token) + 1; - char *auth_header_val = malloc(s); - strlcpy(auth_header_val, AUTH_HEADER_STR_PREFIX, s); - strlcat(auth_header_val, token, s); - headers = curl_slist_append(headers, auth_header_val); - } - - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result); - - status = curl_easy_perform(curl); - if (status) { - log_msg(LOG_WARNING, "get_request", - "unable to request data from %s: %s", url, - curl_easy_strerror(status)); - goto error; - } - - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - if (code != 200) { - log_msg(LOG_WARNING, "get_request", "server responded with code %ld", - code); - goto error; - } - - data[write_result.pos] = '\0'; - - curl_slist_free_all(headers); - curl_easy_cleanup(curl); - return data; - -error: - if (data) - free(data); - if (headers) - curl_slist_free_all(headers); - if (curl) - curl_easy_cleanup(curl); - return NULL; -} - -char *post_request(const char *url, char *post_data) -{ - CURL *curl = NULL; - CURLcode status; - struct curl_slist *headers = NULL; - char *data = NULL; - long code; - - curl = curl_easy_init(); - if (!curl) - goto error; - - data = malloc(BUFFER_SIZE); - if (!data) - goto error; - - struct write_result write_result = {.data = data, .pos = 0}; - - if (is_logged_in()) { - const char *token = get_access_token(); - size_t s = strlen(AUTH_HEADER_STR_PREFIX) + strlen(token) + 1; - char *auth_header_val = malloc(s); - strlcpy(auth_header_val, AUTH_HEADER_STR_PREFIX, s); - strlcat(auth_header_val, token, s); - headers = curl_slist_append(headers, auth_header_val); - } - if (post_data) { - char *content_type_header_val = "Content-Type: application/json"; - headers = curl_slist_append(headers, content_type_header_val); - } - - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_response); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &write_result); - if (post_data) - curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); - - status = curl_easy_perform(curl); - if (status) { - log_msg(LOG_WARNING, "post_request", "unable to request data from %s: %s", - url, curl_easy_strerror(status)); - goto error; - } - - curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code); - if (code != 200) { - log_msg(LOG_WARNING, "post_request", "server responded with code %ld", code); - goto error; - } - - data[write_result.pos] = '\0'; - - curl_slist_free_all(headers); - curl_easy_cleanup(curl); - return data; - -error: - if (data) - free(data); - if (headers) - curl_slist_free_all(headers); - if (curl) - curl_easy_cleanup(curl); - return NULL; -} diff --git a/src/http.h b/src/http.h deleted file mode 100644 index 8e69b55..0000000 --- a/src/http.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __HTTP_H -#define __HTTP_H - -int http_init(); -void http_cleanup(); -char *get_request(const char *url); -char *post_request(const char *url, char *data); -/* int http_post_async(char *url, char *post_data, void (*callback)(char *)); */ - -#endif diff --git a/src/instance_info.c b/src/instance_info.c deleted file mode 100644 index 3c13ebd..0000000 --- a/src/instance_info.c +++ /dev/null @@ -1,38 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include "instance_info.h" -#include "log.h" - -struct instance_info *instance_info_from_json(char *json_data) -{ - struct instance_info *info; - info = malloc(sizeof(struct instance_info)); - - json_t *root; - json_error_t error; - - root = json_loads(json_data, 0, &error); - - if (!root) { - log_msg(LOG_WARNING, "instance_info_from_json", "error: on line %d: %s", - error.line, error.text); - return NULL; - } - - if (!json_is_object(root)) { - log_msg(LOG_WARNING, "instance_info_from_json", "root is not object"); - json_decref(root); - return NULL; - } - - json_t *title = json_object_get(root, "title"); - if (!json_is_string(title)) { - log_msg(LOG_WARNING, "instance_info_from_json", "title is not string"); - return NULL; - } - info->title = strdup(json_string_value(title)); - - json_decref(root); - return info; -} diff --git a/src/instance_info.h b/src/instance_info.h deleted file mode 100644 index 72a5bbb..0000000 --- a/src/instance_info.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __INSTANCE_INFO_H -#define __INSTANCE_INFO_H - -#include - -struct instance_info_urls { - char *streaming_api; -}; - -struct instance_info_stats { - int user_count; - int status_count; - int domain_count; -}; - -struct instance_info { - char *url; - char *title; - char *description; - char *email; - char *version; - struct instance_info_urls urls; - struct instance_info_stats states; - char *thumbnail; - char **languages; - bool registrations; -}; - -struct instance_info *instance_info_from_json(char *json_data); - -#endif diff --git a/src/log.c b/src/log.c index f1cb7d5..6aff619 100644 --- a/src/log.c +++ b/src/log.c @@ -10,7 +10,8 @@ static const char *log_levels[] = { "fatal", }; -void log_msg(enum log_level level, const char *namespace, const char *format, ...) { +void log_msg(enum log_level level, const char *namespace, + const char *format, ...) { va_list args; va_start(args, format); diff --git a/src/log.h b/src/log.h index ecd3428..f38be7a 100644 --- a/src/log.h +++ b/src/log.h @@ -8,6 +8,7 @@ enum log_level { LOG_FATAL }; -void log_msg(enum log_level level, const char *namespace, const char *format, ...); +void log_msg(enum log_level level, const char *namespace, + const char *format, ...); #endif diff --git a/src/login_window.c b/src/login_window.c index 241ca9b..54f7174 100644 --- a/src/login_window.c +++ b/src/login_window.c @@ -1,16 +1,13 @@ #define _POSIX_C_SOURCE 200809L -#include +#include #include #include -#include #include -#include "auth.h" -#include "register.h" -#include "http.h" -#include "timeline.h" +#include #include "main_window.h" #include "log.h" +static GSClient *gs_client = NULL; GtkApplication *application; GObject *login_dialog; GObject *instance_domain_entry, *email_entry, *password_entry; @@ -19,39 +16,40 @@ GObject *spinner; static gboolean login_completed(gpointer data) { - int *val_ptr = data; gtk_spinner_stop(GTK_SPINNER(spinner)); - if (*val_ptr) { - free(val_ptr); - log_msg(LOG_WARNING, "login_callback", "login failed"); - return G_SOURCE_REMOVE; - } else { - free(val_ptr); - gtk_widget_destroy(GTK_WIDGET(login_dialog)); - return G_SOURCE_REMOVE; - } + gtk_widget_destroy(GTK_WIDGET(login_dialog)); + return G_SOURCE_REMOVE; +} + +static gboolean login_failed(gpointer data) +{ + gtk_spinner_stop(GTK_SPINNER(spinner)); + return G_SOURCE_REMOVE; } static gpointer user_register(gpointer data) { - int *val_ptr; const char *instance_name, *email, *password; + struct gs_app *app; instance_name = gtk_entry_get_text(GTK_ENTRY(instance_domain_entry)); email = gtk_entry_get_text(GTK_ENTRY(email_entry)); password = gtk_entry_get_text(GTK_ENTRY(password_entry)); - val_ptr = malloc(sizeof(int)); - *val_ptr = register_app(instance_name); + app = gs_app_register(instance_name); if (*val_ptr) { log_msg(LOG_WARNING, "user_register", "registration failed"); } - *val_ptr = login(email, password); - if (*val_ptr) { + + gs_client = gs_client_new(instance_name, app->client_id, app->client_secret); + + if (gs_auth_authenticate(gs_client, email, password)) { log_msg(LOG_WARNING, "user_register", "login failed"); + gdk_threads_add_idle(login_failed, NULL); + return; } - gdk_threads_add_idle(login_completed, val_ptr); + gdk_threads_add_idle(login_completed, NULL); return NULL; } @@ -73,7 +71,6 @@ static bool is_form_valid() static void login_button_click(GtkButton *button, gpointer user_data) { - g_print("clicked\n"); if (is_form_valid()) submit_login_form(); } @@ -92,14 +89,15 @@ static void login_form_changed(GtkWidget *widget, gpointer data) gtk_widget_set_sensitive(GTK_WIDGET(login_button), false); } -int create_login_dialog() +int create_login_dialog(gpointer data) { GtkBuilder *login_builder; login_builder = gtk_builder_new_from_file("data/login_window.ui"); login_dialog = gtk_builder_get_object(login_builder, "login_dialog"); - instance_domain_entry = gtk_builder_get_object(login_builder, "instance_name"); + instance_domain_entry = gtk_builder_get_object(login_builder, + "instance_name"); g_signal_connect(G_OBJECT(instance_domain_entry), "changed", G_CALLBACK(login_form_changed), NULL); g_signal_connect(GTK_ENTRY(instance_domain_entry), "activate", diff --git a/src/login_window.h b/src/login_window.h index b3d299d..9243e46 100644 --- a/src/login_window.h +++ b/src/login_window.h @@ -1,7 +1,9 @@ #ifndef __LOGIN_WINDOW_H #define __LOGIN_WINDOW_H -int create_login_dialog(); +#include + +int create_login_dialog(gpointer data); #endif diff --git a/src/main.c b/src/main.c index 78968a3..9d8747e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,24 +1,26 @@ #define _POSIX_C_SOURCE 200809L -#include #include #include #include #include -#include "http.h" +#include #include "config.h" #include "main_window.h" -static void startup(GtkApplication *app, gpointer user_data) +static void activate(GtkApplication *app, gpointer user_data) { + GSClient *gs_client; + config_load(); - if (http_init()) { - exit(EXIT_FAILURE); - } -} + if(gs_init()) + g_application_quit(G_APPLICATION(app)); -static void activate(GtkApplication *app, gpointer user_data) -{ - create_main_window(app, NULL); + gs_client = gs_client_new(config->instance_url, config->client_id, + config->client_secret); + if(!gs_client) + g_application_quit(G_APPLICATION(app)); + + create_main_window(app, gs_client); } int main(int argc, char **argv) @@ -27,12 +29,12 @@ int main(int argc, char **argv) int status; app = gtk_application_new("org.gtk.ap_client", G_APPLICATION_FLAGS_NONE); - g_signal_connect(app, "startup", G_CALLBACK(startup), NULL); g_signal_connect(app, "activate", G_CALLBACK(activate), NULL); status = g_application_run(G_APPLICATION(app), argc, argv); + g_object_unref(app); - http_cleanup(); config_cleanup(); + gs_cleanup(); return status; } diff --git a/src/main_window.c b/src/main_window.c index 048de51..bf1f50d 100644 --- a/src/main_window.c +++ b/src/main_window.c @@ -4,15 +4,13 @@ #include #include #include -#include "auth.h" -#include "http.h" -#include "timeline.h" +#include #include "log.h" #include "config.h" #include "login_window.h" -#include "string-util.h" static GtkWidget *window, *scrolled, *list_box; +static GSClient *gs_client = NULL; static gchar *html_to_pango(const char *content) { @@ -61,7 +59,7 @@ static gchar *html_to_pango(const char *content) static gboolean timeline_loaded(gpointer data) { - struct timeline *t = data; + struct gs_timeline *t = data; for (size_t i = 0; i < t->size; i++) { GtkBuilder *post_builder; GObject *post_box, *post_content_label; @@ -116,24 +114,28 @@ static gboolean timeline_loaded(gpointer data) gtk_widget_show_all(GTK_WIDGET(post_box)); } /* gtk_spinner_stop(GTK_SPINNER(spinner)); */ - timeline_free(t); + gs_timeline_free(t); return G_SOURCE_REMOVE; } static gpointer load_timeline(gpointer data) { - struct timeline *t; - t = get_timeline(NULL, NULL, NULL, 20); + struct gs_timeline *t; + + t = gs_timeline_get(gs_client, NULL, NULL, NULL, 20); if (!t) { log_msg(LOG_WARNING, "load_timeline", "failed"); return NULL; } + gdk_threads_add_idle(timeline_loaded, t); return NULL; } void create_main_window(GtkApplication *app, gpointer user_data) { + gs_client = user_data; + window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_application(GTK_WINDOW(window), GTK_APPLICATION(app)); gtk_window_set_title(GTK_WINDOW(window), "ap_client"); @@ -156,12 +158,14 @@ void create_main_window(GtkApplication *app, gpointer user_data) gtk_container_add(GTK_CONTAINER(scrolled), list_box); gtk_widget_show_all(window); - if (!is_logged_in()) { + + if (!gs_auth_is_logged_in(client)) { create_login_dialog(); } - if (!is_logged_in()) { + if (!gs_auth_is_logged_in(client)) { log_msg(LOG_ERROR, "create_main_window", "login is required to view timeline"); } - g_thread_new("load_timeline_thread", &load_timeline, NULL); + + g_thread_new("load_timeline_thread", &load_timeline, client); } diff --git a/src/register.c b/src/register.c deleted file mode 100644 index 4885ec5..0000000 --- a/src/register.c +++ /dev/null @@ -1,128 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include "http.h" -#include "config.h" -#include "log.h" - -#define CLIENT_NAME "ap_client" -#define REGISTER_URL "https://%s/api/v1/apps" - -struct register_response { - char *client_id; - char *client_secret; -}; - -static char *get_register_url(const char *domain) -{ - char *url; - size_t size; - - size = strlen(REGISTER_URL) + strlen(domain) - 1; - url = malloc(size); - if (!url) { - err(1, NULL); - return NULL; - } - - sprintf(url, REGISTER_URL, domain); - return url; -} - -static char *get_register_req() -{ - char *req; - json_t *json_root; - - json_root = json_pack_ex(NULL, 1, "{s:s, s:s, s:s}", "client_name", - CLIENT_NAME, "redirect_uris", "urn:ietf:wg:oauth:2.0:oob", "scopes", - "read write push"); - if (!json_root) { - return NULL; - } - - req = json_dumps(json_root, 0); - json_decref(json_root); - - return req; -} - -static struct register_response *get_register_resp(const char *data) -{ - struct register_response *resp; - json_t *json_root; - json_t *cid, *csec; - - json_root = json_loads(data, 0, NULL); - if (!json_root) { - return NULL; - } - - if (!json_is_object(json_root)) { - json_decref(json_root); - return NULL; - } - - cid = json_object_get(json_root, "client_id"); - csec = json_object_get(json_root, "client_secret"); - if (!json_is_string(cid) || !json_is_string(csec)) { - json_decref(json_root); - return NULL; - } - - resp = calloc(1, sizeof(struct register_response)); - resp->client_id = strdup(json_string_value(cid)); - resp->client_secret = strdup(json_string_value(csec)); - - json_decref(json_root); - return resp; -} - -int register_app(const char *domain) -{ - char *url, *req_data, *resp_data; - struct register_response *resp; - - if (!domain || !*domain) { - log_msg(LOG_WARNING, "register_app", "invalid argument"); - return -1; - } - - url = get_register_url(domain); - if (!url) { - return -1; - } - - req_data = get_register_req(); - if (!req_data) { - free(url); - return -1; - } - - resp_data = post_request(url, req_data); - free(url); - free(req_data); - if (!resp_data) { - log_msg(LOG_WARNING, "register_app", "register request failed"); - return -1; - } - - resp = get_register_resp(resp_data); - free(resp_data); - if (!resp) { - log_msg(LOG_WARNING, "register_app", "invalid response"); - return -1; - } - - config_set_client_id(resp->client_id); - config_set_client_secret(resp->client_secret); - config_set_instance_url(domain); - - free(resp->client_id); - free(resp->client_secret); - free(resp); - - return 0; -} diff --git a/src/register.h b/src/register.h deleted file mode 100644 index 2122d6b..0000000 --- a/src/register.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __REGISTER_H -#define __REGISTER_H - -int register_app(const char *domain); - -#endif - - diff --git a/src/status.c b/src/status.c deleted file mode 100644 index cfb3645..0000000 --- a/src/status.c +++ /dev/null @@ -1,120 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include "status.h" -#include "account.h" -#include "log.h" - -struct status *status_from_json(const char *json_data) -{ - struct status *s; - json_t *root; - json_error_t error; - - root = json_loads(json_data, 0, &error); - if (!root) { - log_msg(LOG_WARNING, "status_from_json", "json parse error: line %d: %s", - error.line, error.text); - return NULL; - } - - s = status_from_json_t(root); - json_decref(root); - return s; -} - -struct status *status_from_json_t(const json_t *root) -{ - struct status *s; - if (!root) { - log_msg(LOG_WARNING, "status_from_json_t", "json data is null"); - return NULL; - } - - if (!json_is_object(root)) { - log_msg(LOG_WARNING, "status_from_json_t", "json root is not object"); - return NULL; - } - - s = calloc(1, sizeof(struct status)); - if (!s) { - err(1, "status_from_json_t"); - return NULL; - } - - json_t *id = json_object_get(root, "id"); - if (json_is_string(id)) { - s->id = strdup(json_string_value(id)); - } - - json_t *uri = json_object_get(root, "uri"); - if (json_is_string(uri)) { - s->uri = strdup(json_string_value(uri)); - } - - json_t *url = json_object_get(root, "url"); - if (json_is_string(url)) { - s->url = strdup(json_string_value(url)); - } - - json_t *account = json_object_get(root, "account"); - if (json_is_object(account)) { - s->account = account_from_json_t(account); - } - - json_t *in_reply_to_id = json_object_get(root, "in_reply_to_id"); - if (json_is_string(in_reply_to_id)) { - s->in_reply_to_id = strdup(json_string_value(in_reply_to_id)); - } - - json_t *in_reply_to_account_id = - json_object_get(root, "in_reply_to_account_id"); - if (json_is_string(in_reply_to_account_id)) { - s->in_reply_to_account_id = - strdup(json_string_value(in_reply_to_account_id)); - } - - json_t *content = json_object_get(root, "content"); - if (json_is_string(content)) { - s->content = strdup(json_string_value(content)); - } - - json_t *replies_count = json_object_get(root, "replies_count"); - if (json_is_integer(replies_count)) { - s->replies_count = json_integer_value(replies_count); - } - - json_t *reblogs_count = json_object_get(root, "reblogs_count"); - if (json_is_integer(reblogs_count)) { - s->reblogs_count = json_integer_value(reblogs_count); - } - - json_t *favourites_count = json_object_get(root, "favourites_count"); - if (json_is_integer(favourites_count)) { - s->favourites_count = json_integer_value(favourites_count); - } - - return s; -} - -void status_free(struct status *s) -{ - if (s->id) - free(s->id); - if (s->uri) - free(s->uri); - if (s->url) - free(s->url); - if (s->account) - account_free(s->account); - if (s->in_reply_to_id) - free(s->in_reply_to_id); - if (s->in_reply_to_account_id) - free(s->in_reply_to_account_id); - if (s->reblog) - status_free(s->reblog); - if (s->content) - free(s->content); - free(s); -} diff --git a/src/status.h b/src/status.h deleted file mode 100644 index a065f50..0000000 --- a/src/status.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef __STATUS_H -#define __STATUS_H - -#include -#include - -#include "account.h" - -struct status { - char *id; - char *uri; - char *url; - struct account *account; - char *in_reply_to_id; - char *in_reply_to_account_id; - struct status *reblog; - char *content; - unsigned int replies_count; - unsigned int reblogs_count; - unsigned int favourites_count; - bool reblogged; - bool favourited; - bool muted; - bool sensitive; -}; - -struct status *status_from_json(const char *); -struct status *status_from_json_t(const json_t *); -void status_free(struct status *); - -#endif diff --git a/src/string-util.c b/src/string-util.c deleted file mode 100644 index d5a702b..0000000 --- a/src/string-util.c +++ /dev/null @@ -1,123 +0,0 @@ -/* $OpenBSD: src/lib/libc/string/strlcpy.c,v 1.11 2006/05/05 15:27:38 - * millert Exp $ */ - -/* - * Copyright (c) 1998 Todd C. Miller - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include - -/* - * Copy src to string dst of size siz. At most siz-1 characters - * will be copied. Always NUL terminates (unless siz == 0). - * Returns strlen(src); if retval >= siz, truncation occurred. - */ -size_t strlcpy(char *dst, const char *src, size_t siz) -{ - char *d = dst; - const char *s = src; - size_t n = siz; - - /* Copy as many bytes as will fit */ - if (n != 0) { - while (--n != 0) { - if ((*d++ = *s++) == '\0') - break; - } - } - - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++) - ; - } - - return (s - src - 1); /* count does not include NUL */ -} - -/* - * Appends src to string dst of size siz (unlike strncat, siz is the - * full size of dst, not space left). At most siz-1 characters - * will be copied. Always NUL terminates (unless siz <= strlen(dst)). - * Returns strlen(src) + MIN(siz, strlen(initial dst)). - * If retval >= siz, truncation occurred. - */ -size_t strlcat(char *dst, const char *src, size_t siz) -{ - char *d = dst; - const char *s = src; - size_t n = siz; - size_t dlen; - - /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0 && *d != '\0') - d++; - dlen = d - dst; - n = siz - dlen; - - if (n == 0) - return (dlen + strlen(s)); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; - n--; - } - s++; - } - *d = '\0'; - - return (dlen + (s - src)); /* count does not include NUL */ -} - -char *str_replace(const char *str, const char *old, const char *new) -{ - const char *ins; - char *result, *tmp; - size_t len_old, len_new, len_front, len_result; - int count; - - if (!str || !old || !new) - return NULL; - len_old = strlen(old); - if (len_old == 0) - return NULL; - len_new = strlen(new); - - ins = str; - for (count = 0; (tmp = strstr(ins, old)); ++count) { - ins = tmp + len_old; - } - - len_result = strlen(str) + (len_new - len_old) * count + 1; - tmp = result = malloc(len_result); - if (!result) - return NULL; - - while (count--) { - ins = strstr(str, old); - len_front = ins - str; - tmp = strncpy(tmp, str, len_front) + len_front; - strlcpy(tmp, new, len_result); - tmp += len_new; - str += len_front + len_old; - } - strlcpy(tmp, str, len_result); - return result; -} - diff --git a/src/string-util.h b/src/string-util.h deleted file mode 100644 index 2625765..0000000 --- a/src/string-util.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __STRING_UTIL_H -#define __STRING_UTIL_H - -#include - -size_t strlcpy(char *dst, const char *src, size_t siz); -size_t strlcat(char *dst, const char *src, size_t siz); -char *str_replace(const char *str, const char *old, const char *new); - -#endif diff --git a/src/timeline.c b/src/timeline.c deleted file mode 100644 index 4cd9fe0..0000000 --- a/src/timeline.c +++ /dev/null @@ -1,118 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include "timeline.h" -#include "auth.h" -#include "config.h" -#include "http.h" -#include "string-util.h" -#include "log.h" - -#define TIMELINE_URL "https://%s/api/v1/timelines/home" - -static char *get_timeline_url(const char *domain) -{ - char *url; - size_t size; - - size = strlen(TIMELINE_URL) + strlen(domain) - 1; - url = malloc(size); - if (!url) { - err(1, NULL); - return NULL; - } - - sprintf(url, TIMELINE_URL, domain); - return url; -} - -struct timeline *timeline_from_json(const char *json_data) -{ - struct timeline *t; - json_t *root; - json_error_t error; - - root = json_loads(json_data, 0, &error); - if (!root) { - log_msg(LOG_WARNING, "timeline_from_json", "json root it null"); - return NULL; - } - - if (!json_is_array(root)) { - log_msg(LOG_WARNING, "timeline_from_json", "json root is not array"); - json_decref(root); - return NULL; - } - - t = calloc(1, sizeof(struct timeline)); - if (!t) { - err(1, NULL); - json_decref(root); - return NULL; - } - - t->size = json_array_size(root); - t->statuses = calloc(t->size, sizeof(struct status *)); - if (!(t->statuses)) { - err(1, NULL); - timeline_free(t); - json_decref(root); - return NULL; - } - - json_t *data; - for (size_t i = 0; i < t->size; i++) { - data = json_array_get(root, i); - if (!data) - goto error; - t->statuses[i] = status_from_json_t(data); - if (!(t->statuses[i])) - goto error; - } - json_decref(root); - return t; - -error: - timeline_free(t); - json_decref(root); - return NULL; -} - -void timeline_free(struct timeline *t) -{ - for (size_t i = 0; i < t->size; i++) { - if (t->statuses[i]) - status_free(t->statuses[i]); - } - free(t->statuses); - free(t); -} - -struct timeline *get_timeline(const char *max_id, const char *since_id, const char *min_id, - int limit) -{ - char *url, *resp; - struct timeline *t; - - url = get_timeline_url(config->instance_url); - if (!url) { - err(1, "get_timeline"); - return NULL; - } - - resp = get_request(url); - free(url); - if (!resp) { - return NULL; - } - - t = timeline_from_json(resp); - free(resp); - if (!t) { - return NULL; - } - - return t; -} diff --git a/src/timeline.h b/src/timeline.h deleted file mode 100644 index 296bbe8..0000000 --- a/src/timeline.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef __TIMELINE_H -#define __TIMELINE_H - -#include "status.h" - -struct timeline { - struct status **statuses; - size_t size; -}; - -struct timeline *timeline_from_json(const char *); -void timeline_free(struct timeline *); -struct timeline *get_timeline(const char *max_id, const char *since_id, const char *min_id, - int limit); - -#endif -- cgit v1.2.3