mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 16:45:10 +00:00
121 lines
2.1 KiB
C
121 lines
2.1 KiB
C
#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);
|
|
}
|