mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 21:25:11 +00:00
[ADD/WIP] Congestion, basic ad-hoc commands
Woah, took me a while, eh? Next up, getting forms!
This commit is contained in:
parent
4007232716
commit
408888ef67
9 changed files with 911 additions and 14 deletions
121
src/XMPPCommand/Commands.c
Normal file
121
src/XMPPCommand/Commands.c
Normal 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);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue