[MOD/WIP] Externalise and add errors to commands

Still need forms...
This commit is contained in:
LDA 2024-07-13 22:34:51 +02:00
commit a880769c48
9 changed files with 311 additions and 89 deletions

77
src/XMPPCommands/Status.c Normal file
View file

@ -0,0 +1,77 @@
#include <Cytoplasm/HashMap.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Json.h>
#include <Cytoplasm/Util.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <XMPPFormTool.h>
#include <XMPPCommand.h>
#include <Parsee.h>
#include <XMPP.h>
void
StatusCallback(XMPPCommandManager *m, char *from, HashMap *form, XMLElement *out)
{
ParseeData *data = XMPPGetManagerCookie(m);
char *trimmed = ParseeTrimJID(from);
size_t alloc = MemoryAllocated();
size_t kb = alloc >> 10;
size_t mb = kb >> 10;
size_t gb = mb >> 10;
size_t min = gb ? gb : (mb ? mb : (kb ? kb : alloc));
char *unit = gb ? "GB" : (mb ? "MB" : (kb ? "KB" : "B"));
XMLElement *x;
XMLElement *title, *txt;
if (!ParseeIsAdmin(data, trimmed))
{
XMLElement *note = XMLCreateTag("note");
XMLAddAttr(note, "type", "error");
txt = XMLCreateText("User is not authorised to execute command.");
XMLAddChild(note, txt);
XMLAddChild(out, note);
Free(trimmed);
return;
}
Free(trimmed);
x = XMLCreateTag("x");
title = XMLCreateTag("title");
{
XMLElement *title_text = XMLCreateText("Parsee statistics");
XMLAddChild(title, title_text);
}
XMLAddChild(x, title);
XMLAddAttr(x, "xmlns", "jabber:x:data");
XMLAddAttr(x, "type", "result");
{
XMLElement *reported, *item, *field, *value;
reported = XMLCreateTag("reported");
XMLAddChild(x, reported);
/* Report */
Report("mem-alloc", "Heap allocated with Cytoplasm");
Report("xml-congest", "Unprocessed stanzas(congestion)");
/* Set */
BeginItem();
{
char *min_str = StrInt(min);
char *congest = StrInt(ParseeCongestion());
char *alloc = StrConcat(3, min_str, " ", unit);
SetField("mem-alloc", alloc);
SetField("xml-congest", congest);
Free(congest);
Free(min_str);
Free(alloc);
}
EndItem();
}
XMLAddChild(out, x);
}