initial commit

This commit is contained in:
James Shiffer 2021-03-23 01:02:39 -07:00
commit d1a5d31b11
No known key found for this signature in database
GPG Key ID: C0DB8774A1B3BA45
10 changed files with 383 additions and 0 deletions

163
FemMonitor/booru.c Normal file
View File

@ -0,0 +1,163 @@
//
// booru.c
// FemMonitor
//
// Created by James Shiffer on 3/22/21.
// Copyright © 2021 FemboyFinancial. All rights reserved.
//
#include <curl/curl.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <pthread.h>
#include <string.h>
#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);
}

20
FemMonitor/booru.h Normal file
View File

@ -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 <ncurses.h>
#include <curl/curl.h>
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 */

9
FemMonitor/company.c Normal file
View File

@ -0,0 +1,9 @@
//
// company.c
// FemMonitor
//
// Created by James Shiffer on 3/22/21.
// Copyright © 2021 FemboyFinancial. All rights reserved.
//
#include "company.h"

15
FemMonitor/company.h Normal file
View File

@ -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 <curl/curl.h>
#endif /* company_h */

9
FemMonitor/howfeed.c Normal file
View File

@ -0,0 +1,9 @@
//
// howfeed.c
// FemMonitor
//
// Created by James Shiffer on 3/22/21.
// Copyright © 2021 FemboyFinancial. All rights reserved.
//
#include "howfeed.h"

15
FemMonitor/howfeed.h Normal file
View File

@ -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 <curl/curl.h>
#endif /* howfeed_h */

111
FemMonitor/main.c Normal file
View File

@ -0,0 +1,111 @@
//
// main.c
// FemMonitor
//
// Created by James Shiffer on 3/21/21.
// Copyright © 2021 FemboyFinancial. All rights reserved.
//
#include <ncurses.h>
#include <curl/curl.h>
#include <libxml/parser.h>
#include <pthread.h>
#include <time.h>
#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;
}

9
FemMonitor/wiki.c Normal file
View File

@ -0,0 +1,9 @@
//
// wiki.c
// FemMonitor
//
// Created by James Shiffer on 3/22/21.
// Copyright © 2021 FemboyFinancial. All rights reserved.
//
#include "wiki.h"

15
FemMonitor/wiki.h Normal file
View File

@ -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 <curl/curl.h>
#endif /* wiki_h */

17
README.md Normal file
View File

@ -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