From d1a5d31b1157042f0f54f36a6d826f79b5cb446e Mon Sep 17 00:00:00 2001 From: James Shiffer <2191476+scoliono@users.noreply.github.com> Date: Tue, 23 Mar 2021 01:02:39 -0700 Subject: [PATCH] initial commit --- FemMonitor/booru.c | 163 +++++++++++++++++++++++++++++++++++++++++++ FemMonitor/booru.h | 20 ++++++ FemMonitor/company.c | 9 +++ FemMonitor/company.h | 15 ++++ FemMonitor/howfeed.c | 9 +++ FemMonitor/howfeed.h | 15 ++++ FemMonitor/main.c | 111 +++++++++++++++++++++++++++++ FemMonitor/wiki.c | 9 +++ FemMonitor/wiki.h | 15 ++++ README.md | 17 +++++ 10 files changed, 383 insertions(+) create mode 100644 FemMonitor/booru.c create mode 100644 FemMonitor/booru.h create mode 100644 FemMonitor/company.c create mode 100644 FemMonitor/company.h create mode 100644 FemMonitor/howfeed.c create mode 100644 FemMonitor/howfeed.h create mode 100644 FemMonitor/main.c create mode 100644 FemMonitor/wiki.c create mode 100644 FemMonitor/wiki.h create mode 100644 README.md diff --git a/FemMonitor/booru.c b/FemMonitor/booru.c new file mode 100644 index 0000000..c25bb97 --- /dev/null +++ b/FemMonitor/booru.c @@ -0,0 +1,163 @@ +// +// booru.c +// FemMonitor +// +// Created by James Shiffer on 3/22/21. +// Copyright © 2021 FemboyFinancial. All rights reserved. +// + +#include +#include +#include +#include +#include + +#include "booru.h" + +#define START_ROW 2 +#define START_COL 2 + +xmlDoc* doc = NULL; +WINDOW* window = NULL; +CURL* curl = NULL; +int width = 0; +int height = 0; + +struct MemoryStruct +{ + char *memory; + size_t size; +}; + +static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) +{ + size_t realsize = size * nmemb; + struct MemoryStruct *mem = (struct MemoryStruct *)userp; + + char *ptr = realloc(mem->memory, mem->size + realsize + 1); + if (ptr == NULL) + { + /* out of memory! */ + printf("not enough memory (realloc returned NULL)\n"); + return 0; + } + + mem->memory = ptr; + memcpy(&(mem->memory[mem->size]), contents, realsize); + mem->size += realsize; + mem->memory[mem->size] = 0; + + return realsize; +} + +void booru_init(WINDOW* win, int h, int w) +{ + // setup curl + curl = curl_easy_init(); + width = w; + height = h; + + window = win; + mvwprintw(window, 0, 1, " Fembooru.jp "); + mvwprintw(window, START_ROW, START_COL, "Pinging..."); + if (curl == NULL) + { + wattron(window, COLOR_PAIR(1)); + mvwprintw(window, START_ROW, START_COL, "ERROR"); + mvwprintw(window, START_ROW+1, START_COL, "Failed to initialize libcurl!"); + wattroff(window, COLOR_PAIR(1)); + curl_global_cleanup(); + } + wrefresh(window); +} + +void* booru_refresh(void* arg) +{ + curl_easy_reset(curl); + + struct MemoryStruct chunk; + chunk.memory = malloc(1); /* will be grown as needed by the realloc above */ + chunk.size = 0; /* no data at this point */ + curl_easy_setopt(curl, CURLOPT_URL, "http://fembooru.jp/api/danbooru/find_posts"); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); + + CURLcode res = curl_easy_perform(curl); + int* status = malloc(sizeof(int)); + + // clear status lines + for (int i = START_ROW; i < START_ROW+1; ++i) + { + for (int j = START_COL; j < width - 1; ++j) + mvwprintw(window, i, j, " "); + } + + if (res != CURLE_OK) + { + wattron(window, COLOR_PAIR(2)); + if (res == CURLE_COULDNT_CONNECT) + { + mvwprintw(window, START_ROW, START_COL, "OFFLINE", res); + } + else + { + mvwprintw(window, START_ROW, START_COL, "ERROR"); + mvwprintw(window, START_ROW+1, START_COL, "CURL status %d", res); + } + wattroff(window, COLOR_PAIR(2)); + wrefresh(window); + *status = 1; + pthread_exit(status); + } + + doc = xmlReadMemory(chunk.memory, chunk.size, "noname.xml", NULL, 0); + if (doc == NULL) + { + wattron(window, COLOR_PAIR(2)); + mvwprintw(window, START_ROW, START_COL, "ERROR"); + mvwprintw(window, START_ROW+1, START_COL, "Server gave empty response"); + wattroff(window, COLOR_PAIR(2)); + wrefresh(window); + *status = 2; + pthread_exit(status); + } + + xmlNode* el = xmlDocGetRootElement(doc); + while (el->type != XML_ELEMENT_NODE || strcmp(el->name, "posts") != 0) + el = el->next; + + el = el->children; + while (el->type != XML_ATTRIBUTE_NODE || strcmp(el->name, "count") != 0) + el = el->next; + + if (el == NULL) + { + wattron(window, COLOR_PAIR(1)); + mvwprintw(window, START_ROW, START_COL, "ONLINE"); + wattroff(window, COLOR_PAIR(1)); + wattron(window, COLOR_PAIR(3)); + mvwprintw(window, START_ROW+1, START_COL, "Couldn't retrieve post count"); + wattroff(window, COLOR_PAIR(3)); + wrefresh(window); + *status = 3; + pthread_exit(status); + } + + // we have arrived at the count attribute of the posts tag + wattron(window, COLOR_PAIR(1)); + mvwprintw(window, START_ROW, START_COL, "ONLINE"); + wattroff(window, COLOR_PAIR(1)); + mvwprintw(window, START_ROW+1, START_COL, "%s posts", el->content); + wrefresh(window); + + *status = 0; + pthread_exit(status); +} + +void booru_destroy() +{ + if (doc != NULL) + xmlFreeDoc(doc); + if (curl != NULL) + curl_easy_cleanup(curl); +} diff --git a/FemMonitor/booru.h b/FemMonitor/booru.h new file mode 100644 index 0000000..b09ee15 --- /dev/null +++ b/FemMonitor/booru.h @@ -0,0 +1,20 @@ +// +// booru.h +// FemMonitor +// +// Created by James Shiffer on 3/22/21. +// Copyright © 2021 FemboyFinancial. All rights reserved. +// + +#ifndef booru_h +#define booru_h + +#include +#include + +void booru_init(WINDOW* win, int w, int h); +void* booru_refresh(void* arg); +/* Note: this function will not free the window! */ +void booru_destroy(void); + +#endif /* booru_h */ diff --git a/FemMonitor/company.c b/FemMonitor/company.c new file mode 100644 index 0000000..64b6de5 --- /dev/null +++ b/FemMonitor/company.c @@ -0,0 +1,9 @@ +// +// company.c +// FemMonitor +// +// Created by James Shiffer on 3/22/21. +// Copyright © 2021 FemboyFinancial. All rights reserved. +// + +#include "company.h" diff --git a/FemMonitor/company.h b/FemMonitor/company.h new file mode 100644 index 0000000..b7f9362 --- /dev/null +++ b/FemMonitor/company.h @@ -0,0 +1,15 @@ +// +// company.h +// FemMonitor +// +// Created by James Shiffer on 3/22/21. +// Copyright © 2021 FemboyFinancial. All rights reserved. +// + +#ifndef company_h +#define company_h + +#include + + +#endif /* company_h */ diff --git a/FemMonitor/howfeed.c b/FemMonitor/howfeed.c new file mode 100644 index 0000000..a041595 --- /dev/null +++ b/FemMonitor/howfeed.c @@ -0,0 +1,9 @@ +// +// howfeed.c +// FemMonitor +// +// Created by James Shiffer on 3/22/21. +// Copyright © 2021 FemboyFinancial. All rights reserved. +// + +#include "howfeed.h" diff --git a/FemMonitor/howfeed.h b/FemMonitor/howfeed.h new file mode 100644 index 0000000..ca331ea --- /dev/null +++ b/FemMonitor/howfeed.h @@ -0,0 +1,15 @@ +// +// howfeed.h +// FemMonitor +// +// Created by James Shiffer on 3/22/21. +// Copyright © 2021 FemboyFinancial. All rights reserved. +// + +#ifndef howfeed_h +#define howfeed_h + +#include + + +#endif /* howfeed_h */ diff --git a/FemMonitor/main.c b/FemMonitor/main.c new file mode 100644 index 0000000..34f30fd --- /dev/null +++ b/FemMonitor/main.c @@ -0,0 +1,111 @@ +// +// main.c +// FemMonitor +// +// Created by James Shiffer on 3/21/21. +// Copyright © 2021 FemboyFinancial. All rights reserved. +// + +#include +#include +#include +#include +#include + +#include "booru.h" +#include "company.h" +#include "howfeed.h" +#include "wiki.h" + +#define KEY_QUIT 'q' + + +WINDOW* create_newwin(int height, int width, int starty, int startx) +{ + WINDOW* local_win; + + local_win = newwin(height, width, starty, startx); + box(local_win, 0, 0); + wrefresh(local_win); + + return local_win; +} + +void destroy_win(WINDOW* local_win) +{ + /* box(local_win, ' ', ' '); : This won't produce the desired + * result of erasing the window. It will leave its four corners, + * an ugly remnant of the window. + */ + wborder(local_win, ' ', ' ', ' ',' ',' ',' ',' ',' '); + wrefresh(local_win); + delwin(local_win); +} + +int main(int argc, const char* argv[]) +{ + LIBXML_TEST_VERSION + + curl_global_init(CURL_GLOBAL_DEFAULT); + + // setup ncurses + initscr(); + halfdelay(20); + keypad(stdscr, TRUE); + noecho(); + refresh(); + + start_color(); + init_pair(1, COLOR_GREEN, COLOR_BLACK); + init_pair(2, COLOR_RED, COLOR_BLACK); + init_pair(3, COLOR_YELLOW, COLOR_BLACK); + + // setup various status windows + WINDOW* topleft = create_newwin(LINES/2, COLS/2, 0, 0); + WINDOW* topright = create_newwin(LINES/2, COLS/2, 0, COLS/2); + WINDOW* botleft = create_newwin(LINES/2 - 1, COLS/2, LINES/2, 0); + WINDOW* botright = create_newwin(LINES/2 - 1, COLS/2, LINES/2, COLS/2); + + booru_init(botleft, LINES/2 - 1, COLS/2); + mvwprintw(topleft, 0, 1, " Company "); + mvwprintw(topright, 0, 1, " Howfeed.biz "); + mvwprintw(botright, 0, 1, " FemWiki "); + + wrefresh(topleft); + wrefresh(topright); + wrefresh(botright); + + // start refresh status threads + pthread_t booru_th; + pthread_create(&booru_th, NULL, booru_refresh, NULL); + + time_t t; + struct tm tm; + char ch; + while ((ch = getch()) != KEY_QUIT) + { + time(&t); + tm = *localtime(&t); + mvprintw(LINES - 1, 0, "Last updated: %d-%02d-%02d %02d:%02d:%02d\n", + tm.tm_year + 1900, + tm.tm_mon + 1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec); + wrefresh(stdscr); + } + + // cleanup everything + booru_destroy(); + + destroy_win(topleft); + destroy_win(topright); + destroy_win(botleft); + destroy_win(botright); + + endwin(); + xmlCleanupParser(); + curl_global_cleanup(); + return 0; +} diff --git a/FemMonitor/wiki.c b/FemMonitor/wiki.c new file mode 100644 index 0000000..47df0a0 --- /dev/null +++ b/FemMonitor/wiki.c @@ -0,0 +1,9 @@ +// +// wiki.c +// FemMonitor +// +// Created by James Shiffer on 3/22/21. +// Copyright © 2021 FemboyFinancial. All rights reserved. +// + +#include "wiki.h" diff --git a/FemMonitor/wiki.h b/FemMonitor/wiki.h new file mode 100644 index 0000000..cf0b58c --- /dev/null +++ b/FemMonitor/wiki.h @@ -0,0 +1,15 @@ +// +// wiki.h +// FemMonitor +// +// Created by James Shiffer on 3/22/21. +// Copyright © 2021 FemboyFinancial. All rights reserved. +// + +#ifndef wiki_h +#define wiki_h + +#include + + +#endif /* wiki_h */ diff --git a/README.md b/README.md new file mode 100644 index 0000000..df7fdfd --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# FemMonitor + +Lightweight application written in C that monitors the status of various FemboyFinancial services. + +Best viewed on a wall-mounted 4:3 monitor with a VGA font. + +## Dependencies + +- libncurses6 + +- libcurl4 + +- libxml2.2 + +## Makefile + +no im developing on xcode rn lol