diff --git a/.defcon.txt b/.defcon.txt new file mode 100644 index 0000000..7813681 --- /dev/null +++ b/.defcon.txt @@ -0,0 +1 @@ +5 \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..96dc984 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,12 @@ +{ + "files.associations": { + "string.h": "c", + "output.h": "c", + "curl.h": "c", + "unistd.h": "c", + "stdlib.h": "c", + "colors.h": "c", + "stdio.h": "c", + "globals.h": "c" + } +} \ No newline at end of file diff --git a/defcon b/defcon new file mode 100755 index 0000000..8e458a7 Binary files /dev/null and b/defcon differ diff --git a/defconv2 b/defconv2 new file mode 100755 index 0000000..1043c8b Binary files /dev/null and b/defconv2 differ diff --git a/main.c b/main.c index b21cd14..177b8b3 100644 --- a/main.c +++ b/main.c @@ -1,146 +1,25 @@ /* defcon-chan: a simple C program that fetches current/last known DEFCON level into your terminal! - requirements: libcurl - compiling w/ GCC: gcc main.c -o defcon -lcurl - running: ./defcon - author: hornetmaidan */ +#define FILENAME "/tmp/defcon.txt" +#define URL "https://defconwarningsystem.com/code.dat" + #include #include #include #include -#include - -#define URL "https://defconwarningsystem.com/code.dat" -#define FILENAME ".defcon.txt" // saves the last known DEFCON level into a local file(hidden for aesthetic purposes) - -// ANSI colors -#define COLOR_RESET "\033[0m" -#define COLOR_RED "\033[31m" -#define COLOR_GREEN "\033[32m" -#define COLOR_YELLOW "\033[33m" -#define COLOR_BLUE "\033[34m" -#define COLOR_MAGENTA "\033[35m" -#define COLOR_CYAN "\033[36m" -#define COLOR_WHITE "\033[37m" -#define COLOR_BLINK "\033[5m" - -// write callback function for curl handler - -size_t write_data(void *ptr1, size_t size, size_t nmemb, FILE *stream) { - size_t written = fwrite(ptr1, size, nmemb, stream); - return written; -} +#include "src/globals.h" +#include "src/cache.h" +#include "src/network.h" +#include "src/output.h" int main(void) { - - CURL *curl; - CURLcode result; - FILE *fp; - - char last_known[256] = "N/A"; - char failsafe[256] = "N/A"; - - curl = curl_easy_init(); - - // load cached DEFCON level into the buffer, also check for file access - if (access(FILENAME, F_OK) != -1) { - fp = fopen(FILENAME, "rb"); - if (fp != NULL) { - if (fgets(last_known, sizeof(last_known), fp) != NULL) { - // remove newline char - size_t len = strlen(last_known); - if (len > 0 && last_known[len - 1] == '\n') { - last_known[len - 1] = '\0'; - } - strncpy(failsafe, last_known, sizeof(failsafe)); - failsafe[sizeof(failsafe) - 1] = '\0'; // ensure null termination - } else { - fprintf(stderr, "%serror reading from file :(%s\n", COLOR_MAGENTA, COLOR_RESET); - } - - fclose(fp); - } else { - fprintf(stderr, "%sfailed to open file :9%s\n", COLOR_MAGENTA, COLOR_RESET); - } - } else { - fprintf(stderr, "%sno cache file found, creating...%s\n", COLOR_MAGENTA, COLOR_RESET); - } - - - fp = fopen(FILENAME, "w"); - // a lil bit of error handling here and there - - if (curl == NULL) { - fprintf(stderr, "%sHTTP request failed :(%s\n", COLOR_MAGENTA, COLOR_RESET); - curl_easy_cleanup(curl); - } - // fun begins - fprintf(stdout, "%s----------------------------------------------------------------------\n", COLOR_GREEN); - fprintf(stdout, "defcon-chan is at your service! getting the current DEFCON level desu~\n"); - // setting options for curl - curl_easy_setopt(curl, CURLOPT_URL, URL); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); - result = curl_easy_perform(curl); // actual request performing - curl_easy_cleanup(curl); - - if (result != CURLE_OK) { - fprintf(stderr, "%serror: %s%s\n", COLOR_MAGENTA, curl_easy_strerror(result), COLOR_RESET); - fprintf(stdout, "%sservice is currently unreachable, the last DEFCON level is: %s%s\n", COLOR_CYAN, failsafe, COLOR_RESET); - fp = fopen(FILENAME, "w"); - fprintf(fp, "%s", failsafe); - fclose(fp); - return -1; - - } else { - fflush(fp); - fclose(fp); - fp = fopen(FILENAME, "r"); // i don't know if there's a better way to do this but since there are only 5 conditions - why not? - if (fgets(last_known, sizeof(last_known), fp) != NULL) { - if (strcmp(last_known, "5") == 0) - { - fprintf(stdout, "the current DEFCON level is: %s%s%s - FADE OUT%s%s\n", COLOR_BLUE, COLOR_BLINK, last_known, COLOR_RESET, COLOR_RESET); - } - else if (strcmp(last_known, "4") == 0) - { - fprintf(stdout, "the current DEFCON level is: %s%s%s - DOUBLE TAKE%s%s\n", COLOR_GREEN, COLOR_BLINK, last_known, COLOR_RESET, COLOR_RESET); - } - else if (strcmp(last_known, "3") == 0) - { - fprintf(stdout, "the current DEFCON level is: %s%s%s - ROUND HOUSE%s%s\n", COLOR_YELLOW, COLOR_BLINK, last_known, COLOR_RESET, COLOR_RESET); - } - else if (strcmp(last_known, "2") == 0) - { - fprintf(stdout, "the current DEFCON level is: %s%s%s - FAST PACE%s%s\n", COLOR_RED, COLOR_BLINK, last_known, COLOR_RESET, COLOR_RESET); - } - else if (strcmp(last_known, "1") == 0) - { - fprintf(stdout, "the current DEFCON level is: %s%s%s - COCKED PISTOL%s%s\n", COLOR_WHITE, COLOR_BLINK, last_known, COLOR_RESET, COLOR_RESET); - fprintf(stdout, "%%ssWHAT ARE YOU DOING? RUN FOR YOUR LIFE!%s%s\n", COLOR_MAGENTA, COLOR_BLINK, COLOR_RESET, COLOR_RESET); - } - else - { - fprintf(stderr, "%serror reading the DEFCON level from the file%s\n", COLOR_RED, COLOR_RESET); - } - } - fclose(fp); - fp = fopen(FILENAME, "w"); - if (strcmp(last_known, "N/A") == 0) { - fprintf(fp, "%s", failsafe); // writing the last safe DEFCON value ever known - fprintf(stdout, "failsafe engaged with level of %s", failsafe); - } - else { - fprintf(fp, "%s", last_known); - } - } - fclose(fp); - fprintf(stdout, "%s----------------------------------------------------------------------%s\n", COLOR_GREEN, COLOR_RESET); - - return 0; + check_cache(); + query_defcon(); + print_output(); } \ No newline at end of file diff --git a/src/cache.h b/src/cache.h new file mode 100644 index 0000000..5f8794b --- /dev/null +++ b/src/cache.h @@ -0,0 +1,10 @@ +#include "globals.h" + +int check_cache(void) { + if (access(FILENAME, F_OK) != 0) { + fprintf(stdout, "%scould not open cache file, creating...%s\n", COLOR_MAGENTA, COLOR_RESET); + FILE *fp = fopen(FILENAME, "w"); + fputc('5', fp); + fclose(fp); + } +} \ No newline at end of file diff --git a/src/globals.h b/src/globals.h new file mode 100644 index 0000000..033270e --- /dev/null +++ b/src/globals.h @@ -0,0 +1,10 @@ +// ANSI colors +#define COLOR_RESET "\033[0m" +#define COLOR_RED "\033[31m" +#define COLOR_GREEN "\033[32m" +#define COLOR_YELLOW "\033[33m" +#define COLOR_BLUE "\033[34m" +#define COLOR_MAGENTA "\033[35m" +#define COLOR_CYAN "\033[36m" +#define COLOR_WHITE "\033[37m" +#define COLOR_BLINK "\033[5m" \ No newline at end of file diff --git a/src/network.h b/src/network.h new file mode 100644 index 0000000..f20d37d --- /dev/null +++ b/src/network.h @@ -0,0 +1,42 @@ +#include +#include "globals.h" + +char failsafe; + +size_t write_data(void *ptr1, size_t size, size_t nmemb, FILE *stream) { + size_t written = fwrite(ptr1, size, nmemb, stream); + return written; + return 0; +} + +int load_failsafe(){ + FILE *fp = fopen(FILENAME, "w"); + failsafe = fgetc(fp); + fclose(fp); +} + +int query_defcon() { + + CURL *curl; + CURLcode result; + + load_failsafe(); + + FILE *fp = fopen(FILENAME, "w"); + curl = curl_easy_init(); + + curl_easy_setopt(curl, CURLOPT_URL, URL); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + result = curl_easy_perform(curl); // actual request performing + curl_easy_cleanup(curl); + fprintf(stdout, "%scurl success!%s\n", COLOR_GREEN, COLOR_RESET); + + if (result != CURLE_OK) { + fprintf(stderr, "%serror: %s%s\n", COLOR_MAGENTA, curl_easy_strerror(result), COLOR_RESET); + fprintf(stdout, "%sservice is currently unreachable, the last DEFCON level is: %s%s\n", COLOR_CYAN, failsafe, COLOR_RESET); + fprintf(fp, "%s", failsafe); + fclose(fp); + return 1; + } +} \ No newline at end of file diff --git a/src/output.h b/src/output.h new file mode 100644 index 0000000..ec9adee --- /dev/null +++ b/src/output.h @@ -0,0 +1,34 @@ +#include "globals.h" + +int print_output() { + + FILE *fp = fopen(FILENAME, "r"); + int ch = fgetc(fp); + char last_known[2]; + last_known[0] = (char)ch; + last_known[1] = '\0'; + fclose(fp); + fprintf(stdout, "%c", last_known); + + if (strcmp(last_known, "5") == 0) { + fprintf(stdout, "the current DEFCON level is: %s%s%s - FADE OUT%s%s\n", COLOR_BLUE, COLOR_BLINK, last_known, COLOR_RESET, COLOR_RESET); + } else if (strcmp(last_known, "4") == 0) + { + fprintf(stdout, "the current DEFCON level is: %s%s%s - DOUBLE TAKE%s%s\n", COLOR_GREEN, COLOR_BLINK, last_known, COLOR_RESET, COLOR_RESET); + } else if (strcmp(last_known, "3") == 0) + { + fprintf(stdout, "the current DEFCON level is: %s%s%s - ROUND HOUSE%s%s\n", COLOR_YELLOW, COLOR_BLINK, last_known, COLOR_RESET, COLOR_RESET); + } else if (strcmp(last_known, "2") == 0) + { + fprintf(stdout, "the current DEFCON level is: %s%s%s - FAST PACE%s%s\n", COLOR_RED, COLOR_BLINK, last_known, COLOR_RESET, COLOR_RESET); + } else if (strcmp(last_known, "1") == 0) + { + fprintf(stdout, "the current DEFCON level is: %s%s%s - COCKED PISTOL%s%s\n", COLOR_WHITE, COLOR_BLINK, last_known, COLOR_RESET, COLOR_RESET); + fprintf(stdout, "%%ssWHAT ARE YOU DOING? RUN FOR YOUR LIFE!%s%s\n", COLOR_MAGENTA, COLOR_BLINK, COLOR_RESET, COLOR_RESET); + } else { + fprintf(stderr, "%serror reading the DEFCON level from the file%s\n", COLOR_RED, COLOR_RESET); + fprintf(stdout, "%s----------------------------------------------------------------------%s\n", COLOR_GREEN, COLOR_RESET); + return 1; + } + fprintf(stdout, "%s----------------------------------------------------------------------%s\n", COLOR_GREEN, COLOR_RESET); +}