Replace howfeed with mail status

This commit is contained in:
James S 2023-04-15 19:57:29 -07:00
parent d662e66e1a
commit b923bf76dd
10 changed files with 276 additions and 352 deletions

View File

@ -10,8 +10,7 @@
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <time.h>
#include "booru.h"
#include "common.h"
@ -59,108 +58,103 @@ void booru_init(WINDOW* win, int h, int w)
void* booru_refresh(void* arg)
{
for ( ; ; sleep(1))
{
// record the response time
struct timeval start, end;
gettimeofday(&start, NULL);
CURLcode res;
CURLcode res2;
struct MemoryStruct posts_data = geturl(curl, "http://fembooru.jp/api/danbooru/find_posts", &res);
struct MemoryStruct tags_data = geturl(curl, "http://fembooru.jp/api/danbooru/find_tags", &res2);
if (res != CURLE_OK || res2 != CURLE_OK)
{
on_curl_error(window, res);
goto cleanup;
}
gettimeofday(&end, NULL);
int index = iter++ % graph.width;
response_times[index] = (end.tv_sec - start.tv_sec) * 1000.0f + (end.tv_usec - start.tv_usec) / 1000.0f;
// clear status lines
for (int i = TITLE_START_COL - 1; i < TITLE_START_COL + 7; ++i)
mvwprintw(window, 0, i, " ");
for (int i = START_ROW; i < START_ROW+1; ++i)
{
for (int j = START_COL; j < width - 1; ++j)
mvwprintw(window, i, j, " ");
}
// clear graph if necessary
if (index == 0)
gclear(graph);
// print status graph
gdrawbar(graph, index, response_times[index] / 100.0f);
// get post count
doc = xmlReadMemory(posts_data.memory, posts_data.size, "noname.xml", NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
if (doc == NULL)
{
on_xml_error(window);
goto cleanup;
}
xmlNode* el = xmlDocGetRootElement(doc);
while (el != NULL && (el->type != XML_ELEMENT_NODE || strcmp(el->name, "posts") != 0))
el = el->next;
xmlAttr* attr = el == NULL ? NULL : el->properties;
while (attr != NULL && (attr->type != XML_ATTRIBUTE_NODE || strcmp(attr->name, "count") != 0))
attr = attr->next;
if (el == NULL || attr == NULL)
{
wattron(window, COLOR_PAIR(COLORS_SUCCESS));
mvwprintw(window, 0, TITLE_START_COL, " ONLINE ");
wattroff(window, COLOR_PAIR(COLORS_SUCCESS));
wattron(window, COLOR_PAIR(COLORS_WARNING));
mvwprintw(window, START_ROW, START_COL, "Couldn't retrieve post count");
wattroff(window, COLOR_PAIR(COLORS_WARNING));
wrefresh(window);
goto cleanup;
}
// we have arrived at the count attribute of the posts tag
wattron(window, COLOR_PAIR(COLORS_SUCCESS));
mvwprintw(window, 0, TITLE_START_COL, " ONLINE ");
wattroff(window, COLOR_PAIR(COLORS_SUCCESS));
mvwprintw(window, START_ROW, START_COL, "%s posts", attr->children->content);
xmlFreeDoc(doc);
// get first 3 tags
doc = xmlReadMemory(tags_data.memory, tags_data.size, "noname.xml", NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
if (doc == NULL)
{
on_xml_error(window);
goto cleanup;
}
el = xmlDocGetRootElement(doc)->children->next;
mvwprintw(window, START_ROW+1, START_COL, "Most recent tag:");
// find tag name
attr = el->properties;
while (attr != NULL && (attr->type != XML_ATTRIBUTE_NODE || strcmp(attr->name, "name") != 0))
attr = attr->next;
wattron(window, WA_BOLD);
mvwprintw(window, START_ROW+1, START_COL+16, " %s", attr->children->content);
wattroff(window, WA_BOLD);
// refresh view
wrefresh(window);
// record the response time
struct timeval start, end;
gettimeofday(&start, NULL);
cleanup:
if (doc != NULL)
xmlFreeDoc(doc);
free(posts_data.memory);
free(tags_data.memory);
}
CURLcode res;
CURLcode res2;
struct MemoryStruct posts_data = geturl(curl, "http://fembooru.jp/api/danbooru/find_posts", &res);
struct MemoryStruct tags_data = geturl(curl, "http://fembooru.jp/api/danbooru/find_tags", &res2);
if (res != CURLE_OK || res2 != CURLE_OK)
{
on_curl_error(window, res);
goto cleanup;
}
gettimeofday(&end, NULL);
int index = iter++ % graph.width;
response_times[index] = (end.tv_sec - start.tv_sec) * 1000.0f + (end.tv_usec - start.tv_usec) / 1000.0f;
// clear status lines
for (int i = TITLE_START_COL - 1; i < TITLE_START_COL + 7; ++i)
mvwprintw(window, 0, i, " ");
for (int i = START_ROW; i < START_ROW+1; ++i)
{
for (int j = START_COL; j < width - 1; ++j)
mvwprintw(window, i, j, " ");
}
// clear graph if necessary
if (index == 0)
gclear(graph);
// print status graph
gdrawbar(graph, index, response_times[index] / 100.0f);
// get post count
doc = xmlReadMemory(posts_data.memory, posts_data.size, "noname.xml", NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
if (doc == NULL)
{
on_xml_error(window);
goto cleanup;
}
xmlNode* el = xmlDocGetRootElement(doc);
while (el != NULL && (el->type != XML_ELEMENT_NODE || strcmp(el->name, "posts") != 0))
el = el->next;
xmlAttr* attr = el == NULL ? NULL : el->properties;
while (attr != NULL && (attr->type != XML_ATTRIBUTE_NODE || strcmp(attr->name, "count") != 0))
attr = attr->next;
if (el == NULL || attr == NULL)
{
wattron(window, COLOR_PAIR(COLORS_SUCCESS));
mvwprintw(window, 0, TITLE_START_COL, " ONLINE ");
wattroff(window, COLOR_PAIR(COLORS_SUCCESS));
wattron(window, COLOR_PAIR(COLORS_WARNING));
mvwprintw(window, START_ROW, START_COL, "Couldn't retrieve post count");
wattroff(window, COLOR_PAIR(COLORS_WARNING));
wrefresh(window);
goto cleanup;
}
// we have arrived at the count attribute of the posts tag
wattron(window, COLOR_PAIR(COLORS_SUCCESS));
mvwprintw(window, 0, TITLE_START_COL, " ONLINE ");
wattroff(window, COLOR_PAIR(COLORS_SUCCESS));
mvwprintw(window, START_ROW, START_COL, "%s posts", attr->children->content);
xmlFreeDoc(doc);
// get first 3 tags
doc = xmlReadMemory(tags_data.memory, tags_data.size, "noname.xml", NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
if (doc == NULL)
{
on_xml_error(window);
goto cleanup;
}
el = xmlDocGetRootElement(doc)->children->next;
mvwprintw(window, START_ROW+1, START_COL, "Most recent tag:");
// find tag name
attr = el->properties;
while (attr != NULL && (attr->type != XML_ATTRIBUTE_NODE || strcmp(attr->name, "name") != 0))
attr = attr->next;
wattron(window, WA_BOLD);
mvwprintw(window, START_ROW+1, START_COL+16, " %s", attr->children->content);
wattroff(window, WA_BOLD);
// refresh view
wrefresh(window);
cleanup:
if (doc != NULL)
xmlFreeDoc(doc);
free(posts_data.memory);
free(tags_data.memory);
}
void booru_destroy()

View File

@ -55,7 +55,7 @@ void gdrawbar(struct Graph graph, int x, int barheight)
{
wattron(graph.win, COLOR_PAIR(COLORS_GRAPH));
for (int i = 0; i < graph.height && i < barheight; ++i)
mvwprintw(graph.win, graph.row + graph.height - 1 - i, graph.col + x, "*");
mvwprintw(graph.win, graph.row + graph.height - 1 - i, graph.col + x, "");
wattroff(graph.win, COLOR_PAIR(COLORS_GRAPH));
}

View File

@ -12,7 +12,7 @@
#include <libxml/xpath.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "common.h"
#include "company.h"
@ -60,17 +60,15 @@ bool parse_meet_time(const char* json, struct tm* time)
void* company_refresh(void* arg)
{
for ( ; ; sleep(5))
{
CURLcode res;
// check if femfinancial is up
struct MemoryStruct ff_data = geturl(curl, "http://howfeed.biz", &res);
struct MemoryStruct ff_data = geturl(curl, "http://femboyfinancial.jp", &res);
// get random reference from booru
struct MemoryStruct booru_data = geturl(curl, "http://fembooru.jp/post/list", NULL);
// get most recent meeting time
struct MemoryStruct meet_data = geturl(curl, "http://howfeed.biz/api/meet?token=1445", NULL);
struct MemoryStruct meet_data = geturl(curl, "http://api.femboyfinancial.jp/meet?token=1445", NULL);
doc = htmlReadMemory(booru_data.memory, booru_data.size, "nopath.xml", NULL, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);
if (doc == NULL)
@ -105,9 +103,9 @@ void* company_refresh(void* arg)
char* tagline = div->children->content;
// clear line
for (int i = START_COL; i < width - 1; ++i)
mvwprintw(window, START_ROW+5, i, " ");
mvwprintw(window, START_ROW+8, i, " ");
// print the funny reference
mvwprintw(window, START_ROW+5, START_COL, "%s", tagline);
mvwprintw(window, START_ROW+8, START_COL, "%s", tagline);
// say the funny reference
char buf[1024];
sprintf(buf, "espeak \"%s\" >/dev/null 2>&1", tagline);
@ -129,7 +127,7 @@ void* company_refresh(void* arg)
mvwprintw(window, START_ROW+1, START_COL+13, " a long time ago");
}
wattroff(window, WA_BOLD);
mvwprintw(window, START_ROW+2, START_COL, "FemboyFinancial website:");
if (res == CURLE_OK)
{
@ -143,7 +141,7 @@ void* company_refresh(void* arg)
mvwprintw(window, START_ROW+2, START_COL+24, " OFFLINE");
wattroff(window, COLOR_PAIR(COLORS_FAILURE));
}
wrefresh(window);
cleanup:
@ -156,7 +154,6 @@ void* company_refresh(void* arg)
free(ff_data.memory);
free(booru_data.memory);
free(meet_data.memory);
}
}
void company_destroy()

View File

@ -1,186 +0,0 @@
//
// howfeed.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 <string.h>
#include <unistd.h>
#include <time.h>
#include "common.h"
#include "howfeed.h"
static xmlDoc* doc = NULL;
static WINDOW* window = NULL;
static CURL* curl = NULL;
static struct Graph graph;
static float* response_times;
static int width;
static int height;
void howfeed_init(WINDOW* win, int h, int w)
{
curl = curl_easy_init();
window = win;
width = w;
height = h;
graph.win = win;
graph.row = START_ROW + 5;
graph.col = START_COL + 1;
graph.width = w - graph.col - 2;
graph.height = h - graph.row - 1;
mvwprintw(window, 0, START_COL, " Howfeed.biz ");
mvwprintw(window, START_ROW, START_COL, "Pinging...");
mvwprintw(window, graph.row - 1, START_COL, "Response time (x100ms)");
if (curl == NULL)
{
wattron(window, COLOR_PAIR(COLORS_FAILURE));
mvwprintw(window, 0, TITLE_START_COL, " ERROR ");
mvwprintw(window, START_ROW, START_COL, "Failed to initialize libcurl!");
wattroff(window, COLOR_PAIR(COLORS_FAILURE));
curl_global_cleanup();
}
response_times = malloc(sizeof(float) * graph.width);
gdrawylabels(graph);
wrefresh(window);
}
void* howfeed_refresh(void* arg)
{
int iter = 0;
for ( ; ; sleep(1))
{
// record the response time
struct timeval start, end;
gettimeofday(&start, NULL);
CURLcode res;
struct MemoryStruct article_data = geturl(curl, "http://howfeed.biz/rss.xml", &res);
if (res != CURLE_OK)
{
on_curl_error(window, res);
goto cleanup;
}
gettimeofday(&end, NULL);
int index = iter++ % graph.width;
response_times[index] = (end.tv_sec - start.tv_sec) * 1000.0f + (end.tv_usec - start.tv_usec) / 1000.0f;
// clear status lines
for (int i = TITLE_START_COL - 1; i < TITLE_START_COL + 7; ++i)
mvwprintw(window, 0, i, " ");
for (int i = START_ROW; i < START_ROW+2; ++i)
{
for (int j = START_COL; j < width - 1; ++j)
mvwprintw(window, i, j, " ");
}
// clear graph if necessary
if (index == 0)
gclear(graph);
// print status graph
gdrawbar(graph, index, response_times[index] / 100.0f);
// get article count
doc = xmlReadMemory(article_data.memory, article_data.size, "noname.xml", NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
if (doc == NULL)
{
on_xml_error(window);
goto cleanup;
}
xmlNode* el = xmlDocGetRootElement(doc)->children->children;
while (el != NULL && (el->type != XML_ELEMENT_NODE || strcmp(el->name, "item") != 0))
el = el->next;
int articles_count = 0;
while (el != NULL && el->next != NULL && el->next->type == XML_ELEMENT_NODE && strcmp(el->next->name, "item") == 0)
{
++articles_count;
el = el->next;
}
++articles_count;
if (el != NULL)
el = el->children;
while (el != NULL && (el->type != XML_ELEMENT_NODE || strcmp(el->name, "title") != 0))
el = el->next;
char* title;
if (el != NULL)
title = el->children->content;
while (el != NULL && (el->type != XML_ELEMENT_NODE || strcmp(el->name, "pubDate") != 0))
el = el->next;
struct tm pubdate;
if (el != NULL)
strptime(el->children->content, "%a, %d %b %Y %H:%M:%S %Z", &pubdate);
wattron(window, COLOR_PAIR(COLORS_SUCCESS));
mvwprintw(window, 0, TITLE_START_COL, " ONLINE ");
wattroff(window, COLOR_PAIR(COLORS_SUCCESS));
if (el == NULL)
{
wattron(window, COLOR_PAIR(COLORS_WARNING));
mvwprintw(window, START_ROW, START_COL, "Failed to get latest article");
wattroff(window, COLOR_PAIR(COLORS_WARNING));
}
else
{
mvwprintw(window, START_ROW, START_COL, "%d articles", articles_count);
mvwprintw(window, START_ROW+1, START_COL, "Latest article:");
wattron(window, WA_BOLD);
mvwprintw(window, START_ROW+1, START_COL+15, " %s", title);
wattroff(window, WA_BOLD);
double days_elapsed = difftime(time(NULL), timelocal(&pubdate)) / (3600.0 * 24.0);
int colorpair;
if (days_elapsed < 7)
colorpair = COLORS_SUCCESS;
else if (days_elapsed < 14)
colorpair = COLORS_WARNING;
else
colorpair = COLORS_FAILURE;
wattron(window, WA_BOLD);
wattron(window, COLOR_PAIR(colorpair));
mvwprintw(window, START_ROW+2, START_COL, "%.0f days", days_elapsed);
wattroff(window, COLOR_PAIR(colorpair));
wattroff(window, WA_BOLD);
mvwprintw(window, START_ROW+2, START_COL+8, " since last article");
}
// refresh view
wrefresh(window);
cleanup:
if (doc != NULL)
xmlFreeDoc(doc);
free(article_data.memory);
}
}
void howfeed_destroy(void)
{
if (curl != NULL)
curl_easy_cleanup(curl);
free(response_times);
}

152
FemMonitor/mail.c Normal file
View File

@ -0,0 +1,152 @@
//
// mail.c
// FemMonitor
//
// Created by James Shiffer on 3/22/21.
// Copyright © 2021 FemboyFinancial. All rights reserved.
//
#include <curl/curl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "common.h"
#include "mail.h"
static CURL* curl = NULL;
static WINDOW* window = NULL;
static int width;
static int height;
typedef struct _mail_info {
char uptime[64];
unsigned users;
char disk_used[8];
char disk_total[8];
} mail_info_t;
void mail_init(WINDOW* win, int h, int w)
{
curl = curl_easy_init();
window = win;
width = w;
height = h;
mvwprintw(window, 0, START_COL, " E-Mail ");
mvwprintw(window, START_ROW, START_COL, "Pinging...");
if (curl == NULL)
{
wattron(window, COLOR_PAIR(COLORS_FAILURE));
mvwprintw(window, 0, TITLE_START_COL, " ERROR ");
mvwprintw(window, START_ROW, START_COL, "Failed to initialize libcurl!");
wattroff(window, COLOR_PAIR(COLORS_FAILURE));
curl_global_cleanup();
}
wrefresh(window);
}
bool parse_mail(const char* json, mail_info_t* time)
{
const char* prop = "\"uptime\":\"";
const char* read = strstr(json, prop);
if (read == NULL)
return FALSE;
read += strlen(prop);
const char* endprop = strstr(read, "\"");
if (endprop == NULL)
return FALSE;
strncpy(time->uptime, read, endprop - read);
prop = "\"users\":";
read = strstr(json, prop);
if (read == NULL)
return FALSE;
read += strlen(prop);
endprop = strstr(read, ",");
if (endprop == NULL)
return FALSE;
char users_str[8] = {0};
strncpy(users_str, read, endprop - read);
time->users = atoi(users_str);
prop = "\"disk_used\":\"";
read = strstr(json, prop);
if (read == NULL)
return FALSE;
read += strlen(prop);
endprop = strstr(read, "\"");
if (endprop == NULL)
return FALSE;
strncpy(time->disk_used, read, endprop - read);
prop = "\"disk_total\":\"";
read = strstr(json, prop);
if (read == NULL)
return FALSE;
read += strlen(prop);
endprop = strstr(read, "\"");
if (endprop == NULL)
return FALSE;
strncpy(time->disk_total, read, endprop - read);
return TRUE;
}
void* mail_refresh(void* arg)
{
// get mail server status
CURLcode res;
struct MemoryStruct mail_data = geturl(curl, "https://mail.femboyfinancial.jp/status.php", &res);
if (res != CURLE_OK)
{
on_curl_error(window, res);
goto cleanup;
}
// clear status lines
for (int i = TITLE_START_COL - 1; i < TITLE_START_COL + 7; ++i)
mvwprintw(window, 0, i, " ");
for (int i = START_ROW; i < START_ROW+3; ++i)
{
for (int j = START_COL; j < width - 1; ++j)
mvwprintw(window, i, j, " ");
}
mail_info_t mail = {0};
if (parse_mail(mail_data.memory, &mail))
{
wattron(window, COLOR_PAIR(COLORS_SUCCESS));
mvwprintw(window, 0, TITLE_START_COL, " ONLINE ");
wattroff(window, COLOR_PAIR(COLORS_SUCCESS));
mvwprintw(window, START_ROW, START_COL, "Uptime: %s", mail.uptime);
mvwprintw(window, START_ROW+1, START_COL, "Users: %d", mail.users);
mvwprintw(window, START_ROW+2, START_COL, "Disk: %s / %s used", mail.disk_used, mail.disk_total);
}
else
{
wattron(window, COLOR_PAIR(COLORS_FAILURE));
mvwprintw(window, 0, TITLE_START_COL, " ONLINE ");
wattroff(window, COLOR_PAIR(COLORS_FAILURE));
mvwprintw(window, START_ROW, START_COL, "Couldn't retrieve mail server status");
}
// refresh view
wrefresh(window);
cleanup:
free(mail_data.memory);
}
void mail_destroy(void)
{
if (curl != NULL)
curl_easy_cleanup(curl);
}

View File

@ -1,19 +1,19 @@
//
// howfeed.h
// mail.h
// FemMonitor
//
// Created by James Shiffer on 3/22/21.
// Copyright © 2021 FemboyFinancial. All rights reserved.
//
#ifndef howfeed_h
#define howfeed_h
#ifndef mail_h
#define mail_h
#include <ncurses.h>
void howfeed_init(WINDOW* win, int h, int w);
void* howfeed_refresh(void* arg);
void mail_init(WINDOW* win, int h, int w);
void* mail_refresh(void* arg);
/* Note: this function will not free the window! */
void howfeed_destroy(void);
void mail_destroy(void);
#endif /* howfeed_h */
#endif /* mail_h */

View File

@ -10,14 +10,13 @@
#include <curl/curl.h>
#include <libxml/parser.h>
#include <ncurses.h>
#include <pthread.h>
#include <unistd.h>
#include <time.h>
#include "booru.h"
#include "common.h"
#include "company.h"
#include "howfeed.h"
#include "wiki.h"
#include "mail.h"
#define KEY_QUIT 'q'
@ -70,22 +69,18 @@ int main(int argc, const char* argv[])
WINDOW* botright = create_newwin(LINES/2 - 1, COLS/2, LINES/2, COLS/2);
company_init(topleft, LINES/2, COLS);
howfeed_init(botleft, LINES/2 - 1, COLS/2);
mail_init(botleft, LINES/2 - 1, COLS/2);
booru_init(botright, LINES/2 - 1, COLS/2);
// start refresh status threads
pthread_t booru_th;
pthread_create(&booru_th, NULL, booru_refresh, NULL);
pthread_t howfeed_th;
pthread_create(&howfeed_th, NULL, howfeed_refresh, NULL);
pthread_t company_th;
pthread_create(&company_th, NULL, company_refresh, NULL);
time_t t;
struct tm tm;
char ch;
while ((ch = tolower(getch())) != KEY_QUIT)
{
booru_refresh(NULL);
mail_refresh(NULL);
company_refresh(NULL);
time(&t);
tm = *localtime(&t);
mvprintw(LINES - 1, 0, "Last updated: %d-%02d-%02d %02d:%02d:%02d\n",
@ -97,16 +92,12 @@ int main(int argc, const char* argv[])
tm.tm_sec);
mvprintw(LINES - 1, COLS - 15, "Press Q to quit");
wrefresh(stdscr);
sleep(1);
}
// cleanup everything
pthread_cancel(booru_th);
booru_destroy();
pthread_cancel(howfeed_th);
howfeed_destroy();
pthread_cancel(company_th);
mail_destroy();
company_destroy();
destroy_win(topleft);

View File

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

View File

@ -1,15 +0,0 @@
//
// 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 */

View File

@ -1,8 +1,8 @@
CFLAGS = -std=c99 `curl-config --cflags` `xml2-config --cflags`
LDFLAGS = `curl-config --libs` `xml2-config --libs` -lncurses -lpthread
CFLAGS = -std=gnu99 `curl-config --cflags` `xml2-config --cflags` `ncurses6-config --cflags`
LDFLAGS = `curl-config --libs` `xml2-config --libs` `ncurses6-config --libs` -lpthread
OUT = fem
all:
gcc -o $(OUT) $(CFLAGS) FemMonitor/*.c $(LDFLAGS)
gcc -g -o $(OUT) $(CFLAGS) FemMonitor/*.c $(LDFLAGS)
clean:
rm -f $(OUT)