diff --git a/LICENSE.txt b/LICENSE.txt old mode 100644 new mode 100755 diff --git a/Makefile b/Makefile old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/neocities b/neocities new file mode 100755 index 0000000..2a1b388 Binary files /dev/null and b/neocities differ diff --git a/src/0 b/src/0 new file mode 100755 index 0000000..e69de29 diff --git a/src/info.c b/src/info.c new file mode 100644 index 0000000..0b7ac12 --- /dev/null +++ b/src/info.c @@ -0,0 +1,84 @@ +int fetch_neocities_info() { + const char *user = getenv("NEOCITIES_USER"); + const char *pass = getenv("NEOCITIES_PASS"); + if (!user || !pass) { + fprintf(stderr, "Variáveis NEOCITIES_USER ou NEOCITIES_PASS não definidas!\n"); + return 1; + } + + char auth[256]; + snprintf(auth, sizeof(auth), "%s:%s", user, pass); + + CURL *curl = curl_easy_init(); + if (!curl) { + fprintf(stderr, "Erro ao inicializar cURL\n"); + return 1; + } + + char infourl[256]; + snprintf(infourl, sizeof(infourl), "https://neocities.org/api/info?sitename=%s", user); + + curl_easy_setopt(curl, CURLOPT_URL, infourl); + curl_easy_setopt(curl, CURLOPT_USERPWD, auth); + + struct response resp = { .data = malloc(1), .len = 0 }; + resp.data[0] = '\0'; + + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp); + + CURLcode res = curl_easy_perform(curl); + if (res != CURLE_OK) { + fprintf(stderr, "Erro na requisição: %s\n", curl_easy_strerror(res)); + curl_easy_cleanup(curl); + free(resp.data); + return 1; + } + curl_easy_cleanup(curl); + + json_error_t error; + json_t *obj = json_loads(resp.data, 0, &error); + free(resp.data); + + if (!obj) { + fprintf(stderr, "Erro ao parsear JSON: %s\n", error.text); + return 1; + } + + json_t *result_obj = json_object_get(obj, "result"); + const char *result_str = json_string_value(result_obj); + + if (strcmp(result_str, "error") == 0) { + const char *err_type = json_string_value(json_object_get(obj, "error_type")); + const char *err_msg = json_string_value(json_object_get(obj, "message")); + + if (strcmp(err_type, "invalid_auth") == 0) { + printf("Usuário ou senha incorretos!\n"); + } else { + printf("Erro! %s\n", err_msg); + } + json_decref(obj); + return 1; + } + + if (strcmp(result_str, "success") == 0) { + json_t *info_obj = json_object_get(obj, "info"); + printf("\nSitename: %s\n", json_string_value(json_object_get(info_obj, "sitename"))); + printf("Hits: %d\n", (int)json_integer_value(json_object_get(info_obj, "hits"))); + printf("Created at: %s\n", json_string_value(json_object_get(info_obj, "created_at"))); + printf("Last updated: %s\n", json_string_value(json_object_get(info_obj, "last_updated"))); + printf("Domain: %s\n", json_string_value(json_object_get(info_obj, "domain"))); + + json_t *tags = json_object_get(info_obj, "tags"); + printf("Tags: "); + size_t index; + json_t *tag; + json_array_foreach(tags, index, tag) { + printf("#%s%s", json_string_value(tag), (index < json_array_size(tags) - 1) ? ", " : "."); + } + printf("\n\n"); + } + + json_decref(obj); + return 0; +} \ No newline at end of file diff --git a/src/info.h b/src/info.h new file mode 100644 index 0000000..4096c3f --- /dev/null +++ b/src/info.h @@ -0,0 +1,6 @@ +#ifndef INFO +#define INFO + +int fetch_neocities_info(void); + +#endif \ No newline at end of file diff --git a/src/login.sh b/src/login.sh new file mode 100644 index 0000000..e69de29 diff --git a/src/main.c b/src/main.c old mode 100644 new mode 100755 index 93f28c8..685086a --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,9 @@ #include #include +#include "main.h" +#include "info.h" + struct response { char *data; size_t len; @@ -25,91 +28,6 @@ size_t write_callback(void *data, size_t size, size_t nmemb, void *userdata) { return chunk_size; } -int fetch_neocities_info() { - const char *user = getenv("NEOCITIES_USER"); - const char *pass = getenv("NEOCITIES_PASS"); - if (!user || !pass) { - fprintf(stderr, "Variáveis NEOCITIES_USER ou NEOCITIES_PASS não definidas!\n"); - return 1; - } - - char auth[256]; - snprintf(auth, sizeof(auth), "%s:%s", user, pass); - - CURL *curl = curl_easy_init(); - if (!curl) { - fprintf(stderr, "Erro ao inicializar cURL\n"); - return 1; - } - - char infourl[256]; - snprintf(infourl, sizeof(infourl), "https://neocities.org/api/info?sitename=%s", user); - - curl_easy_setopt(curl, CURLOPT_URL, infourl); - curl_easy_setopt(curl, CURLOPT_USERPWD, auth); - - struct response resp = { .data = malloc(1), .len = 0 }; - resp.data[0] = '\0'; - - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &resp); - - CURLcode res = curl_easy_perform(curl); - if (res != CURLE_OK) { - fprintf(stderr, "Erro na requisição: %s\n", curl_easy_strerror(res)); - curl_easy_cleanup(curl); - free(resp.data); - return 1; - } - curl_easy_cleanup(curl); - - json_error_t error; - json_t *obj = json_loads(resp.data, 0, &error); - free(resp.data); - - if (!obj) { - fprintf(stderr, "Erro ao parsear JSON: %s\n", error.text); - return 1; - } - - json_t *result_obj = json_object_get(obj, "result"); - const char *result_str = json_string_value(result_obj); - - if (strcmp(result_str, "error") == 0) { - const char *err_type = json_string_value(json_object_get(obj, "error_type")); - const char *err_msg = json_string_value(json_object_get(obj, "message")); - - if (strcmp(err_type, "invalid_auth") == 0) { - printf("Usuário ou senha incorretos!\n"); - } else { - printf("Erro! %s\n", err_msg); - } - json_decref(obj); - return 1; - } - - if (strcmp(result_str, "success") == 0) { - json_t *info_obj = json_object_get(obj, "info"); - printf("\nSitename: %s\n", json_string_value(json_object_get(info_obj, "sitename"))); - printf("Hits: %d\n", (int)json_integer_value(json_object_get(info_obj, "hits"))); - printf("Created at: %s\n", json_string_value(json_object_get(info_obj, "created_at"))); - printf("Last updated: %s\n", json_string_value(json_object_get(info_obj, "last_updated"))); - printf("Domain: %s\n", json_string_value(json_object_get(info_obj, "domain"))); - - json_t *tags = json_object_get(info_obj, "tags"); - printf("Tags: "); - size_t index; - json_t *tag; - json_array_foreach(tags, index, tag) { - printf("#%s%s", json_string_value(tag), (index < json_array_size(tags) - 1) ? ", " : "."); - } - printf("\n\n"); - } - - json_decref(obj); - return 0; -} - int main(int argc, char *argv[]) { printf("\nNeocities C CLI\n"); diff --git a/src/main.h b/src/main.h new file mode 100644 index 0000000..1f34f56 --- /dev/null +++ b/src/main.h @@ -0,0 +1,7 @@ +#ifndef MAIN_H +#define MAIN_H + +extern const char *user; +extern const char *pass; + +#endif