[ADD/WIP] Congestion, basic ad-hoc commands

Woah, took me a while, eh? Next up, getting forms!
This commit is contained in:
LDA 2024-07-13 16:26:33 +02:00
commit 408888ef67
9 changed files with 911 additions and 14 deletions

121
src/XMPPCommand/Commands.c Normal file
View file

@ -0,0 +1,121 @@
#include <XMPPCommand.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Array.h>
#include <Cytoplasm/Str.h>
struct XMPPCommand {
XMPPCmdCallback callback;
char *node, *name;
Array *options;
};
XMPPCommand *
XMPPBasicCmd(char *node, char *name, XMPPCmdCallback callback_funct)
{
XMPPCommand *cmd;
if (!node || !name || !callback_funct)
{
return NULL;
}
cmd = Malloc(sizeof(*cmd));
cmd->callback = callback_funct;
cmd->node = StrDuplicate(node);
cmd->name = StrDuplicate(name);
/* No options -> no form required */
cmd->options = NULL;
return cmd;
}
void
XMPPFreeCommand(XMPPCommand *cmd)
{
size_t i;
if (!cmd)
{
return;
}
for (i = 0; i < ArraySize(cmd->options); i++)
{
XMPPOption *opt = ArrayGet(cmd->options, i);
XMPPFreeOption(opt);
}
ArrayFree(cmd->options);
Free(cmd->node);
Free(cmd->name);
Free(cmd);
}
void
XMPPAddOption(XMPPCommand *cmd, XMPPOption *opt)
{
if (!cmd || !opt)
{
return;
}
if (!cmd->options)
{
cmd->options = ArrayCreate();
}
ArrayAdd(cmd->options, opt);
}
XMLElement *
XMPPFormifyCommand(XMPPCommand *cmd)
{
XMLElement *x, *field;
size_t i;
if (!cmd || !cmd->options)
{
return NULL;
}
x = XMLCreateTag("x");
XMLAddAttr(x, "xmlns", "jabber:x:data");
XMLAddAttr(x, "type", "form");
/* TODO: Other fields */
for (i = 0; i < ArraySize(cmd->options); i++)
{
XMPPOption *opt = ArrayGet(cmd->options, i);
field = XMPPOptionToXML(opt);
XMLAddChild(x, field);
}
return x;
}
char *
XMPPGetCommandNode(XMPPCommand *cmd)
{
return cmd ? cmd->node : NULL;
}
char *
XMPPGetCommandDesc(XMPPCommand *cmd)
{
return cmd ? cmd->name : NULL;
}
bool
XMPPCommandRequiresForm(XMPPCommand *cmd)
{
return cmd ? !!cmd->options : false;
}
void
XMPPExecuteCommand(XMPPCommandManager *m, XMPPCommand *cmd, XMLElement *to, HashMap *arg_table)
{
if (!m || !cmd || !to)
{
return;
}
cmd->callback(m, arg_table, to);
/* TODO Free arg_table's strings */
HashMapFree(arg_table);
}