defcon_chan/main.c
2024-10-02 19:22:39 +00:00

146 lines
No EOL
5.2 KiB
C

/*
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
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#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;
}
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;
}