[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

68
src/XMPPCommands/Admins.c Normal file
View file

@ -0,0 +1,68 @@
#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
AdminsCallback(XMPPCommandManager *m, char *from, HashMap *form, XMLElement *out)
{
ParseeData *data = XMPPGetManagerCookie(m);
char *trimmed = ParseeTrimJID(from);
size_t i;
XMLElement *x;
XMLElement *title;
XMLElement *reported, *item, *field, *value, *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 administrators");
XMLAddChild(title, title_text);
}
XMLAddChild(x, title);
XMLAddAttr(x, "xmlns", "jabber:x:data");
XMLAddAttr(x, "type", "result");
{
DbRef *ref = DbLock(data->db, 1, "admins");
Array *admins = GrabArray(DbJson(ref), 1, "admins");
reported = XMLCreateTag("reported");
XMLAddChild(x, reported);
/* Report */
Report("admins", "Administrator globs");
/* Set */
for (i = 0; i < ArraySize(admins); i++)
{
char *glob = JsonValueAsString(ArrayGet(admins, i));
BeginItem();
SetField("admins", glob);
EndItem();
}
DbUnlock(data->db, ref);
}
XMLAddChild(out, x);
}

89
src/XMPPCommands/Nofly.c Normal file
View file

@ -0,0 +1,89 @@
#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>
#define TITLE "Parsee global bans"
void
NoflyCallback(XMPPCommandManager *m, char *from, HashMap *form, XMLElement *out)
{
ParseeData *data = XMPPGetManagerCookie(m);
char *trimmed = ParseeTrimJID(from);
XMLElement *x;
XMLElement *title;
XMLElement *reported, *item, *field, *value, *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;
}
x = XMLCreateTag("x");
XMLAddAttr(x, "xmlns", "jabber:x:data");
title = XMLCreateTag("title");
XMLAddChild(x, title);
XMLAddChild(out, x);
Free(trimmed);
{
XMLElement *title_text = XMLCreateText(TITLE);
XMLAddChild(title, title_text);
}
XMLAddAttr(x, "type", "result");
{
DbRef *ref = DbLock(data->db, 1, "global_bans");
HashMap *bans = DbJson(ref);
char *banned, *reason;
JsonValue *ban_val;
reported = XMLCreateTag("reported");
XMLAddChild(x, reported);
/* Report */
Report("ban", "Banned glob");
Report("reason", "Reason for the nofly");
Report("ts", "UNIX timestamp for creation");
/* Set */
while (HashMapIterate(bans, &banned, (void **) &ban_val))
{
HashMap *ban_obj = JsonValueAsObject(ban_val);
reason = GrabString(ban_obj, 1, "reason");
char *ts = StrDuplicate("N/A");
if (!reason)
{
reason = "N/A";
}
if (HashMapGet(ban_obj, "date"))
{
Free(ts);
ts = StrInt(GrabInteger(ban_obj, 1, "date") / (1 SECONDS));
}
BeginItem();
SetField("ban", banned);
SetField("reason", reason);
SetField("ts", ts);
EndItem();
Free(ts);
}
DbUnlock(data->db, ref);
}
}

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);
}