rewrite the shit
This commit is contained in:
parent
770d846206
commit
bce150bdcf
9 changed files with 119 additions and 131 deletions
1
.defcon.txt
Normal file
1
.defcon.txt
Normal file
|
|
@ -0,0 +1 @@
|
|||
5
|
||||
12
.vscode/settings.json
vendored
Normal file
12
.vscode/settings.json
vendored
Normal file
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
BIN
defcon
Executable file
BIN
defcon
Executable file
Binary file not shown.
BIN
defconv2
Executable file
BIN
defconv2
Executable file
Binary file not shown.
141
main.c
141
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 <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;
|
||||
}
|
||||
#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();
|
||||
}
|
||||
10
src/cache.h
Normal file
10
src/cache.h
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
10
src/globals.h
Normal file
10
src/globals.h
Normal file
|
|
@ -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"
|
||||
42
src/network.h
Normal file
42
src/network.h
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#include <curl/curl.h>
|
||||
#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;
|
||||
}
|
||||
}
|
||||
34
src/output.h
Normal file
34
src/output.h
Normal file
|
|
@ -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);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue